]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: quic: Make circular buffer internal buffers be variable-sized.
authorFrédéric Lécaille <flecaille@haproxy.com>
Wed, 4 Aug 2021 12:53:06 +0000 (14:53 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 23 Sep 2021 13:27:25 +0000 (15:27 +0200)
For now on thanks to this simple patch we can use circular buffers with
a variable-sized internal buffer.

include/haproxy/cbuf-t.h
include/haproxy/cbuf.h
src/cbuf.c

index 876c804b555a818d9f0e7da9d54baa9526357f2c..8f917bb472598dfa46d973893e505e41a16f1e0b 100644 (file)
 
 #include <haproxy/list-t.h>
 
-/* QUIC circular buffer internal buffer size (must be a power of 2) */
-#define CBUF_BUFSZ  (1UL << 11)
-
 extern struct pool_head *pool_head_cbuf;
 
 struct cbuf {
        /* buffer */
-       unsigned char buf[CBUF_BUFSZ];
+       unsigned char *buf;
+       /* buffer size */
+       size_t sz;
        /* Writer index */
-       int wr;
+       size_t wr;
        /* Reader index */
-       int rd;
+       size_t rd;
 };
 
 #endif /* _HAPROXY_CBUF_T_H */
index 2b0ea656ed369209a31b5393d08cf2f6db076c76..f3d8839b86c519338ca63a338cd837f8c43471c9 100644 (file)
@@ -32,7 +32,7 @@
 #include <haproxy/list.h>
 #include <haproxy/cbuf-t.h>
 
-struct cbuf *cbuf_new(void);
+struct cbuf *cbuf_new(unsigned char *buf, size_t sz);
 void cbuf_free(struct cbuf *cbuf);
 
 /* Amount of data between <rd> and <wr> */
@@ -67,7 +67,7 @@ static inline void cb_wr_reset(struct cbuf *cbuf)
  */
 static inline void cb_add(struct cbuf *cbuf, size_t count)
 {
-       cbuf->wr = (cbuf->wr + count) & (CBUF_BUFSZ - 1);
+       cbuf->wr = (cbuf->wr + count) & (cbuf->sz - 1);
 }
 
 /* Return the reader position in <cbuf>.
@@ -83,55 +83,53 @@ static inline unsigned char *cb_rd(struct cbuf *cbuf)
  */
 static inline void cb_del(struct cbuf *cbuf, size_t count)
 {
-       cbuf->rd = (cbuf->rd + count) & (CBUF_BUFSZ - 1);
+       cbuf->rd = (cbuf->rd + count) & (cbuf->sz - 1);
 }
 
 /* Return the amount of data left in <cbuf>.
  * To be used only by the writer!
  */
-static inline int cb_data(struct cbuf *cbuf)
+static inline size_t cb_data(struct cbuf *cbuf)
 {
-       int rd;
+       size_t rd;
 
        rd = HA_ATOMIC_LOAD(&cbuf->rd);
-       return CBUF_DATA(cbuf->wr, rd, CBUF_BUFSZ);
+       return CBUF_DATA(cbuf->wr, rd, cbuf->sz);
 }
 
 /* Return the amount of room left in <cbuf> minus 1 to distinguish
  * the case where the buffer is full from the case where is is empty
  * To be used only by the write!
  */
-static inline int cb_room(struct cbuf *cbuf)
+static inline size_t cb_room(struct cbuf *cbuf)
 {
-       int rd;
+       size_t rd;
 
        rd = HA_ATOMIC_LOAD(&cbuf->rd);
-       return CBUF_DATA(rd, cbuf->wr + 1, CBUF_BUFSZ);
+       return CBUF_DATA(rd, cbuf->wr + 1, cbuf->sz);
 }
 
 /* Return the amount of contiguous data left in <cbuf>.
  * To be used only by the reader!
  */
-static inline int cb_contig_data(struct cbuf *cbuf)
+static inline size_t cb_contig_data(struct cbuf *cbuf)
 {
-       int end, n;
-
-       end = CBUF_BUFSZ - cbuf->rd;
-       n = (HA_ATOMIC_LOAD(&cbuf->wr) + end) & (CBUF_BUFSZ - 1);
+       size_t end, n;
 
+       end = cbuf->sz - cbuf->rd;
+       n = (HA_ATOMIC_LOAD(&cbuf->wr) + end) & (cbuf->sz - 1);
        return n < end ? n : end;
 }
 
 /* Return the amount of contiguous space left in <cbuf>.
  * To be used only by the writer!
  */
-static inline int cb_contig_space(struct cbuf *cbuf)
+static inline size_t cb_contig_space(struct cbuf *cbuf)
 {
-       int end, n;
-
-       end = CBUF_BUFSZ - 1 - cbuf->wr;
-       n = (HA_ATOMIC_LOAD(&cbuf->rd) + end) & (CBUF_BUFSZ - 1);
+       size_t end, n;
 
+       end = cbuf->sz - 1 - cbuf->wr;
+       n = (HA_ATOMIC_LOAD(&cbuf->rd) + end) & (cbuf->sz - 1);
        return n <= end ? n : end + 1;
 }
 
index d58ff973e95ddf01ab3ba7bf21bb6b980c7a857e..f719435e1deaab9acb80e9256d88eeb56e7c1999 100644 (file)
 
 DECLARE_POOL(pool_head_cbuf, "cbuf_pool", sizeof(struct cbuf));
 
-/* Allocate and return a new circular buffer if succeeded, NULL if not. */
-struct cbuf *cbuf_new(void)
+/* Allocate and return a new circular buffer with <buf> as <sz> byte internal buffer
+ * if succeeded, NULL if not.
+ */
+struct cbuf *cbuf_new(unsigned char *buf, size_t sz)
 {
        struct cbuf *cbuf;
 
        cbuf = pool_alloc(pool_head_cbuf);
        if (cbuf) {
+               cbuf->sz = sz;
+               cbuf->buf = buf;
                cbuf->wr = 0;
                cbuf->rd = 0;
        }