libfuncs is collection of code (list, queue, circular buffer, io, logging, etc.). https://georgi.unixsol.org/programs/libfuncs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cbuf.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Circular buffer
  3. * Copyright (C) 2010 Unix Solutions Ltd.
  4. *
  5. * Released under MIT license.
  6. * See LICENSE-MIT.txt for license terms.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <netdb.h>
  12. #include <pthread.h>
  13. #include <assert.h>
  14. #include <inttypes.h>
  15. #include "libfuncs.h"
  16. #include "cbuf.h"
  17. static void cbuf_lock(CBUF *b) {
  18. pthread_mutex_lock(&b->lock);
  19. }
  20. static void cbuf_unlock(CBUF *b) {
  21. pthread_mutex_unlock(&b->lock);
  22. }
  23. /* Returns how much data is filled in the buffer */
  24. int cbuf_free_data_size(CBUF *b) {
  25. int ret = b->size - (b->input - b->output);
  26. assert(ret >= 0);
  27. return ret;
  28. }
  29. void cbuf_dump(CBUF *b) {
  30. LOGf("CBUF [%10s]: size:%d pos:%d writepos:%d input:%"PRIu64" output:%"PRIu64" free_data:%d buffered:%"PRId64"\n",
  31. b->name,
  32. b->size,
  33. b->pos,
  34. b->writepos,
  35. b->input,
  36. b->output,
  37. cbuf_free_data_size(b),
  38. b->input - b->output
  39. );
  40. /*
  41. char *z = b->buffer;
  42. printf("cbuf(%s), dump:", b->name);
  43. int i;
  44. for (i=0;i<b->size;i++) {
  45. printf("%c", z[i]);
  46. }
  47. printf("\n\n");
  48. */
  49. }
  50. CBUF *cbuf_init(int buffer_size, char *name) {
  51. if (!buffer_size)
  52. return 0;
  53. CBUF *b = calloc(1, sizeof(CBUF));
  54. if (!b)
  55. return NULL;
  56. if (pthread_mutex_init(&b->lock, NULL) != 0) {
  57. perror("cbuf_new: mutex_init");
  58. free(b);
  59. return NULL;
  60. }
  61. b->name = strdup(name);
  62. b->size = buffer_size;
  63. b->pos = 0;
  64. b->writepos = 0;
  65. b->buffer = calloc(1, buffer_size);
  66. if (!b->buffer) {
  67. LOGf("CBUF [%10s]: Can't allocate buffer size: %d\n", name, buffer_size);
  68. free(b);
  69. return NULL;
  70. }
  71. return b;
  72. }
  73. void cbuf_free(CBUF **pb) {
  74. CBUF *b = *pb;
  75. if (!b)
  76. return;
  77. pthread_mutex_destroy(&b->lock);
  78. FREE(b->buffer);
  79. FREE(b->name);
  80. FREE(*pb);
  81. }
  82. // Returns -1 on buffer wrap around
  83. int cbuf_fill(CBUF *b, uint8_t *data, int datasize) {
  84. int ret = 0;
  85. cbuf_lock(b);
  86. // LOGf(" cbuf_fill(%s, '%s', %d)\n", b->name, data, datasize);
  87. // cbuf_dump(b);
  88. assert(datasize <= b->size);
  89. int to_copy = min(datasize, (b->size - b->pos));
  90. if (!to_copy || !data) {
  91. LOGf("CBUF [%10s]: Nothing to fill.\n", b->name);
  92. ret = -2;
  93. goto OUT;
  94. }
  95. if (cbuf_free_data_size(b)-to_copy <= 0) {
  96. // LOGf("CBUF [%10s]: Buffer will wrap by (%d bytes). Data not filled, consume more!\n", b->name, -(cbuf_free_data_size(b)-to_copy));
  97. // cbuf_dump(b);
  98. // b->debug_get = 1;
  99. ret = -1;
  100. goto OUT;
  101. }
  102. memcpy(b->buffer + b->pos, data, to_copy);
  103. int copied = to_copy;
  104. b->pos += copied; // Move current buffer position
  105. b->input += copied;
  106. assert(b->pos <= b->size);
  107. if (b->pos == b->size) { // Buffer wrap around
  108. b->pos = 0;
  109. }
  110. if (copied < datasize) { // Move the rest
  111. // Logs when wrapping
  112. // LOGf("cbuf(%10s) copied < datasize, copied:%d datasize:%d datasize-copied: %d pos:%d\n",
  113. // b->name, copied, datasize, datasize - copied, b->pos);
  114. // cbuf_dump(b);
  115. cbuf_unlock(b);
  116. ret = cbuf_fill(b, data + copied, datasize - copied);
  117. goto OUT;
  118. }
  119. OUT:
  120. cbuf_unlock(b);
  121. return ret;
  122. }
  123. /* Returns how much space is left to the end of the buffer */
  124. static int cbuf_size_to_end(CBUF *b) {
  125. int ret = b->input - b->output;
  126. if (b->writepos + ret > b->size) {
  127. ret = b->size - b->writepos;
  128. }
  129. return ret;
  130. }
  131. int cbuf_data_size(CBUF *b) {
  132. return cbuf_size_to_end(b);
  133. }
  134. void *cbuf_get(CBUF *b, int size, int *ret_datasize) {
  135. cbuf_lock(b);
  136. void *ret = NULL;
  137. int new_size = min(size, cbuf_size_to_end(b));
  138. if (b->debug_get) {
  139. LOGf("1 cbuf_get(%s, %d) new_size: %d size_to_end: %d\n",
  140. b->name, size, new_size, cbuf_size_to_end(b));
  141. cbuf_dump(b);
  142. }
  143. if (new_size <= 0) { // No data
  144. *ret_datasize = 0;
  145. ret = NULL;
  146. goto OUT;
  147. }
  148. *ret_datasize = new_size;
  149. ret = b->buffer + b->writepos;
  150. b->writepos += new_size; // Move writepos
  151. b->output += new_size;
  152. if (b->writepos > b->size) {
  153. LOGf("!!! b->writepos > b->size !!! size:%d new_size:%d\n", size, new_size);
  154. cbuf_dump(b);
  155. assert(b->writepos <= b->size);
  156. }
  157. if (b->writepos == b->size) // Buffer wraparound
  158. b->writepos = 0;
  159. OUT:
  160. if (b->debug_get) {
  161. LOGf("2 cbuf_get(%s, %d) new_size: %d size_to_end: %d ret_sz:%d\n",
  162. b->name, size, new_size, cbuf_size_to_end(b), *ret_datasize);
  163. cbuf_dump(b);
  164. b->debug_get = 0;
  165. }
  166. cbuf_unlock(b);
  167. return ret;
  168. }
  169. void *cbuf_peek(CBUF *b, int size, int *ret_datasize) {
  170. cbuf_lock(b);
  171. void *ret = NULL;
  172. int new_size = min(size, cbuf_size_to_end(b));
  173. if (new_size <= 0) { // No data
  174. *ret_datasize = 0;
  175. ret = NULL;
  176. goto OUT;
  177. }
  178. *ret_datasize = new_size;
  179. ret = b->buffer + b->writepos;
  180. OUT:
  181. cbuf_unlock(b);
  182. return ret;
  183. }
  184. void cbuf_copy(CBUF *from, CBUF *to) {
  185. // LOGf("cbuf_copy(%s, %s)\n", from->name, to->name);
  186. int data_size;
  187. void *data;
  188. do {
  189. data = cbuf_get(from, from->input - from->output, &data_size);
  190. if (from->debug_get)
  191. LOGf("copied from %s to %s size=%d\n", from->name, to->name, data_size);
  192. if (!data || data_size <= 0)
  193. break;
  194. cbuf_fill(to, data, data_size);
  195. } while (1);
  196. }
  197. void cbuf_poison(CBUF *b, char poison_byte) {
  198. memset(b->buffer, poison_byte, b->size);
  199. }
  200. /*
  201. void consume(CBUF *b, int size) {
  202. int data_size, i;
  203. char *data = cbuf_get(b, size, &data_size);
  204. if (data && data_size > 0) {
  205. printf("Consumed %d Data: \"", data_size);
  206. for (i=0;i<data_size;i++) {
  207. printf("%c", data[i]);
  208. }
  209. printf("\"\n");
  210. } else {
  211. printf("%s", "There is nothing to consume!\n");
  212. }
  213. }
  214. void cbuf_test(void) {
  215. CBUF *in;
  216. CBUF *out;
  217. out = cbuf_init(64, "out");
  218. cbuf_poison(out, 'O');
  219. cbuf_dump(out);
  220. in = cbuf_init(4, "in");
  221. cbuf_poison(in, '*');
  222. cbuf_fill(in, "12", 2);
  223. cbuf_fill(in, "34", 2);
  224. cbuf_fill(in, "z" , 1);
  225. cbuf_dump(in);
  226. cbuf_copy(in, out);
  227. cbuf_dump(out);
  228. consume(in, 16);
  229. cbuf_dump(in);
  230. cbuf_fill(in, "a", 1);
  231. cbuf_fill(in, "b", 1);
  232. cbuf_fill(in, "c", 1);
  233. cbuf_fill(in, "d", 1);
  234. cbuf_dump(in);
  235. cbuf_copy(in, out);
  236. cbuf_dump(out);
  237. consume(in, 4);
  238. cbuf_dump(in);
  239. cbuf_fill(in, "gfgf", 4);
  240. cbuf_dump(in);
  241. consume(in, 4);
  242. cbuf_dump(in);
  243. cbuf_fill(in, "r", 1);
  244. cbuf_fill(in, "r", 1);
  245. cbuf_fill(in, "r", 1);
  246. cbuf_fill(in, "r", 1);
  247. cbuf_dump(in);
  248. consume(out, 6);
  249. cbuf_copy(in, out);
  250. consume(out, 6);
  251. consume(out, 6);
  252. consume(out, 6);
  253. consume(out, 6);
  254. cbuf_free(in);
  255. cbuf_free(out);
  256. }
  257. */