void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock = luaM_realloc_(L, block, osize, nsize); if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ luaM_error(L); return newblock; }
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; }
/* Glue functions for DynASM memory allocation. */ static void dasm_m_grow(Dst_DECL, void **pp, size_t *szp, int need) { size_t sz = *szp; if (sz > (size_t)need) return; if (sz < 16) sz = 16; while (sz < (size_t)need) sz += sz; *pp = luaM_realloc_(J->L, *pp, *szp, sz); *szp = sz; }