]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ring: use ring_size(), ring_area(), ring_head() and ring_tail()
authorWilly Tarreau <w@1wt.eu>
Wed, 6 Mar 2024 15:50:40 +0000 (16:50 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 25 Mar 2024 17:34:19 +0000 (17:34 +0000)
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.

src/ring.c
src/sink.c

index 37f2474d4ac1a7193608b18e6871d64c8ba3c3b9..8d0b704bbac4b376361dbd8c4a72bce1567ddda8 100644 (file)
@@ -126,7 +126,7 @@ struct ring *ring_resize(struct ring *ring, size_t size)
 {
        void *area;
 
-       if (b_size(&ring->buf) >= size)
+       if (ring_size(ring) >= size)
                return ring;
 
        area = malloc(size);
@@ -136,9 +136,9 @@ struct ring *ring_resize(struct ring *ring, size_t size)
        HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
 
        /* 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 */
-               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);
                ring->buf.size = size;
        }
@@ -159,7 +159,7 @@ void ring_free(struct ring *ring)
        if (ring->buf.area == (void *)ring + sizeof(*ring))
                return;
 
-       free(ring->buf.area);
+       free(ring_area(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);
        if (ofs != ~0) {
                /* reader was still attached */
-               if (ofs < b_head_ofs(&ring->buf))
-                       ofs += b_size(&ring->buf) - b_head_ofs(&ring->buf);
+               if (ofs < ring_head(ring))
+                       ofs += ring_size(ring) - ring_head(ring);
                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);
                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 */
                        HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
                        LIST_APPEND(&ring->waiters, &appctx->wait_entry);
-                       ofs = b_tail_ofs(&ring->buf);
+                       ofs = ring_tail(ring);
                        HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
                        if (ofs != last_ofs) {
                                /* 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;
 
        /* 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) */
        max -= varint_bytes(max);
index 7cc95a68f44d432370c310925a121f4e1820f63f..0f730e10002c8e3cdf3c6966480e3fd777de2a9f 100644 (file)
@@ -392,7 +392,7 @@ static void sink_forward_io_handler(struct appctx *appctx)
                /* let's be woken up once new data arrive */
                HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
                LIST_APPEND(&ring->waiters, &appctx->wait_entry);
-               ofs = b_tail_ofs(&ring->buf);
+               ofs = ring_tail(ring);
                HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
                if (ofs != last_ofs) {
                        /* 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 */
                HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
                LIST_APPEND(&ring->waiters, &appctx->wait_entry);
-               ofs = b_tail_ofs(&ring->buf);
+               ofs = ring_tail(ring);
                HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
                if (ofs != last_ofs) {
                        /* more data was added into the ring between the
@@ -701,8 +701,8 @@ static void sink_free(struct sink *sink)
                return;
        if (sink->type == SINK_TYPE_BUFFER) {
                if (sink->store) {
-                       size_t size = (sink->ctx.ring->buf.size + 4095UL) & -4096UL;
-                       void *area = (sink->ctx.ring->buf.area - sizeof(*sink->ctx.ring));
+                       size_t size = (ring_size(sink->ctx.ring) + 4095UL) & -4096UL;
+                       void *area = (ring_area(sink->ctx.ring) - sizeof(*sink->ctx.ring));
 
                        msync(area, size, MS_SYNC);
                        munmap(area, size);
@@ -907,16 +907,16 @@ int cfg_parse_ring(const char *file, int linenum, char **args, int kwm)
                        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",
-                                  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;
                        goto err;
                }
 
                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,
-                                (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;
                        goto err;
                }
@@ -956,7 +956,7 @@ int cfg_parse_ring(const char *file, int linenum, char **args, int kwm)
                        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) {
                        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));