diff options
author | Glenn Strauss <gstrauss@gluelogic.com> | 2022-12-08 00:26:46 -0500 |
---|---|---|
committer | Glenn Strauss <gstrauss@gluelogic.com> | 2022-12-10 11:58:14 -0500 |
commit | c412bb59ce1488347a9f4d6d55e42de3f8567956 (patch) | |
tree | 0be4efd67374e6856abb35252847c5166a710629 | |
parent | 0318ef7b64768cc937ee944da445248376793cd3 (diff) | |
download | lighttpd-git-c412bb59ce1488347a9f4d6d55e42de3f8567956.tar.gz |
[multiple] employ ck_realloc_u32() shared code
employ ck_realloc_u32() shared code to slightly reduce code size
-rw-r--r-- | src/array.c | 6 | ||||
-rw-r--r-- | src/base.h | 4 | ||||
-rw-r--r-- | src/fdevent_impl.c | 9 | ||||
-rw-r--r-- | src/fdlog_maint.c | 22 | ||||
-rw-r--r-- | src/gw_backend.c | 59 | ||||
-rw-r--r-- | src/gw_backend.h | 3 | ||||
-rw-r--r-- | src/keyvalue.c | 6 | ||||
-rw-r--r-- | src/mod_dirlisting.c | 20 | ||||
-rw-r--r-- | src/mod_expire.c | 4 | ||||
-rw-r--r-- | src/mod_magnet_cache.c | 7 | ||||
-rw-r--r-- | src/mod_magnet_cache.h | 1 | ||||
-rw-r--r-- | src/mod_webdav.c | 25 | ||||
-rw-r--r-- | src/network.c | 14 | ||||
-rw-r--r-- | src/plugin.c | 23 |
14 files changed, 62 insertions, 141 deletions
diff --git a/src/array.c b/src/array.c index cbd1b929..8a24d7df 100644 --- a/src/array.c +++ b/src/array.c @@ -116,10 +116,8 @@ static void array_extend(array * const a, uint32_t n) { /* This data structure should not be used for nearly so many entries */ force_assert(a->size <= INT32_MAX-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); - force_assert(a->sorted); + a->data = ck_realloc_u32((void**)&a->data, a->size,0,sizeof(*a->data)); + a->sorted = ck_realloc_u32((void**)&a->sorted,a->size,0,sizeof(*a->sorted)); memset(a->data+a->used, 0, (a->size-a->used)*sizeof(*a->data)); } @@ -136,8 +136,6 @@ typedef struct server_socket { typedef struct { server_socket **ptr; - - uint32_t size; uint32_t used; } server_socket_array; @@ -177,7 +175,7 @@ struct server { server_socket_array srv_sockets; server_socket_array srv_sockets_inherited; - struct { void *ptr; uint32_t used; uint32_t size; } plugins; + struct { void *ptr; uint32_t used; } plugins; unix_time64_t startup_ts; unix_time64_t graceful_expire_ts; diff --git a/src/fdevent_impl.c b/src/fdevent_impl.c index 04422ae2..ceaaf356 100644 --- a/src/fdevent_impl.c +++ b/src/fdevent_impl.c @@ -763,10 +763,9 @@ fdevent_poll_event_del (fdevents *ev, fdnode *fdn) /* ev->pollfds[k].revents = 0; */ if (ev->unused.size == ev->unused.used) { + ck_realloc_u32((void **)&ev->unused.ptr, ev->unused.size, + 16, sizeof(*ev->unused.ptr)); ev->unused.size += 16; - ev->unused.ptr = realloc(ev->unused.ptr, - sizeof(*(ev->unused.ptr)) * ev->unused.size); - force_assert(NULL != ev->unused.ptr); } ev->unused.ptr[ev->unused.used++] = k; @@ -796,9 +795,9 @@ fdevent_poll_event_set (fdevents *ev, fdnode *fdn, int events) } else { if (ev->size == ev->used) { + ck_realloc_u32((void **)&ev->pollfds, ev->size, + 16, sizeof(*ev->pollfds)); ev->size += 16; - ev->pollfds = realloc(ev->pollfds, sizeof(*ev->pollfds) * ev->size); - force_assert(NULL != ev->pollfds); } k = ev->used++; diff --git a/src/fdlog_maint.c b/src/fdlog_maint.c index 9f5b9832..0865cf0e 100644 --- a/src/fdlog_maint.c +++ b/src/fdlog_maint.c @@ -29,7 +29,6 @@ struct fdlog_files_t { fdlog_st **ptr; uint32_t used; - uint32_t size; }; static struct fdlog_files_t fdlog_files; @@ -48,7 +47,6 @@ typedef struct fdlog_pipe { struct fdlog_pipes_t { fdlog_pipe *ptr; uint32_t used; - uint32_t size; }; static struct fdlog_pipes_t fdlog_pipes; @@ -136,7 +134,6 @@ fdlog_pipes_close (fdlog_st * const retain) free(fdlog_pipes.ptr); fdlog_pipes.ptr = NULL; fdlog_pipes.used = 0; - fdlog_pipes.size = 0; } @@ -166,12 +163,9 @@ fdlog_pipe_serrh (const int fd) static fdlog_st * fdlog_pipe_init (const char * const fn, const int fds[2], const pid_t pid) { - if (fdlog_pipes.used == fdlog_pipes.size) { - fdlog_pipes.size += 4; - fdlog_pipes.ptr = - realloc(fdlog_pipes.ptr, fdlog_pipes.size * sizeof(fdlog_pipe)); - force_assert(fdlog_pipes.ptr); - } + if (!(fdlog_pipes.used & (4-1))) + ck_realloc_u32((void **)&fdlog_pipes.ptr, fdlog_pipes.used, + 4, sizeof(*fdlog_pipes.ptr)); fdlog_pipe * const fdp = fdlog_pipes.ptr + fdlog_pipes.used++; fdp->fd = fds[0]; fdp->pid = pid; @@ -212,12 +206,9 @@ fdlog_pipe_open (const char * const fn) static fdlog_st * fdlog_file_init (const char * const fn, const int fd) { - if (fdlog_files.used == fdlog_files.size) { - fdlog_files.size += 4; - fdlog_files.ptr = - realloc(fdlog_files.ptr, fdlog_files.size * sizeof(fdlog_st *)); - force_assert(fdlog_files.ptr); - } + if (!(fdlog_files.used & (4-1))) + ck_realloc_u32((void **)&fdlog_files.ptr, fdlog_files.used, + 4, sizeof(*fdlog_files.ptr)); return (fdlog_files.ptr[fdlog_files.used++] = fdlog_init(fn,fd,FDLOG_FILE)); } @@ -311,7 +302,6 @@ fdlog_files_close (fdlog_st * const retain) free(fdlog_files.ptr); fdlog_files.ptr = NULL; fdlog_files.used = 0; - fdlog_files.size = 0; } diff --git a/src/gw_backend.c b/src/gw_backend.c index bc97dd3e..1eb037ad 100644 --- a/src/gw_backend.c +++ b/src/gw_backend.c @@ -241,11 +241,9 @@ static int gw_extension_insert(gw_exts *ext, const buffer *key, gw_host *fh) { } if (NULL == fe) { - if (ext->used == ext->size) { - ext->size += 8; - ext->exts = realloc(ext->exts, ext->size * sizeof(gw_extension)); - force_assert(ext->exts); - memset(ext->exts + ext->used, 0, 8 * sizeof(gw_extension)); + if (!(ext->used & (8-1))) { + ck_realloc_u32((void **)&ext->exts,ext->used,8,sizeof(*ext->exts)); + memset(ext->exts + ext->used, 0, 8 * sizeof(*ext->exts)); } fe = ext->exts + ext->used++; fe->last_used_ndx = -1; @@ -254,12 +252,8 @@ static int gw_extension_insert(gw_exts *ext, const buffer *key, gw_host *fh) { memcpy(b, key, sizeof(buffer)); /*(copy; not later free'd)*/ } - if (fe->size == fe->used) { - fe->size += 4; - fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts))); - force_assert(fe->hosts); - } - + if (!(fe->used & (4-1))) + ck_realloc_u32((void **)&fe->hosts, fe->used, 4, sizeof(*fe->hosts)); fe->hosts[fe->used++] = fh; return 0; } @@ -489,15 +483,8 @@ static int env_add(char_array *env, const char *key, size_t key_len, const char } } - if (env->size <= env->used + 1) { - env->size += 16; - env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr)); - force_assert(env->ptr); - } - - #ifdef __COVERITY__ - force_assert(env->ptr); /*(non-NULL if env->used != 0; guaranteed above)*/ - #endif + if (!(env->used & (16-1))) + ck_realloc_u32((void **)&env->ptr, env->used, 16, sizeof(*env->ptr)); env->ptr[env->used++] = dst; return 0; @@ -567,7 +554,6 @@ static int gw_spawn_connection(gw_host * const host, gw_proc * const proc, log_e { /* create environment */ env.ptr = NULL; - env.size = 0; env.used = 0; /* build clean environment */ @@ -614,6 +600,8 @@ static int gw_spawn_connection(gw_host * const host, gw_proc * const proc, log_e CONST_STR_LEN("1")); } + if (!(env.used & (16-1))) + ck_realloc_u32((void **)&env.ptr,env.used,1,sizeof(*env.ptr)); env.ptr[env.used] = NULL; } @@ -780,7 +768,7 @@ static gw_host * unixsocket_is_dup(gw_plugin_data *p, const buffer *unixsocket) return NULL; } -static int parse_binpath(char_array *env, const buffer *b) { +static void parse_binpath(char_array *env, const buffer *b) { char *start = b->ptr; char c; /* search for spaces */ @@ -790,11 +778,8 @@ static int parse_binpath(char_array *env, const buffer *b) { case '\t': /* a WS, stop here and copy the argument */ - if (env->size == env->used) { - env->size += 16; - env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr)); - force_assert(env->ptr); - } + if (!(env->used & (4-1))) + ck_realloc_u32((void**)&env->ptr,env->used,4,sizeof(*env->ptr)); c = b->ptr[i]; b->ptr[i] = '\0'; @@ -808,23 +793,10 @@ static int parse_binpath(char_array *env, const buffer *b) { } } - if (env->size == env->used) { /*need one extra for terminating NULL*/ - env->size += 16; - env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr)); - } - - /* the rest */ + if (!(env->used & (4-1)) || !((env->used+1) & (4-1))) + ck_realloc_u32((void **)&env->ptr, env->used, 2, sizeof(*env->ptr)); env->ptr[env->used++] = strdup(start); - - if (env->size == env->used) { /*need one extra for terminating NULL*/ - env->size += 16; - env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr)); - } - - /* terminate */ - env->ptr[env->used++] = NULL; - - return 0; + env->ptr[env->used] = NULL; } enum { @@ -1631,7 +1603,6 @@ int gw_set_defaults_backend(server *srv, gw_plugin_data *p, const array *a, gw_p host->args.ptr = calloc(4, sizeof(char *)); force_assert(host->args.ptr); host->args.used = 3; - host->args.size = 4; host->args.ptr[0] = malloc(sizeof("/bin/sh")); force_assert(host->args.ptr[0]); memcpy(host->args.ptr[0], "/bin/sh", sizeof("/bin/sh")); diff --git a/src/gw_backend.h b/src/gw_backend.h index 037e61a1..80704727 100644 --- a/src/gw_backend.h +++ b/src/gw_backend.h @@ -11,8 +11,6 @@ typedef struct { char **ptr; - - uint32_t size; uint32_t used; } char_array; @@ -248,7 +246,6 @@ typedef struct { int last_used_ndx; gw_host **hosts; - uint32_t used; uint32_t size; } gw_extension; diff --git a/src/keyvalue.c b/src/keyvalue.c index 1ce3fb04..68cb0a53 100644 --- a/src/keyvalue.c +++ b/src/keyvalue.c @@ -54,10 +54,8 @@ int pcre_keyvalue_buffer_append(log_error_st *errh, pcre_keyvalue_buffer *kvb, c pcre_keyvalue *kv; - if (0 == (kvb->used & 3)) { /*(allocate in groups of 4)*/ - kvb->kv = realloc(kvb->kv, (kvb->used + 4) * sizeof(*kvb->kv)); - force_assert(NULL != kvb->kv); - } + if (!(kvb->used & (4-1))) /*(allocate in groups of 4)*/ + ck_realloc_u32((void **)&kvb->kv,kvb->used,4,sizeof(*kvb->kv)); kv = kvb->kv + kvb->used++; diff --git a/src/mod_dirlisting.c b/src/mod_dirlisting.c index 21ba23ba..37ad989d 100644 --- a/src/mod_dirlisting.c +++ b/src/mod_dirlisting.c @@ -110,10 +110,10 @@ typedef struct { typedef struct { dirls_entry_t **ent; uint32_t used; - uint32_t size; } dirls_list_t; #define DIRLIST_ENT_NAME(ent) ((char*)(ent) + sizeof(dirls_entry_t)) +/* DIRLIST_BLOB_SIZE must be power of 2 for current internal usage */ #define DIRLIST_BLOB_SIZE 16 typedef struct { @@ -1023,15 +1023,9 @@ static int http_open_directory(request_st * const r, handler_ctx * const hctx) { dirls_list_t * const dirs = &hctx->dirs; dirls_list_t * const files = &hctx->files; - dirs->ent = - (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE); - force_assert(dirs->ent); - dirs->size = DIRLIST_BLOB_SIZE; + dirs->ent = NULL; dirs->used = 0; - files->ent = - (dirls_entry_t**) malloc(sizeof(dirls_entry_t*) * DIRLIST_BLOB_SIZE); - force_assert(files->ent); - files->size = DIRLIST_BLOB_SIZE; + files->ent = NULL; files->used = 0; return 0; @@ -1125,11 +1119,9 @@ static int http_read_directory(handler_ctx * const p) { } dirls_list_t * const list = !S_ISDIR(st.st_mode) ? &p->files : &p->dirs; - if (list->used == list->size) { - list->size += DIRLIST_BLOB_SIZE; - list->ent = (dirls_entry_t**) realloc(list->ent, sizeof(dirls_entry_t*) * list->size); - force_assert(list->ent); - } + if (!(list->used & (DIRLIST_BLOB_SIZE-1))) + ck_realloc_u32((void **)&list->ent, list->used, + DIRLIST_BLOB_SIZE, sizeof(*list->ent)); dirls_entry_t * const tmp = list->ent[list->used++] = (dirls_entry_t*) malloc(sizeof(dirls_entry_t) + 1 + dsz); force_assert(tmp); diff --git a/src/mod_expire.c b/src/mod_expire.c index a6c9f486..25da5b46 100644 --- a/src/mod_expire.c +++ b/src/mod_expire.c @@ -227,8 +227,8 @@ SETDEFAULTS_FUNC(mod_expire_set_defaults) { /* parse array values into structured data */ if (NULL != a && a->used) { - p->toffsets = - realloc(p->toffsets, sizeof(time_t) * (p->tused + a->used*2)); + ck_realloc_u32((void **)&p->toffsets, p->tused, + a->used*2, sizeof(*p->toffsets)); time_t *toff = p->toffsets + p->tused; for (uint32_t k = 0; k < a->used; ++k, toff+=2, p->tused+=2) { buffer *v = &((data_string *)a->data[k])->value; diff --git a/src/mod_magnet_cache.c b/src/mod_magnet_cache.c index 3a9ac5f0..81ddf4e6 100644 --- a/src/mod_magnet_cache.c +++ b/src/mod_magnet_cache.c @@ -102,11 +102,8 @@ static script *script_cache_new_script(script_cache * const cache, const buffer { script * const sc = script_init(); - if (cache->used == cache->size) { - cache->size += 16; - cache->ptr = realloc(cache->ptr, cache->size * sizeof(*(cache->ptr))); - force_assert(cache->ptr); - } + if (!(cache->used & (16-1))) + ck_realloc_u32((void **)&cache->ptr,cache->used,16,sizeof(*cache->ptr)); cache->ptr[cache->used++] = sc; buffer_copy_buffer(&sc->name, name); diff --git a/src/mod_magnet_cache.h b/src/mod_magnet_cache.h index 571f9286..d55367b7 100644 --- a/src/mod_magnet_cache.h +++ b/src/mod_magnet_cache.h @@ -18,7 +18,6 @@ typedef struct { typedef struct { script **ptr; uint32_t used; - uint32_t size; } script_cache; #if 0 diff --git a/src/mod_webdav.c b/src/mod_webdav.c index 0f9463c8..234da10d 100644 --- a/src/mod_webdav.c +++ b/src/mod_webdav.c @@ -722,7 +722,6 @@ typedef struct { typedef struct { webdav_property_name *ptr; int used; - int size; } webdav_property_names; /* @@ -3924,7 +3923,6 @@ webdav_parse_chunkqueue (request_st * const r, struct webdav_lock_token_submitted_st { buffer *tokens; int used; - int size; const buffer *authn_user; buffer *b; request_st *r; @@ -4005,7 +4003,6 @@ webdav_has_lock (request_st * const r, cbdata.r = r; cbdata.tokens = NULL; cbdata.used = 0; - cbdata.size = 0; cbdata.nlocks = 0; cbdata.slocks = 0; cbdata.smatch = 0; @@ -4101,16 +4098,14 @@ webdav_has_lock (request_st * const r, * ; No linear whitespace (LWS) allowed in Coded-URL * ; absolute-URI defined in RFC 3986, Section 4.3 */ - if (cbdata.size == cbdata.used) { - if (cbdata.size == 16) { /* arbitrary limit */ + if (!(cbdata.used & (16-1))) { + if (cbdata.used == 16) { /* arbitrary limit */ http_status_set_error(r, 400); /* Bad Request */ chunk_buffer_release(cbdata.b); return 0; } - cbdata.size = 16; - cbdata.tokens = - realloc(cbdata.tokens, sizeof(*(cbdata.tokens)) * 16); - force_assert(cbdata.tokens); /*(see above limit)*/ + ck_realloc_u32((void **)&cbdata.tokens, (uint32_t)cbdata.used, + 16, sizeof(*cbdata.tokens)); } cbdata.tokens[cbdata.used].ptr = p+1; @@ -4251,7 +4246,6 @@ mod_webdav_propfind (request_st * const r, const plugin_config * const pconf) pb.proplist.ptr = NULL; pb.proplist.used = 0; - pb.proplist.size = 0; #ifdef USE_PROPPATCH xmlDocPtr xml = NULL; @@ -4292,8 +4286,8 @@ mod_webdav_propfind (request_st * const r, const plugin_config * const pconf) } /* add property to requested list */ - if (pb.proplist.size == pb.proplist.used) { - if (pb.proplist.size == 32) { + if (!(pb.proplist.used & (32-1))) { + if (pb.proplist.used == 32) { /* arbitrarily chosen limit of 32 */ log_error(r->conf.errh, __FILE__, __LINE__, "too many properties in request (> 32)"); @@ -4302,10 +4296,9 @@ mod_webdav_propfind (request_st * const r, const plugin_config * const pconf) xmlFreeDoc(xml); return HANDLER_FINISHED; } - pb.proplist.size = 32; - pb.proplist.ptr = - realloc(pb.proplist.ptr, sizeof(*(pb.proplist.ptr)) * 32); - force_assert(pb.proplist.ptr); /*(see above limit)*/ + ck_realloc_u32((void **)&pb.proplist.ptr, + (uint32_t)pb.proplist.used, + 32, sizeof(*pb.proplist.ptr)); } const size_t namelen = strlen((char *)prop->name); diff --git a/src/network.c b/src/network.c index 0e668829..f428ffce 100644 --- a/src/network.c +++ b/src/network.c @@ -266,13 +266,11 @@ static int network_host_parse_addr(server *srv, sock_addr *addr, socklen_t *addr } static void network_srv_sockets_append(server *srv, server_socket *srv_socket) { - if (srv->srv_sockets.used == srv->srv_sockets.size) { - srv->srv_sockets.size += 4; - srv->srv_sockets.ptr = realloc(srv->srv_sockets.ptr, srv->srv_sockets.size * sizeof(server_socket*)); - force_assert(NULL != srv->srv_sockets.ptr); - } - - srv->srv_sockets.ptr[srv->srv_sockets.used++] = srv_socket; + server_socket_array * const srv_sockets = &srv->srv_sockets; + if (!(srv_sockets->used & (4-1))) + ck_realloc_u32((void **)&srv_sockets->ptr, srv_sockets->used, + 4, sizeof(*srv_sockets->ptr)); + srv_sockets->ptr[srv_sockets->used++] = srv_socket; } typedef struct { @@ -613,7 +611,6 @@ int network_close(server *srv) { free(srv->srv_sockets.ptr); srv->srv_sockets.ptr = NULL; srv->srv_sockets.used = 0; - srv->srv_sockets.size = 0; for (uint32_t i = 0; i < srv->srv_sockets_inherited.used; ++i) { server_socket *srv_socket = srv->srv_sockets_inherited.ptr[i]; @@ -629,7 +626,6 @@ int network_close(server *srv) { free(srv->srv_sockets_inherited.ptr); srv->srv_sockets_inherited.ptr = NULL; srv->srv_sockets_inherited.used = 0; - srv->srv_sockets_inherited.size = 0; return 0; } diff --git a/src/plugin.c b/src/plugin.c index 436d546c..376c118d 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -81,18 +81,6 @@ static void plugin_free(plugin *p) { free(p); } -static void plugins_register(server *srv, plugin *p) { - plugin **ps; - if (srv->plugins.used == srv->plugins.size) { - srv->plugins.size += 4; - srv->plugins.ptr = realloc(srv->plugins.ptr, srv->plugins.size * sizeof(*ps)); - force_assert(NULL != srv->plugins.ptr); - } - - ps = srv->plugins.ptr; - ps[srv->plugins.used++] = p; -} - #ifdef _WIN32 __attribute_cold__ static void @@ -140,6 +128,9 @@ static const plugin_load_functions load_functions[] = { }; int plugins_load(server *srv) { + ck_realloc_u32(&srv->plugins.ptr, 0, + srv->srvconf.modules->used, sizeof(plugin *)); + for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) { data_string *ds = (data_string *)srv->srvconf.modules->data[i]; char *module = ds->value.ptr; @@ -153,7 +144,7 @@ int plugins_load(server *srv) { plugin_free(p); return -1; } - plugins_register(srv, p); + ((plugin **)srv->plugins.ptr)[srv->plugins.used++] = p; break; } } @@ -173,6 +164,9 @@ int plugins_load(server *srv) { #else /* defined(LIGHTTPD_STATIC) */ int plugins_load(server *srv) { + ck_realloc_u32(&srv->plugins.ptr, 0, + srv->srvconf.modules->used, sizeof(plugin *)); + buffer * const tb = srv->tmp_buf; int (*init)(plugin *pl); @@ -254,7 +248,7 @@ int plugins_load(server *srv) { plugin_free(p); return -1; } - plugins_register(srv, p); + ((plugin **)srv->plugins.ptr)[srv->plugins.used++] = p; } return 0; @@ -576,6 +570,5 @@ void plugins_free(server *srv) { free(srv->plugins.ptr); srv->plugins.ptr = NULL; srv->plugins.used = 0; - srv->plugins.size = 0; array_free_data(&plugin_stats); } |