diff options
Diffstat (limited to 'src/tools.c')
| -rw-r--r-- | src/tools.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/tools.c b/src/tools.c index acb78e6..e72d441 100644 --- a/src/tools.c +++ b/src/tools.c | |||
| @@ -174,11 +174,12 @@ static int luaG_provide_protected_allocator( lua_State* L) | |||
| 174 | return 1; | 174 | return 1; |
| 175 | } | 175 | } |
| 176 | 176 | ||
| 177 | // called once at the creation of the universe (therefore L is the master Lua state everything originates from) | ||
| 177 | // Do I need to disable this when compiling for LuaJIT to prevent issues? | 178 | // Do I need to disable this when compiling for LuaJIT to prevent issues? |
| 178 | void initialize_allocator_function( Universe* U, lua_State* L) | 179 | void initialize_allocator_function( Universe* U, lua_State* L) |
| 179 | { | 180 | { |
| 180 | STACK_CHECK( L, 0); | 181 | STACK_CHECK( L, 0); |
| 181 | lua_getfield( L, -1, "allocator"); // settings allocator|nil|"protected" | 182 | lua_getfield( L, -1, "allocator"); // settings allocator|nil|"protected" |
| 182 | if( !lua_isnil( L, -1)) | 183 | if( !lua_isnil( L, -1)) |
| 183 | { | 184 | { |
| 184 | // store C function pointer in an internal variable | 185 | // store C function pointer in an internal variable |
| @@ -186,17 +187,17 @@ void initialize_allocator_function( Universe* U, lua_State* L) | |||
| 186 | if( U->provide_allocator != NULL) | 187 | if( U->provide_allocator != NULL) |
| 187 | { | 188 | { |
| 188 | // make sure the function doesn't have upvalues | 189 | // make sure the function doesn't have upvalues |
| 189 | char const* upname = lua_getupvalue( L, -1, 1); // settings allocator upval? | 190 | char const* upname = lua_getupvalue( L, -1, 1); // settings allocator upval? |
| 190 | if( upname != NULL) // should be "" for C functions with upvalues if any | 191 | if( upname != NULL) // should be "" for C functions with upvalues if any |
| 191 | { | 192 | { |
| 192 | (void) luaL_error( L, "config.allocator() shouldn't have upvalues"); | 193 | (void) luaL_error( L, "config.allocator() shouldn't have upvalues"); |
| 193 | } | 194 | } |
| 194 | // remove this C function from the config table so that it doesn't cause problems | 195 | // remove this C function from the config table so that it doesn't cause problems |
| 195 | // when we transfer the config table in newly created Lua states | 196 | // when we transfer the config table in newly created Lua states |
| 196 | lua_pushnil( L); // settings allocator nil | 197 | lua_pushnil( L); // settings allocator nil |
| 197 | lua_setfield( L, -3, "allocator"); // settings allocator | 198 | lua_setfield( L, -3, "allocator"); // settings allocator |
| 198 | } | 199 | } |
| 199 | else if( lua_type( L, -1) == LUA_TSTRING) | 200 | else if( lua_type( L, -1) == LUA_TSTRING) // should be "protected" |
| 200 | { | 201 | { |
| 201 | // initialize all we need for the protected allocator | 202 | // initialize all we need for the protected allocator |
| 202 | MUTEX_INIT( &U->protected_allocator.lock); // the mutex | 203 | MUTEX_INIT( &U->protected_allocator.lock); // the mutex |
| @@ -208,7 +209,14 @@ void initialize_allocator_function( Universe* U, lua_State* L) | |||
| 208 | lua_setallocf( L, protected_lua_Alloc, &U->protected_allocator); | 209 | lua_setallocf( L, protected_lua_Alloc, &U->protected_allocator); |
| 209 | } | 210 | } |
| 210 | } | 211 | } |
| 211 | lua_pop( L, 1); // settings | 212 | else |
| 213 | { | ||
| 214 | // initialize the mutex even if we are not going to use it, because cleanup_allocator_function will deinitialize it | ||
| 215 | MUTEX_INIT( &U->protected_allocator.lock); | ||
| 216 | // just grab whatever allocator was provided to lua_newstate | ||
| 217 | U->protected_allocator.definition.allocF = lua_getallocf( L, &U->protected_allocator.definition.allocUD); | ||
| 218 | } | ||
| 219 | lua_pop( L, 1); // settings | ||
| 212 | STACK_END( L, 0); | 220 | STACK_END( L, 0); |
| 213 | } | 221 | } |
| 214 | 222 | ||
