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.1KB

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