5 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
10 t = index2adr(L, idx);
11 api_checkvalidindex(L, t);
12 - setsvalue(L, &key, luaS_new(L, k));
13 - luaV_settable(L, t, &key, L->top - 1);
14 - L->top--; /* pop value */
15 + setsvalue2s(L, L->top, luaS_new(L, k));
17 + luaV_settable(L, t, L->top - 1, L->top - 2);
18 + L->top -= 2; /* pop key and value */
27 + lu_mem old_thres = g->GCthreshold;
28 + if(g->GCthreshold != MAX_LUMEM) {
29 + g->GCthreshold = MAX_LUMEM;
31 + g->GCthreshold = old_thres;
39 struct SParser *p = cast(struct SParser *, ud);
40 int c = luaZ_lookahead(p->z);
42 + lua_gc(L, LUA_GCSTOP, 0); /* stop collector during parsing */
43 tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
45 cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
47 cl->l.upvals[i] = luaF_newupval(L);
48 setclvalue(L, L->top, cl);
50 + lua_gc(L, LUA_GCRESTART, 0);
57 /* check size of buffer */
58 if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */
59 size_t newsize = luaZ_sizebuffer(&g->buff) / 2;
60 - luaZ_resizebuffer(L, &g->buff, newsize);
61 + /* make sure newsize is larger then the buffer's in use size. */
62 + newsize = (luaZ_bufflen(&g->buff) > newsize) ? luaZ_bufflen(&g->buff) : newsize;
63 + if(newsize < luaZ_sizebuffer(&g->buff))
64 + luaZ_resizebuffer(L, &g->buff, newsize);
72 lua_State *luaE_newthread (lua_State *L) {
73 lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
74 - luaC_link(L, obj2gco(L1), LUA_TTHREAD);
75 preinit_state(L1, G(L));
76 stack_init(L1, L); /* init stack */
77 setobj2n(L, gt(L1), gt(L)); /* share table of globals */
79 L1->basehookcount = L->basehookcount;
82 + luaC_link(L, obj2gco(L1), LUA_TTHREAD);
83 lua_assert(iswhite(obj2gco(L1)));
90 if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
93 + if ((tb->nuse + 1) > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
94 + luaS_resize(L, tb->size*2); /* too crowded */
95 ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
100 memcpy(ts+1, str, l*sizeof(char));
101 ((char *)(ts+1))[l] = '\0'; /* ending 0 */
103 h = lmod(h, tb->size);
104 ts->tsv.next = tb->hash[h]; /* chain new entry */
105 tb->hash[h] = obj2gco(ts);
107 - if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
108 - luaS_resize(L, tb->size*2); /* too crowded */
116 Table *luaH_new (lua_State *L, int narray, int nhash) {
117 Table *t = luaM_new(L, Table);
118 - luaC_link(L, obj2gco(t), LUA_TTABLE);
120 t->flags = cast_byte(~0);
121 /* temporary values (kept only if some malloc fails) */
123 t->node = cast(Node *, dummynode);
124 setarrayvector(L, t, narray);
125 setnodevector(L, t, nhash);
126 + luaC_link(L, obj2gco(t), LUA_TTABLE);
133 if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
137 buffer = luaZ_openspace(L, &G(L)->buff, tl);
139 for (i=n; i>0; i--) { /* concat all strings */
143 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
144 + luaZ_resetbuffer(&G(L)->buff);
146 total -= n-1; /* got `n' strings to create 1 new */
158 + size_t peak_memused;
160 + size_t max_memused;
165 +static void *script_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
167 + script_info_t *info=(script_info_t *)ud;
168 + size_t old_size = info->memused;
170 + info->memused -= osize;
175 + info->memused += nsize;
176 + if(info->max_memused > 0 && nsize > osize &&
177 + (info->memused >= info->max_memused || info->memused >= info->gc_memused)) {
178 +#ifdef LOW_MEM_DEBUG
179 + printf("LOW MEM: 1 osize=%zd, nsize=%zd, used=%zu, peak=%zu, need=%zd\n", osize, nsize,
180 + info->memused, info->peak_memused, (info->memused - info->max_memused));
182 + info->memused = old_size;
183 + /* don't allow a recursive garbage collection call. */
184 + if(info->collecting != 0) {
187 + info->collecting = 1;
188 + /* try to free memory by collecting garbage. */
189 + lua_gc(info->L, LUA_GCCOLLECT, 0);
190 + info->collecting = 0;
191 +#ifdef LOW_MEM_DEBUG
192 + printf("LOW MEM: 2 used=%zu, peak=%zu\n", info->memused, info->peak_memused);
194 + /* check memory usage again. */
195 + old_size = info->memused;
196 + info->memused -= osize;
197 + info->memused += nsize;
198 + if(info->memused >= info->max_memused) {
199 + info->memused = old_size;
200 +#ifdef LOW_MEM_DEBUG
201 + printf("OUT OF MEMORY: memused=%zd, osize=%zd, nsize=%zd\n", info->memused, osize, nsize);
206 + if(info->memused > info->peak_memused) info->peak_memused = info->memused;
207 + return realloc(ptr, nsize);
210 +static int set_memory_limit(lua_State *L)
212 + int hardlimit = luaL_checknumber(L, 1);
213 + int softlimit = luaL_optnumber(L, 2, 0);
215 + script_info_t *info;
216 + lua_getallocf(L, (void *)(&info));
218 + if( hardlimit >= 0 )
220 + if( softlimit <= 0 )
221 + softlimit = (int)((float)hardlimit * 0.75);
223 + info->max_memused = hardlimit;
224 + info->gc_memused = softlimit;
227 + lua_pushnumber(L, hardlimit);
228 + lua_pushnumber(L, softlimit);
232 +static int get_memory_limit(lua_State *L)
234 + script_info_t *info;
235 + lua_getallocf(L, (void *)(&info));
236 + lua_pushnumber(L, info->max_memused);
237 + lua_pushnumber(L, info->gc_memused);
242 static lua_State *globalL = NULL;
244 static const char *progname = LUA_PROGNAME;
245 @@ -377,11 +465,28 @@
246 int main (int argc, char **argv) {
249 - lua_State *L = lua_open(); /* create state */
250 + script_info_t *info;
252 + info = (script_info_t *)calloc(1, sizeof(script_info_t));
253 + info->max_memused = 0;
254 + info->collecting = 0;
255 + info->name = argv[0];
257 + info->peak_memused = 0;
259 + lua_State *L = lua_newstate(script_alloc, info);
262 l_message(argv[0], "cannot create state: not enough memory");
269 + lua_register(L, "set_memory_limit", set_memory_limit);
270 + lua_register(L, "get_memory_limit", get_memory_limit);
272 /* Checking 'sizeof(lua_Integer)' cannot be made in preprocessor on all compilers.
276 status = lua_cpcall(L, &pmain, &s);
280 +#ifdef LOW_MEM_DEBUG
281 + printf("%s: memused=%zd, peak_memused=%zd\n", info->name,
282 + info->memused, info->peak_memused);
287 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;