]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ncbuf2: implement init / is_empty
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 16 Oct 2025 14:41:24 +0000 (16:41 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 17 Oct 2025 09:06:07 +0000 (11:06 +0200)
include/haproxy/ncbuf2.h
src/ncbuf2.c

index 7220b4e78c17e1aa53cd53a9c2b150876aef59a7..40b2586c66a5e6927ad3a8a5326865120bbdf67e 100644 (file)
@@ -9,6 +9,7 @@ static inline int ncb2_is_null(const struct ncbuf2 *buf)
        return buf->size == 0;
 }
 
+void ncb2_init(struct ncbuf2 *buf, ncb2_sz_t head);
 struct ncbuf2 ncb2_make(char *area, ncb2_sz_t size, ncb2_sz_t head);
 
 static inline char *ncb2_orig(const struct ncbuf2 *buf)
@@ -34,6 +35,8 @@ static inline ncb2_sz_t ncb2_size(const struct ncbuf2 *buf)
        return buf->size;
 }
 
+int ncb2_is_empty(const struct ncbuf2 *buf);
+
 ncb2_sz_t ncb2_data(const struct ncbuf2 *buf, ncb2_sz_t offset);
 
 enum ncb_ret ncb2_add(struct ncbuf2 *buf, ncb2_sz_t off,
index ba1a6ab6147dab3adf70072da3f4aa4c80485bdd..f396a6372dc643ca3b9fb05974c529ae1e9e2ffd 100644 (file)
@@ -132,6 +132,15 @@ static void bit_unset(unsigned char *value, char i)
 
 /* ******** public API ******** */
 
+void ncb2_init(struct ncbuf2 *buf, ncb2_sz_t head)
+{
+       BUG_ON_HOT(ncb2_is_null(buf));
+
+       BUG_ON_HOT(head >= buf->size);
+       buf->head = head;
+       memset(buf->bitmap, 0, buf->bitmap_sz);
+}
+
 struct ncbuf2 ncb2_make(char *area, ncb2_sz_t size, ncb2_sz_t head)
 {
        struct ncbuf2 buf;
@@ -158,8 +167,17 @@ ncb2_sz_t ncb2_total_data(const struct ncbuf2 *buf)
 
 int ncb2_is_empty(const struct ncbuf2 *buf)
 {
-       /* TODO */
-       return 0;
+       size_t i = 0;
+
+       if (ncb2_is_null(buf))
+               return 1;
+
+       for (i = 0; i < buf->bitmap_sz; ++i) {
+               if (buf->bitmap[i])
+                       return 0;
+       }
+
+       return 1;
 }
 
 int ncb2_is_full(const struct ncbuf2 *buf)