]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ncbuf2: implement add
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Tue, 14 Oct 2025 14:17:14 +0000 (16:17 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 17 Oct 2025 07:29:01 +0000 (09:29 +0200)
include/haproxy/ncbuf2-t.h
include/haproxy/ncbuf2.h
src/ncbuf2.c

index 31411dd9b173ab5ee9ab4f8702387f0691c2f966..7e1aedc3f93982174f4be298c1c658fd28e91580 100644 (file)
@@ -11,6 +11,7 @@ struct ncbuf2 {
        char *area;
        char *bitmap;
        ncb2_sz_t size;
+       ncb2_sz_t bitmap_sz;
        ncb2_sz_t head;
 };
 
index e837187e8ad8a2b8a3f6402e6c1aeb7b8fa625e1..f7ead782f56b7e6da8ded3b52477b3016cde6511 100644 (file)
@@ -6,4 +6,8 @@
 
 struct ncbuf2 ncb2_make(char *area, ncb2_sz_t size, ncb2_sz_t head);
 
+
+enum ncb_ret ncb2_add(struct ncbuf2 *buf, ncb2_sz_t off,
+                      const char *data, ncb2_sz_t len, enum ncb_add_mode mode);
+
 #endif /* _HAPROXY_NCBUF2_H */
index 0cea43d01d528285b27ebfcfd9cbe8f14a7972fb..448fbd9a2334c442f79003f344e87dae004d22c3 100644 (file)
@@ -2,6 +2,8 @@
 
 #include <string.h>
 
+#include <haproxy/bug.h>
+
 /* ******** internal API ******** */
 
 struct itbmap {
@@ -89,9 +91,10 @@ struct ncbuf2 ncb2_make(char *area, ncb2_sz_t size, ncb2_sz_t head)
        ncb2_sz_t bitmap_sz;
 
        bitmap_sz = (size + 8) / 9;
+       buf.bitmap_sz = bitmap_sz;
 
        buf.area = area;
-       buf.bitmap = area + size - bitmap_sz;
+       buf.bitmap = area + (size - bitmap_sz);
        buf.size = size - bitmap_sz;
        buf.head = head;
 
@@ -131,10 +134,35 @@ ncb2_sz_t ncb2_data(const struct ncbuf2 *buf, ncb2_sz_t off)
 }
 
 enum ncb_ret ncb2_add(struct ncbuf2 *buf, ncb2_sz_t off,
-                       const char *data, ncb2_sz_t len, enum ncb_add_mode mode)
+                      const char *data, ncb2_sz_t len, enum ncb_add_mode mode)
 {
-       /* TODO */
-       return NCB_RET_OK;
+       char *b;
+
+       BUG_ON_HOT(off + len > buf->size);
+       /* first copy data into buffer */
+       memcpy(&buf->area[off], data, len);
+
+       /* adjust bitmap to reflect newly filled content */
+       b = buf->bitmap + (off / 8);
+       if (off % 8) {
+               size_t to_copy = len < 8 - (off % 8) ? len : 8 - (off % 8);
+               /* adjust first bitmap byte relative shifted by offset */
+               *b++ |= ((unsigned char)(0xff << (8 - to_copy))) >> (off % 8);
+               len -= to_copy;
+       }
+
+       if (len) {
+               size_t to_copy = len / 8;
+               /* bulk set bitmap as many as possible */
+               memset(b, 0xff, to_copy);
+               len -= 8 * to_copy;
+               b += to_copy;
+
+               if (len) {
+                       /* adjust last bitmap byte shifted by remaining len */
+                       *b |= 0xff << (8 - len);
+               }
+       }
 }
 
 enum ncb_ret ncb2_advance(struct ncbuf2 *buf, ncb2_sz_t adv)