|
version 1.7.2.1, 2026/02/18 11:39:03
|
version 1.9.4.1, 2026/07/27 20:12:35
|
|
Line 59 rbuf_init(ringbuf_t *rbuf, int num)
|
Line 59 rbuf_init(ringbuf_t *rbuf, int num)
|
| if (!rbuf) |
if (!rbuf) |
| return -1; |
return -1; |
| |
|
| atomic_store_explicit((atomic_int*) &rbuf->rb_head, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &rbuf->rb_head, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &rbuf->rb_tail, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &rbuf->rb_tail, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &rbuf->rb_full, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &rbuf->rb_full, 0, memory_order_release); |
| |
|
| rbuf->rb_buffer = e_calloc(num, sizeof(struct iovec)); |
rbuf->rb_buffer = e_calloc(num, sizeof(struct iovec)); |
| if (!rbuf->rb_buffer) |
if (!rbuf->rb_buffer) |
|
Line 91 rbuf_free(ringbuf_t *rbuf)
|
Line 91 rbuf_free(ringbuf_t *rbuf)
|
| rbuf->rb_bufnum = 0; |
rbuf->rb_bufnum = 0; |
| } |
} |
| |
|
| atomic_store_explicit((atomic_int*) &rbuf->rb_head, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &rbuf->rb_head, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &rbuf->rb_tail, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &rbuf->rb_tail, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &rbuf->rb_full, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &rbuf->rb_full, 0, memory_order_release); |
| } |
} |
| |
|
| /* |
/* |
|
Line 111 rbuf_purge(ringbuf_t *rbuf)
|
Line 111 rbuf_purge(ringbuf_t *rbuf)
|
| if (rbuf->rb_buffer) |
if (rbuf->rb_buffer) |
| memset(rbuf->rb_buffer, 0, rbuf->rb_bufnum * sizeof(struct iovec)); |
memset(rbuf->rb_buffer, 0, rbuf->rb_bufnum * sizeof(struct iovec)); |
| |
|
| atomic_store_explicit((atomic_int*) &rbuf->rb_head, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &rbuf->rb_head, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &rbuf->rb_tail, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &rbuf->rb_tail, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &rbuf->rb_full, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &rbuf->rb_full, 0, memory_order_release); |
| } |
} |
| |
|
| /* |
/* |
|
Line 214 rbuf_enqueue(ringbuf_t *rbuf, void *data, size_t len,
|
Line 214 rbuf_enqueue(ringbuf_t *rbuf, void *data, size_t len,
|
| * |
* |
| * @rbuf = Ring buffer |
* @rbuf = Ring buffer |
| * @out = Data, if =NULL, just dequeue data |
* @out = Data, if =NULL, just dequeue data |
| |
* @peek = Peek only data, keep the same tail position |
| * return: -1 error, 1 buffer is empty or 0 ok |
* return: -1 error, 1 buffer is empty or 0 ok |
| */ |
*/ |
| int |
int |
| rbuf_dequeue(ringbuf_t *rbuf, struct iovec **out) | rbuf_dequeue(ringbuf_t *rbuf, struct iovec **out, int peek) |
| { |
{ |
| int h, t, n, f; |
int h, t, n, f; |
| |
|
|
Line 234 rbuf_dequeue(ringbuf_t *rbuf, struct iovec **out)
|
Line 235 rbuf_dequeue(ringbuf_t *rbuf, struct iovec **out)
|
| |
|
| n = (t + 1) % rbuf->rb_bufnum; |
n = (t + 1) % rbuf->rb_bufnum; |
| |
|
| |
if (out) |
| |
*out = rbuf->rb_buffer + t; |
| |
|
| |
if (peek) |
| |
break; |
| |
|
| f = t; |
f = t; |
| if (atomic_compare_exchange_weak_explicit((atomic_int*) &rbuf->rb_tail, |
if (atomic_compare_exchange_weak_explicit((atomic_int*) &rbuf->rb_tail, |
| &f, n, memory_order_release, memory_order_relaxed)) { |
&f, n, memory_order_release, memory_order_relaxed)) { |
| if (out) |
|
| *out = rbuf->rb_buffer + t; |
|
| |
|
| atomic_store_explicit((atomic_int*) &rbuf->rb_full, 0, memory_order_release); |
atomic_store_explicit((atomic_int*) &rbuf->rb_full, 0, memory_order_release); |
| break; |
break; |
| } |
} |
|
Line 262 lrb_init(lrbuf_t *lrb, u_int size)
|
Line 266 lrb_init(lrbuf_t *lrb, u_int size)
|
| if (!lrb) |
if (!lrb) |
| return -1; |
return -1; |
| |
|
| atomic_store_explicit((atomic_int*) &lrb->lrb_head, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_head, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &lrb->lrb_tail, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_tail, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &lrb->lrb_full, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_full, 0, memory_order_release); |
| |
|
| lrb->lrb_data = e_malloc(size); |
lrb->lrb_data = e_malloc(size); |
| if (!lrb->lrb_data) |
if (!lrb->lrb_data) |
|
Line 294 lrb_free(lrbuf_t *lrb)
|
Line 298 lrb_free(lrbuf_t *lrb)
|
| lrb->lrb_size = 0; |
lrb->lrb_size = 0; |
| } |
} |
| |
|
| atomic_store_explicit((atomic_int*) &lrb->lrb_head, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_head, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &lrb->lrb_tail, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_tail, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &lrb->lrb_full, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_full, 0, memory_order_release); |
| } |
} |
| |
|
| /* |
/* |
|
Line 314 lrb_purge(lrbuf_t *lrb)
|
Line 318 lrb_purge(lrbuf_t *lrb)
|
| if (lrb->lrb_data) |
if (lrb->lrb_data) |
| memset(lrb->lrb_data, 0, lrb->lrb_size); |
memset(lrb->lrb_data, 0, lrb->lrb_size); |
| |
|
| atomic_store_explicit((atomic_int*) &lrb->lrb_head, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_head, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &lrb->lrb_tail, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_tail, 0, memory_order_release); |
| atomic_store_explicit((atomic_int*) &lrb->lrb_full, 0, memory_order_relaxed); | atomic_store_explicit((atomic_int*) &lrb->lrb_full, 0, memory_order_release); |
| } |
} |
| |
|
| /* |
/* |
|
Line 377 lrb_getw(lrbuf_t *lrb, size_t *len)
|
Line 381 lrb_getw(lrbuf_t *lrb, size_t *len)
|
| if (!lrb || !lrb->lrb_data || !lrb->lrb_size) |
if (!lrb || !lrb->lrb_data || !lrb->lrb_size) |
| return NULL; |
return NULL; |
| |
|
| h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_relaxed); | h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_acquire); |
| if (len) |
if (len) |
| *len = lrb->lrb_size - h; |
*len = lrb->lrb_size - h; |
| |
|
|
Line 417 lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int
|
Line 421 lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int
|
| t = atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire); |
t = atomic_load_explicit((atomic_int*) &lrb->lrb_tail, memory_order_acquire); |
| t2 = (t + drop) % lrb->lrb_size; |
t2 = (t + drop) % lrb->lrb_size; |
| } |
} |
| h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_relaxed); | h = atomic_load_explicit((atomic_int*) &lrb->lrb_head, memory_order_acquire); |
| n = lrb->lrb_size - h; |
n = lrb->lrb_size - h; |
| if (len < n) { |
if (len < n) { |
| if (data) |
if (data) |
|
Line 478 lrb_getr(lrbuf_t *lrb, size_t *len)
|
Line 482 lrb_getr(lrbuf_t *lrb, size_t *len)
|
| * @lrb = Linear ring buffer |
* @lrb = Linear ring buffer |
| * @data = Data, if =NULL, just dequeue data |
* @data = Data, if =NULL, just dequeue data |
| * @len = Length of data |
* @len = Length of data |
| |
* @peek = Peek only data, keep the same tail position |
| * return: -1 error, 0 buffer is empty or >0 stored data bytes |
* return: -1 error, 0 buffer is empty or >0 stored data bytes |
| */ |
*/ |
| int |
int |
| lrb_dequeue(lrbuf_t *lrb, void *data, size_t len) | lrb_dequeue(lrbuf_t *lrb, void *data, size_t len, int peek) |
| { |
{ |
| int h, t, t2, n, l, f; |
int h, t, t2, n, l, f; |
| |
|
|
Line 520 lrb_dequeue(lrbuf_t *lrb, void *data, size_t len)
|
Line 525 lrb_dequeue(lrbuf_t *lrb, void *data, size_t len)
|
| } |
} |
| t2 = l - n; |
t2 = l - n; |
| } |
} |
| |
|
| |
if (peek) |
| |
return l; |
| |
|
| 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, |