diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2014-04-10 09:48:59 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2014-04-12 10:20:55 -0400 |
commit | ca2d37841476e6272c9957c3f5a0cbe869a531ca (patch) | |
tree | ab1dea346bd6342248858be26e9638437141a0d1 /src/shared/util.c | |
parent | 7cc832b91e8f5883b505c42f9f403e03dfc83c89 (diff) |
Unify GREEDY_REALLOC and GREEDY_REALLOC_T
greedy_realloc() and greedy_realloc0() now store the allocated
size as the count, not bytes.
Replace GREEDY_REALLOC uses with GREEDY_REALLOC_T everywhere,
and then rename GREEDY_REALLOC_T to GREEDY_REALLOC. It is just
too error-prone to have two slightly different macros which do the
same thing.
Diffstat (limited to 'src/shared/util.c')
-rw-r--r-- | src/shared/util.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index ffe6624f9a..1a727ca40a 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -5819,8 +5819,8 @@ char *strrep(const char *s, unsigned n) { return r; } -void* greedy_realloc(void **p, size_t *allocated, size_t need) { - size_t a; +void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) { + size_t a, newalloc; void *q; assert(p); @@ -5829,10 +5829,11 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need) { if (*allocated >= need) return *p; - a = MAX(64u, need * 2); + newalloc = MAX(need * 2, 64u / size); + a = newalloc * size; /* check for overflows */ - if (a < need) + if (a < size * need) return NULL; q = realloc(*p, a); @@ -5840,11 +5841,11 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need) { return NULL; *p = q; - *allocated = a; + *allocated = newalloc; return q; } -void* greedy_realloc0(void **p, size_t *allocated, size_t need) { +void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) { size_t prev; uint8_t *q; @@ -5853,12 +5854,12 @@ void* greedy_realloc0(void **p, size_t *allocated, size_t need) { prev = *allocated; - q = greedy_realloc(p, allocated, need); + q = greedy_realloc(p, allocated, need, size); if (!q) return NULL; if (*allocated > prev) - memzero(&q[prev], *allocated - prev); + memzero(q + prev * size, (*allocated - prev) * size); return q; } |