diff options
author | Alejandro Colomar <alx.manpages@gmail.com> | 2021-12-18 21:39:01 +0100 |
---|---|---|
committer | Alejandro Colomar <alx.manpages@gmail.com> | 2022-04-26 12:38:48 +0200 |
commit | e525605d057fd923aa2728babe5b49e95d86d22b (patch) | |
tree | 3ca2e29947b00c35eb231ab960ed51a0920ed602 /src/nxt_conf.c | |
parent | 940d695f827eaffd19723429732c07e2dc7fb467 (diff) | |
download | unit-e525605d057fd923aa2728babe5b49e95d86d22b.tar.gz unit-e525605d057fd923aa2728babe5b49e95d86d22b.tar.bz2 |
Added new array APIs that also work with non-arrays.
Similar to how C pointers to variables can always be considered as
pointers to the first element of an array of size 1 (see the
following code for an example of how they are equivalent),
treating non-NXT_CONF_VALUE_ARRAY as if they were
NXT_CONF_VALUE_ARRAYs of size 1 allows for simpler and more
generic code.
void foo(ptrdiff_t sz, int arr[sz])
{
for (ptrdiff_t i = 0; i < sz; i++)
arr[i] = 0;
}
void bar(void)
{
int x;
int y[1];
foo(1, &x);
foo(1, y);
}
nxt_conf_array_elements_count_or_1():
Similar to nxt_conf_array_elements_count().
Return a size of 1 when input is non-array, instead of
causing undefined behavior. That value (1) makes sense
because it will be used as the limiter of a loop that
loops over the array and calls
nxt_conf_get_array_element_or_itself(), which will return
a correct element for such loops.
nxt_conf_get_array_element_or_itself():
Similar to nxt_conf_get_array_element().
Return the input pointer unmodified (i.e., a pointer to
the unique element of a hypothetical array), instead of
returning NULL, which wasn't very useful.
nxt_conf_array_qsort():
Since it's a no-op for non-arrays, this API can be reused.
Diffstat (limited to '')
-rw-r--r-- | src/nxt_conf.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/nxt_conf.c b/src/nxt_conf.c index 8546e2b7..79e776a0 100644 --- a/src/nxt_conf.c +++ b/src/nxt_conf.c @@ -393,6 +393,13 @@ nxt_conf_array_elements_count(nxt_conf_value_t *value) nxt_uint_t +nxt_conf_array_elements_count_or_1(nxt_conf_value_t *value) +{ + return (value->type == NXT_CONF_VALUE_ARRAY) ? value->u.array->count : 1; +} + + +nxt_uint_t nxt_conf_type(nxt_conf_value_t *value) { switch (value->type) { @@ -750,6 +757,25 @@ nxt_conf_get_array_element(nxt_conf_value_t *value, uint32_t index) } +nxt_conf_value_t * +nxt_conf_get_array_element_or_itself(nxt_conf_value_t *value, uint32_t index) +{ + nxt_conf_array_t *array; + + if (value->type != NXT_CONF_VALUE_ARRAY) { + return (index == 0) ? value : NULL; + } + + array = value->u.array; + + if (index >= array->count) { + return NULL; + } + + return &array->elements[index]; +} + + void nxt_conf_array_qsort(nxt_conf_value_t *value, int (*compare)(const void *, const void *)) |