From 4e6de42b27a60f6bf3d747237a3573ce7c3a45e1 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 14 Mar 2024 06:48:41 +0100 Subject: [PATCH] MINOR: ring: allow to reduce a ring size In ring_resize() we used to check if the new ring was at least as large as the previous one before resizing it, but what counts is that it's as large as the previous one's contents. Initially it was thought this would not really matter, but given that rings are initially created as BUFSIZE, it's currently not possible to shrink them for debugging purposes. Now with this change it is. --- src/ring.c | 7 +++---- src/sink.c | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/ring.c b/src/ring.c index c2668afec..6abbe2caf 100644 --- a/src/ring.c +++ b/src/ring.c @@ -115,7 +115,7 @@ struct ring *ring_resize(struct ring *ring, size_t size) { struct ring_storage *new; - if (size <= ring_size(ring) + sizeof(*ring->storage)) + if (size <= ring_data(ring) + sizeof(*ring->storage)) return ring; new = malloc(size); @@ -124,9 +124,8 @@ struct ring *ring_resize(struct ring *ring, size_t size) thread_isolate(); - /* recheck the buffer's size, it may have changed during the malloc */ - - if (size > ring_size(ring) + sizeof(*ring->storage)) { + /* recheck the ring's size, it may have changed during the malloc */ + if (size > ring_data(ring) + sizeof(*ring->storage)) { /* copy old contents */ new->buf = b_make(new->area, size - sizeof(*ring->storage), 0, 0); b_getblk(&ring->storage->buf, new->area, ring_data(ring), 0); diff --git a/src/sink.c b/src/sink.c index 897f6766f..7225ddd7f 100644 --- a/src/sink.c +++ b/src/sink.c @@ -907,9 +907,9 @@ int cfg_parse_ring(const char *file, int linenum, char **args, int kwm) goto err; } - 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", - file, linenum, (ullong)size, (ullong)ring_size(cfg_sink->ctx.ring), cfg_sink->name); + if (size < ring_data(cfg_sink->ctx.ring)) { + ha_warning("parsing [%s:%d] : ignoring new size '%llu' that is smaller than contents '%llu' for ring '%s'.\n", + file, linenum, (ullong)size, (ullong)ring_data(cfg_sink->ctx.ring), cfg_sink->name); err_code |= ERR_WARN; goto err; }