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.
This commit is contained in:
Frédéric Lécaille 2021-08-04 14:53:06 +02:00 committed by Amaury Denoyelle
parent 9445abc013
commit a2e954a817
3 changed files with 28 additions and 27 deletions

View File

@ -29,18 +29,17 @@
#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 */

View 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;
}

View File

@ -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 <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;
}