CLEANUP: server: a separate function for initializing the per_thr field
To avoid repeating the same source code, allocating memory and initializing
the per_thr field from the server structure is transferred to a separate
function.
diff --git a/include/haproxy/server.h b/include/haproxy/server.h
index 74ac740..b807a8a 100644
--- a/include/haproxy/server.h
+++ b/include/haproxy/server.h
@@ -60,6 +60,7 @@
struct server *cli_find_server(struct appctx *appctx, char *arg);
struct server *new_server(struct proxy *proxy);
void free_server(struct server *srv);
+int srv_init_per_thr(struct server *srv);
/* functions related to server name resolution */
int srv_prepare_for_resolution(struct server *srv, const char *hostname);
diff --git a/src/cfgparse.c b/src/cfgparse.c
index 4fa5bcc..aa07a2f 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -3832,21 +3832,13 @@
list_for_each_entry(newsrv, &servers_list, global_list) {
/* initialize idle conns lists */
- newsrv->per_thr = calloc(global.nbthread, sizeof(*newsrv->per_thr));
- if (!newsrv->per_thr) {
+ if (srv_init_per_thr(newsrv) == -1) {
ha_alert("parsing [%s:%d] : failed to allocate per-thread lists for server '%s'.\n",
newsrv->conf.file, newsrv->conf.line, newsrv->id);
cfgerr++;
continue;
}
- for (i = 0; i < global.nbthread; i++) {
- newsrv->per_thr[i].idle_conns = EB_ROOT;
- newsrv->per_thr[i].safe_conns = EB_ROOT;
- newsrv->per_thr[i].avail_conns = EB_ROOT;
- MT_LIST_INIT(&newsrv->per_thr[i].streams);
- }
-
if (newsrv->max_idle_conns != 0) {
newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
if (!newsrv->curr_idle_thr) {
diff --git a/src/server.c b/src/server.c
index b3d82f1..471139b 100644
--- a/src/server.c
+++ b/src/server.c
@@ -4326,6 +4326,27 @@
return 1;
}
+/* Memory allocation and initialization of the per_thr field.
+ * Returns 0 if the field has been successfully initialized, -1 on failure.
+ */
+int srv_init_per_thr(struct server *srv)
+{
+ int i;
+
+ srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
+ if (!srv->per_thr)
+ return -1;
+
+ for (i = 0; i < global.nbthread; i++) {
+ srv->per_thr[i].idle_conns = EB_ROOT;
+ srv->per_thr[i].safe_conns = EB_ROOT;
+ srv->per_thr[i].avail_conns = EB_ROOT;
+ MT_LIST_INIT(&srv->per_thr[i].streams);
+ }
+
+ return 0;
+}
+
/* Parse a "add server" command
* Returns 0 if the server has been successfully initialized, 1 on failure.
*/
@@ -4335,7 +4356,7 @@
struct server *srv;
char *be_name, *sv_name;
int errcode, argc;
- int next_id, i;
+ int next_id;
const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
usermsgs_clr("CLI");
@@ -4405,19 +4426,11 @@
}
}
- srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
- if (!srv->per_thr) {
+ if (srv_init_per_thr(srv) == -1) {
ha_alert("failed to allocate per-thread lists for server.\n");
goto out;
}
- for (i = 0; i < global.nbthread; i++) {
- srv->per_thr[i].idle_conns = EB_ROOT;
- srv->per_thr[i].safe_conns = EB_ROOT;
- srv->per_thr[i].avail_conns = EB_ROOT;
- MT_LIST_INIT(&srv->per_thr[i].streams);
- }
-
if (srv->max_idle_conns != 0) {
srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
if (!srv->curr_idle_thr) {
diff --git a/src/sink.c b/src/sink.c
index 6b8a234..30dcfd5 100644
--- a/src/sink.c
+++ b/src/sink.c
@@ -939,7 +939,6 @@
struct sink *sink = NULL;
struct server *srv = NULL;
struct sink_forward_target *sft = NULL;
- int i;
/* allocate new proxy to handle
* forward to a stream server
@@ -971,17 +970,9 @@
HA_SPIN_INIT(&srv->lock);
/* process per thread init */
- srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
- if (!srv->per_thr)
+ if (srv_init_per_thr(srv) == -1)
goto error;
- for (i = 0; i < global.nbthread; i++) {
- srv->per_thr[i].idle_conns = EB_ROOT;
- srv->per_thr[i].safe_conns = EB_ROOT;
- srv->per_thr[i].avail_conns = EB_ROOT;
- MT_LIST_INIT(&srv->per_thr[i].streams);
- }
-
/* the servers are linked backwards
* first into proxy
*/