summaryrefslogtreecommitdiff
path: root/package/lua/patches/040-memory-limits.patch
blob: 52bae6ae85ce7389344fa0bce74528f76c3682ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
--- a/src/lapi.c
+++ b/src/lapi.c
@@ -716,14 +716,14 @@
 
 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
   StkId t;
-  TValue key;
   lua_lock(L);
   api_checknelems(L, 1);
   t = index2adr(L, idx);
   api_checkvalidindex(L, t);
-  setsvalue(L, &key, luaS_new(L, k));
-  luaV_settable(L, t, &key, L->top - 1);
-  L->top--;  /* pop value */
+  setsvalue2s(L, L->top, luaS_new(L, k));
+  api_incr_top(L);
+  luaV_settable(L, t, L->top - 1, L->top - 2);
+  L->top -= 2;  /* pop key and value */
   lua_unlock(L);
 }
 
@@ -971,7 +971,12 @@
       break;
     }
     case LUA_GCCOLLECT: {
-      luaC_fullgc(L);
+      lu_mem old_thres = g->GCthreshold;
+      if(g->GCthreshold != MAX_LUMEM) {
+        g->GCthreshold = MAX_LUMEM;
+        luaC_fullgc(L);
+        g->GCthreshold = old_thres;
+      }
       break;
     }
     case LUA_GCCOUNT: {
--- a/src/ldo.c
+++ b/src/ldo.c
@@ -494,6 +494,7 @@
   struct SParser *p = cast(struct SParser *, ud);
   int c = luaZ_lookahead(p->z);
   luaC_checkGC(L);
+	lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during parsing */
   tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
                                                              &p->buff, p->name);
   cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
@@ -502,6 +503,7 @@
     cl->l.upvals[i] = luaF_newupval(L);
   setclvalue(L, L->top, cl);
   incr_top(L);
+	lua_gc(L, LUA_GCRESTART, 0);
 }
 
 
--- a/src/lgc.c
+++ b/src/lgc.c
@@ -437,7 +437,10 @@
   /* check size of buffer */
   if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) {  /* buffer too big? */
     size_t newsize = luaZ_sizebuffer(&g->buff) / 2;
-    luaZ_resizebuffer(L, &g->buff, newsize);
+    /* make sure newsize is larger then the buffer's in use size. */
+    newsize = (luaZ_bufflen(&g->buff) > newsize) ? luaZ_bufflen(&g->buff) : newsize;
+    if(newsize < luaZ_sizebuffer(&g->buff))
+      luaZ_resizebuffer(L, &g->buff, newsize);
   }
 }
 
--- a/src/lstate.c
+++ b/src/lstate.c
@@ -118,7 +118,6 @@
 
 lua_State *luaE_newthread (lua_State *L) {
   lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
-  luaC_link(L, obj2gco(L1), LUA_TTHREAD);
   preinit_state(L1, G(L));
   stack_init(L1, L);  /* init stack */
   setobj2n(L, gt(L1), gt(L));  /* share table of globals */
@@ -126,6 +125,7 @@
   L1->basehookcount = L->basehookcount;
   L1->hook = L->hook;
   resethookcount(L1);
+  luaC_link(L, obj2gco(L1), LUA_TTHREAD);
   lua_assert(iswhite(obj2gco(L1)));
   return L1;
 }
--- a/src/lstring.c
+++ b/src/lstring.c
@@ -53,6 +53,9 @@
   stringtable *tb;
   if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
     luaM_toobig(L);
+  tb = &G(L)->strt;
+  if ((tb->nuse + 1) > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
+    luaS_resize(L, tb->size*2);  /* too crowded */
   ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
   ts->tsv.len = l;
   ts->tsv.hash = h;
@@ -61,13 +64,10 @@
   ts->tsv.reserved = 0;
   memcpy(ts+1, str, l*sizeof(char));
   ((char *)(ts+1))[l] = '\0';  /* ending 0 */
-  tb = &G(L)->strt;
   h = lmod(h, tb->size);
   ts->tsv.next = tb->hash[h];  /* chain new entry */
   tb->hash[h] = obj2gco(ts);
   tb->nuse++;
-  if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
-    luaS_resize(L, tb->size*2);  /* too crowded */
   return ts;
 }
 
--- a/src/ltable.c
+++ b/src/ltable.c
@@ -371,7 +371,6 @@
 
 Table *luaH_new (lua_State *L, int narray, int nhash) {
   Table *t = luaM_new(L, Table);
-  luaC_link(L, obj2gco(t), LUA_TTABLE);
   t->metatable = NULL;
   t->flags = cast_byte(~0);
   /* temporary values (kept only if some malloc fails) */
@@ -381,6 +380,7 @@
   t->node = cast(Node *, dummynode);
   setarrayvector(L, t, narray);
   setnodevector(L, t, nhash);
+  luaC_link(L, obj2gco(t), LUA_TTABLE);
   return t;
 }
 
