]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: chunks: Use dedicated function to init/deinit trash buffers
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 26 Jul 2017 12:59:46 +0000 (14:59 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 5 Sep 2017 08:22:20 +0000 (10:22 +0200)
Now, we use init_trash_buffers and deinit_trash_buffers to, respectively,
initialize and deinitialize trash buffers (trash, trash_buf1 and trash_buf2).

These functions have been introduced to be used by threads, to deal with
thread-local trash buffers.

include/common/chunk.h
src/cfgparse.c
src/chunk.c
src/haproxy.c

index 1f89e9bf21152f15d58f591d16102c32ddaa9471..82667733a952b06dcf4d21d3d825d15a8da8cfce 100644 (file)
@@ -50,10 +50,10 @@ int chunk_htmlencode(struct chunk *dst, struct chunk *src);
 int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
 int chunk_strcmp(const struct chunk *chk, const char *str);
 int chunk_strcasecmp(const struct chunk *chk, const char *str);
-int alloc_trash_buffers(int bufsize);
-void free_trash_buffers(void);
 struct chunk *get_trash_chunk(void);
 struct chunk *alloc_trash_chunk(void);
+int init_trash_buffers(void);
+void deinit_trash_buffers(void);
 
 /*
  * free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL.
index bdf63c2add319a4cc3c0b210376f99f4bd3d9123..7e939eea199a30bb585edc04bf7f35a36808b689 100644 (file)
@@ -773,8 +773,11 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
                }
-               chunk_init(&trash, realloc(trash.str, global.tune.bufsize), global.tune.bufsize);
-               alloc_trash_buffers(global.tune.bufsize);
+               if (!init_trash_buffers()) {
+                       Alert("parsing [%s:%d] : failed to initialize trash buffers.\n", file, linenum);
+                       err_code |= ERR_ALERT | ERR_FATAL;
+                       goto out;
+               }
        }
        else if (!strcmp(args[0], "tune.maxrewrite")) {
                if (alertif_too_many_args(1, file, linenum, args, &err_code))
index 60902127c64c4eee688f2f15527c0fdaf6556070..e99535a438293a6ef2d443ec81431814d3b6c730 100644 (file)
@@ -19,6 +19,8 @@
 #include <common/chunk.h>
 #include <common/standard.h>
 
+#include <types/global.h>
+
 /* trash chunks used for various conversions */
 static struct chunk *trash_chunk;
 static struct chunk trash_chunk1;
@@ -32,6 +34,9 @@ static char *trash_buf2;
 /* the trash pool for reentrant allocations */
 struct pool_head *pool2_trash = NULL;
 
+/* this is used to drain data, and as a temporary buffer for sprintf()... */
+struct chunk trash = { .str = NULL };
+
 /*
 * Returns a pre-allocated and initialized trash chunk that can be used for any
 * type of conversion. Two chunks and their respective buffers are alternatively
@@ -61,7 +66,7 @@ struct chunk *get_trash_chunk(void)
 /* (re)allocates the trash buffers. Returns 0 in case of failure. It is
  * possible to call this function multiple times if the trash size changes.
  */
-int alloc_trash_buffers(int bufsize)
+static int alloc_trash_buffers(int bufsize)
 {
        trash_size = bufsize;
        trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize);
@@ -70,11 +75,21 @@ int alloc_trash_buffers(int bufsize)
        return trash_buf1 && trash_buf2 && pool2_trash;
 }
 
+/* Initialize the trash buffers. It returns 0 if an error occurred. */
+int init_trash_buffers()
+{
+       chunk_init(&trash, my_realloc2(trash.str, global.tune.bufsize), global.tune.bufsize);
+       if (!trash.str || !alloc_trash_buffers(global.tune.bufsize))
+               return 0;
+       return 1;
+}
+
 /*
  * free the trash buffers
  */
-void free_trash_buffers(void)
+void deinit_trash_buffers(void)
 {
+       chunk_destroy(&trash);
        free(trash_buf2);
        free(trash_buf1);
        trash_buf2 = NULL;
index 30e850c4ac4719b71adccb3b6bd41248ef5bb470..c27fca0ddc59462d665a74715c83d3e8f4d67d4f 100644 (file)
@@ -177,9 +177,6 @@ static const char *old_unixsocket;
 
 static char *cur_unixsocket = NULL;
 
-/* this is used to drain data, and as a temporary buffer for sprintf()... */
-struct chunk trash = { };
-
 /* this buffer is always the same size as standard buffers and is used for
  * swapping data inside a buffer.
  */
@@ -1137,8 +1134,10 @@ static void init(int argc, char **argv)
 
        next_argv = copy_argv(argc, argv);
 
-       chunk_init(&trash, malloc(global.tune.bufsize), global.tune.bufsize);
-       alloc_trash_buffers(global.tune.bufsize);
+       if (!init_trash_buffers()) {
+               Alert("failed to initialize trash buffers.\n");
+               exit(1);
+       }
 
        /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
         * the string in case of truncation, and at least FreeBSD appears not to do
@@ -2102,8 +2101,7 @@ void deinit(void)
 
        cfg_unregister_sections();
 
-       free_trash_buffers();
-       chunk_destroy(&trash);
+       deinit_trash_buffers();
 
        protocol_unbind_all();