perci.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef GORP_PERCI_H
  2. #define GORP_PERCI_H
  3. /*
  4. * Copyright 2010 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*
  37. * \file include/gorp/perci.h
  38. * \brief Persistent circular buffer definitions.
  39. *
  40. * \verbatim
  41. * $Id$
  42. * \endverbatim
  43. */
  44. #include <cfg/perci.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <stdarg.h>
  48. /*!
  49. * \addtogroup xgPerci
  50. */
  51. /*@{*/
  52. #ifndef PERCI_MAX_RECORDS
  53. #define PERCI_MAX_RECORDS 256
  54. #endif
  55. #if PERCI_MAX_RECORDS <= 256
  56. typedef uint8_t perci_recnum_t;
  57. typedef uint_fast8_t perci_fast_recnum_t;
  58. #elif PERCI_MAX_RECORDS <= 65536
  59. typedef uint16_t perci_recnum_t;
  60. typedef uint_fast16_t perci_fast_recnum_t;
  61. #else
  62. typedef uint32_t perci_recnum_t;
  63. typedef uint_fast32_t perci_fast_recnum_t;
  64. #endif
  65. #ifndef PERCI_RECSIZE
  66. #define PERCI_RECSIZE 256
  67. #endif
  68. #if PERCI_RECSIZE <= 256
  69. typedef uint8_t perci_reclen_t;
  70. typedef uint_fast8_t perci_fast_reclen_t;
  71. #elif PERCI_RECSIZE <= 65536
  72. typedef uint16_t perci_reclen_t;
  73. typedef uint_fast16_t perci_fast_reclen_t;
  74. #else
  75. typedef uint32_t perci_reclen_t;
  76. typedef uint_fast32_t perci_fast_reclen_t;
  77. #endif
  78. #define PERCI_DATASIZE (PERCI_RECSIZE - sizeof(perci_reclen_t))
  79. typedef struct NUT_PACKED_TYPE _PERCI_RECORD {
  80. perci_reclen_t pcd_len;
  81. uint8_t pcd_data[PERCI_RECSIZE];
  82. } PERCI_RECORD;
  83. typedef struct _PERCI_WRITER {
  84. int pcw_fd;
  85. long pcw_size;
  86. HANDLE pcw_mutex;
  87. perci_fast_recnum_t pcw_recnum;
  88. PERCI_RECORD pcw_rec;
  89. } PERCI_WRITER;
  90. typedef struct _PERCI_READER {
  91. PERCI_WRITER *pcr_cil;
  92. perci_fast_recnum_t pcr_recnum;
  93. perci_fast_reclen_t pcr_reclen;
  94. perci_fast_reclen_t pcr_recpos;
  95. } PERCI_READER;
  96. extern int PerCiInit(char *path, int recs);
  97. extern PERCI_WRITER *PerCiOpen(char *path);
  98. extern void PerCiClose(PERCI_WRITER *writer);
  99. extern void PerCiFlush(PERCI_WRITER * writer);
  100. extern int PerCiWrite(PERCI_WRITER *writer, const char *data, int len);
  101. extern int PerCiWriteFormat(PERCI_WRITER * writer, const char *fmt, ...);
  102. extern int PerCiWriteVarList(PERCI_WRITER * writer, const char *fmt, va_list ap);
  103. extern PERCI_READER *PerCiAttachReader(PERCI_WRITER *writer);
  104. extern void PerCiDetachReader(PERCI_READER *reader);
  105. extern int PerCiRead(PERCI_READER *reader, char *data, int len);
  106. extern int PerCiReadLine(PERCI_READER * reader, char *line, int len);
  107. extern void PerCiDump(FILE *stream, char *path);
  108. #endif