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 1f89e9b..8266773 100644
--- a/include/common/chunk.h
+++ b/include/common/chunk.h
@@ -50,10 +50,10 @@
 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 bdf63c2..7e939ee 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -773,8 +773,11 @@
 			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 6090212..e99535a 100644
--- a/src/chunk.c
+++ b/src/chunk.c
@@ -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 @@
 /* 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 @@
 /* (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 @@
 	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 30e850c..c27fca0 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -177,9 +177,6 @@
 
 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 @@
 
 	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 @@
 
 	cfg_unregister_sections();
 
-	free_trash_buffers();
-	chunk_destroy(&trash);
+	deinit_trash_buffers();
 
 	protocol_unbind_all();