diff options
author | Mike Pall <mike> | 2011-05-30 01:32:50 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2011-05-30 01:32:50 +0200 |
commit | 03b5c8c935da7753057cee872e4f55f0aace3aff (patch) | |
tree | bdaea72ef19393537ca87aee471034303df3a24d /src/lib_aux.c | |
parent | 638f9689783243dd2f552de541c535cb2d635d20 (diff) | |
download | luajit-03b5c8c935da7753057cee872e4f55f0aace3aff.tar.gz luajit-03b5c8c935da7753057cee872e4f55f0aace3aff.tar.bz2 luajit-03b5c8c935da7753057cee872e4f55f0aace3aff.zip |
Clean up memory allocator initialization and catch early OOM.
Diffstat (limited to 'src/lib_aux.c')
-rw-r--r-- | src/lib_aux.c | 35 |
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) | |||
312 | 312 | ||
313 | /* -- Default allocator and panic function -------------------------------- */ | 313 | /* -- Default allocator and panic function -------------------------------- */ |
314 | 314 | ||
315 | static int panic(lua_State *L) | ||
316 | { | ||
317 | fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", | ||
318 | lua_tostring(L, -1)); | ||
319 | return 0; | ||
320 | } | ||
321 | |||
315 | #ifdef LUAJIT_USE_SYSMALLOC | 322 | #ifdef LUAJIT_USE_SYSMALLOC |
316 | 323 | ||
317 | #if LJ_64 | 324 | #if LJ_64 |
@@ -330,30 +337,26 @@ static void *mem_alloc(void *ud, void *ptr, size_t osize, size_t nsize) | |||
330 | } | 337 | } |
331 | } | 338 | } |
332 | 339 | ||
333 | #define mem_create() NULL | 340 | LUALIB_API lua_State *luaL_newstate(void) |
341 | { | ||
342 | lua_State *L = lua_newstate(mem_alloc, NULL); | ||
343 | if (L) G(L)->panic = panic; | ||
344 | return L; | ||
345 | } | ||
334 | 346 | ||
335 | #else | 347 | #else |
336 | 348 | ||
337 | #include "lj_alloc.h" | 349 | #include "lj_alloc.h" |
338 | 350 | ||
339 | #define mem_alloc lj_alloc_f | ||
340 | #define mem_create lj_alloc_create | ||
341 | |||
342 | #endif | ||
343 | |||
344 | static int panic(lua_State *L) | ||
345 | { | ||
346 | fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", | ||
347 | lua_tostring(L, -1)); | ||
348 | return 0; | ||
349 | } | ||
350 | |||
351 | LUALIB_API lua_State *luaL_newstate(void) | 351 | LUALIB_API lua_State *luaL_newstate(void) |
352 | { | 352 | { |
353 | lua_State *L; | ||
354 | void *ud = lj_alloc_create(); | ||
355 | if (ud == NULL) return NULL; | ||
353 | #if LJ_64 | 356 | #if LJ_64 |
354 | lua_State *L = lj_state_newstate(mem_alloc, mem_create()); | 357 | L = lj_state_newstate(lj_alloc_f, ud); |
355 | #else | 358 | #else |
356 | lua_State *L = lua_newstate(mem_alloc, mem_create()); | 359 | L = lua_newstate(lj_alloc_f, ud); |
357 | #endif | 360 | #endif |
358 | if (L) G(L)->panic = panic; | 361 | if (L) G(L)->panic = panic; |
359 | return L; | 362 | return L; |
@@ -368,3 +371,5 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) | |||
368 | } | 371 | } |
369 | #endif | 372 | #endif |
370 | 373 | ||
374 | #endif | ||
375 | |||