From: Christopher Faulet Date: Wed, 26 Jul 2017 12:59:46 +0000 (+0200) Subject: MINOR: chunks: Use dedicated function to init/deinit trash buffers X-Git-Tag: v1.8-dev3~129 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=748919a4c75495bc6464e82b52dd9b71cbbc23ca;p=thirdparty%2Fhaproxy.git MINOR: chunks: Use dedicated function to init/deinit trash buffers 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. --- diff --git a/include/common/chunk.h b/include/common/chunk.h index 1f89e9bf21..82667733a9 100644 --- a/include/common/chunk.h +++ b/include/common/chunk.h @@ -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. diff --git a/src/cfgparse.c b/src/cfgparse.c index bdf63c2add..7e939eea19 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -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)) diff --git a/src/chunk.c b/src/chunk.c index 60902127c6..e99535a438 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -19,6 +19,8 @@ #include #include +#include + /* 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; diff --git a/src/haproxy.c b/src/haproxy.c index 30e850c4ac..c27fca0ddc 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -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();