From a2e954a817d3b988f333d9ed706543735f9db14b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20L=C3=A9caille?= Date: Wed, 4 Aug 2021 14:53:06 +0200 Subject: [PATCH] MINOR: quic: Make circular buffer internal buffers be variable-sized. For now on thanks to this simple patch we can use circular buffers with a variable-sized internal buffer. --- include/haproxy/cbuf-t.h | 11 +++++------ include/haproxy/cbuf.h | 36 +++++++++++++++++------------------- src/cbuf.c | 8 ++++++-- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/include/haproxy/cbuf-t.h b/include/haproxy/cbuf-t.h index 876c804b5..8f917bb47 100644 --- a/include/haproxy/cbuf-t.h +++ b/include/haproxy/cbuf-t.h @@ -29,18 +29,17 @@ #include -/* 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 */ diff --git a/include/haproxy/cbuf.h b/include/haproxy/cbuf.h index 2b0ea656e..f3d8839b8 100644 --- a/include/haproxy/cbuf.h +++ b/include/haproxy/cbuf.h @@ -32,7 +32,7 @@ #include #include -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 and */ @@ -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 . @@ -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 . * 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 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 . * 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 . * 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; } diff --git a/src/cbuf.c b/src/cbuf.c index d58ff973e..f719435e1 100644 --- a/src/cbuf.c +++ b/src/cbuf.c @@ -24,13 +24,17 @@ 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 as 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; }