circlebuf.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 = data_end_pos - cb->capacity;
  108. size_t loop_size = size - back_size;
  109. if (back_size)
  110. memcpy((uint8_t *)cb->data + position, data, loop_size);
  111. memcpy(cb->data, (uint8_t *)data + loop_size, back_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_push_front(struct circlebuf *cb, const void *data,
  136. size_t size)
  137. {
  138. cb->size += size;
  139. circlebuf_ensure_capacity(cb);
  140. if (cb->start_pos < size) {
  141. size_t back_size = size - cb->start_pos;
  142. if (cb->start_pos)
  143. memcpy(cb->data, (uint8_t *)data + back_size,
  144. cb->start_pos);
  145. cb->start_pos = cb->capacity - back_size;
  146. memcpy((uint8_t *)cb->data + cb->start_pos, data, back_size);
  147. } else {
  148. cb->start_pos -= size;
  149. memcpy((uint8_t *)cb->data + cb->start_pos, data, size);
  150. }
  151. }
  152. static inline void circlebuf_push_back_zero(struct circlebuf *cb, size_t size)
  153. {
  154. size_t new_end_pos = cb->end_pos + size;
  155. cb->size += size;
  156. circlebuf_ensure_capacity(cb);
  157. if (new_end_pos > cb->capacity) {
  158. size_t back_size = cb->capacity - cb->end_pos;
  159. size_t loop_size = size - back_size;
  160. if (back_size)
  161. memset((uint8_t *)cb->data + cb->end_pos, 0, back_size);
  162. memset(cb->data, 0, loop_size);
  163. new_end_pos -= cb->capacity;
  164. } else {
  165. memset((uint8_t *)cb->data + cb->end_pos, 0, size);
  166. }
  167. cb->end_pos = new_end_pos;
  168. }
  169. static inline void circlebuf_push_front_zero(struct circlebuf *cb, size_t size)
  170. {
  171. cb->size += size;
  172. circlebuf_ensure_capacity(cb);
  173. if (cb->start_pos < size) {
  174. size_t back_size = size - cb->start_pos;
  175. if (cb->start_pos)
  176. memset(cb->data, 0, cb->start_pos);
  177. cb->start_pos = cb->capacity - back_size;
  178. memset((uint8_t *)cb->data + cb->start_pos, 0, back_size);
  179. } else {
  180. cb->start_pos -= size;
  181. memset((uint8_t *)cb->data + cb->start_pos, 0, size);
  182. }
  183. }
  184. static inline void circlebuf_peek_front(struct circlebuf *cb, void *data,
  185. size_t size)
  186. {
  187. assert(size <= cb->size);
  188. if (data) {
  189. size_t start_size = cb->capacity - cb->start_pos;
  190. if (start_size < size) {
  191. memcpy(data, (uint8_t *)cb->data + cb->start_pos,
  192. start_size);
  193. memcpy((uint8_t *)data + start_size, cb->data,
  194. size - start_size);
  195. } else {
  196. memcpy(data, (uint8_t *)cb->data + cb->start_pos, size);
  197. }
  198. }
  199. }
  200. static inline void circlebuf_peek_back(struct circlebuf *cb, void *data,
  201. size_t size)
  202. {
  203. assert(size <= cb->size);
  204. if (data) {
  205. size_t back_size = (cb->end_pos ? cb->end_pos : cb->capacity);
  206. if (back_size < size) {
  207. size_t front_size = size - back_size;
  208. size_t new_end_pos = cb->capacity - front_size;
  209. memcpy((uint8_t *)data + (size - back_size), cb->data,
  210. back_size);
  211. memcpy(data, (uint8_t *)cb->data + new_end_pos,
  212. front_size);
  213. } else {
  214. memcpy(data, (uint8_t *)cb->data + cb->end_pos - size,
  215. size);
  216. }
  217. }
  218. }
  219. static inline void circlebuf_pop_front(struct circlebuf *cb, void *data,
  220. size_t size)
  221. {
  222. circlebuf_peek_front(cb, data, size);
  223. cb->size -= size;
  224. if (!cb->size) {
  225. cb->start_pos = cb->end_pos = 0;
  226. return;
  227. }
  228. cb->start_pos += size;
  229. if (cb->start_pos >= cb->capacity)
  230. cb->start_pos -= cb->capacity;
  231. }
  232. static inline void circlebuf_pop_back(struct circlebuf *cb, void *data,
  233. size_t size)
  234. {
  235. circlebuf_peek_back(cb, data, size);
  236. cb->size -= size;
  237. if (!cb->size) {
  238. cb->start_pos = cb->end_pos = 0;
  239. return;
  240. }
  241. if (cb->end_pos <= size)
  242. cb->end_pos = cb->capacity - (size - cb->end_pos);
  243. else
  244. cb->end_pos -= size;
  245. }
  246. static inline void *circlebuf_data(struct circlebuf *cb, size_t idx)
  247. {
  248. uint8_t *ptr = (uint8_t *)cb->data;
  249. size_t offset = cb->start_pos + idx;
  250. if (idx >= cb->size)
  251. return NULL;
  252. if (offset >= cb->capacity)
  253. offset -= cb->capacity;
  254. return ptr + offset;
  255. }
  256. #ifdef __cplusplus
  257. }
  258. #endif