circlebuffer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #if !defined(_circlebuffer_h)
  2. #define _circlebuffer_h
  3. #include <stdlib.h>
  4. #if defined(__cplusplus)
  5. extern "C" {
  6. #endif
  7. #if defined(_WIN32)
  8. typedef __int64 int64_t;
  9. #elif defined(__POWERPC) || defined(__APPLE)
  10. #include <ppc/types.h>
  11. #else
  12. typedef long long int64_t;
  13. #endif
  14. #if !defined(_WIN32)
  15. #pragma bool on
  16. #endif
  17. typedef unsigned char CircleRecord_t;
  18. typedef void (*FuncLock_t)() ;
  19. /* assume that assert, alerts, messages to go off before this ever is allowed to fill */
  20. /*------------------------------------------------------------------------------------*/
  21. typedef struct CircleBuf_tt
  22. {
  23. size_t head; /* points to start of usable data in buffer */
  24. size_t count;
  25. size_t bufSize;
  26. int64_t bytesConsumed;
  27. size_t recordSize;
  28. size_t userData; /* might store actual recordsize */
  29. int balance;
  30. CircleRecord_t* buffer; /* 10 seconds of 16 bit stereo nice quality */
  31. unsigned char* maxChunk;
  32. size_t maxChunkLen;
  33. int percent; /* level where buffer considered stable */
  34. int wrapped; /* non-zero if data has wrapped at least once */
  35. int muted;
  36. FuncLock_t lock; /* in case there could be competition for any members */
  37. FuncLock_t unlock; /* in case there could be competition for any members */
  38. int starvedBytes; /* how many bytes we had to "conjure up" because we were empty (debug) */
  39. int starvedRequests; /* how many request we honored when we have been in a starved state (debug) */
  40. } CircleBuffer_t;
  41. void testCircleBuffer(void);
  42. void destroyCircleBuffer(CircleBuffer_t* cb);
  43. int initCircleBuffer(CircleBuffer_t* cb, size_t size, int percent, size_t maxChunk, FuncLock_t lock, FuncLock_t unlock);
  44. int addToCircleBuffer(CircleBuffer_t* cb, void* data, size_t count);
  45. int readFromCircleBuffer(CircleBuffer_t* cb, void* dest, size_t count);
  46. int accessCircleBuffer(CircleBuffer_t* cb, void* dest, size_t count);
  47. void FreeWrapless(const CircleBuffer_t* cb, void* handle, size_t* sizeWrapless);
  48. int resetCircleBuffer(CircleBuffer_t* cb);
  49. int RewindBuffer(CircleBuffer_t* cb, int64_t len);
  50. int ForwardBuffer(CircleBuffer_t* cb, int64_t len);
  51. void CircleReport(const CircleBuffer_t* cb, const char* title);
  52. int CirclePercent(CircleBuffer_t* cb);
  53. int CircleAtLevel(CircleBuffer_t* cb);
  54. int CircleOverLevel(CircleBuffer_t* cb);
  55. typedef enum {
  56. CB_NOERR = 0, /* OK */
  57. CB_FULL = -1, /* Buffer overflow */
  58. CB_MAX_LEVEL = -2, /* Buffer is over target full level (percent) */
  59. CB_MIN_LEVEL = -3, /* Buffer is under target min level (percent) */
  60. CB_EMPTY = -4 /* Buffer is empty */
  61. } CB_Err_t;
  62. #if defined(__cplusplus)
  63. }
  64. #endif
  65. #endif