mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-08-07 07:37:02 +02:00
MINOR: ring: use ring_size(), ring_area(), ring_head() and ring_tail()
Some open-coded constructs were updated to make use of the ring accessors instead. This allows to remove some direct dependencies on the buffers API a bit more.
This commit is contained in:
parent
a75052d665
commit
80441a6983
20
src/ring.c
20
src/ring.c
@ -126,7 +126,7 @@ struct ring *ring_resize(struct ring *ring, size_t size)
|
|||||||
{
|
{
|
||||||
void *area;
|
void *area;
|
||||||
|
|
||||||
if (b_size(&ring->buf) >= size)
|
if (ring_size(ring) >= size)
|
||||||
return ring;
|
return ring;
|
||||||
|
|
||||||
area = malloc(size);
|
area = malloc(size);
|
||||||
@ -136,9 +136,9 @@ struct ring *ring_resize(struct ring *ring, size_t size)
|
|||||||
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
||||||
|
|
||||||
/* recheck the buffer's size, it may have changed during the malloc */
|
/* recheck the buffer's size, it may have changed during the malloc */
|
||||||
if (b_size(&ring->buf) < size) {
|
if (ring_size(ring) < size) {
|
||||||
/* copy old contents */
|
/* copy old contents */
|
||||||
b_getblk(&ring->buf, area, ring->buf.data, 0);
|
b_getblk(&ring->buf, area, ring_data(ring), 0);
|
||||||
area = HA_ATOMIC_XCHG(&ring->buf.area, area);
|
area = HA_ATOMIC_XCHG(&ring->buf.area, area);
|
||||||
ring->buf.size = size;
|
ring->buf.size = size;
|
||||||
}
|
}
|
||||||
@ -159,7 +159,7 @@ void ring_free(struct ring *ring)
|
|||||||
if (ring->buf.area == (void *)ring + sizeof(*ring))
|
if (ring->buf.area == (void *)ring + sizeof(*ring))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
free(ring->buf.area);
|
free(ring_area(ring));
|
||||||
free(ring);
|
free(ring);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,12 +297,12 @@ void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs)
|
|||||||
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
||||||
if (ofs != ~0) {
|
if (ofs != ~0) {
|
||||||
/* reader was still attached */
|
/* reader was still attached */
|
||||||
if (ofs < b_head_ofs(&ring->buf))
|
if (ofs < ring_head(ring))
|
||||||
ofs += b_size(&ring->buf) - b_head_ofs(&ring->buf);
|
ofs += ring_size(ring) - ring_head(ring);
|
||||||
else
|
else
|
||||||
ofs -= b_head_ofs(&ring->buf);
|
ofs -= ring_head(ring);
|
||||||
|
|
||||||
BUG_ON(ofs >= b_size(&ring->buf));
|
BUG_ON(ofs >= ring_size(ring));
|
||||||
LIST_DEL_INIT(&appctx->wait_entry);
|
LIST_DEL_INIT(&appctx->wait_entry);
|
||||||
HA_ATOMIC_DEC(b_peek(&ring->buf, ofs));
|
HA_ATOMIC_DEC(b_peek(&ring->buf, ofs));
|
||||||
}
|
}
|
||||||
@ -455,7 +455,7 @@ int cli_io_handler_show_ring(struct appctx *appctx)
|
|||||||
/* let's be woken up once new data arrive */
|
/* let's be woken up once new data arrive */
|
||||||
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
||||||
LIST_APPEND(&ring->waiters, &appctx->wait_entry);
|
LIST_APPEND(&ring->waiters, &appctx->wait_entry);
|
||||||
ofs = b_tail_ofs(&ring->buf);
|
ofs = ring_tail(ring);
|
||||||
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
|
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
|
||||||
if (ofs != last_ofs) {
|
if (ofs != last_ofs) {
|
||||||
/* more data was added into the ring between the
|
/* more data was added into the ring between the
|
||||||
@ -495,7 +495,7 @@ size_t ring_max_payload(const struct ring *ring)
|
|||||||
size_t max;
|
size_t max;
|
||||||
|
|
||||||
/* initial max = bufsize - 1 (initial RC) - 1 (payload RC) */
|
/* initial max = bufsize - 1 (initial RC) - 1 (payload RC) */
|
||||||
max = b_size(&ring->buf) - 1 - 1;
|
max = ring_size(ring) - 1 - 1;
|
||||||
|
|
||||||
/* subtract payload VI (varint-encoded size) */
|
/* subtract payload VI (varint-encoded size) */
|
||||||
max -= varint_bytes(max);
|
max -= varint_bytes(max);
|
||||||
|
16
src/sink.c
16
src/sink.c
@ -392,7 +392,7 @@ static void sink_forward_io_handler(struct appctx *appctx)
|
|||||||
/* let's be woken up once new data arrive */
|
/* let's be woken up once new data arrive */
|
||||||
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
||||||
LIST_APPEND(&ring->waiters, &appctx->wait_entry);
|
LIST_APPEND(&ring->waiters, &appctx->wait_entry);
|
||||||
ofs = b_tail_ofs(&ring->buf);
|
ofs = ring_tail(ring);
|
||||||
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
|
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
|
||||||
if (ofs != last_ofs) {
|
if (ofs != last_ofs) {
|
||||||
/* more data was added into the ring between the
|
/* more data was added into the ring between the
|
||||||
@ -460,7 +460,7 @@ static void sink_forward_oc_io_handler(struct appctx *appctx)
|
|||||||
/* let's be woken up once new data arrive */
|
/* let's be woken up once new data arrive */
|
||||||
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
|
||||||
LIST_APPEND(&ring->waiters, &appctx->wait_entry);
|
LIST_APPEND(&ring->waiters, &appctx->wait_entry);
|
||||||
ofs = b_tail_ofs(&ring->buf);
|
ofs = ring_tail(ring);
|
||||||
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
|
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
|
||||||
if (ofs != last_ofs) {
|
if (ofs != last_ofs) {
|
||||||
/* more data was added into the ring between the
|
/* more data was added into the ring between the
|
||||||
@ -701,8 +701,8 @@ static void sink_free(struct sink *sink)
|
|||||||
return;
|
return;
|
||||||
if (sink->type == SINK_TYPE_BUFFER) {
|
if (sink->type == SINK_TYPE_BUFFER) {
|
||||||
if (sink->store) {
|
if (sink->store) {
|
||||||
size_t size = (sink->ctx.ring->buf.size + 4095UL) & -4096UL;
|
size_t size = (ring_size(sink->ctx.ring) + 4095UL) & -4096UL;
|
||||||
void *area = (sink->ctx.ring->buf.area - sizeof(*sink->ctx.ring));
|
void *area = (ring_area(sink->ctx.ring) - sizeof(*sink->ctx.ring));
|
||||||
|
|
||||||
msync(area, size, MS_SYNC);
|
msync(area, size, MS_SYNC);
|
||||||
munmap(area, size);
|
munmap(area, size);
|
||||||
@ -907,16 +907,16 @@ int cfg_parse_ring(const char *file, int linenum, char **args, int kwm)
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size < cfg_sink->ctx.ring->buf.size) {
|
if (size < ring_size(cfg_sink->ctx.ring)) {
|
||||||
ha_warning("parsing [%s:%d] : ignoring new size '%llu' that is smaller than current size '%llu' for ring '%s'.\n",
|
ha_warning("parsing [%s:%d] : ignoring new size '%llu' that is smaller than current size '%llu' for ring '%s'.\n",
|
||||||
file, linenum, (ullong)size, (ullong)cfg_sink->ctx.ring->buf.size, cfg_sink->name);
|
file, linenum, (ullong)size, (ullong)ring_size(cfg_sink->ctx.ring), cfg_sink->name);
|
||||||
err_code |= ERR_WARN;
|
err_code |= ERR_WARN;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ring_resize(cfg_sink->ctx.ring, size)) {
|
if (!ring_resize(cfg_sink->ctx.ring, size)) {
|
||||||
ha_alert("parsing [%s:%d] : fail to set sink buffer size '%llu' for ring '%s'.\n", file, linenum,
|
ha_alert("parsing [%s:%d] : fail to set sink buffer size '%llu' for ring '%s'.\n", file, linenum,
|
||||||
(ullong)cfg_sink->ctx.ring->buf.size, cfg_sink->name);
|
(ullong)ring_size(cfg_sink->ctx.ring), cfg_sink->name);
|
||||||
err_code |= ERR_ALERT | ERR_FATAL;
|
err_code |= ERR_ALERT | ERR_FATAL;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
@ -956,7 +956,7 @@ int cfg_parse_ring(const char *file, int linenum, char **args, int kwm)
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
size = (cfg_sink->ctx.ring->buf.size + 4095UL) & -4096UL;
|
size = (ring_size(cfg_sink->ctx.ring) + 4095UL) & -4096UL;
|
||||||
if (ftruncate(fd, size) != 0) {
|
if (ftruncate(fd, size) != 0) {
|
||||||
close(fd);
|
close(fd);
|
||||||
ha_alert("parsing [%s:%d] : could not adjust size of backing-file for ring '%s': %s.\n", file, linenum, cfg_sink->name, strerror(errno));
|
ha_alert("parsing [%s:%d] : could not adjust size of backing-file for ring '%s': %s.\n", file, linenum, cfg_sink->name, strerror(errno));
|
||||||
|
Loading…
Reference in New Issue
Block a user