void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, int size_elems, int limit, const char *what) { void *newblock; int size = *psize; if (nelems + 1 <= size) /* does one extra element still fit? */ return block; /* nothing to be done */ if (size >= limit / 2) { /* cannot double it? */ if (unlikely(size >= limit)) /* cannot grow even a little? */ luaG_runerror(L, "too many %s (limit is %d)", what, limit); size = limit; /* still have at least one free place */ } else { size *= 2; if (size < MINSIZEARRAY) size = MINSIZEARRAY; /* minimum size */ } lua_assert(nelems + 1 <= size && size <= limit); /* 'limit' ensures that multiplication will not overflow */ newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems, cast_sizet(size) * size_elems); if (unlikely(newblock == NULL)) luaM_error(L); *psize = size; /* update only when everything else is OK */ return newblock; }
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, int final_n, int size_elem) { global_State *g = G(L); void *newblock; size_t oldsize = cast_sizet((*size) * size_elem); size_t newsize = cast_sizet(final_n * size_elem); lua_assert(newsize <= oldsize); newblock = (*g->frealloc)(g->ud, block, oldsize, newsize); if (unlikely(newblock == NULL && final_n > 0)) /* allocation failed? */ luaM_error(L); else { g->GCdebt += newsize - oldsize; *size = final_n; return newblock; } }
static void freehash (lua_State *L, Table *t) { if (!isdummy(t)) luaM_freearray(L, t->node, cast_sizet(sizenode(t))); }