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.
This commit is contained in:
Christopher Faulet 2017-07-26 14:59:46 +02:00 committed by Willy Tarreau
parent 6c57dc9145
commit 748919a4c7
4 changed files with 29 additions and 13 deletions

View 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_asciiencode(struct chunk *dst, struct chunk *src, char qc);
int chunk_strcmp(const struct chunk *chk, const char *str); int chunk_strcmp(const struct chunk *chk, const char *str);
int chunk_strcasecmp(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 *get_trash_chunk(void);
struct chunk *alloc_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. * free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL.

View File

@ -773,8 +773,11 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
err_code |= ERR_ALERT | ERR_FATAL; err_code |= ERR_ALERT | ERR_FATAL;
goto out; goto out;
} }
chunk_init(&trash, realloc(trash.str, global.tune.bufsize), global.tune.bufsize); if (!init_trash_buffers()) {
alloc_trash_buffers(global.tune.bufsize); 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")) { else if (!strcmp(args[0], "tune.maxrewrite")) {
if (alertif_too_many_args(1, file, linenum, args, &err_code)) if (alertif_too_many_args(1, file, linenum, args, &err_code))

View File

@ -19,6 +19,8 @@
#include <common/chunk.h> #include <common/chunk.h>
#include <common/standard.h> #include <common/standard.h>
#include <types/global.h>
/* trash chunks used for various conversions */ /* trash chunks used for various conversions */
static struct chunk *trash_chunk; static struct chunk *trash_chunk;
static struct chunk trash_chunk1; static struct chunk trash_chunk1;
@ -32,6 +34,9 @@ static char *trash_buf2;
/* the trash pool for reentrant allocations */ /* the trash pool for reentrant allocations */
struct pool_head *pool2_trash = NULL; 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 * 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 * 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 /* (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. * 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_size = bufsize;
trash_buf1 = (char *)my_realloc2(trash_buf1, 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; 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 * free the trash buffers
*/ */
void free_trash_buffers(void) void deinit_trash_buffers(void)
{ {
chunk_destroy(&trash);
free(trash_buf2); free(trash_buf2);
free(trash_buf1); free(trash_buf1);
trash_buf2 = NULL; trash_buf2 = NULL;

View File

@ -177,9 +177,6 @@ static const char *old_unixsocket;
static char *cur_unixsocket = NULL; 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 /* this buffer is always the same size as standard buffers and is used for
* swapping data inside a buffer. * swapping data inside a buffer.
*/ */
@ -1137,8 +1134,10 @@ static void init(int argc, char **argv)
next_argv = copy_argv(argc, argv); next_argv = copy_argv(argc, argv);
chunk_init(&trash, malloc(global.tune.bufsize), global.tune.bufsize); if (!init_trash_buffers()) {
alloc_trash_buffers(global.tune.bufsize); Alert("failed to initialize trash buffers.\n");
exit(1);
}
/* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate /* 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 * the string in case of truncation, and at least FreeBSD appears not to do
@ -2102,8 +2101,7 @@ void deinit(void)
cfg_unregister_sections(); cfg_unregister_sections();
free_trash_buffers(); deinit_trash_buffers();
chunk_destroy(&trash);
protocol_unbind_all(); protocol_unbind_all();