MINOR: rules: add a new function new_act_rule() to allocate act_rules
Rules are currently allocated using calloc() by their caller, which does
not make it very convenient to pass more information such as the file
name and line number.
This patch introduces new_act_rule() which performs the malloc() and
already takes in argument the ruleset (ACT_F_*), the file name and the
line number. This saves the caller from having to assing ->from, and
will allow to improve the internal storage with more info.
diff --git a/include/haproxy/action.h b/include/haproxy/action.h
index 9ea5847..5d751ac 100644
--- a/include/haproxy/action.h
+++ b/include/haproxy/action.h
@@ -111,6 +111,7 @@
release_sample_expr(rule->arg.timeout.expr);
}
+struct act_rule *new_act_rule(enum act_from from, const char *file, int linenum);
void free_act_rules(struct list *rules);
#endif /* _HAPROXY_ACTION_H */
diff --git a/src/action.c b/src/action.c
index 1644e0b..a363dea 100644
--- a/src/action.c
+++ b/src/action.c
@@ -285,6 +285,21 @@
return best_ptr;
}
+/* allocates a rule for ruleset <from> (ACT_F_*), from file name <file> and
+ * line <linenum>. <file> and <linenum> may be zero if unknown. Returns the
+ * rule, otherwise NULL in case of memory allocation error.
+ */
+struct act_rule *new_act_rule(enum act_from from, const char *file, int linenum)
+{
+ struct act_rule *rule;
+
+ rule = calloc(1, sizeof(*rule));
+ if (!rule)
+ return NULL;
+ rule->from = from;
+ return rule;
+}
+
void free_act_rules(struct list *rules)
{
struct act_rule *rule, *ruleb;
diff --git a/src/http_rules.c b/src/http_rules.c
index 4182958..bcff27b 100644
--- a/src/http_rules.c
+++ b/src/http_rules.c
@@ -81,12 +81,11 @@
const struct action_kw *custom = NULL;
int cur_arg;
- rule = calloc(1, sizeof(*rule));
+ rule = new_act_rule(ACT_F_HTTP_REQ, file, linenum);
if (!rule) {
ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
goto out_err;
}
- rule->from = ACT_F_HTTP_REQ;
if (((custom = action_http_req_custom(args[0])) != NULL)) {
char *errmsg = NULL;
@@ -160,12 +159,11 @@
const struct action_kw *custom = NULL;
int cur_arg;
- rule = calloc(1, sizeof(*rule));
+ rule = new_act_rule(ACT_F_HTTP_RES, file, linenum);
if (!rule) {
ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
goto out_err;
}
- rule->from = ACT_F_HTTP_RES;
if (((custom = action_http_res_custom(args[0])) != NULL)) {
char *errmsg = NULL;
@@ -240,12 +238,11 @@
const struct action_kw *custom = NULL;
int cur_arg;
- rule = calloc(1, sizeof(*rule));
+ rule = new_act_rule(ACT_F_HTTP_RES, file, linenum);
if (!rule) {
ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
goto out_err;
}
- rule->from = ACT_F_HTTP_RES;
if (((custom = action_http_after_res_custom(args[0])) != NULL)) {
char *errmsg = NULL;
diff --git a/src/tcp_rules.c b/src/tcp_rules.c
index 9af3005..375da6b 100644
--- a/src/tcp_rules.c
+++ b/src/tcp_rules.c
@@ -1060,7 +1060,7 @@
return 0;
}
- rule = calloc(1, sizeof(*rule));
+ rule = new_act_rule(ACT_F_TCP_RES_CNT, file, line);
if (!rule) {
memprintf(err, "parsing [%s:%d] : out of memory", file, line);
return -1;
@@ -1076,7 +1076,6 @@
where |= SMP_VAL_FE_RES_CNT;
if (curpx->cap & PR_CAP_BE)
where |= SMP_VAL_BE_RES_CNT;
- rule->from = ACT_F_TCP_RES_CNT;
if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
goto error;
@@ -1178,7 +1177,7 @@
return 0;
}
- rule = calloc(1, sizeof(*rule));
+ rule = new_act_rule(0, file, line);
if (!rule) {
memprintf(err, "parsing [%s:%d] : out of memory", file, line);
return -1;
diff --git a/src/tcpcheck.c b/src/tcpcheck.c
index 6109f5a..aa6db67 100644
--- a/src/tcpcheck.c
+++ b/src/tcpcheck.c
@@ -2336,13 +2336,12 @@
struct tcpcheck_rule *chk = NULL;
struct act_rule *actrule = NULL;
- actrule = calloc(1, sizeof(*actrule));
+ actrule = new_act_rule(ACT_F_TCP_CHK, file, line);
if (!actrule) {
memprintf(errmsg, "out of memory");
goto error;
}
actrule->kw = kw;
- actrule->from = ACT_F_TCP_CHK;
cur_arg++;
if (kw->parse((const char **)args, &cur_arg, px, actrule, errmsg) == ACT_RET_PRS_ERR) {