circlebuf.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include "c99defs.h"
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <assert.h>
  21. #include "bmem.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Dynamic circular buffer */
  26. struct circlebuf {
  27. void *data;
  28. size_t size;
  29. size_t start_pos;
  30. size_t end_pos;
  31. size_t capacity;
  32. };
  33. static inline void circlebuf_init(struct circlebuf *cb)
  34. {
  35. memset(cb, 0, sizeof(struct circlebuf));
  36. }
  37. static inline void circlebuf_free(struct circlebuf *cb)
  38. {
  39. bfree(cb->data);
  40. memset(cb, 0, sizeof(struct circlebuf));
  41. }
  42. static inline void circlebuf_reorder_data(struct circlebuf *cb,
  43. size_t new_capacity)
  44. {
  45. size_t difference;
  46. uint8_t *data;
  47. if (!cb->size || !cb->start_pos || cb->end_pos > cb->start_pos)
  48. return;
  49. difference = new_capacity - cb->capacity;
  50. data = (uint8_t*)cb->data + cb->start_pos;
  51. memmove(data+difference, data, cb->capacity - cb->start_pos);
  52. cb->start_pos += difference;
  53. }
  54. static inline void circlebuf_ensure_capacity(struct circlebuf *cb)
  55. {
  56. size_t new_capacity;
  57. if (cb->size <= cb->capacity)
  58. return;
  59. new_capacity = cb->capacity*2;
  60. if (cb->size > new_capacity)
  61. new_capacity = cb->size;
  62. cb->data = brealloc(cb->data, new_capacity);
  63. circlebuf_reorder_data(cb, new_capacity);
  64. cb->capacity = new_capacity;
  65. }
  66. static inline void circlebuf_reserve(struct circlebuf *cb, size_t capacity)
  67. {
  68. if (capacity <= cb->capacity)
  69. return;
  70. cb->data = brealloc(cb->data, capacity);
  71. circlebuf_reorder_data(cb, capacity);
  72. cb->capacity = capacity;
  73. }
  74. static inline void circlebuf_upsize(struct circlebuf *cb, size_t size)
  75. {
  76. size_t add_size = size - cb->size;
  77. size_t new_end_pos = cb->end_pos + add_size;
  78. if (size <= cb->size)
  79. return;
  80. cb->size = size;
  81. circlebuf_ensure_capacity(cb);
  82. if (new_end_pos > cb->capacity) {
  83. size_t back_size = cb->capacity - cb->end_pos;
  84. size_t loop_size = add_size - back_size;
  85. if (back_size)
  86. memset((uint8_t*)cb->data + cb->end_pos, 0, back_size);
  87. memset(cb->data, 0, loop_size);
  88. new_end_pos -= cb->capacity;
  89. } else {
  90. memset((uint8_t*)cb->data + cb->end_pos, 0, add_size);
  91. }
  92. cb->end_pos = new_end_pos;
  93. }
  94. /** Overwrites data at a specific point in the buffer (relative). */
  95. static inline void circlebuf_place(struct circlebuf *cb, size_t position,
  96. const void *data, size_t size)
  97. {
  98. size_t end_point = position + size;
  99. size_t data_end_pos;
  100. if (end_point > cb->size)
  101. circlebuf_upsize(cb, end_point);
  102. position += cb->start_pos;
  103. if (position >= cb->capacity)
  104. position -= cb->capacity;
  105. data_end_pos = position + size;
  106. if (data_end_pos > cb->capacity) {
  107. size_t back_size = cb->capacity - data_end_pos;
  108. size_t loop_size = size - back_size;
  109. if (back_size)
  110. memcpy((uint8_t*)cb->data + position, data, back_size);
  111. memcpy(cb->data, (uint8_t*)data + back_size, loop_size);
  112. } else {
  113. memcpy((uint8_t*)cb->data + position, data, size);
  114. }
  115. }
  116. static inline void circlebuf_push_back(struct circlebuf *cb, const void *data,
  117. size_t size)
  118. {
  119. size_t new_end_pos = cb->end_pos + size;
  120. cb->size += size;
  121. circlebuf_ensure_capacity(cb);
  122. if (new_end_pos > cb->capacity) {
  123. size_t back_size = cb->capacity - cb->end_pos;
  124. size_t loop_size = size - back_size;
  125. if (back_size)
  126. memcpy((uint8_t*)cb->data + cb->end_pos, data,
  127. back_size);
  128. memcpy(cb->data, (uint8_t*)data + back_size, loop_size);
  129. new_end_pos -= cb->capacity;
  130. } else {
  131. memcpy((uint8_t*)cb->data + cb->end_pos, data, size);
  132. }
  133. cb->end_pos = new_end_pos;
  134. }
  135. static inline void circlebuf_pop_front(struct circlebuf *cb, void *data,
  136. size_t size)
  137. {
  138. size_t start_size;
  139. assert(size <= cb->size);
  140. start_size = cb->capacity - cb->start_pos;
  141. if (start_size < size) {
  142. memcpy(data, (uint8_t*)cb->data + cb->start_pos, start_size);
  143. memcpy((uint8_t*)data + start_size, cb->data,
  144. size - start_size);
  145. } else {
  146. memcpy(data, (uint8_t*)cb->data + cb->start_pos, size);
  147. }
  148. cb->size -= size;
  149. cb->start_pos += size;
  150. if (cb->start_pos >= cb->capacity)
  151. cb->start_pos -= cb->capacity;
  152. }
  153. #ifdef __cplusplus
  154. }
  155. #endif