--- a/src/lvm.c
+++ b/src/lvm.c
@@ -375,6 +375,7 @@
         if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
         tl += l;
       }
+      G(L)->buff.n = tl;
       buffer = luaZ_openspace(L, &G(L)->buff, tl);
       tl = 0;
       for (i=n; i>0; i--) {  /* concat all strings */
@@ -383,6 +384,7 @@
         tl += l;
       }
       setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
+      luaZ_resetbuffer(&G(L)->buff);
     }
     total -= n-1;  /* got `n' strings to create 1 new */
     last -= n-1;
--- a/src/lua.c
+++ b/src/lua.c
@@ -19,6 +19,82 @@
 #include "llimits.h"
 
 
+typedef struct {
+	char		*name;
+	lua_State	*L;
+	size_t		memused;
+	size_t		peak_memused;
+	size_t		max_memused;
+	int		collecting;
+} script_info_t;
+
+
+static void *script_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
+{
+	script_info_t *info=(script_info_t *)ud;
+	size_t old_size = info->memused;
+
+	info->memused -= osize;
+	if (nsize == 0) {
+		free(ptr);
+		return NULL;
+	}
+	info->memused += nsize;
+	if(info->max_memused > 0 && nsize > osize && info->memused >= info->max_memused) {
+#ifdef LOW_MEM_DEBUG
+		printf("LOW MEM: 1 osize=%zd, nsize=%zd, used=%zu, peak=%zu, need=%zd\n", osize, nsize,
+			info->memused, info->peak_memused, (info->memused - info->max_memused));
+#endif
+		info->memused = old_size;
+		/* don't allow a recursive garbage collection call. */
+		if(info->collecting != 0) {
+			return NULL;
+		}
+		info->collecting = 1;
+		/* try to free memory by collecting garbage. */
+		lua_gc(info->L, LUA_GCCOLLECT, 0);
+		info->collecting = 0;
+#ifdef LOW_MEM_DEBUG
+		printf("LOW MEM: 2 used=%zu, peak=%zu\n", info->memused, info->peak_memused);
+#endif
+		/* check memory usage again. */
+		old_size = info->memused;
+		info->memused -= osize;
+		info->memused += nsize;
+		if(info->memused >= info->max_memused) {
+			info->memused = old_size;
+#ifdef LOW_MEM_DEBUG
+			printf("OUT OF MEMORY: memused=%zd, osize=%zd, nsize=%zd\n", info->memused, osize, nsize);
+#endif
+			return NULL;
+		}
+	}
+	if(info->memused > info->peak_memused) info->peak_memused = info->memused;
+	return realloc(ptr, nsize);
+}
+
+static int set_memory_limit(lua_State *L)
+{
+	int limit = luaL_checknumber(L, 1);
+	script_info_t *info;
+	lua_getallocf(L, (void *)(&info));
+
+	if( limit >= 0 )
+		info->max_memused = limit;
+
+	lua_pushnumber(L, limit);
+	return 1;
+}
+
+static int get_memory_limit(lua_State *L)
+{
+	script_info_t *info;
+	lua_getallocf(L, (void *)(&info));
+	lua_pushnumber(L, info->max_memused);
+	return 1;
+}
+
+
 static lua_State *globalL = NULL;
 
 static const char *progname = LUA_PROGNAME;
@@ -377,11 +453,28 @@
 int main (int argc, char **argv) {
   int status;
   struct Smain s;
-  lua_State *L = lua_open();  /* create state */
+  script_info_t *info;
+
+  info = (script_info_t *)calloc(1, sizeof(script_info_t));
+  info->max_memused = 0;
+  info->collecting = 0;
+  info->name = argv[0];
+  info->memused = 0;
+  info->peak_memused = 0;
+
+  lua_State *L = lua_newstate(script_alloc, info);
+
   if (L == NULL) {
     l_message(argv[0], "cannot create state: not enough memory");
     return EXIT_FAILURE;
   }
+
+  info->L = L;
+
+  luaL_openlibs(L);
+  lua_register(L, "set_memory_limit", set_memory_limit);
+  lua_register(L, "get_memory_limit", get_memory_limit);
+
   /* Checking 'sizeof(lua_Integer)' cannot be made in preprocessor on all compilers.
   */
 #ifdef LNUM_INT16
@@ -396,6 +489,14 @@
   status = lua_cpcall(L, &pmain, &s);
   report(L, status);
   lua_close(L);
+
+#ifdef LOW_MEM_DEBUG
+  printf("%s: memused=%zd, peak_memused=%zd\n", info->name,
+	info->memused, info->peak_memused);
+#endif
+
+  free(info);
+
   return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
 }