From 24680a914269be024aa9dac22bcebfe8f5aaaf82 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Fri, 22 Nov 2019 00:57:48 -0500 Subject: [PATCH] [core] array_init() arg for initial size --- src/array.c | 10 +++++----- src/array.h | 2 +- src/base.h | 2 +- src/configfile.c | 10 +++++----- src/configparser.y | 4 ++-- src/data_config.c | 2 +- src/mod_auth.c | 2 +- src/mod_extforward.c | 6 +++--- src/mod_magnet.c | 2 +- src/mod_maxminddb.c | 2 +- src/mod_openssl.c | 2 +- src/mod_ssi.c | 4 ++-- src/mod_status.c | 2 +- src/server.c | 12 +----------- src/status_counter.h | 8 ++++---- src/t/test_array.c | 6 +++--- src/t/test_mod_access.c | 4 ++-- src/t/test_mod_evhost.c | 2 +- 18 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/array.c b/src/array.c index 81a4dd6b..9b1e912c 100644 --- a/src/array.c +++ b/src/array.c @@ -12,8 +12,8 @@ #include __attribute_cold__ -static void array_extend(array * const a) { - a->size += 16; +static void array_extend(array * const a, uint32_t n) { + a->size += n; a->data = realloc(a->data, sizeof(*a->data) * a->size); a->sorted = realloc(a->sorted, sizeof(*a->sorted) * a->size); force_assert(a->data); @@ -21,12 +21,12 @@ static void array_extend(array * const a) { memset(a->data+a->used, 0, (a->size-a->used)*sizeof(*a->data)); } -array *array_init(void) { +array *array_init(uint32_t n) { array *a; a = calloc(1, sizeof(*a)); force_assert(a); - array_extend(a); + if (n) array_extend(a, n); return a; } @@ -216,7 +216,7 @@ static void array_insert_data_at_pos(array * const a, data_unset * const entry, force_assert(a->used + 1 <= INT32_MAX); if (a->size == a->used) { - array_extend(a); + array_extend(a, 16); } const uint32_t ndx = a->used++; diff --git a/src/array.h b/src/array.h index cdcf7fe1..28b32344 100644 --- a/src/array.h +++ b/src/array.h @@ -56,7 +56,7 @@ typedef struct { data_integer *data_integer_init(void); __attribute_returns_nonnull__ -array *array_init(void); +array *array_init(uint32_t n); __attribute_cold__ void array_copy_array(array *dst, const array *src); diff --git a/src/base.h b/src/base.h index c29dc390..98f10eec 100644 --- a/src/base.h +++ b/src/base.h @@ -368,7 +368,7 @@ struct server { * * fastcgi.backend..disconnects = ... */ - array *status; + array status; server_config srvconf; diff --git a/src/configfile.c b/src/configfile.c index c4dbc0c8..dbdb3ee6 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -311,7 +311,7 @@ static void config_compat_module_load (server *srv) { if (prepend_mod_indexfile) { /* mod_indexfile has to be loaded before mod_fastcgi and friends */ - array *modules = array_init(); + array *modules = array_init(srv->srvconf.modules->used+4); array_insert_value(modules, CONST_STR_LEN("mod_indexfile")); for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) { @@ -1042,8 +1042,8 @@ void config_free(server *srv) { } void config_init(server *srv) { - srv->config_context = array_init(); - srv->srvconf.config_touched = array_init(); + srv->config_context = array_init(16); + srv->srvconf.config_touched = array_init(128); srv->srvconf.port = 0; srv->srvconf.dont_daemonize = 0; @@ -1065,9 +1065,9 @@ void config_init(server *srv) { | HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE | HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REMOVE; - srv->srvconf.modules = array_init(); + srv->srvconf.modules = array_init(16); srv->srvconf.modules_dir = buffer_init_string(LIBRARY_DIR); - srv->srvconf.upload_tempdirs = array_init(); + srv->srvconf.upload_tempdirs = array_init(2); } diff --git a/src/configparser.y b/src/configparser.y index 3fe8cb2f..03aa70a2 100644 --- a/src/configparser.y +++ b/src/configparser.y @@ -345,7 +345,7 @@ value(A) ::= array(B). { B = NULL; } array(A) ::= LPARAN RPARAN. { - A = array_init(); + A = array_init(8); } array(A) ::= LPARAN aelements(B) RPARAN. { A = B; @@ -382,7 +382,7 @@ aelements(A) ::= aelements(C) COMMA. { aelements(A) ::= aelement(B). { A = NULL; if (ctx->ok) { - A = array_init(); + A = array_init(4); array_insert_unique(A, B); B = NULL; } diff --git a/src/data_config.c b/src/data_config.c index f11784df..15310b90 100644 --- a/src/data_config.c +++ b/src/data_config.c @@ -137,7 +137,7 @@ data_config *data_config_init(void) { ds->comp_tag = buffer_init(); ds->comp_key = buffer_init(); - ds->value = array_init(); + ds->value = array_init(4); vector_config_weak_init(&ds->children); ds->type = TYPE_CONFIG; diff --git a/src/mod_auth.c b/src/mod_auth.c index ee4af2dc..269bccd5 100644 --- a/src/mod_auth.c +++ b/src/mod_auth.c @@ -430,7 +430,7 @@ SETDEFAULTS_FUNC(mod_auth_set_defaults) { break; case 1: /* auth.require */ if (array_is_kvarray(cpv->v.a)) { - array * const a = array_init(); + array * const a = array_init(4); if (HANDLER_GO_ON != mod_auth_require_parse_array(srv, cpv->v.a, a)) { array_free(a); diff --git a/src/mod_extforward.c b/src/mod_extforward.c index 1348b360..cb0736a1 100644 --- a/src/mod_extforward.c +++ b/src/mod_extforward.c @@ -419,7 +419,7 @@ SETDEFAULTS_FUNC(mod_extforward_set_defaults) { * is not specified or is empty (and not using hap_PROXY) */ if (!p->defaults.hap_PROXY && (NULL == p->defaults.headers || 0 == p->defaults.headers->used)) { - p->defaults.headers = p->default_headers = array_init(); + p->defaults.headers = p->default_headers = array_init(2); array_insert_value(p->default_headers,CONST_STR_LEN("X-Forwarded-For")); array_insert_value(p->default_headers,CONST_STR_LEN("Forwarded-For")); } @@ -466,7 +466,7 @@ SETDEFAULTS_FUNC(mod_extforward_set_defaults) { */ static array *extract_forward_array(const buffer *pbuffer) { - array *result = array_init(); + array *result = array_init(8); if (!buffer_string_is_empty(pbuffer)) { const char *base, *curr; /* state variable, 0 means not in string, 1 means in string */ @@ -1610,7 +1610,7 @@ static int mod_extforward_hap_PROXY_v2 (connection * const con, subtlv = (struct pp2_tlv *)((char *)subtlv + 3 + n); n = ((uint32_t)subtlv->length_hi << 8) | subtlv->length_lo; if (3 + n > subsz) break; /*(invalid TLV)*/ - if (NULL == hctx->env) hctx->env = array_init(); + if (NULL == hctx->env) hctx->env = array_init(8); switch (subtlv->type) { case PP2_SUBTYPE_SSL_VERSION: array_set_key_value(hctx->env, diff --git a/src/mod_magnet.c b/src/mod_magnet.c index 7839d028..48830af5 100644 --- a/src/mod_magnet.c +++ b/src/mod_magnet.c @@ -423,7 +423,7 @@ static int magnet_status_set(lua_State *L) { static int magnet_status_pairs(lua_State *L) { server *srv = magnet_get_server(L); - return magnet_array_pairs(L, srv->status); + return magnet_array_pairs(L, &srv->status); } typedef struct { diff --git a/src/mod_maxminddb.c b/src/mod_maxminddb.c index 7579c2b8..6b695f77 100644 --- a/src/mod_maxminddb.c +++ b/src/mod_maxminddb.c @@ -426,7 +426,7 @@ CONNECTION_FUNC(mod_maxminddb_request_env_handler) array *env = con->plugin_ctx[p->id]; if (NULL == env) { - env = con->plugin_ctx[p->id] = array_init(); + env = con->plugin_ctx[p->id] = array_init(pconf.env->used); if (pconf.mmdb) mod_maxmind_geoip2(env, &con->dst_addr, &pconf); } diff --git a/src/mod_openssl.c b/src/mod_openssl.c index e106dc5b..67647862 100644 --- a/src/mod_openssl.c +++ b/src/mod_openssl.c @@ -1576,7 +1576,7 @@ SETDEFAULTS_FUNC(mod_openssl_set_defaults) plugin_data * const p = p_d; p->srv = srv; - p->cafiles = array_init(); + p->cafiles = array_init(0); if (!config_plugin_values_init(srv, p, cpk, "mod_openssl")) return HANDLER_ERROR; diff --git a/src/mod_ssi.c b/src/mod_ssi.c index 3425cb7b..daf878df 100644 --- a/src/mod_ssi.c +++ b/src/mod_ssi.c @@ -64,8 +64,8 @@ INIT_FUNC(mod_ssi_init) { p->timefmt = buffer_init(); p->stat_fn = buffer_init(); - p->ssi_vars = array_init(); - p->ssi_cgi_env = array_init(); + p->ssi_vars = array_init(8); + p->ssi_cgi_env = array_init(32); return p; } diff --git a/src/mod_status.c b/src/mod_status.c index fe5ce9b6..5669df06 100644 --- a/src/mod_status.c +++ b/src/mod_status.c @@ -714,7 +714,7 @@ static handler_t mod_status_handle_server_status_json(server *srv, connection *c static handler_t mod_status_handle_server_statistics(server *srv, connection *con) { buffer *b; size_t i; - array *st = srv->status; + array *st = &srv->status; if (0 == st->used) { /* we have nothing to send */ diff --git a/src/server.c b/src/server.c index 827896d0..bf2a9e5c 100644 --- a/src/server.c +++ b/src/server.c @@ -242,12 +242,6 @@ static server *server_init(void) { CLEAN(tmp_chunk_len); #undef CLEAN -#define CLEAN(x) \ - srv->x = array_init(); - - CLEAN(status); -#undef CLEAN - for (int i = 0; i < FILE_CACHE_MAX; ++i) { srv->mtime_cache[i].mtime = (time_t)-1; } @@ -293,11 +287,7 @@ static void server_free(server *srv) { config_free(srv); -#define CLEAN(x) \ - array_free(srv->x); - - CLEAN(status); -#undef CLEAN + array_free_data(&srv->status); free(srv->joblist.ptr); free(srv->fdwaitqueue.ptr); diff --git a/src/status_counter.h b/src/status_counter.h index 06a821ad..d1eeabef 100644 --- a/src/status_counter.h +++ b/src/status_counter.h @@ -21,22 +21,22 @@ void status_counter_set(server *srv, const char *s, size_t len, int val); __attribute_returns_nonnull__ static inline int *status_counter_get_counter(server *srv, const char *s, size_t len) { - return array_get_int_ptr(srv->status, s, len); + return array_get_int_ptr(&srv->status, s, len); } static inline void status_counter_inc(server *srv, const char *s, size_t len) { - ++(*array_get_int_ptr(srv->status, s, len)); + ++(*array_get_int_ptr(&srv->status, s, len)); } static inline void status_counter_dec(server *srv, const char *s, size_t len) { - --(*array_get_int_ptr(srv->status, s, len)); + --(*array_get_int_ptr(&srv->status, s, len)); } static inline void status_counter_set(server *srv, const char *s, size_t len, int val) { - *array_get_int_ptr(srv->status, s, len) = val; + *array_get_int_ptr(&srv->status, s, len) = val; } diff --git a/src/t/test_array.c b/src/t/test_array.c index daa6f03f..cfffb142 100644 --- a/src/t/test_array.c +++ b/src/t/test_array.c @@ -11,7 +11,7 @@ static void test_array_get_int_ptr (void) { data_integer *di; int *i; - array *a = array_init(); + array *a = array_init(0); i = array_get_int_ptr(a, CONST_STR_LEN("abc")); assert(NULL != i); @@ -30,7 +30,7 @@ static void test_array_get_int_ptr (void) { static void test_array_insert_value (void) { data_string *ds; - array *a = array_init(); + array *a = array_init(0); array_insert_value(a, CONST_STR_LEN("def")); ds = (data_string *)a->data[0]; @@ -42,7 +42,7 @@ static void test_array_insert_value (void) { static void test_array_set_key_value (void) { data_string *ds; - array *a = array_init(); + array *a = array_init(0); array_set_key_value(a, CONST_STR_LEN("abc"), CONST_STR_LEN("def")); ds = (data_string *)array_get_element_klen(a, CONST_STR_LEN("does-not-exist")); diff --git a/src/t/test_mod_access.c b/src/t/test_mod_access.c index 820cee4c..8e1d4391 100644 --- a/src/t/test_mod_access.c +++ b/src/t/test_mod_access.c @@ -8,8 +8,8 @@ #include "mod_access.c" static void test_mod_access_check(void) { - array *allow = array_init(); - array *deny = array_init(); + array *allow = array_init(0); + array *deny = array_init(0); buffer *urlpath = buffer_init(); int lc = 0; diff --git a/src/t/test_mod_evhost.c b/src/t/test_mod_evhost.c index 0244626c..c6cbda18 100644 --- a/src/t/test_mod_evhost.c +++ b/src/t/test_mod_evhost.c @@ -26,7 +26,7 @@ static void test_mod_evhost_build_doc_root_path_loop(struct ttt *tt, size_t nelt static void test_mod_evhost_build_doc_root_path(void) { buffer *authority = buffer_init(); buffer *b = buffer_init(); - array *a = array_init(); + array *a = array_init(0); struct ttt tt1[] = { /* "host.example.org" */ /* correct pattern not using dot notation */ { CONST_STR_LEN("/web/%3/"),