|
|
| version 1.4.4.1, 2026/02/10 15:49:08 | version 1.5.2.2, 2026/02/11 13:33:22 |
|---|---|
| Line 284 lrb_purge(lrbuf_t *lrb) | Line 284 lrb_purge(lrbuf_t *lrb) |
| atomic_store_explicit((atomic_int*) &lrb->lrb_head, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_head, 0, memory_order_relaxed); |
| atomic_store_explicit((atomic_int*) &lrb->lrb_tail, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_tail, 0, memory_order_relaxed); |
| atomic_store_explicit((atomic_int*) &lrb->lrb_full, 0, memory_order_relaxed); | |
| } | } |
| /* | /* |
| Line 298 lrb_isempty(lrbuf_t *lrb) | Line 299 lrb_isempty(lrbuf_t *lrb) |
| if (!lrb) | if (!lrb) |
| return -1; | return -1; |
| return (atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_acquire) == | return (!atomic_load_explicit((atomic_int*) &lrb->lrb_full, memory_order_acquire) && |
| atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire)); | (atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_acquire) == |
| atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire))); | |
| } | } |
| /* | /* |
| Line 318 lrb_isfull(lrbuf_t *lrb) | Line 320 lrb_isfull(lrbuf_t *lrb) |
| if (!lrb->lrb_size) | if (!lrb->lrb_size) |
| return 1; | return 1; |
| t = atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire); | if (!atomic_load_explicit((atomic_int*) &lrb->lrb_full, memory_order_acquire)) |
| h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_relaxed) + 1; | return 0; |
| if (h >= lrb->lrb_size) | |
| h ^= h; | |
| t = atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire); | |
| h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_acquire); | |
| return (h == t); | return (h == t); |
| } | } |
| /* | /* |
| * lrb_getw() - Get address for write | |
| * | |
| * @lrb = Linear ring buffer | |
| * @len = Return available buffer length for write | |
| * return: NULL error or !=NULL pointer for write | |
| * remark: After use of lrb_getw() and write to pointer. | |
| * You should update ring buffer with lrb_enqueue(,NULL,wrote_len,) | |
| */ | |
| void * | |
| lrb_getw(lrbuf_t *lrb, size_t *len) | |
| { | |
| int h; | |
| if (!lrb || !lrb->lrb_data || !lrb->lrb_size) | |
| return NULL; | |
| h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_relaxed); | |
| if (len) | |
| *len = lrb->lrb_size - h; | |
| return (lrb->lrb_data + h); | |
| } | |
| /* | |
| * lrb_enqueue() - Enqueue data to buffer | * lrb_enqueue() - Enqueue data to buffer |
| * | * |
| * @lrb = Linear ring buffer | * @lrb = Linear ring buffer |
| Line 338 lrb_isfull(lrbuf_t *lrb) | Line 364 lrb_isfull(lrbuf_t *lrb) |
| int | int |
| lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int lost) | lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int lost) |
| { | { |
| int h, t, n, t2, unused, drop = 0; | int h, t = 0, n, t2 = 0, unused, drop = 0; |
| if (!lrb || !lrb->lrb_data) | if (!lrb || !lrb->lrb_data) |
| return -1; | return -1; |
| Line 362 lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int | Line 388 lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int |
| h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_relaxed); | h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_relaxed); |
| n = lrb->lrb_size - h; | n = lrb->lrb_size - h; |
| if (len < n) { | if (len < n) { |
| memcpy(lrb->lrb_data + h, data, len); | if (data) |
| memcpy(lrb->lrb_data + h, data, len); | |
| n = h + len; | n = h + len; |
| } else { | } else { |
| memcpy(lrb->lrb_data + h, data, n); | if (data) { |
| memcpy(lrb->lrb_data, data + n, len - n); | memcpy(lrb->lrb_data + h, data, n); |
| memcpy(lrb->lrb_data, data + n, len - n); | |
| } | |
| n = len - n; | n = len - n; |
| } | } |
| atomic_store_explicit((atomic_int*) &lrb->lrb_head, n, memory_order_release); | h = n; |
| atomic_store_explicit((atomic_int*) &lrb->lrb_head, h, memory_order_release); | |
| if (drop > 0) | if (drop > 0) |
| while (42) { | while (42) { |
| n = t; | n = t; |
| Line 380 lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int | Line 410 lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int |
| t = n; | t = n; |
| t2 = (t + drop) % lrb->lrb_size; | t2 = (t + drop) % lrb->lrb_size; |
| } | } |
| else | |
| t2 = atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire); | |
| atomic_store_explicit((atomic_int*) &lrb->lrb_full, (h == t2), memory_order_release); | |
| return 0; | return 0; |
| } | } |
| /* | /* |
| * lrb_getr() - Get address for read | |
| * | |
| * @lrb = Linear ring buffer | |
| * @len = Return available data length for read | |
| * return: NULL error or !=NULL pointer for read | |
| * remark: After use of lrb_getr() and read from pointer. | |
| * You could update ring buffer with lrb_dequeue(,NULL,read_len) | |
| */ | |
| void * | |
| lrb_getr(lrbuf_t *lrb, size_t *len) | |
| { | |
| int t; | |
| if (!lrb || !lrb->lrb_data || !lrb->lrb_size) | |
| return NULL; | |
| t = atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire); | |
| if (len) | |
| lrb_queued(lrb, *len); | |
| return (lrb->lrb_data + t); | |
| } | |
| /* | |
| * lrb_dequeue() - Dequeue data from buffer | * lrb_dequeue() - Dequeue data from buffer |
| * | * |
| * @lrb = Linear ring buffer | * @lrb = Linear ring buffer |
| Line 394 lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int | Line 451 lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int |
| int | int |
| lrb_dequeue(lrbuf_t *lrb, void *data, size_t len) | lrb_dequeue(lrbuf_t *lrb, void *data, size_t len) |
| { | { |
| int h, t, t2, n, l; | int h, t, t2, n, l, f; |
| if (!lrb) | if (!lrb) |
| return -1; | return -1; |
| Line 406 lrb_dequeue(lrbuf_t *lrb, void *data, size_t len) | Line 463 lrb_dequeue(lrbuf_t *lrb, void *data, size_t len) |
| while (42) { | while (42) { |
| t = atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire); | t = atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire); |
| h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_acquire); | h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_acquire); |
| f = atomic_load_explicit((atomic_int*) &lrb->lrb_full, memory_order_acquire); | |
| l = h - t; | l = h - t; |
| if (l < 0) | if (l < 0) |
| l += lrb->lrb_size; | l += lrb->lrb_size; |
| if (!l) | if (!l) { |
| return 0; | if (!f) |
| return 0; | |
| l = lrb->lrb_size; | |
| } | |
| if (l > len) | if (l > len) |
| l = len; | l = len; |
| Line 430 lrb_dequeue(lrbuf_t *lrb, void *data, size_t len) | Line 491 lrb_dequeue(lrbuf_t *lrb, void *data, size_t len) |
| n = t; | n = t; |
| if (atomic_compare_exchange_weak_explicit((atomic_int*) &lrb->lrb_tail, | if (atomic_compare_exchange_weak_explicit((atomic_int*) &lrb->lrb_tail, |
| &n, t2, memory_order_release, memory_order_relaxed)) | &n, t2, memory_order_release, memory_order_relaxed)) { |
| atomic_store_explicit((atomic_int*) &lrb->lrb_full, 0, memory_order_release); | |
| return l; | return l; |
| } | |
| } | } |
| return 0; | return 0; |