]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ring: add a function to compute max ring payload
authorAurelien DARRAGON <adarragon@haproxy.com>
Mon, 26 Jun 2023 17:22:38 +0000 (19:22 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 6 Sep 2023 14:06:39 +0000 (16:06 +0200)
Add a helper function to the ring API to compute the maximum payload
length that could fit into the ring based on ring size.

include/haproxy/ring.h
src/ring.c

index d6c527c5bc78594d3dfd5aa8642a9e3e35c4777a..71217d52329387c242449b1722d45a9dfd4ae408 100644 (file)
@@ -41,6 +41,8 @@ int ring_attach_cli(struct ring *ring, struct appctx *appctx, uint flags);
 int cli_io_handler_show_ring(struct appctx *appctx);
 void cli_io_release_show_ring(struct appctx *appctx);
 
+size_t ring_max_payload(const struct ring *ring);
+
 #endif /* _HAPROXY_RING_H */
 
 /*
index 2b8e3c85e9a17014203d1312a5d5d6ab28125f72..9b8863afe37415138ac4bf03346ca00329d527c0 100644 (file)
@@ -457,6 +457,22 @@ void cli_io_release_show_ring(struct appctx *appctx)
        ring_detach_appctx(ring, appctx, ofs);
 }
 
+/* Returns the MAXIMUM payload len that could theoretically fit into the ring
+ * based on ring buffer size.
+ *
+ * Computation logic relies on implementation details from 'ring-t.h'.
+ */
+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;
+
+       /* substract payload VI (varint-encoded size) */
+       max -= varint_bytes(max);
+       return max;
+}
 
 /*
  * Local variables: