]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: hbuf: add a very lightweight hbuf API
authorFrederic Lecaille <flecaille@haproxy.com>
Mon, 2 Mar 2026 09:25:17 +0000 (10:25 +0100)
committerFrederic Lecaille <flecaille@haproxy.com>
Fri, 24 Apr 2026 09:32:45 +0000 (11:32 +0200)
Add a new lightweight hbuf API to buffer formatted strings, similar to the
existing buffer API (struct buffer). This is required by haterm to build
its configuration in memory (fileless mode).

Update haterm to use this new API.

Note: hstream_str_buf_append() has been renamed to hbuf_str_append().

Makefile
include/haproxy/hbuf.h [new file with mode: 0644]
src/haterm_init.c
src/hbuf.c [new file with mode: 0644]

index 624d58e062f8c3d8ff328cc567a89f1ab64ea9f1..8bde128285be5f019a7b89ba85671066871f13d3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1020,7 +1020,7 @@ ifneq ($(TRACE),)
   OBJS += src/calltrace.o
 endif
 
-HATERM_OBJS += $(OBJS) src/haterm_init.o
+HATERM_OBJS += $(OBJS) src/haterm_init.o src/hbuf.o
 
 # Used only for forced dependency checking. May be cleared during development.
 INCLUDES = $(wildcard include/*/*.h)
diff --git a/include/haproxy/hbuf.h b/include/haproxy/hbuf.h
new file mode 100644 (file)
index 0000000..373d2f0
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef _HAPROXY_HBUF_H
+#define _HAPROXY_HBUF_H
+
+#include <stdlib.h>
+
+/* Very small API similar to buffer API to carefully bufferize some strings */
+#define HBUF_NULL ((struct hbuf) { })
+#define HBUF_SIZE (16 << 10) /* bytes */
+
+struct hbuf {
+       char *area;
+       size_t data;
+       size_t size;
+};
+
+static inline struct hbuf *hbuf_alloc(struct hbuf *h)
+{
+       h->area = malloc(HBUF_SIZE);
+       if (!h->area)
+               return NULL;
+
+       h->size = HBUF_SIZE;
+       h->data = 0;
+       return h;
+}
+
+static inline void free_hbuf(struct hbuf *h)
+{
+       free(h->area);
+       h->area = NULL;
+}
+
+static inline size_t hbuf_is_null(const struct hbuf *h)
+{
+       return h->size == 0;
+}
+
+__attribute__ ((format(printf, 2, 3)))
+void hbuf_appendf(struct hbuf *h, char *fmt, ...);
+void hbuf_str_append(struct hbuf *h, const char *line);
+
+#endif /* _HAPROXY_HBUF_H */
index a56ac3eb3238672d818f9ed07d4fe9a1450bfc4f..f166fa1b761a4ddbcccc5f29e63f0f43365ea280 100644 (file)
@@ -3,6 +3,7 @@
 #include <haproxy/chunk.h>
 #include <haproxy/errors.h>
 #include <haproxy/global.h>
+#include <haproxy/hbuf.h>
 #include <haproxy/version.h>
 
 static int haterm_debug;
@@ -58,104 +59,6 @@ static const char *haterm_cfg_traces_str =
             "\ttrace h3 sink stderr level user start now verbosity minimal\n"
             "\ttrace qmux sink stderr level user start now verbosity minimal\n";
 
-/* Very small API similar to buffer API to carefully build some strings */
-#define HBUF_NULL ((struct hbuf) { })
-#define HBUF_SIZE (16 << 10) /* bytes */
-struct hbuf {
-       char *area;
-       size_t data;
-       size_t size;
-};
-
-static struct hbuf *hbuf_alloc(struct hbuf *h)
-{
-       h->area = malloc(HBUF_SIZE);
-       if (!h->area)
-               return NULL;
-
-       h->size = HBUF_SIZE;
-       h->data = 0;
-       return h;
-}
-
-static inline void free_hbuf(struct hbuf *h)
-{
-       free(h->area);
-       h->area = NULL;
-}
-
-__attribute__ ((format(printf, 2, 3)))
-static void hbuf_appendf(struct hbuf *h, char *fmt, ...)
-{
-       va_list argp;
-       size_t room;
-       int ret;
-
-       room = h->size - h->data;
-       if (!room)
-               return;
-
-       va_start(argp, fmt);
-       ret = vsnprintf(h->area + h->data, room, fmt, argp);
-       if (ret >= room)
-               h->area[h->data] = '\0';
-       else
-               h->data += ret;
-       va_end(argp);
-}
-
-static inline size_t hbuf_is_null(const struct hbuf *h)
-{
-       return h->size == 0;
-}
-
-/* Simple function, to append <line> to <b> without without
- * trailing '\0' character.
- * Take into an account the '\t' and '\n' escaped sequences.
- */
-static void hstream_str_buf_append(struct hbuf *h, const char *line)
-{
-       const char *p, *end;
-       char *to = h->area + h->data;
-       char *wrap = h->area + h->size;
-       int nl = 0; /* terminal '\n' */
-
-       p = line;
-       end = line + strlen(line);
-
-       /* prepend '\t' if missing */
-       if (strncmp(line, "\\t", 2) != 0 && to < wrap) {
-               *to++ = '\t';
-               h->data++;
-       }
-
-       while (p < end && to < wrap) {
-               if (*p == '\\') {
-                       if (!*++p || p >= end)
-                               break;
-                       if (*p == 'n') {
-                               *to++ = '\n';
-                               if (p + 1 >= end)
-                                       nl = 1;
-                       }
-                       else if (*p == 't')
-                               *to++ = '\t';
-                       p++;
-                       h->data++;
-               }
-               else {
-                       *to++ = *p++;
-                       h->data++;
-               }
-       }
-
-       /* add a terminal '\n' if not already present */
-       if (to < wrap && !nl) {
-               *to++ = '\n';
-               h->data++;
-       }
-}
-
 /* This function initialises the haterm HTTP benchmark server from
  * <argv>. This consists in building a configuration file in memory
  * using the haproxy configuration language.
@@ -318,7 +221,7 @@ void haproxy_init_args(int argc, char **argv)
                                        hbuf_appendf(&fbuf, "\toption accept-unsafe-violations-in-http-request\n");
                                }
 
-                               hstream_str_buf_append(&fbuf, *argv);
+                               hbuf_str_append(&fbuf, *argv);
                        }
                        else if (*opt == 'G') {
                                argv++; argc--;
@@ -334,7 +237,7 @@ void haproxy_init_args(int argc, char **argv)
                                        hbuf_appendf(&gbuf, "global\n");
                                }
 
-                               hstream_str_buf_append(&gbuf, *argv);
+                               hbuf_str_append(&gbuf, *argv);
                        }
                        else if (*opt == 'T') {
                                argv++; argc--;
@@ -347,7 +250,7 @@ void haproxy_init_args(int argc, char **argv)
                                }
 
                                haterm_debug = 1;
-                               hstream_str_buf_append(&tbuf, *argv);
+                               hbuf_str_append(&tbuf, *argv);
                        }
                        else if (*opt == 'L') {
                                /* binding */
diff --git a/src/hbuf.c b/src/hbuf.c
new file mode 100644 (file)
index 0000000..6c16580
--- /dev/null
@@ -0,0 +1,73 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <haproxy/hbuf.h>
+
+__attribute__ ((format(printf, 2, 3)))
+void hbuf_appendf(struct hbuf *h, char *fmt, ...)
+{
+       va_list argp;
+       size_t room;
+       int ret;
+
+       room = h->size - h->data;
+       if (!room)
+               return;
+
+       va_start(argp, fmt);
+       ret = vsnprintf(h->area + h->data, room, fmt, argp);
+       if (ret >= room)
+               h->area[h->data] = '\0';
+       else
+               h->data += ret;
+       va_end(argp);
+}
+
+/* Simple function, to append <line> to <b> without without
+ * trailing '\0' character.
+ * Take into an account the '\t' and '\n' escaped sequeces.
+ */
+void hbuf_str_append(struct hbuf *h, const char *line)
+{
+       const char *p, *end;
+       char *to = h->area + h->data;
+       char *wrap = h->area + h->size;
+       int nl = 0; /* terminal '\n' */
+
+       p = line;
+       end = line + strlen(line);
+
+       /* prepend '\t' if missing */
+       if (strncmp(line, "\\t", 2) != 0 && to < wrap) {
+               *to++ = '\t';
+               h->data++;
+       }
+
+       while (p < end && to < wrap) {
+               if (*p == '\\') {
+                       if (!*++p || p >= end)
+                               break;
+                       if (*p == 'n') {
+                               *to++ = '\n';
+                               if (p + 1 >= end)
+                                       nl = 1;
+                       }
+                       else if (*p == 't')
+                               *to++ = '\t';
+                       p++;
+                       h->data++;
+               }
+               else {
+                       *to++ = *p++;
+                       h->data++;
+               }
+       }
+
+       /* add a terminal '\n' if not already present */
+       if (to < wrap && !nl) {
+               *to++ = '\n';
+               h->data++;
+       }
+}
+