From ce201a40cefb7f25fd5460271130b57afae7c48d Mon Sep 17 00:00:00 2001 From: Amaury Denoyelle Date: Tue, 14 Oct 2025 11:29:40 +0200 Subject: [PATCH] MINOR: ncbmbuf: define new ncbmbuf type --- Makefile | 2 +- include/haproxy/ncbmbuf-t.h | 29 +++++++++++++++++ include/haproxy/ncbmbuf.h | 24 ++++++++++++++ src/ncbmbuf.c | 64 +++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 include/haproxy/ncbmbuf-t.h create mode 100644 include/haproxy/ncbmbuf.h create mode 100644 src/ncbmbuf.c diff --git a/Makefile b/Makefile index 88c7548e8..59b706ec3 100644 --- a/Makefile +++ b/Makefile @@ -999,7 +999,7 @@ OBJS += src/mux_h2.o src/mux_h1.o src/mux_fcgi.o src/log.o \ src/ebsttree.o src/freq_ctr.o src/systemd.o src/init.o \ src/http_acl.o src/dict.o src/dgram.o src/pipe.o \ src/hpack-huff.o src/hpack-enc.o src/ebtree.o src/hash.o \ - src/httpclient_cli.o src/version.o + src/httpclient_cli.o src/version.o src/ncbmbuf.o ifneq ($(TRACE),) OBJS += src/calltrace.o diff --git a/include/haproxy/ncbmbuf-t.h b/include/haproxy/ncbmbuf-t.h new file mode 100644 index 000000000..518a3ffe4 --- /dev/null +++ b/include/haproxy/ncbmbuf-t.h @@ -0,0 +1,29 @@ +#ifndef _HAPROXY_NCBMBUF_T_H +#define _HAPROXY_NCBMBUF_T_H + +#include + +/* Non-contiguous bitmap buffer + * + * This module is an alternative implementation to ncbuf type. Its main + * difference is that filled blocks and gaps are encoded via a bitmap. + * + * The main advantage of the bitmap is that contrary to ncbuf type there is no + * limitation on the minimal size of gaps. Thus, operation such as add and + * advance are guaranteed to succeed. + * + * Storage is reserved for the bitmap at the end of the buffer area, + * representing roughly 1/9 of the total space. Thus, usable buffer storage is + * smaller than the default ncbuf type. + */ + +#define NCBMBUF_NULL ((struct ncbmbuf){ }) + +struct ncbmbuf { + char *area; + char *bitmap; + ncb_sz_t size; + ncb_sz_t head; +}; + +#endif /* _HAPROXY_NCBMBUF_T_H */ diff --git a/include/haproxy/ncbmbuf.h b/include/haproxy/ncbmbuf.h new file mode 100644 index 000000000..04b1a2514 --- /dev/null +++ b/include/haproxy/ncbmbuf.h @@ -0,0 +1,24 @@ +#ifndef _HAPROXY_NCBMBUF_H +#define _HAPROXY_NCBMBUF_H + +#include + +static inline int ncbmb_is_null(const struct ncbmbuf *buf) +{ + return buf->size == 0; +} + +struct ncbmbuf ncbmb_make(char *area, ncb_sz_t size, ncb_sz_t head); + +/* Returns the usable size of for data storage. This is the size of the + * allocated buffer without the bitmap space. + */ +static inline ncb_sz_t ncbmb_size(const struct ncbmbuf *buf) +{ + if (ncbmb_is_null(buf)) + return 0; + + return buf->size; +} + +#endif /* _HAPROXY_NCBMBUF_H */ diff --git a/src/ncbmbuf.c b/src/ncbmbuf.c new file mode 100644 index 000000000..8cb61b8a4 --- /dev/null +++ b/src/ncbmbuf.c @@ -0,0 +1,64 @@ +#include + +#include + +/* Construct a ncbmbuf with all its parameters. */ +struct ncbmbuf ncbmb_make(char *area, ncb_sz_t size, ncb_sz_t head) +{ + struct ncbmbuf buf; + ncb_sz_t bitmap_sz; + + bitmap_sz = (size + 8) / 9; + + buf.area = area; + buf.bitmap = area + size - bitmap_sz; + buf.size = size - bitmap_sz; + buf.head = head; + + memset(area, 0, size); + + return buf; +} + +ncb_sz_t ncbmb_total_data(const struct ncbmbuf *buf) +{ + /* TODO */ + return 0; +} + +int ncbmb_is_empty(const struct ncbmbuf *buf) +{ + /* TODO */ + return 0; +} + +int ncbmb_is_full(const struct ncbmbuf *buf) +{ + /* TODO */ + return 0; +} + +int ncbmb_is_fragmented(const struct ncbmbuf *buf) +{ + /* TODO */ + return 0; +} + +ncb_sz_t ncbmb_data(const struct ncbmbuf *buf, ncb_sz_t off) +{ + /* TODO */ + return 0; +} + +enum ncb_ret ncbmb_add(struct ncbmbuf *buf, ncb_sz_t off, + const char *data, ncb_sz_t len, enum ncb_add_mode mode) +{ + /* TODO */ + return NCB_RET_OK; +} + +enum ncb_ret ncbmb_advance(struct ncbmbuf *buf, ncb_sz_t adv) +{ + /* TODO */ + return NCB_RET_OK; +} -- 2.47.3