From: Amaury Denoyelle Date: Thu, 16 Oct 2025 14:41:24 +0000 (+0200) Subject: MINOR: ncbmbuf: implement init / is_empty X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6eb009378e6da436f73fa85d30bf7c31711ebf4b;p=thirdparty%2Fhaproxy.git MINOR: ncbmbuf: implement init / is_empty --- diff --git a/include/haproxy/ncbmbuf.h b/include/haproxy/ncbmbuf.h index 4ca5ea728..4974f53cf 100644 --- a/include/haproxy/ncbmbuf.h +++ b/include/haproxy/ncbmbuf.h @@ -8,29 +8,30 @@ static inline int ncbmb_is_null(const struct ncbmbuf *buf) return buf->size == 0; } +void ncbmb_init(struct ncbmbuf *buf, ncb_sz_t head); struct ncbmbuf ncbmb_make(char *area, ncb_sz_t size, ncb_sz_t head); -<<<<<<< HEAD -/* Returns the usable size of for data storage. This is the size of the - * allocated buffer without the bitmap space. - */ -======= +/* Returns start of allocated buffer area. */ static inline char *ncbmb_orig(const struct ncbmbuf *buf) { return buf->area; } +/* Returns current head pointer into buffer area. */ static inline char *ncbmb_head(const struct ncbmbuf *buf) { return buf->area + buf->head; } +/* Returns the first byte after the allocated buffer area. */ static inline char *ncbmb_wrap(const struct ncbmbuf *buf) { return buf->area + buf->size; } ->>>>>>> 932ad4878 (MINOR: ncbmbuf: support wrapping during add operation) +/* 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)) @@ -39,6 +40,8 @@ static inline ncb_sz_t ncbmb_size(const struct ncbmbuf *buf) return buf->size; } +int ncbmb_is_empty(const struct ncbmbuf *buf); + ncb_sz_t ncbmb_data(const struct ncbmbuf *buf, ncb_sz_t offset); enum ncb_ret ncbmb_add(struct ncbmbuf *buf, ncb_sz_t off, diff --git a/src/ncbmbuf.c b/src/ncbmbuf.c index 9ca28b84b..8631c8559 100644 --- a/src/ncbmbuf.c +++ b/src/ncbmbuf.c @@ -151,6 +151,19 @@ static void bit_unset(unsigned char *value, char i) /* ******** public API ******** */ +/* Initialize or reset by clearing all data. Its size is untouched. + * Buffer is positioned to offset. Use 0 to realign it. must not + * be NCBUF_NULL. + */ +void ncbmb_init(struct ncbmbuf *buf, ncb_sz_t head) +{ + BUG_ON_HOT(ncbmb_is_null(buf)); + + BUG_ON_HOT(head >= buf->size); + buf->head = head; + memset(buf->bitmap, 0, buf->bitmap_sz); +} + /* Construct a ncbmbuf with all its parameters. */ struct ncbmbuf ncbmb_make(char *area, ncb_sz_t size, ncb_sz_t head) { @@ -178,8 +191,17 @@ ncb_sz_t ncbmb_total_data(const struct ncbmbuf *buf) int ncbmb_is_empty(const struct ncbmbuf *buf) { - /* TODO */ - return 0; + size_t i = 0; + + if (ncbmb_is_null(buf)) + return 1; + + for (i = 0; i < buf->bitmap_sz; ++i) { + if (buf->bitmap[i]) + return 0; + } + + return 1; } int ncbmb_is_full(const struct ncbmbuf *buf)