Skip to content

Commit d139740

Browse files
committed
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.
1 parent 79a346a commit d139740

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

src/pattern.c

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,20 +1792,23 @@ int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **
17921792
return 1;
17931793
}
17941794

1795-
/* This function creates a new reference. <ref> is the reference name.
1796-
* <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1797-
* be unique. The user must check the reference with "pat_ref_lookup()"
1798-
* before calling this function. If the function fails, it returns NULL,
1799-
* otherwise it returns the new struct pat_ref.
1795+
/* helper function to create and initialize a generic pat_ref struct
1796+
*
1797+
* Returns the new struct on success and NULL on failure (memory allocation
1798+
* error)
18001799
*/
1801-
struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
1800+
static struct pat_ref *_pat_ref_new(const char *display, unsigned int flags)
18021801
{
18031802
struct pat_ref *ref;
18041803

18051804
ref = calloc(1, sizeof(*ref));
18061805
if (!ref)
18071806
return NULL;
18081807

1808+
/* For now is assumed <ref> was allocated with calloc() thus we don't
1809+
* have to explicitly set all members to 0.
1810+
*/
1811+
18091812
if (display) {
18101813
ref->display = strdup(display);
18111814
if (!ref->display) {
@@ -1814,16 +1817,44 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned
18141817
}
18151818
}
18161819

1820+
ref->reference = NULL;
1821+
ref->flags = flags;
1822+
ref->curr_gen = 0;
1823+
ref->next_gen = 0;
1824+
ref->unique_id = -1;
1825+
ref->revision = 0;
1826+
ref->entry_cnt = 0;
1827+
LIST_INIT(&ref->head);
1828+
ref->ebmb_root = EB_ROOT;
1829+
LIST_INIT(&ref->pat);
1830+
HA_RWLOCK_INIT(&ref->lock);
1831+
1832+
return ref;
1833+
}
1834+
1835+
/* This function creates a new reference. <ref> is the reference name.
1836+
* <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1837+
* be unique. The user must check the reference with "pat_ref_lookup()"
1838+
* before calling this function. If the function fails, it returns NULL,
1839+
* otherwise it returns the new struct pat_ref.
1840+
*/
1841+
struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
1842+
{
1843+
struct pat_ref *ref;
1844+
1845+
ref = _pat_ref_new(display, flags);
1846+
if (!ref)
1847+
return NULL;
18171848

18181849
if (strlen(reference) > 5 && strncmp(reference, "virt@", 5) == 0)
1819-
flags |= PAT_REF_ID;
1850+
ref->flags |= PAT_REF_ID;
18201851
else if (strlen(reference) > 4 && strncmp(reference, "opt@", 4) == 0) {
1821-
flags |= (PAT_REF_ID|PAT_REF_FILE); // Will be decided later
1852+
ref->flags |= (PAT_REF_ID|PAT_REF_FILE); // Will be decided later
18221853
reference += 4;
18231854
}
18241855
else {
18251856
/* A file by default */
1826-
flags |= PAT_REF_FILE;
1857+
ref->flags |= PAT_REF_FILE;
18271858
/* Skip file@ prefix to be mixed with ref omitting the prefix */
18281859
if (strlen(reference) > 5 && strncmp(reference, "file@", 5) == 0)
18291860
reference += 5;
@@ -1837,17 +1868,7 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned
18371868
return NULL;
18381869
}
18391870

1840-
ref->flags = flags;
1841-
ref->unique_id = -1;
1842-
ref->revision = 0;
1843-
ref->entry_cnt = 0;
1844-
1845-
LIST_INIT(&ref->head);
1846-
ref->ebmb_root = EB_ROOT;
1847-
LIST_INIT(&ref->pat);
1848-
HA_RWLOCK_INIT(&ref->lock);
18491871
LIST_APPEND(&pattern_reference, &ref->list);
1850-
18511872
return ref;
18521873
}
18531874

@@ -1862,29 +1883,13 @@ struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int f
18621883
{
18631884
struct pat_ref *ref;
18641885

1865-
ref = calloc(1, sizeof(*ref));
1886+
ref = _pat_ref_new(display, flags);
18661887
if (!ref)
18671888
return NULL;
18681889

1869-
if (display) {
1870-
ref->display = strdup(display);
1871-
if (!ref->display) {
1872-
free(ref);
1873-
return NULL;
1874-
}
1875-
}
1876-
1877-
ref->reference = NULL;
1878-
ref->flags = flags;
1879-
ref->curr_gen = 0;
1880-
ref->next_gen = 0;
18811890
ref->unique_id = unique_id;
1882-
LIST_INIT(&ref->head);
1883-
ref->ebmb_root = EB_ROOT;
1884-
LIST_INIT(&ref->pat);
1885-
HA_RWLOCK_INIT(&ref->lock);
1886-
LIST_APPEND(&pattern_reference, &ref->list);
18871891

1892+
LIST_APPEND(&pattern_reference, &ref->list);
18881893
return ref;
18891894
}
18901895

0 commit comments

Comments
 (0)