summaryrefslogtreecommitdiff
path: root/src/lib_aux.c
diff options
context:
space:
mode:
authorMike Pall <mike>2011-05-30 01:32:50 +0200
committerMike Pall <mike>2011-05-30 01:32:50 +0200
commit03b5c8c935da7753057cee872e4f55f0aace3aff (patch)
treebdaea72ef19393537ca87aee471034303df3a24d /src/lib_aux.c
parent638f9689783243dd2f552de541c535cb2d635d20 (diff)
downloadluajit2-03b5c8c935da7753057cee872e4f55f0aace3aff.tar.gz
Clean up memory allocator initialization and catch early OOM.
Diffstat (limited to 'src/lib_aux.c')
-rw-r--r--src/lib_aux.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/src/lib_aux.c b/src/lib_aux.c
index 64d5c3c6..bb5995aa 100644
--- a/src/lib_aux.c
+++ b/src/lib_aux.c
@@ -312,6 +312,13 @@ LUALIB_API int luaL_loadstring(lua_State *L, const char *s)
/* -- Default allocator and panic function -------------------------------- */
+static int panic(lua_State *L)
+{
+ fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
+ lua_tostring(L, -1));
+ return 0;
+}
+
#ifdef LUAJIT_USE_SYSMALLOC
#if LJ_64
@@ -330,30 +337,26 @@ static void *mem_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
}
}
-#define mem_create() NULL
+LUALIB_API lua_State *luaL_newstate(void)
+{
+ lua_State *L = lua_newstate(mem_alloc, NULL);
+ if (L) G(L)->panic = panic;
+ return L;
+}
#else
#include "lj_alloc.h"
-#define mem_alloc lj_alloc_f
-#define mem_create lj_alloc_create
-
-#endif
-
-static int panic(lua_State *L)
-{
- fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
- lua_tostring(L, -1));
- return 0;
-}
-
LUALIB_API lua_State *luaL_newstate(void)
{
+ lua_State *L;
+ void *ud = lj_alloc_create();
+ if (ud == NULL) return NULL;
#if LJ_64
- lua_State *L = lj_state_newstate(mem_alloc, mem_create());
+ L = lj_state_newstate(lj_alloc_f, ud);
#else
- lua_State *L = lua_newstate(mem_alloc, mem_create());
+ L = lua_newstate(lj_alloc_f, ud);
#endif
if (L) G(L)->panic = panic;
return L;
@@ -368,3 +371,5 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
}
#endif
+#endif
+