From 055104f5b6a0264974c5d9f2a55499420a1c9c2a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 Apr 2010 14:16:46 -0300 Subject: [PATCH] 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 --- ldo.c | 6 +++--- lmem.h | 4 +--- lstate.c | 9 +++++++-- lstate.h | 3 ++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/ldo.c b/ldo.c index d00ed860..400afa0f 100644 --- a/ldo.c +++ b/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: { diff --git a/lmem.h b/lmem.h index 74f236cd..710a4904 100644 --- a/lmem.h +++ b/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 */ \ diff --git a/lstate.c b/lstate.c index 51272622..0d88c960 100644 --- a/lstate.c +++ b/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; } diff --git a/lstate.h b/lstate.h index 1c70aca8..17cf2e06 100644 --- a/lstate.h +++ b/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;