18e3e3a7aSWarner Losh /* 2*0495ed39SKyle Evans ** $Id: lmem.c $ 38e3e3a7aSWarner Losh ** Interface to Memory Manager 48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h 58e3e3a7aSWarner Losh */ 68e3e3a7aSWarner Losh 78e3e3a7aSWarner Losh #define lmem_c 88e3e3a7aSWarner Losh #define LUA_CORE 98e3e3a7aSWarner Losh 108e3e3a7aSWarner Losh #include "lprefix.h" 118e3e3a7aSWarner Losh 128e3e3a7aSWarner Losh 138e3e3a7aSWarner Losh #include <stddef.h> 148e3e3a7aSWarner Losh 158e3e3a7aSWarner Losh #include "lua.h" 168e3e3a7aSWarner Losh 178e3e3a7aSWarner Losh #include "ldebug.h" 188e3e3a7aSWarner Losh #include "ldo.h" 198e3e3a7aSWarner Losh #include "lgc.h" 208e3e3a7aSWarner Losh #include "lmem.h" 218e3e3a7aSWarner Losh #include "lobject.h" 228e3e3a7aSWarner Losh #include "lstate.h" 238e3e3a7aSWarner Losh 248e3e3a7aSWarner Losh 25*0495ed39SKyle Evans #if defined(EMERGENCYGCTESTS) 26*0495ed39SKyle Evans /* 27*0495ed39SKyle Evans ** First allocation will fail whenever not building initial state 28*0495ed39SKyle Evans ** and not shrinking a block. (This fail will trigger 'tryagain' and 29*0495ed39SKyle Evans ** a full GC cycle at every allocation.) 30*0495ed39SKyle Evans */ 31*0495ed39SKyle Evans static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { 32*0495ed39SKyle Evans if (ttisnil(&g->nilvalue) && ns > os) 33*0495ed39SKyle Evans return NULL; /* fail */ 34*0495ed39SKyle Evans else /* normal allocation */ 35*0495ed39SKyle Evans return (*g->frealloc)(g->ud, block, os, ns); 36*0495ed39SKyle Evans } 37*0495ed39SKyle Evans #else 38*0495ed39SKyle Evans #define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) 39*0495ed39SKyle Evans #endif 40*0495ed39SKyle Evans 41*0495ed39SKyle Evans 42*0495ed39SKyle Evans 43*0495ed39SKyle Evans 448e3e3a7aSWarner Losh 458e3e3a7aSWarner Losh /* 468e3e3a7aSWarner Losh ** About the realloc function: 478e3e3a7aSWarner Losh ** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 488e3e3a7aSWarner Losh ** ('osize' is the old size, 'nsize' is the new size) 498e3e3a7aSWarner Losh ** 50*0495ed39SKyle Evans ** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL. 51*0495ed39SKyle Evans ** Particularly, frealloc(ud, NULL, 0, 0) does nothing, 52*0495ed39SKyle Evans ** which is equivalent to free(NULL) in ISO C. 538e3e3a7aSWarner Losh ** 54*0495ed39SKyle Evans ** - frealloc(ud, NULL, x, s) creates a new block of size 's' 55*0495ed39SKyle Evans ** (no matter 'x'). Returns NULL if it cannot create the new block. 568e3e3a7aSWarner Losh ** 57*0495ed39SKyle Evans ** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from 58*0495ed39SKyle Evans ** size 'x' to size 'y'. Returns NULL if it cannot reallocate the 59*0495ed39SKyle Evans ** block to the new size. 608e3e3a7aSWarner Losh */ 618e3e3a7aSWarner Losh 628e3e3a7aSWarner Losh 638e3e3a7aSWarner Losh 64*0495ed39SKyle Evans 65*0495ed39SKyle Evans /* 66*0495ed39SKyle Evans ** {================================================================== 67*0495ed39SKyle Evans ** Functions to allocate/deallocate arrays for the Parser 68*0495ed39SKyle Evans ** =================================================================== 69*0495ed39SKyle Evans */ 70*0495ed39SKyle Evans 71*0495ed39SKyle Evans /* 72*0495ed39SKyle Evans ** Minimum size for arrays during parsing, to avoid overhead of 73*0495ed39SKyle Evans ** reallocating to size 1, then 2, and then 4. All these arrays 74*0495ed39SKyle Evans ** will be reallocated to exact sizes or erased when parsing ends. 75*0495ed39SKyle Evans */ 768e3e3a7aSWarner Losh #define MINSIZEARRAY 4 778e3e3a7aSWarner Losh 788e3e3a7aSWarner Losh 79*0495ed39SKyle Evans void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, 80*0495ed39SKyle Evans int size_elems, int limit, const char *what) { 818e3e3a7aSWarner Losh void *newblock; 82*0495ed39SKyle Evans int size = *psize; 83*0495ed39SKyle Evans if (nelems + 1 <= size) /* does one extra element still fit? */ 84*0495ed39SKyle Evans return block; /* nothing to be done */ 85*0495ed39SKyle Evans if (size >= limit / 2) { /* cannot double it? */ 86*0495ed39SKyle Evans if (unlikely(size >= limit)) /* cannot grow even a little? */ 878e3e3a7aSWarner Losh luaG_runerror(L, "too many %s (limit is %d)", what, limit); 88*0495ed39SKyle Evans size = limit; /* still have at least one free place */ 898e3e3a7aSWarner Losh } 908e3e3a7aSWarner Losh else { 91*0495ed39SKyle Evans size *= 2; 92*0495ed39SKyle Evans if (size < MINSIZEARRAY) 93*0495ed39SKyle Evans size = MINSIZEARRAY; /* minimum size */ 948e3e3a7aSWarner Losh } 95*0495ed39SKyle Evans lua_assert(nelems + 1 <= size && size <= limit); 96*0495ed39SKyle Evans /* 'limit' ensures that multiplication will not overflow */ 97*0495ed39SKyle Evans newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems, 98*0495ed39SKyle Evans cast_sizet(size) * size_elems); 99*0495ed39SKyle Evans *psize = size; /* update only when everything else is OK */ 1008e3e3a7aSWarner Losh return newblock; 1018e3e3a7aSWarner Losh } 1028e3e3a7aSWarner Losh 1038e3e3a7aSWarner Losh 104*0495ed39SKyle Evans /* 105*0495ed39SKyle Evans ** In prototypes, the size of the array is also its number of 106*0495ed39SKyle Evans ** elements (to save memory). So, if it cannot shrink an array 107*0495ed39SKyle Evans ** to its number of elements, the only option is to raise an 108*0495ed39SKyle Evans ** error. 109*0495ed39SKyle Evans */ 110*0495ed39SKyle Evans void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, 111*0495ed39SKyle Evans int final_n, int size_elem) { 112*0495ed39SKyle Evans void *newblock; 113*0495ed39SKyle Evans size_t oldsize = cast_sizet((*size) * size_elem); 114*0495ed39SKyle Evans size_t newsize = cast_sizet(final_n * size_elem); 115*0495ed39SKyle Evans lua_assert(newsize <= oldsize); 116*0495ed39SKyle Evans newblock = luaM_saferealloc_(L, block, oldsize, newsize); 117*0495ed39SKyle Evans *size = final_n; 118*0495ed39SKyle Evans return newblock; 119*0495ed39SKyle Evans } 120*0495ed39SKyle Evans 121*0495ed39SKyle Evans /* }================================================================== */ 122*0495ed39SKyle Evans 123*0495ed39SKyle Evans 1248e3e3a7aSWarner Losh l_noret luaM_toobig (lua_State *L) { 1258e3e3a7aSWarner Losh luaG_runerror(L, "memory allocation error: block too big"); 1268e3e3a7aSWarner Losh } 1278e3e3a7aSWarner Losh 1288e3e3a7aSWarner Losh 129*0495ed39SKyle Evans /* 130*0495ed39SKyle Evans ** Free memory 131*0495ed39SKyle Evans */ 132*0495ed39SKyle Evans void luaM_free_ (lua_State *L, void *block, size_t osize) { 133*0495ed39SKyle Evans global_State *g = G(L); 134*0495ed39SKyle Evans lua_assert((osize == 0) == (block == NULL)); 135*0495ed39SKyle Evans (*g->frealloc)(g->ud, block, osize, 0); 136*0495ed39SKyle Evans g->GCdebt -= osize; 137*0495ed39SKyle Evans } 138*0495ed39SKyle Evans 1398e3e3a7aSWarner Losh 1408e3e3a7aSWarner Losh /* 141*0495ed39SKyle Evans ** In case of allocation fail, this function will call the GC to try 142*0495ed39SKyle Evans ** to free some memory and then try the allocation again. 143*0495ed39SKyle Evans ** (It should not be called when shrinking a block, because then the 144*0495ed39SKyle Evans ** interpreter may be in the middle of a collection step.) 145*0495ed39SKyle Evans */ 146*0495ed39SKyle Evans static void *tryagain (lua_State *L, void *block, 147*0495ed39SKyle Evans size_t osize, size_t nsize) { 148*0495ed39SKyle Evans global_State *g = G(L); 149*0495ed39SKyle Evans if (ttisnil(&g->nilvalue)) { /* is state fully build? */ 150*0495ed39SKyle Evans luaC_fullgc(L, 1); /* try to free some memory... */ 151*0495ed39SKyle Evans return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 152*0495ed39SKyle Evans } 153*0495ed39SKyle Evans else return NULL; /* cannot free any memory without a full state */ 154*0495ed39SKyle Evans } 155*0495ed39SKyle Evans 156*0495ed39SKyle Evans 157*0495ed39SKyle Evans /* 158*0495ed39SKyle Evans ** Generic allocation routine. 159*0495ed39SKyle Evans ** If allocation fails while shrinking a block, do not try again; the 160*0495ed39SKyle Evans ** GC shrinks some blocks and it is not reentrant. 1618e3e3a7aSWarner Losh */ 1628e3e3a7aSWarner Losh void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 1638e3e3a7aSWarner Losh void *newblock; 1648e3e3a7aSWarner Losh global_State *g = G(L); 165*0495ed39SKyle Evans lua_assert((osize == 0) == (block == NULL)); 166*0495ed39SKyle Evans newblock = firsttry(g, block, osize, nsize); 167*0495ed39SKyle Evans if (unlikely(newblock == NULL && nsize > 0)) { 168*0495ed39SKyle Evans if (nsize > osize) /* not shrinking a block? */ 169*0495ed39SKyle Evans newblock = tryagain(L, block, osize, nsize); 170*0495ed39SKyle Evans if (newblock == NULL) /* still no memory? */ 171*0495ed39SKyle Evans return NULL; /* do not update 'GCdebt' */ 1728e3e3a7aSWarner Losh } 1738e3e3a7aSWarner Losh lua_assert((nsize == 0) == (newblock == NULL)); 174*0495ed39SKyle Evans g->GCdebt = (g->GCdebt + nsize) - osize; 1758e3e3a7aSWarner Losh return newblock; 1768e3e3a7aSWarner Losh } 1778e3e3a7aSWarner Losh 178*0495ed39SKyle Evans 179*0495ed39SKyle Evans void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, 180*0495ed39SKyle Evans size_t nsize) { 181*0495ed39SKyle Evans void *newblock = luaM_realloc_(L, block, osize, nsize); 182*0495ed39SKyle Evans if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ 183*0495ed39SKyle Evans luaM_error(L); 184*0495ed39SKyle Evans return newblock; 185*0495ed39SKyle Evans } 186*0495ed39SKyle Evans 187*0495ed39SKyle Evans 188*0495ed39SKyle Evans void *luaM_malloc_ (lua_State *L, size_t size, int tag) { 189*0495ed39SKyle Evans if (size == 0) 190*0495ed39SKyle Evans return NULL; /* that's all */ 191*0495ed39SKyle Evans else { 192*0495ed39SKyle Evans global_State *g = G(L); 193*0495ed39SKyle Evans void *newblock = firsttry(g, NULL, tag, size); 194*0495ed39SKyle Evans if (unlikely(newblock == NULL)) { 195*0495ed39SKyle Evans newblock = tryagain(L, NULL, tag, size); 196*0495ed39SKyle Evans if (newblock == NULL) 197*0495ed39SKyle Evans luaM_error(L); 198*0495ed39SKyle Evans } 199*0495ed39SKyle Evans g->GCdebt += size; 200*0495ed39SKyle Evans return newblock; 201*0495ed39SKyle Evans } 202*0495ed39SKyle Evans } 203