Explorar o código

keep memory-error message in the global state, so that its use
does not depend on Lua internalizing strings to avoid a string
creation on memory errors

Roberto Ierusalimschy %!s(int64=15) %!d(string=hai) anos
pai
achega
055104f5b6
Modificáronse 4 ficheiros con 13 adicións e 9 borrados
  1. 3 3
      ldo.c
  2. 1 3
      lmem.h
  3. 7 2
      lstate.c
  4. 2 1
      lstate.h

+ 3 - 3
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 2.81 2010/02/09 11:56:29 roberto Exp roberto $
+** $Id: ldo.c,v 2.82 2010/03/26 20:58:11 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -83,8 +83,8 @@ struct lua_longjmp {
 
 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
   switch (errcode) {
-    case LUA_ERRMEM: {
-      setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));
+    case LUA_ERRMEM: {  /* memory error? */
+      setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
       break;
     }
     case LUA_ERRERR: {

+ 1 - 3
lmem.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lmem.h,v 1.34 2009/04/17 14:40:13 roberto Exp roberto $
+** $Id: lmem.h,v 1.35 2009/12/16 16:42:58 roberto Exp roberto $
 ** Interface to Memory Manager
 ** See Copyright Notice in lua.h
 */
@@ -13,8 +13,6 @@
 #include "llimits.h"
 #include "lua.h"
 
-#define MEMERRMSG	"not enough memory"
-
 
 #define luaM_reallocv(L,b,on,n,e) \
 	((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ?  /* +1 to avoid warnings */ \

+ 7 - 2
lstate.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 2.76 2010/03/29 17:43:14 roberto Exp roberto $
+** $Id: lstate.c,v 2.77 2010/04/05 16:35:37 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -34,6 +34,9 @@
 #endif
 
 
+#define MEMERRMSG       "not enough memory"
+
+
 /*
 ** thread state + extra space
 */
@@ -157,7 +160,9 @@ static void f_luaopen (lua_State *L, void *ud) {
   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
   luaT_init(L);
   luaX_init(L);
-  luaS_fix(luaS_newliteral(L, MEMERRMSG));
+  /* pre-create memory-error message */
+  g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
+  luaS_fix(g->memerrmsg);  /* it should never be collected */
   g->GCthreshold = 4*g->totalbytes;
 }
 

+ 2 - 1
lstate.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 2.59 2010/03/29 17:43:14 roberto Exp roberto $
+** $Id: lstate.h,v 2.60 2010/04/05 16:35:37 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -142,6 +142,7 @@ typedef struct global_State {
   lua_CFunction panic;  /* to be called in unprotected errors */
   struct lua_State *mainthread;
   const lua_Number *version;  /* pointer to version number */
+  TString *memerrmsg;  /* memory-error message */
   TString *tmname[TM_N];  /* array with tag-method names */
   struct Table *mt[NUM_TAGS];  /* metatables for basic types */
 } global_State;