]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ncbmbuf: 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>
Mon, 20 Oct 2025 12:30:37 +0000 (14:30 +0200)
include/haproxy/ncbmbuf.h
src/ncbmbuf.c

index 4ca5ea728b1a8f3a142fc40077eb806c7abea7df..4974f53cfe0e1cd6523324ac6500da3f9c15533f 100644 (file)
@@ -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 <buf> 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 <buf> 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,
index 9ca28b84beb749b5c6a68121d03de0119cf4439a..8631c8559790690bd6c820613b1035f9f6768cc8 100644 (file)
@@ -151,6 +151,19 @@ static void bit_unset(unsigned char *value, char i)
 
 /* ******** public API ******** */
 
+/* Initialize or reset <buf> by clearing all data. Its size is untouched.
+ * Buffer is positioned to <head> offset. Use 0 to realign it. <buf> 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)