circlebuf.h 7.8 KB

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