Browse Source

[multiple] reduce code dup in list resizing

reduce code duplication in list resizing
realloc() of NULL ptr has behavior similar to malloc()

Note that if initial size == 0, then code used to adjust size
must be += x to ensure the size is non-zero for reallocation.
(Multiplying 0 * x, e.g. power-2 resizing, will result in 0.)
personal/stbuehler/fix-fdevent
Glenn Strauss 3 years ago
parent
commit
d28bac32fe
  1. 9
      src/array.c
  2. 12
      src/fdevent_poll.c
  3. 27
      src/gw_backend.c
  4. 12
      src/joblist.c
  5. 13
      src/keyvalue.c
  6. 18
      src/mod_accesslog.c
  7. 6
      src/mod_cgi.c
  8. 11
      src/mod_dirlisting.c
  9. 5
      src/mod_magnet_cache.c
  10. 8
      src/mod_uploadprogress.c
  11. 5
      src/mod_webdav.c
  12. 8
      src/network.c
  13. 7
      src/plugin.c

9
src/array.c

@ -275,14 +275,7 @@ static data_unset **array_find_or_insert(array *a, data_unset *entry) {
/* there couldn't possibly be enough memory to store so many entries */
force_assert(a->used + 1 <= SSIZE_MAX);
if (a->size == 0) {
a->size = 16;
a->data = malloc(sizeof(*a->data) * a->size);
a->sorted = malloc(sizeof(*a->sorted) * a->size);
force_assert(a->data);
force_assert(a->sorted);
for (j = a->used; j < a->size; j++) a->data[j] = NULL;
} else if (a->size == a->used) {
if (a->size == a->used) {
a->size += 16;
a->data = realloc(a->data, sizeof(*a->data) * a->size);
a->sorted = realloc(a->sorted, sizeof(*a->sorted) * a->size);

12
src/fdevent_poll.c

@ -45,11 +45,7 @@ static int fdevent_poll_event_del(fdevents *ev, int fde_ndx, int fd) {
/* ev->pollfds[k].events = 0; */
/* ev->pollfds[k].revents = 0; */
if (ev->unused.size == 0) {
ev->unused.size = 16;
ev->unused.ptr = malloc(sizeof(*(ev->unused.ptr)) * ev->unused.size);
force_assert(NULL != ev->unused.ptr);
} else if (ev->unused.size == ev->unused.used) {
if (ev->unused.size == ev->unused.used) {
ev->unused.size += 16;
ev->unused.ptr = realloc(ev->unused.ptr, sizeof(*(ev->unused.ptr)) * ev->unused.size);
force_assert(NULL != ev->unused.ptr);
@ -106,11 +102,7 @@ static int fdevent_poll_event_set(fdevents *ev, int fde_ndx, int fd, int events)
return k;
} else {
if (ev->size == 0) {
ev->size = 16;
ev->pollfds = malloc(sizeof(*ev->pollfds) * ev->size);
force_assert(NULL != ev->pollfds);
} else if (ev->size == ev->used) {
if (ev->size == ev->used) {
ev->size += 16;
ev->pollfds = realloc(ev->pollfds, sizeof(*ev->pollfds) * ev->size);
force_assert(NULL != ev->pollfds);

27
src/gw_backend.c

@ -200,11 +200,7 @@ static int gw_extension_insert(gw_exts *ext, buffer *key, gw_host *fh) {
fe->last_used_ndx = -1;
buffer_copy_buffer(fe->key, key);
if (ext->size == 0) {
ext->size = 8;
ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
force_assert(ext->exts);
} else if (ext->used == ext->size) {
if (ext->used == ext->size) {
ext->size += 8;
ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
force_assert(ext->exts);
@ -435,11 +431,7 @@ static int env_add(char_array *env, const char *key, size_t key_len, const char
}
}
if (env->size == 0) {
env->size = 16;
env->ptr = malloc(env->size * sizeof(*env->ptr));
force_assert(env->ptr);
} else if (env->size == env->used + 1) {
if (env->size <= env->used + 1) {
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
force_assert(env->ptr);
@ -738,10 +730,7 @@ static int parse_binpath(char_array *env, buffer *b) {
case '\t':
/* a WS, stop here and copy the argument */
if (env->size == 0) {
env->size = 16;
env->ptr = malloc(env->size * sizeof(*env->ptr));
} else if (env->size == env->used) {
if (env->size == env->used) {
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
@ -758,10 +747,7 @@ static int parse_binpath(char_array *env, buffer *b) {
}
}
if (env->size == 0) {
env->size = 16;
env->ptr = malloc(env->size * sizeof(*env->ptr));
} else if (env->size == env->used) { /*need one extra for terminating NULL*/
if (env->size == env->used) { /*need one extra for terminating NULL*/
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
@ -769,10 +755,7 @@ static int parse_binpath(char_array *env, buffer *b) {
/* the rest */
env->ptr[env->used++] = strdup(start);
if (env->size == 0) {
env->size = 16;
env->ptr = malloc(env->size * sizeof(*env->ptr));
} else if (env->size == env->used) { /*need one extra for terminating NULL*/
if (env->size == env->used) { /*need one extra for terminating NULL*/
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}

12
src/joblist.c

@ -7,11 +7,7 @@
#include <string.h>
int joblist_append(server *srv, connection *con) {
if (srv->joblist->size == 0) {
srv->joblist->size = 16;
srv->joblist->ptr = malloc(sizeof(*srv->joblist->ptr) * srv->joblist->size);
force_assert(NULL != srv->joblist->ptr);
} else if (srv->joblist->used == srv->joblist->size) {
if (srv->joblist->used == srv->joblist->size) {
srv->joblist->size += 16;
srv->joblist->ptr = realloc(srv->joblist->ptr, sizeof(*srv->joblist->ptr) * srv->joblist->size);
force_assert(NULL != srv->joblist->ptr);
@ -44,11 +40,7 @@ connection *fdwaitqueue_unshift(server *srv, connections *fdwaitqueue) {
}
int fdwaitqueue_append(server *srv, connection *con) {
if (srv->fdwaitqueue->size == 0) {
srv->fdwaitqueue->size = 16;
srv->fdwaitqueue->ptr = malloc(sizeof(*(srv->fdwaitqueue->ptr)) * srv->fdwaitqueue->size);
force_assert(NULL != srv->fdwaitqueue->ptr);
} else if (srv->fdwaitqueue->used == srv->fdwaitqueue->size) {
if (srv->fdwaitqueue->used == srv->fdwaitqueue->size) {
srv->fdwaitqueue->size += 16;
srv->fdwaitqueue->ptr = realloc(srv->fdwaitqueue->ptr, sizeof(*(srv->fdwaitqueue->ptr)) * srv->fdwaitqueue->size);
force_assert(NULL != srv->fdwaitqueue->ptr);

13
src/keyvalue.c

@ -38,18 +38,7 @@ int pcre_keyvalue_buffer_append(server *srv, pcre_keyvalue_buffer *kvb, buffer *
if (!key) return -1;
if (kvb->size == 0) {
kvb->size = 4;
kvb->used = 0;
kvb->kv = malloc(kvb->size * sizeof(*kvb->kv));
force_assert(NULL != kvb->kv);
for(i = 0; i < kvb->size; i++) {
kvb->kv[i] = calloc(1, sizeof(**kvb->kv));
force_assert(NULL != kvb->kv[i]);
}
} else if (kvb->used == kvb->size) {
if (kvb->used == kvb->size) {
kvb->size += 4;
kvb->kv = realloc(kvb->kv, kvb->size * sizeof(*kvb->kv));

18
src/mod_accesslog.c

@ -257,11 +257,7 @@ static int accesslog_parse_format(server *srv, format_fields *fields, buffer *fo
case '%':
if (i > 0 && start != i) {
/* copy the string before this % */
if (fields->size == 0) {
fields->size = 16;
fields->used = 0;
fields->ptr = malloc(fields->size * sizeof(format_field * ));
} else if (fields->used == fields->size) {
if (fields->used == fields->size) {
fields->size += 16;
fields->ptr = realloc(fields->ptr, fields->size * sizeof(format_field * ));
}
@ -277,11 +273,7 @@ static int accesslog_parse_format(server *srv, format_fields *fields, buffer *fo
/* we need a new field */
if (fields->size == 0) {
fields->size = 16;
fields->used = 0;
fields->ptr = malloc(fields->size * sizeof(format_field * ));
} else if (fields->used == fields->size) {
if (fields->used == fields->size) {
fields->size += 16;
fields->ptr = realloc(fields->ptr, fields->size * sizeof(format_field * ));
}
@ -412,11 +404,7 @@ static int accesslog_parse_format(server *srv, format_fields *fields, buffer *fo
if (start < i) {
/* copy the string */
if (fields->size == 0) {
fields->size = 16;
fields->used = 0;
fields->ptr = malloc(fields->size * sizeof(format_field * ));
} else if (fields->used == fields->size) {
if (fields->used == fields->size) {
fields->size += 16;
fields->ptr = realloc(fields->ptr, fields->size * sizeof(format_field * ));
}

6
src/mod_cgi.c

@ -249,11 +249,7 @@ SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
static void cgi_pid_add(plugin_data *p, pid_t pid, void *ctx) {
buffer_pid_t *r = &(p->cgi_pid);
if (r->size == 0) {
r->size = 16;
r->ptr = malloc(sizeof(*r->ptr) * r->size);
force_assert(r->ptr);
} else if (r->used == r->size) {
if (r->used == r->size) {
r->size += 16;
r->ptr = realloc(r->ptr, sizeof(*r->ptr) * r->size);
force_assert(r->ptr);

11
src/mod_dirlisting.c

@ -95,16 +95,7 @@ static int excludes_buffer_append(excludes_buffer *exb, buffer *string) {
if (!string) return -1;
if (exb->size == 0) {
exb->size = 4;
exb->used = 0;
exb->ptr = malloc(exb->size * sizeof(*exb->ptr));
for(i = 0; i < exb->size ; i++) {
exb->ptr[i] = calloc(1, sizeof(**exb->ptr));
}
} else if (exb->used == exb->size) {
if (exb->used == exb->size) {
exb->size += 4;
exb->ptr = realloc(exb->ptr, exb->size * sizeof(*exb->ptr));

5
src/mod_magnet_cache.c

@ -96,10 +96,7 @@ lua_State *script_cache_get_script(server *srv, connection *con, script_cache *c
if (sc == NULL) {
sc = script_init();
if (cache->size == 0) {
cache->size = 16;
cache->ptr = malloc(cache->size * sizeof(*(cache->ptr)));
} else if (cache->used == cache->size) {
if (cache->used == cache->size) {
cache->size += 16;
cache->ptr = realloc(cache->ptr, cache->size * sizeof(*(cache->ptr)));
}

8
src/mod_uploadprogress.c

@ -78,13 +78,7 @@ static int connection_map_insert(connection_map *cm, connection *con, const char
connection_map_entry *cme;
size_t i;
if (cm->size == 0) {
cm->size = 16;
cm->ptr = malloc(cm->size * sizeof(*(cm->ptr)));
for (i = 0; i < cm->size; i++) {
cm->ptr[i] = NULL;
}
} else if (cm->used == cm->size) {
if (cm->used == cm->size) {
cm->size += 16;
cm->ptr = realloc(cm->ptr, cm->size * sizeof(*(cm->ptr)));
for (i = cm->used; i < cm->size; i++) {

5
src/mod_webdav.c

@ -1391,10 +1391,7 @@ static handler_t mod_webdav_propfind(server *srv, connection *con, plugin_data *
}
/* add property to requested list */
if (req_props->size == 0) {
req_props->size = 16;
req_props->ptr = malloc(sizeof(*(req_props->ptr)) * req_props->size);
} else if (req_props->used == req_props->size) {
if (req_props->used == req_props->size) {
req_props->size += 16;
req_props->ptr = realloc(req_props->ptr, sizeof(*(req_props->ptr)) * req_props->size);
}

8
src/network.c

@ -124,16 +124,12 @@ 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.size == 0) {
srv->srv_sockets.size = 4;
srv->srv_sockets.used = 0;
srv->srv_sockets.ptr = malloc(srv->srv_sockets.size * sizeof(server_socket*));
force_assert(NULL != srv->srv_sockets.ptr);
} else if (srv->srv_sockets.used == srv->srv_sockets.size) {
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;
}

7
src/plugin.c

@ -88,12 +88,7 @@ static void plugin_free(plugin *p) {
static int plugins_register(server *srv, plugin *p) {
plugin **ps;
if (0 == srv->plugins.size) {
srv->plugins.size = 4;
srv->plugins.ptr = malloc(srv->plugins.size * sizeof(*ps));
force_assert(NULL != srv->plugins.ptr);
srv->plugins.used = 0;
} else if (srv->plugins.used == srv->plugins.size) {
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);

Loading…
Cancel
Save