2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "array.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2021-05-20 20:58:53 +00:00
|
|
|
|
|
|
|
__attribute_cold__
|
|
|
|
static data_unset *array_data_string_copy(const data_unset *s) {
|
|
|
|
data_string *src = (data_string *)s;
|
|
|
|
data_string *ds = array_data_string_init();
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (!buffer_is_unset(&src->key)) buffer_copy_buffer(&ds->key, &src->key);
|
2021-05-20 20:58:53 +00:00
|
|
|
buffer_copy_buffer(&ds->value, &src->value);
|
|
|
|
return (data_unset *)ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute_cold__
|
|
|
|
static void array_data_string_insert_dup(data_unset *dst, data_unset *src) {
|
|
|
|
data_string *ds_dst = (data_string *)dst;
|
|
|
|
data_string *ds_src = (data_string *)src;
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (!buffer_is_blank(&ds_dst->value))
|
2021-05-20 20:58:53 +00:00
|
|
|
buffer_append_str2(&ds_dst->value, CONST_STR_LEN(", "),
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
BUF_PTR_LEN(&ds_src->value));
|
2021-05-20 20:58:53 +00:00
|
|
|
else
|
|
|
|
buffer_copy_buffer(&ds_dst->value, &ds_src->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void array_data_string_free(data_unset *du) {
|
|
|
|
data_string *ds = (data_string *)du;
|
|
|
|
free(ds->key.ptr);
|
|
|
|
free(ds->value.ptr);
|
|
|
|
free(ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute_noinline__
|
|
|
|
data_string *array_data_string_init(void) {
|
|
|
|
static const struct data_methods fn = {
|
|
|
|
array_data_string_copy,
|
|
|
|
array_data_string_free,
|
|
|
|
array_data_string_insert_dup,
|
|
|
|
};
|
|
|
|
data_string *ds = calloc(1, sizeof(*ds));
|
|
|
|
force_assert(NULL != ds);
|
|
|
|
ds->type = TYPE_STRING;
|
|
|
|
ds->fn = &fn;
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
__attribute_cold__
|
|
|
|
static data_unset *array_data_integer_copy(const data_unset *s) {
|
|
|
|
data_integer *src = (data_integer *)s;
|
|
|
|
data_integer *di = array_data_integer_init();
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (!buffer_is_unset(&src->key)) buffer_copy_buffer(&di->key, &src->key);
|
2021-05-20 20:58:53 +00:00
|
|
|
di->value = src->value;
|
|
|
|
return (data_unset *)di;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void array_data_integer_free(data_unset *du) {
|
|
|
|
data_integer *di = (data_integer *)du;
|
|
|
|
free(di->key.ptr);
|
|
|
|
free(di);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute_noinline__
|
|
|
|
data_integer *array_data_integer_init(void) {
|
|
|
|
static const struct data_methods fn = {
|
|
|
|
array_data_integer_copy,
|
|
|
|
array_data_integer_free,
|
2021-05-23 00:34:58 +00:00
|
|
|
NULL
|
2021-05-20 20:58:53 +00:00
|
|
|
};
|
|
|
|
data_integer *di = calloc(1, sizeof(*di));
|
|
|
|
force_assert(NULL != di);
|
|
|
|
di->type = TYPE_INTEGER;
|
|
|
|
di->fn = &fn;
|
|
|
|
return di;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
__attribute_cold__
|
|
|
|
static data_unset *array_data_array_copy(const data_unset *s) {
|
|
|
|
data_array *src = (data_array *)s;
|
|
|
|
data_array *da = array_data_array_init();
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (!buffer_is_unset(&src->key)) buffer_copy_buffer(&da->key, &src->key);
|
2021-05-20 20:58:53 +00:00
|
|
|
array_copy_array(&da->value, &src->value);
|
|
|
|
return (data_unset *)da;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void array_data_array_free(data_unset *du) {
|
|
|
|
data_array *da = (data_array *)du;
|
|
|
|
free(da->key.ptr);
|
|
|
|
array_free_data(&da->value);
|
|
|
|
free(da);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute_noinline__
|
|
|
|
data_array *array_data_array_init(void) {
|
|
|
|
static const struct data_methods fn = {
|
|
|
|
array_data_array_copy,
|
|
|
|
array_data_array_free,
|
2021-05-23 00:34:58 +00:00
|
|
|
NULL
|
2021-05-20 20:58:53 +00:00
|
|
|
};
|
|
|
|
data_array *da = calloc(1, sizeof(*da));
|
|
|
|
force_assert(NULL != da);
|
|
|
|
da->type = TYPE_ARRAY;
|
|
|
|
da->fn = &fn;
|
|
|
|
return da;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
__attribute_cold__
|
2019-11-22 05:57:48 +00:00
|
|
|
static void array_extend(array * const a, uint32_t n) {
|
2021-05-18 19:19:55 +00:00
|
|
|
/* This data structure should not be used for nearly so many entries */
|
|
|
|
force_assert(a->size <= INT32_MAX-n);
|
2019-11-22 05:57:48 +00:00
|
|
|
a->size += n;
|
2019-10-02 05:54:15 +00:00
|
|
|
a->data = realloc(a->data, sizeof(*a->data) * a->size);
|
2019-10-14 05:44:06 +00:00
|
|
|
a->sorted = realloc(a->sorted, sizeof(*a->sorted) * a->size);
|
2019-10-02 05:54:15 +00:00
|
|
|
force_assert(a->data);
|
2019-10-14 05:44:06 +00:00
|
|
|
force_assert(a->sorted);
|
2019-10-02 05:54:15 +00:00
|
|
|
memset(a->data+a->used, 0, (a->size-a->used)*sizeof(*a->data));
|
|
|
|
}
|
2016-03-15 18:41:57 +00:00
|
|
|
|
2019-11-22 05:57:48 +00:00
|
|
|
array *array_init(uint32_t n) {
|
2005-02-20 14:27:00 +00:00
|
|
|
array *a;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
a = calloc(1, sizeof(*a));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(a);
|
2019-11-22 05:57:48 +00:00
|
|
|
if (n) array_extend(a, n);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2019-10-13 21:06:05 +00:00
|
|
|
void array_free_data(array * const a) {
|
2019-10-14 05:44:06 +00:00
|
|
|
if (a->sorted) free(a->sorted);
|
2019-10-13 21:06:05 +00:00
|
|
|
data_unset ** const data = a->data;
|
|
|
|
const uint32_t sz = a->size;
|
|
|
|
for (uint32_t i = 0; i < sz; ++i) {
|
|
|
|
if (data[i]) data[i]->fn->free(data[i]);
|
|
|
|
}
|
|
|
|
free(data);
|
2019-11-28 15:27:01 +00:00
|
|
|
a->data = NULL;
|
|
|
|
a->sorted = NULL;
|
|
|
|
a->used = 0;
|
|
|
|
a->size = 0;
|
2019-10-13 21:06:05 +00:00
|
|
|
}
|
2005-08-08 14:40:47 +00:00
|
|
|
|
2019-10-13 21:06:05 +00:00
|
|
|
void array_copy_array(array * const dst, const array * const src) {
|
|
|
|
array_free_data(dst);
|
|
|
|
if (0 == src->size) return;
|
2013-11-13 11:43:26 +00:00
|
|
|
|
2020-12-16 01:01:23 +00:00
|
|
|
array_extend(dst, src->size);
|
2019-10-02 05:54:15 +00:00
|
|
|
for (uint32_t i = 0; i < src->used; ++i) {
|
2020-12-16 01:01:23 +00:00
|
|
|
array_insert_unique(dst, src->data[i]->fn->copy(src->data[i]));
|
2019-10-02 05:54:15 +00:00
|
|
|
}
|
2005-08-08 14:40:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
void array_free(array * const a) {
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!a) return;
|
2019-10-13 21:06:05 +00:00
|
|
|
array_free_data(a);
|
2005-02-20 14:27:00 +00:00
|
|
|
free(a);
|
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
void array_reset_data_strings(array * const a) {
|
2018-10-23 00:28:53 +00:00
|
|
|
if (!a) return;
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
data_string ** const data = (data_string **)a->data;
|
|
|
|
const uint32_t used = a->used;
|
|
|
|
a->used = 0;
|
|
|
|
for (uint32_t i = 0; i < used; ++i) {
|
|
|
|
data_string * const ds = data[i];
|
2018-10-23 00:28:53 +00:00
|
|
|
/*force_assert(ds->type == TYPE_STRING);*/
|
2020-08-10 23:46:17 +00:00
|
|
|
buffer_reset(&ds->key);
|
|
|
|
buffer_reset(&ds->value);
|
2018-10-23 00:28:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
#if 0 /*(unused; see array_extract_element_klen())*/
|
|
|
|
data_unset *array_pop(array * const a) {
|
2005-08-08 13:48:33 +00:00
|
|
|
data_unset *du;
|
|
|
|
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(a->used != 0);
|
2005-08-08 13:48:33 +00:00
|
|
|
|
|
|
|
a->used --;
|
|
|
|
du = a->data[a->used];
|
2019-10-16 02:45:30 +00:00
|
|
|
force_assert(a->sorted[a->used] == du); /* only works on "simple" lists */
|
2005-08-08 13:48:33 +00:00
|
|
|
a->data[a->used] = NULL;
|
|
|
|
|
|
|
|
return du;
|
|
|
|
}
|
2019-10-02 05:54:15 +00:00
|
|
|
#endif
|
2005-08-08 13:48:33 +00:00
|
|
|
|
2019-06-05 01:16:58 +00:00
|
|
|
__attribute_pure__
|
2020-09-11 18:22:34 +00:00
|
|
|
static int array_caseless_compare(const char * const a, const char * const b, const uint32_t len) {
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2019-06-05 01:16:58 +00:00
|
|
|
unsigned int ca = ((unsigned char *)a)[i];
|
|
|
|
unsigned int cb = ((unsigned char *)b)[i];
|
|
|
|
if (ca == cb) continue;
|
|
|
|
|
|
|
|
/* always lowercase for transitive results */
|
2020-09-10 04:15:29 +00:00
|
|
|
if (light_isupper(ca)) ca |= 0x20;
|
|
|
|
if (light_isupper(cb)) cb |= 0x20;
|
2019-06-05 01:16:58 +00:00
|
|
|
|
|
|
|
if (ca == cb) continue;
|
|
|
|
return (int)(ca - cb);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute_pure__
|
2020-09-11 18:22:34 +00:00
|
|
|
static int array_keycmp(const char * const a, const uint32_t alen, const char * const b, const uint32_t blen) {
|
2019-06-05 01:16:58 +00:00
|
|
|
return alen < blen ? -1 : alen > blen ? 1 : array_caseless_compare(a, b, blen);
|
2018-10-27 05:36:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 21:25:43 +00:00
|
|
|
__attribute_cold__
|
|
|
|
__attribute_pure__
|
|
|
|
static int array_keycmpb(const char * const k, const uint32_t klen, const buffer * const b) {
|
|
|
|
/* key is non-empty (0==b->used), though possibly blank (1==b->used)
|
|
|
|
* if inserted into key-value array */
|
|
|
|
/*force_assert(b && b->used);*/
|
|
|
|
return array_keycmp(k, klen, b->ptr, b->used-1);
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
/*return array_keycmp(k, klen, BUF_PTR_LEN(b));*/
|
2020-09-11 21:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns pos into a->sorted[] which contains copy of data (ptr) in a->data[]
|
|
|
|
* if pos >= 0, or returns -pos-1 if that is the position-1 in a->sorted[]
|
|
|
|
* where the key needs to be inserted (-1 to avoid -0)
|
|
|
|
*/
|
|
|
|
__attribute_hot__
|
|
|
|
__attribute_pure__
|
|
|
|
static int32_t array_get_index_ext(const array * const a, const int ext, const char * const k, const uint32_t klen) {
|
|
|
|
/* invariant: [lower-1] < probe < [upper]
|
|
|
|
* invariant: 0 <= lower <= upper <= a->used
|
|
|
|
*/
|
|
|
|
uint32_t lower = 0, upper = a->used;
|
|
|
|
while (lower != upper) {
|
|
|
|
const uint32_t probe = (lower + upper) / 2;
|
|
|
|
const int x = ((data_string *)a->sorted[probe])->ext;
|
|
|
|
/* (compare strings only if ext is 0 for both)*/
|
|
|
|
const int e = (ext|x)
|
|
|
|
? ext
|
|
|
|
: array_keycmpb(k, klen, &a->sorted[probe]->key);
|
|
|
|
if (e < x) /* e < [probe] */
|
|
|
|
upper = probe; /* still: lower <= upper */
|
|
|
|
else if (e > x) /* e > [probe] */
|
|
|
|
lower = probe + 1; /* still: lower <= upper */
|
|
|
|
else /*(e == x)*/ /* found */
|
|
|
|
return (int32_t)probe;
|
|
|
|
}
|
|
|
|
/* not found: [lower-1] < key < [upper] = [lower] ==> insert at [lower] */
|
|
|
|
return -(int)lower - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_unset *array_get_element_klen_ext(const array * const a, const int ext, const char *key, const uint32_t klen) {
|
|
|
|
const int32_t ipos = array_get_index_ext(a, ext, key, klen);
|
|
|
|
return ipos >= 0 ? a->sorted[ipos] : NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-16 02:45:30 +00:00
|
|
|
/* returns pos into a->sorted[] which contains copy of data (ptr) in a->data[]
|
2019-10-14 05:44:06 +00:00
|
|
|
* if pos >= 0, or returns -pos-1 if that is the position-1 in a->sorted[]
|
|
|
|
* where the key needs to be inserted (-1 to avoid -0)
|
2016-03-15 18:41:57 +00:00
|
|
|
*/
|
2019-10-02 05:54:15 +00:00
|
|
|
__attribute_hot__
|
|
|
|
__attribute_pure__
|
2020-09-11 18:22:34 +00:00
|
|
|
static int32_t array_get_index(const array * const a, const char * const k, const uint32_t klen) {
|
2019-10-02 05:54:15 +00:00
|
|
|
/* invariant: [lower-1] < probe < [upper]
|
|
|
|
* invariant: 0 <= lower <= upper <= a->used
|
|
|
|
*/
|
|
|
|
uint32_t lower = 0, upper = a->used;
|
|
|
|
while (lower != upper) {
|
|
|
|
uint32_t probe = (lower + upper) / 2;
|
2019-10-16 02:45:30 +00:00
|
|
|
const buffer * const b = &a->sorted[probe]->key;
|
2019-10-06 20:05:00 +00:00
|
|
|
/* key is non-empty (0==b->used), though possibly blank (1==b->used),
|
|
|
|
* if inserted into key-value array */
|
|
|
|
/*force_assert(b && b->used);*/
|
|
|
|
int cmp = array_keycmp(k, klen, b->ptr, b->used-1);
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
/*int cmp = array_keycmp(k, klen, BUF_PTR_LEN(b));*/
|
2019-10-02 05:54:15 +00:00
|
|
|
if (cmp < 0) /* key < [probe] */
|
|
|
|
upper = probe; /* still: lower <= upper */
|
|
|
|
else if (cmp > 0) /* key > [probe] */
|
|
|
|
lower = probe + 1; /* still: lower <= upper */
|
|
|
|
else /*(cmp == 0)*/ /* found */
|
|
|
|
return (int32_t)probe;
|
|
|
|
}
|
|
|
|
/* not found: [lower-1] < key < [upper] = [lower] ==> insert at [lower] */
|
|
|
|
return -(int)lower - 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
__attribute_hot__
|
2021-01-26 19:23:42 +00:00
|
|
|
const data_unset *array_get_element_klen(const array * const a, const char *key, const uint32_t klen) {
|
2019-10-10 03:24:25 +00:00
|
|
|
const int32_t ipos = array_get_index(a, key, klen);
|
2019-10-16 02:45:30 +00:00
|
|
|
return ipos >= 0 ? a->sorted[ipos] : NULL;
|
2019-10-10 03:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* non-const (data_config *) for configparser.y (not array_get_element_klen())*/
|
2020-09-11 18:22:34 +00:00
|
|
|
data_unset *array_get_data_unset(const array * const a, const char *key, const uint32_t klen) {
|
2019-10-02 05:54:15 +00:00
|
|
|
const int32_t ipos = array_get_index(a, key, klen);
|
2019-10-16 02:45:30 +00:00
|
|
|
return ipos >= 0 ? a->sorted[ipos] : NULL;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 18:22:34 +00:00
|
|
|
data_unset *array_extract_element_klen(array * const a, const char *key, const uint32_t klen) {
|
2019-10-06 21:57:11 +00:00
|
|
|
const int32_t ipos = array_get_index(a, key, klen);
|
|
|
|
if (ipos < 0) return NULL;
|
|
|
|
|
2019-10-16 02:45:30 +00:00
|
|
|
/* remove entry from a->sorted: move everything after pos one step left */
|
|
|
|
data_unset * const entry = a->sorted[ipos];
|
2019-10-06 21:57:11 +00:00
|
|
|
const uint32_t last_ndx = --a->used;
|
2019-10-16 02:45:30 +00:00
|
|
|
if (last_ndx != (uint32_t)ipos) {
|
|
|
|
data_unset ** const d = a->sorted + ipos;
|
|
|
|
memmove(d, d+1, (last_ndx - (uint32_t)ipos) * sizeof(*d));
|
|
|
|
}
|
2019-10-14 05:44:06 +00:00
|
|
|
|
2019-10-16 02:45:30 +00:00
|
|
|
if (entry != a->data[last_ndx]) {
|
|
|
|
/* walk a->data[] to find data ptr */
|
|
|
|
/* (not checking (ndx <= last_ndx) since entry must be in a->data[]) */
|
|
|
|
uint32_t ndx = 0;
|
|
|
|
while (entry != a->data[ndx]) ++ndx;
|
|
|
|
a->data[ndx] = a->data[last_ndx]; /* swap with last element */
|
|
|
|
}
|
2019-10-06 21:57:11 +00:00
|
|
|
a->data[last_ndx] = NULL;
|
|
|
|
return entry;
|
2016-03-15 18:56:02 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
static data_unset *array_get_unused_element(array * const a, const data_type_t t) {
|
2020-02-22 22:24:12 +00:00
|
|
|
/* After initial startup and config, most array usage is of homogeneous types
|
2019-10-02 05:54:15 +00:00
|
|
|
* and arrays are cleared once per request, so check only the first unused
|
|
|
|
* element to see if it can be reused */
|
|
|
|
#if 1
|
|
|
|
data_unset * const du = (a->used < a->size) ? a->data[a->used] : NULL;
|
|
|
|
if (NULL != du && du->type == t) {
|
|
|
|
a->data[a->used] = NULL;/* make empty slot at a->used for next insert */
|
|
|
|
return du;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
data_unset ** const data = a->data;
|
|
|
|
for (uint32_t i = a->used, sz = a->size; i < sz; ++i) {
|
|
|
|
if (data[i] && data[i]->type == t) {
|
|
|
|
data_unset * const ds = data[i];
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2010-08-05 21:08:23 +00:00
|
|
|
/* make empty slot at a->used for next insert */
|
2019-10-02 05:54:15 +00:00
|
|
|
data[i] = data[a->used];
|
|
|
|
data[a->used] = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2010-08-05 21:08:23 +00:00
|
|
|
return ds;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2010-08-05 21:08:23 +00:00
|
|
|
return NULL;
|
2019-10-02 05:54:15 +00:00
|
|
|
#endif
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 21:25:43 +00:00
|
|
|
__attribute_hot__
|
2021-05-23 02:30:17 +00:00
|
|
|
static data_unset * array_insert_data_at_pos(array * const a, data_unset * const entry, const uint_fast32_t pos) {
|
2021-05-18 19:19:55 +00:00
|
|
|
if (a->used < a->size) {
|
|
|
|
data_unset * const prev = a->data[a->used];
|
|
|
|
if (__builtin_expect( (prev != NULL), 0))
|
|
|
|
prev->fn->free(prev); /* free prior data, if any, from slot */
|
|
|
|
}
|
|
|
|
else {
|
2019-11-22 05:57:48 +00:00
|
|
|
array_extend(a, 16);
|
2019-10-02 05:54:15 +00:00
|
|
|
}
|
2009-06-10 13:08:15 +00:00
|
|
|
|
2021-05-23 02:30:17 +00:00
|
|
|
uint_fast32_t ndx = a->used++;
|
2019-10-14 05:44:06 +00:00
|
|
|
a->data[ndx] = entry;
|
2018-09-08 18:23:23 +00:00
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
/* move everything one step to the right */
|
2021-05-23 02:30:17 +00:00
|
|
|
ndx -= pos;
|
|
|
|
data_unset ** const d = a->sorted + pos;
|
|
|
|
if (__builtin_expect( (ndx), 1))
|
|
|
|
memmove(d+1, d, ndx * sizeof(*a->sorted));
|
|
|
|
*d = entry;
|
|
|
|
return entry;
|
2019-10-02 05:54:15 +00:00
|
|
|
}
|
2018-09-08 18:23:23 +00:00
|
|
|
|
2021-05-23 02:30:17 +00:00
|
|
|
static data_integer * array_insert_integer_at_pos(array * const a, const uint_fast32_t pos) {
|
2019-10-02 05:54:15 +00:00
|
|
|
#if 0 /*(not currently used by lighttpd in way that reuse would occur)*/
|
|
|
|
data_integer *di = (data_integer *)array_get_unused_element(a,TYPE_INTEGER);
|
2021-05-20 20:58:53 +00:00
|
|
|
if (NULL == di) di = array_data_integer_init();
|
2019-10-02 05:54:15 +00:00
|
|
|
#else
|
2021-05-20 20:58:53 +00:00
|
|
|
data_integer * const di = array_data_integer_init();
|
2019-10-02 05:54:15 +00:00
|
|
|
#endif
|
2021-05-23 02:30:17 +00:00
|
|
|
return (data_integer *)array_insert_data_at_pos(a, (data_unset *)di, pos);
|
2018-09-08 18:23:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 21:25:43 +00:00
|
|
|
__attribute_hot__
|
2021-05-23 02:30:17 +00:00
|
|
|
static data_string * array_insert_string_at_pos(array * const a, const uint_fast32_t pos) {
|
2019-10-02 05:54:15 +00:00
|
|
|
data_string *ds = (data_string *)array_get_unused_element(a, TYPE_STRING);
|
2021-05-20 20:58:53 +00:00
|
|
|
if (NULL == ds) ds = array_data_string_init();
|
2021-05-23 02:30:17 +00:00
|
|
|
return (data_string *)array_insert_data_at_pos(a, (data_unset *)ds, pos);
|
2019-10-02 05:54:15 +00:00
|
|
|
}
|
2018-09-08 18:23:23 +00:00
|
|
|
|
2020-09-11 21:25:43 +00:00
|
|
|
__attribute_hot__
|
|
|
|
buffer * array_get_buf_ptr_ext(array * const a, const int ext, const char * const k, const uint32_t klen) {
|
|
|
|
int32_t ipos = array_get_index_ext(a, ext, k, klen);
|
|
|
|
if (ipos >= 0) return &((data_string *)a->sorted[ipos])->value;
|
|
|
|
|
|
|
|
data_string * const ds = array_insert_string_at_pos(a, (uint32_t)(-ipos-1));
|
|
|
|
ds->ext = ext;
|
|
|
|
buffer_copy_string_len(&ds->key, k, klen);
|
|
|
|
buffer_clear(&ds->value);
|
|
|
|
return &ds->value;
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:22:34 +00:00
|
|
|
int * array_get_int_ptr(array * const a, const char * const k, const uint32_t klen) {
|
2019-10-02 05:54:15 +00:00
|
|
|
int32_t ipos = array_get_index(a, k, klen);
|
2019-10-16 02:45:30 +00:00
|
|
|
if (ipos >= 0) return &((data_integer *)a->sorted[ipos])->value;
|
2009-06-10 13:08:15 +00:00
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
data_integer * const di =array_insert_integer_at_pos(a,(uint32_t)(-ipos-1));
|
2019-10-13 07:37:59 +00:00
|
|
|
buffer_copy_string_len(&di->key, k, klen);
|
2019-10-06 19:55:53 +00:00
|
|
|
di->value = 0;
|
2019-10-02 05:54:15 +00:00
|
|
|
return &di->value;
|
2009-06-10 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 18:22:34 +00:00
|
|
|
buffer * array_get_buf_ptr(array * const a, const char * const k, const uint32_t klen) {
|
2019-10-02 05:54:15 +00:00
|
|
|
int32_t ipos = array_get_index(a, k, klen);
|
2019-10-16 02:45:30 +00:00
|
|
|
if (ipos >= 0) return &((data_string *)a->sorted[ipos])->value;
|
2018-09-23 00:10:51 +00:00
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
data_string * const ds = array_insert_string_at_pos(a, (uint32_t)(-ipos-1));
|
2019-10-13 07:37:59 +00:00
|
|
|
buffer_copy_string_len(&ds->key, k, klen);
|
2019-10-13 08:59:57 +00:00
|
|
|
buffer_clear(&ds->value);
|
|
|
|
return &ds->value;
|
2019-10-02 05:54:15 +00:00
|
|
|
}
|
2018-09-23 00:10:51 +00:00
|
|
|
|
2020-09-11 18:22:34 +00:00
|
|
|
void array_insert_value(array * const a, const char * const v, const uint32_t vlen) {
|
2019-10-02 05:54:15 +00:00
|
|
|
data_string * const ds = array_insert_string_at_pos(a, a->used);
|
2019-10-13 07:37:59 +00:00
|
|
|
buffer_clear(&ds->key);
|
2019-10-13 08:59:57 +00:00
|
|
|
buffer_copy_string_len(&ds->value, v, vlen);
|
2018-09-23 00:10:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 18:26:57 +00:00
|
|
|
/* if entry already exists return pointer to existing entry, otherwise insert entry and return NULL */
|
2019-10-02 05:54:15 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static data_unset **array_find_or_insert(array * const a, data_unset * const entry) {
|
|
|
|
force_assert(NULL != entry);
|
|
|
|
|
|
|
|
/* push value onto end of array if there is no key */
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_unset(&entry->key)) {
|
2019-10-02 05:54:15 +00:00
|
|
|
array_insert_data_at_pos(a, entry, a->used);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
/* try to find the entry */
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
const int32_t ipos = array_get_index(a, BUF_PTR_LEN(&entry->key));
|
2019-10-16 02:45:30 +00:00
|
|
|
if (ipos >= 0) return &a->sorted[ipos];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
array_insert_data_at_pos(a, entry, (uint32_t)(-ipos - 1));
|
|
|
|
return NULL;
|
2016-03-15 18:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* replace or insert data (free existing entry) */
|
2019-10-02 05:54:15 +00:00
|
|
|
void array_replace(array * const a, data_unset * const entry) {
|
2019-10-16 02:45:30 +00:00
|
|
|
if (NULL == array_find_or_insert(a, entry)) return;
|
2016-03-15 18:26:57 +00:00
|
|
|
|
2019-10-16 02:45:30 +00:00
|
|
|
/* find the entry (array_find_or_insert() returned non-NULL) */
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
const int32_t ipos = array_get_index(a, BUF_PTR_LEN(&entry->key));
|
2019-10-16 02:45:30 +00:00
|
|
|
force_assert(ipos >= 0);
|
|
|
|
data_unset *old = a->sorted[ipos];
|
|
|
|
force_assert(old != entry);
|
|
|
|
a->sorted[ipos] = entry;
|
|
|
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
while (i < a->used && a->data[i] != old) ++i;
|
|
|
|
force_assert(i != a->used);
|
|
|
|
a->data[i] = entry;
|
|
|
|
|
|
|
|
old->fn->free(old);
|
2016-03-15 18:26:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
void array_insert_unique(array * const a, data_unset * const entry) {
|
2016-03-15 18:26:57 +00:00
|
|
|
data_unset **old;
|
|
|
|
|
|
|
|
if (NULL != (old = array_find_or_insert(a, entry))) {
|
2021-05-23 00:34:58 +00:00
|
|
|
if (entry->fn->insert_dup) {
|
|
|
|
force_assert((*old)->type == entry->type);
|
|
|
|
entry->fn->insert_dup(*old, entry);
|
|
|
|
}
|
|
|
|
entry->fn->free(entry);
|
2016-03-15 18:26:57 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
int array_is_vlist(const array * const a) {
|
|
|
|
for (uint32_t i = 0; i < a->used; ++i) {
|
2017-03-05 20:39:45 +00:00
|
|
|
data_unset *du = a->data[i];
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (!buffer_is_unset(&du->key) || du->type != TYPE_STRING) return 0;
|
2017-03-05 20:39:45 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
int array_is_kvany(const array * const a) {
|
|
|
|
for (uint32_t i = 0; i < a->used; ++i) {
|
2017-03-05 20:39:45 +00:00
|
|
|
data_unset *du = a->data[i];
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_unset(&du->key)) return 0;
|
2017-03-05 20:39:45 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
int array_is_kvarray(const array * const a) {
|
|
|
|
for (uint32_t i = 0; i < a->used; ++i) {
|
2017-03-05 20:39:45 +00:00
|
|
|
data_unset *du = a->data[i];
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_unset(&du->key) || du->type != TYPE_ARRAY) return 0;
|
2017-03-05 20:39:45 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-02 05:54:15 +00:00
|
|
|
int array_is_kvstring(const array * const a) {
|
|
|
|
for (uint32_t i = 0; i < a->used; ++i) {
|
2017-03-05 20:39:45 +00:00
|
|
|
data_unset *du = a->data[i];
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_unset(&du->key) || du->type != TYPE_STRING) return 0;
|
2017-03-05 20:39:45 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-16 17:27:18 +00:00
|
|
|
/* array_match_*() routines follow very similar pattern, but operate on slightly
|
|
|
|
* different data: array key/value, prefix/suffix match, case-insensitive or not
|
|
|
|
* While these could be combined into fewer routines with flags to modify the
|
|
|
|
* behavior, the interface distinctions are useful to add clarity to the code,
|
|
|
|
* and the specialized routines run slightly faster */
|
|
|
|
|
|
|
|
data_unset *
|
2020-09-11 18:22:34 +00:00
|
|
|
array_match_key_prefix_klen (const array * const a, const char * const s, const uint32_t slen)
|
2018-09-16 17:27:18 +00:00
|
|
|
{
|
2019-10-02 05:54:15 +00:00
|
|
|
for (uint32_t i = 0; i < a->used; ++i) {
|
2019-10-13 07:37:59 +00:00
|
|
|
const buffer * const key = &a->data[i]->key;
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
const uint32_t klen = buffer_clen(key);
|
2018-09-16 17:27:18 +00:00
|
|
|
if (klen <= slen && 0 == memcmp(s, key->ptr, klen))
|
|
|
|
return a->data[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_unset *
|
2020-09-11 18:22:34 +00:00
|
|
|
array_match_key_prefix_nc_klen (const array * const a, const char * const s, const uint32_t slen)
|
2018-09-16 17:27:18 +00:00
|
|
|
{
|
2019-10-02 05:54:15 +00:00
|
|
|
for (uint32_t i = 0; i < a->used; ++i) {
|
2019-10-13 07:37:59 +00:00
|
|
|
const buffer * const key = &a->data[i]->key;
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
const uint32_t klen = buffer_clen(key);
|
2019-06-06 05:17:43 +00:00
|
|
|
if (klen <= slen && buffer_eq_icase_ssn(s, key->ptr, klen))
|
2018-09-16 17:27:18 +00:00
|
|
|
return a->data[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_unset *
|
|
|
|
array_match_key_prefix (const array * const a, const buffer * const b)
|
|
|
|
{
|
2020-07-08 22:01:52 +00:00
|
|
|
#ifdef __clang_analyzer__
|
|
|
|
force_assert(b);
|
|
|
|
#endif
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
return array_match_key_prefix_klen(a, BUF_PTR_LEN(b));
|
2018-09-16 17:27:18 +00:00
|
|
|
}
|
|