From d36c7fa5ec1edb2eb0faed1ffde72aee6b875d44 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 2 Dec 2020 12:26:29 +0100 Subject: [PATCH] MINOR: lua: simplify hlua_alloc() to only rely on realloc() The function really has the semantics of a realloc() except that it also passes the old size to help with accounting. No need to special case the free or malloc, realloc does everything we need. --- src/hlua.c | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/hlua.c b/src/hlua.c index 1829e87b6..176e6f78b 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -8229,37 +8229,22 @@ int hlua_post_init() /* The memory allocator used by the Lua stack. is a pointer to the * allocator's context. is the pointer to alloc/free/realloc. * is the previously allocated size or the kind of object in case of a new - * allocation. is the requested new size. + * allocation. is the requested new size. A new allocation is + * indicated by being NULL. A free is indicated by being + * zero. */ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) { struct hlua_mem_allocator *zone = ud; - if (nsize == 0) { - /* it's a free */ - if (ptr) - zone->allocated -= osize; - free(ptr); - return NULL; - } + if (!ptr) + osize = 0; - if (!ptr) { - /* it's a new allocation */ - if (zone->limit && zone->allocated + nsize > zone->limit) - return NULL; - - ptr = malloc(nsize); - if (ptr) - zone->allocated += nsize; - return ptr; - } - - /* it's a realloc */ if (zone->limit && zone->allocated + nsize - osize > zone->limit) return NULL; ptr = realloc(ptr, nsize); - if (ptr) + if (ptr || !nsize) zone->allocated += nsize - osize; return ptr; }