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

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