circlebuf.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (c) 2023 Lain 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. OBS_DEPRECATED 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. OBS_DEPRECATED static inline void circlebuf_init(struct circlebuf *cb)
  34. {
  35. memset(cb, 0, sizeof(struct circlebuf));
  36. }
  37. OBS_DEPRECATED static inline void circlebuf_free(struct circlebuf *cb)
  38. {
  39. bfree(cb->data);
  40. memset(cb, 0, sizeof(struct circlebuf));
  41. }
  42. OBS_DEPRECATED static inline void circlebuf_reorder_data(struct circlebuf *cb, size_t new_capacity)
  43. {
  44. size_t difference;
  45. uint8_t *data;
  46. if (!cb->size || !cb->start_pos || cb->end_pos > cb->start_pos)
  47. return;
  48. difference = new_capacity - cb->capacity;
  49. data = (uint8_t *)cb->data + cb->start_pos;
  50. memmove(data + difference, data, cb->capacity - cb->start_pos);
  51. cb->start_pos += difference;
  52. }
  53. OBS_DEPRECATED static inline void circlebuf_ensure_capacity(struct circlebuf *cb)
  54. {
  55. size_t new_capacity;
  56. if (cb->size <= cb->capacity)
  57. return;
  58. new_capacity = cb->capacity * 2;
  59. if (cb->size > new_capacity)
  60. new_capacity = cb->size;
  61. cb->data = brealloc(cb->data, new_capacity);
  62. circlebuf_reorder_data(cb, new_capacity);
  63. cb->capacity = new_capacity;
  64. }
  65. OBS_DEPRECATED static inline void circlebuf_reserve(struct circlebuf *cb, size_t capacity)
  66. {
  67. if (capacity <= cb->capacity)
  68. return;
  69. cb->data = brealloc(cb->data, capacity);
  70. circlebuf_reorder_data(cb, capacity);
  71. cb->capacity = capacity;
  72. }
  73. OBS_DEPRECATED static inline void circlebuf_upsize(struct circlebuf *cb, size_t size)
  74. {
  75. size_t add_size = size - cb->size;
  76. size_t new_end_pos = cb->end_pos + add_size;
  77. if (size <= cb->size)
  78. return;
  79. cb->size = size;
  80. circlebuf_ensure_capacity(cb);
  81. if (new_end_pos > cb->capacity) {
  82. size_t back_size = cb->capacity - cb->end_pos;
  83. size_t loop_size = add_size - back_size;
  84. if (back_size)
  85. memset((uint8_t *)cb->data + cb->end_pos, 0, back_size);
  86. memset(cb->data, 0, loop_size);
  87. new_end_pos -= cb->capacity;
  88. } else {
  89. memset((uint8_t *)cb->data + cb->end_pos, 0, add_size);
  90. }
  91. cb->end_pos = new_end_pos;
  92. }
  93. /** Overwrites data at a specific point in the buffer (relative). */
  94. OBS_DEPRECATED static inline void circlebuf_place(struct circlebuf *cb, size_t position, const void *data, size_t size)
  95. {
  96. size_t end_point = position + size;
  97. size_t data_end_pos;
  98. if (end_point > cb->size)
  99. circlebuf_upsize(cb, end_point);
  100. position += cb->start_pos;
  101. if (position >= cb->capacity)
  102. position -= cb->capacity;
  103. data_end_pos = position + size;
  104. if (data_end_pos > cb->capacity) {
  105. size_t back_size = data_end_pos - cb->capacity;
  106. size_t loop_size = size - back_size;
  107. memcpy((uint8_t *)cb->data + position, data, loop_size);
  108. memcpy(cb->data, (uint8_t *)data + loop_size, back_size);
  109. } else {
  110. memcpy((uint8_t *)cb->data + position, data, size);
  111. }
  112. }
  113. OBS_DEPRECATED static inline void circlebuf_push_back(struct circlebuf *cb, const void *data, size_t size)
  114. {
  115. size_t new_end_pos = cb->end_pos + size;
  116. cb->size += size;
  117. circlebuf_ensure_capacity(cb);
  118. if (new_end_pos > cb->capacity) {
  119. size_t back_size = cb->capacity - cb->end_pos;
  120. size_t loop_size = size - back_size;
  121. if (back_size)
  122. memcpy((uint8_t *)cb->data + cb->end_pos, data, back_size);
  123. memcpy(cb->data, (uint8_t *)data + back_size, loop_size);
  124. new_end_pos -= cb->capacity;
  125. } else {
  126. memcpy((uint8_t *)cb->data + cb->end_pos, data, size);
  127. }
  128. cb->end_pos = new_end_pos;
  129. }
  130. OBS_DEPRECATED static inline void circlebuf_push_front(struct circlebuf *cb, const void *data, size_t size)
  131. {
  132. cb->size += size;
  133. circlebuf_ensure_capacity(cb);
  134. if (cb->size == size) {
  135. cb->start_pos = 0;
  136. cb->end_pos = size;
  137. memcpy((uint8_t *)cb->data, data, size);
  138. } else if (cb->start_pos < size) {
  139. size_t back_size = size - cb->start_pos;
  140. if (cb->start_pos)
  141. memcpy(cb->data, (uint8_t *)data + back_size, cb->start_pos);
  142. cb->start_pos = cb->capacity - back_size;
  143. memcpy((uint8_t *)cb->data + cb->start_pos, data, back_size);
  144. } else {
  145. cb->start_pos -= size;
  146. memcpy((uint8_t *)cb->data + cb->start_pos, data, size);
  147. }
  148. }
  149. OBS_DEPRECATED static inline void circlebuf_push_back_zero(struct circlebuf *cb, size_t size)
  150. {
  151. size_t new_end_pos = cb->end_pos + size;
  152. cb->size += size;
  153. circlebuf_ensure_capacity(cb);
  154. if (new_end_pos > cb->capacity) {
  155. size_t back_size = cb->capacity - cb->end_pos;
  156. size_t loop_size = size - back_size;
  157. if (back_size)
  158. memset((uint8_t *)cb->data + cb->end_pos, 0, back_size);
  159. memset(cb->data, 0, loop_size);
  160. new_end_pos -= cb->capacity;
  161. } else {
  162. memset((uint8_t *)cb->data + cb->end_pos, 0, size);
  163. }
  164. cb->end_pos = new_end_pos;
  165. }
  166. OBS_DEPRECATED static inline void circlebuf_push_front_zero(struct circlebuf *cb, size_t size)
  167. {
  168. cb->size += size;
  169. circlebuf_ensure_capacity(cb);
  170. if (cb->size == size) {
  171. cb->start_pos = 0;
  172. cb->end_pos = size;
  173. memset((uint8_t *)cb->data, 0, size);
  174. } else if (cb->start_pos < size) {
  175. size_t back_size = size - cb->start_pos;
  176. if (cb->start_pos)
  177. memset(cb->data, 0, cb->start_pos);
  178. cb->start_pos = cb->capacity - back_size;
  179. memset((uint8_t *)cb->data + cb->start_pos, 0, back_size);
  180. } else {
  181. cb->start_pos -= size;
  182. memset((uint8_t *)cb->data + cb->start_pos, 0, size);
  183. }
  184. }
  185. OBS_DEPRECATED static inline void circlebuf_peek_front(struct circlebuf *cb, void *data, 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, start_size);
  192. memcpy((uint8_t *)data + start_size, cb->data, size - start_size);
  193. } else {
  194. memcpy(data, (uint8_t *)cb->data + cb->start_pos, size);
  195. }
  196. }
  197. }
  198. OBS_DEPRECATED static inline void circlebuf_peek_back(struct circlebuf *cb, void *data, size_t size)
  199. {
  200. assert(size <= cb->size);
  201. if (data) {
  202. size_t back_size = (cb->end_pos ? cb->end_pos : cb->capacity);
  203. if (back_size < size) {
  204. size_t front_size = size - back_size;
  205. size_t new_end_pos = cb->capacity - front_size;
  206. memcpy((uint8_t *)data + (size - back_size), cb->data, back_size);
  207. memcpy(data, (uint8_t *)cb->data + new_end_pos, front_size);
  208. } else {
  209. memcpy(data, (uint8_t *)cb->data + cb->end_pos - size, size);
  210. }
  211. }
  212. }
  213. OBS_DEPRECATED static inline void circlebuf_pop_front(struct circlebuf *cb, void *data, size_t size)
  214. {
  215. circlebuf_peek_front(cb, data, size);
  216. cb->size -= size;
  217. if (!cb->size) {
  218. cb->start_pos = cb->end_pos = 0;
  219. return;
  220. }
  221. cb->start_pos += size;
  222. if (cb->start_pos >= cb->capacity)
  223. cb->start_pos -= cb->capacity;
  224. }
  225. OBS_DEPRECATED static inline void circlebuf_pop_back(struct circlebuf *cb, void *data, size_t size)
  226. {
  227. circlebuf_peek_back(cb, data, size);
  228. cb->size -= size;
  229. if (!cb->size) {
  230. cb->start_pos = cb->end_pos = 0;
  231. return;
  232. }
  233. if (cb->end_pos <= size)
  234. cb->end_pos = cb->capacity - (size - cb->end_pos);
  235. else
  236. cb->end_pos -= size;
  237. }
  238. OBS_DEPRECATED static inline void *circlebuf_data(struct circlebuf *cb, size_t idx)
  239. {
  240. uint8_t *ptr = (uint8_t *)cb->data;
  241. size_t offset = cb->start_pos + idx;
  242. if (idx >= cb->size)
  243. return NULL;
  244. if (offset >= cb->capacity)
  245. offset -= cb->capacity;
  246. return ptr + offset;
  247. }
  248. #ifdef __cplusplus
  249. }
  250. #endif