1 /*
2 ** $Id: lmem.c $
3 ** Interface to Memory Manager
4 ** See Copyright Notice in lua.h
5 */
6 
7 #define lmem_c
8 #define LUA_CORE
9 
10 #include "lprefix.h"
11 
12 
13 #include <stddef.h>
14 
15 #include "lua.h"
16 
17 #include "ldebug.h"
18 #include "ldo.h"
19 #include "lgc.h"
20 #include "lmem.h"
21 #include "lobject.h"
22 #include "lstate.h"
23 
24 
25 #if defined(EMERGENCYGCTESTS)
26 /*
27 ** First allocation will fail whenever not building initial state.
28 ** (This fail will trigger 'tryagain' and a full GC cycle at every
29 ** allocation.)
30 */
firsttry(global_State * g,void * block,size_t os,size_t ns)31 static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
32   if (completestate(g) && ns > 0)  /* frees never fail */
33     return NULL;  /* fail */
34   else  /* normal allocation */
35     return (*g->frealloc)(g->ud, block, os, ns);
36 }
37 #else
38 #define firsttry(g,block,os,ns)    ((*g->frealloc)(g->ud, block, os, ns))
39 #endif
40 
41 
42 
43 
44 
45 /*
46 ** About the realloc function:
47 ** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
48 ** ('osize' is the old size, 'nsize' is the new size)
49 **
50 ** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.
51 ** Particularly, frealloc(ud, NULL, 0, 0) does nothing,
52 ** which is equivalent to free(NULL) in ISO C.
53 **
54 ** - frealloc(ud, NULL, x, s) creates a new block of size 's'
55 ** (no matter 'x'). Returns NULL if it cannot create the new block.
56 **
57 ** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from
58 ** size 'x' to size 'y'. Returns NULL if it cannot reallocate the
59 ** block to the new size.
60 */
61 
62 
63 
64 
65 /*
66 ** {==================================================================
67 ** Functions to allocate/deallocate arrays for the Parser
68 ** ===================================================================
69 */
70 
71 /*
72 ** Minimum size for arrays during parsing, to avoid overhead of
73 ** reallocating to size 1, then 2, and then 4. All these arrays
74 ** will be reallocated to exact sizes or erased when parsing ends.
75 */
76 #define MINSIZEARRAY	4
77 
78 
luaM_growaux_(lua_State * L,void * block,int nelems,int * psize,int size_elems,int limit,const char * what)79 void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
80                      int size_elems, int limit, const char *what) {
81   void *newblock;
82   int size = *psize;
83   if (nelems + 1 <= size)  /* does one extra element still fit? */
84     return block;  /* nothing to be done */
85   if (size >= limit / 2) {  /* cannot double it? */
86     if (l_unlikely(size >= limit))  /* cannot grow even a little? */
87       luaG_runerror(L, "too many %s (limit is %d)", what, limit);
88     size = limit;  /* still have at least one free place */
89   }
90   else {
91     size *= 2;
92     if (size < MINSIZEARRAY)
93       size = MINSIZEARRAY;  /* minimum size */
94   }
95   lua_assert(nelems + 1 <= size && size <= limit);
96   /* 'limit' ensures that multiplication will not overflow */
97   newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
98                                          cast_sizet(size) * size_elems);
99   *psize = size;  /* update only when everything else is OK */
100   return newblock;
101 }
102 
103 
104 /*
105 ** In prototypes, the size of the array is also its number of
106 ** elements (to save memory). So, if it cannot shrink an array
107 ** to its number of elements, the only option is to raise an
108 ** error.
109 */
luaM_shrinkvector_(lua_State * L,void * block,int * size,int final_n,int size_elem)110 void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
111                           int final_n, int size_elem) {
112   void *newblock;
113   size_t oldsize = cast_sizet((*size) * size_elem);
114   size_t newsize = cast_sizet(final_n * size_elem);
115   lua_assert(newsize <= oldsize);
116   newblock = luaM_saferealloc_(L, block, oldsize, newsize);
117   *size = final_n;
118   return newblock;
119 }
120 
121 /* }================================================================== */
122 
123 
luaM_toobig(lua_State * L)124 l_noret luaM_toobig (lua_State *L) {
125   luaG_runerror(L, "memory allocation error: block too big");
126 }
127 
128 
129 /*
130 ** Free memory
131 */
luaM_free_(lua_State * L,void * block,size_t osize)132 void luaM_free_ (lua_State *L, void *block, size_t osize) {
133   global_State *g = G(L);
134   lua_assert((osize == 0) == (block == NULL));
135   (*g->frealloc)(g->ud, block, osize, 0);
136   g->GCdebt -= osize;
137 }
138 
139 
140 /*
141 ** In case of allocation fail, this function will do an emergency
142 ** collection to free some memory and then try the allocation again.
143 ** The GC should not be called while state is not fully built, as the
144 ** collector is not yet fully initialized. Also, it should not be called
145 ** when 'gcstopem' is true, because then the interpreter is in the
146 ** middle of a collection step.
147 */
tryagain(lua_State * L,void * block,size_t osize,size_t nsize)148 static void *tryagain (lua_State *L, void *block,
149                        size_t osize, size_t nsize) {
150   global_State *g = G(L);
151   if (completestate(g) && !g->gcstopem) {
152     luaC_fullgc(L, 1);  /* try to free some memory... */
153     return (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
154   }
155   else return NULL;  /* cannot free any memory without a full state */
156 }
157 
158 
159 /*
160 ** Generic allocation routine.
161 */
luaM_realloc_(lua_State * L,void * block,size_t osize,size_t nsize)162 void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
163   void *newblock;
164   global_State *g = G(L);
165   lua_assert((osize == 0) == (block == NULL));
166   newblock = firsttry(g, block, osize, nsize);
167   if (l_unlikely(newblock == NULL && nsize > 0)) {
168     newblock = tryagain(L, block, osize, nsize);
169     if (newblock == NULL)  /* still no memory? */
170       return NULL;  /* do not update 'GCdebt' */
171   }
172   lua_assert((nsize == 0) == (newblock == NULL));
173   g->GCdebt = (g->GCdebt + nsize) - osize;
174   return newblock;
175 }
176 
177 
luaM_saferealloc_(lua_State * L,void * block,size_t osize,size_t nsize)178 void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
179                                                     size_t nsize) {
180   void *newblock = luaM_realloc_(L, block, osize, nsize);
181   if (l_unlikely(newblock == NULL && nsize > 0))  /* allocation failed? */
182     luaM_error(L);
183   return newblock;
184 }
185 
186 
luaM_malloc_(lua_State * L,size_t size,int tag)187 void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
188   if (size == 0)
189     return NULL;  /* that's all */
190   else {
191     global_State *g = G(L);
192     void *newblock = firsttry(g, NULL, tag, size);
193     if (l_unlikely(newblock == NULL)) {
194       newblock = tryagain(L, NULL, tag, size);
195       if (newblock == NULL)
196         luaM_error(L);
197     }
198     g->GCdebt += size;
199     return newblock;
200   }
201 }
202