From: Aurelien DARRAGON Date: Thu, 7 Nov 2024 09:01:40 +0000 (+0100) Subject: MINOR: pattern: add _pat_ref_new() helper func X-Git-Tag: v3.1-dev12~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d1397401f04b4f920b1d545912f96d9f67c23daf;p=thirdparty%2Fhaproxy.git MINOR: pattern: add _pat_ref_new() helper func pat_ref_newid() and pat_ref_new() are two functions to create and initialize a pat_ref struct based on input parameters. Both function perform the same generic allocation and initialization for pat_ref struct, thus there is quite a lot of code redundancy. This is error-prone if the pat_ref init sequence has to be updated at some point. To reduce maintenance costs, let's add a _pat_ref_new() helper func that takes care of the generic allocation and base initialization for pat_ref struct. --- diff --git a/src/pattern.c b/src/pattern.c index 6817bf11e9..bc3d8537ec 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -1792,13 +1792,12 @@ int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char ** return 1; } -/* This function creates a new reference. is the reference name. - * are PAT_REF_*. /!\ The reference is not checked, and must - * be unique. The user must check the reference with "pat_ref_lookup()" - * before calling this function. If the function fails, it returns NULL, - * otherwise it returns the new struct pat_ref. +/* helper function to create and initialize a generic pat_ref struct + * + * Returns the new struct on success and NULL on failure (memory allocation + * error) */ -struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags) +static struct pat_ref *_pat_ref_new(const char *display, unsigned int flags) { struct pat_ref *ref; @@ -1806,6 +1805,10 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned if (!ref) return NULL; + /* For now is assumed was allocated with calloc() thus we don't + * have to explicitly set all members to 0. + */ + if (display) { ref->display = strdup(display); if (!ref->display) { @@ -1814,16 +1817,44 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned } } + ref->reference = NULL; + ref->flags = flags; + ref->curr_gen = 0; + ref->next_gen = 0; + ref->unique_id = -1; + ref->revision = 0; + ref->entry_cnt = 0; + LIST_INIT(&ref->head); + ref->ebmb_root = EB_ROOT; + LIST_INIT(&ref->pat); + HA_RWLOCK_INIT(&ref->lock); + + return ref; +} + +/* This function creates a new reference. is the reference name. + * are PAT_REF_*. /!\ The reference is not checked, and must + * be unique. The user must check the reference with "pat_ref_lookup()" + * before calling this function. If the function fails, it returns NULL, + * otherwise it returns the new struct pat_ref. + */ +struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags) +{ + struct pat_ref *ref; + + ref = _pat_ref_new(display, flags); + if (!ref) + return NULL; if (strlen(reference) > 5 && strncmp(reference, "virt@", 5) == 0) - flags |= PAT_REF_ID; + ref->flags |= PAT_REF_ID; else if (strlen(reference) > 4 && strncmp(reference, "opt@", 4) == 0) { - flags |= (PAT_REF_ID|PAT_REF_FILE); // Will be decided later + ref->flags |= (PAT_REF_ID|PAT_REF_FILE); // Will be decided later reference += 4; } else { /* A file by default */ - flags |= PAT_REF_FILE; + ref->flags |= PAT_REF_FILE; /* Skip file@ prefix to be mixed with ref omitting the prefix */ if (strlen(reference) > 5 && strncmp(reference, "file@", 5) == 0) reference += 5; @@ -1837,17 +1868,7 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned return NULL; } - ref->flags = flags; - ref->unique_id = -1; - ref->revision = 0; - ref->entry_cnt = 0; - - LIST_INIT(&ref->head); - ref->ebmb_root = EB_ROOT; - LIST_INIT(&ref->pat); - HA_RWLOCK_INIT(&ref->lock); LIST_APPEND(&pattern_reference, &ref->list); - return ref; } @@ -1862,29 +1883,13 @@ struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int f { struct pat_ref *ref; - ref = calloc(1, sizeof(*ref)); + ref = _pat_ref_new(display, flags); if (!ref) return NULL; - if (display) { - ref->display = strdup(display); - if (!ref->display) { - free(ref); - return NULL; - } - } - - ref->reference = NULL; - ref->flags = flags; - ref->curr_gen = 0; - ref->next_gen = 0; ref->unique_id = unique_id; - LIST_INIT(&ref->head); - ref->ebmb_root = EB_ROOT; - LIST_INIT(&ref->pat); - HA_RWLOCK_INIT(&ref->lock); - LIST_APPEND(&pattern_reference, &ref->list); + LIST_APPEND(&pattern_reference, &ref->list); return ref; }