aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2022-02-17 16:30:36 +0100
committerBenoit Germain <bnt.germain@gmail.com>2022-02-17 16:30:36 +0100
commitc2f88350772c01631ea3b7d60e56cea335c458b7 (patch)
treeb4fcc4bebb094190b0dc13561ab24c1593da86f7 /src
parent2c0000d5169cacf950d06637ada1a371cf382896 (diff)
downloadlanes-c2f88350772c01631ea3b7d60e56cea335c458b7.tar.gz
lanes-c2f88350772c01631ea3b7d60e56cea335c458b7.tar.bz2
lanes-c2f88350772c01631ea3b7d60e56cea335c458b7.zip
NEVER use allocator obtained from lua_getallocf to allocate stuff manually when compiling for LuaJIT
Diffstat (limited to 'src')
-rw-r--r--src/lanes.c22
-rw-r--r--src/linda.c27
-rw-r--r--src/macros_and_utils.h10
-rw-r--r--src/state.c35
-rw-r--r--src/uniquekey.h8
5 files changed, 74 insertions, 28 deletions
diff --git a/src/lanes.c b/src/lanes.c
index 8817071..bf0f0a3 100644
--- a/src/lanes.c
+++ b/src/lanes.c
@@ -238,7 +238,6 @@ static bool_t tracking_remove( Lane* s)
238 238
239static void lane_cleanup( Lane* s) 239static void lane_cleanup( Lane* s)
240{ 240{
241 AllocatorDefinition* const allocD = &s->U->protected_allocator.definition;
242 // Clean up after a (finished) thread 241 // Clean up after a (finished) thread
243 // 242 //
244#if THREADWAIT_METHOD == THREADWAIT_CONDVAR 243#if THREADWAIT_METHOD == THREADWAIT_CONDVAR
@@ -254,7 +253,15 @@ static void lane_cleanup( Lane* s)
254 } 253 }
255#endif // HAVE_LANE_TRACKING 254#endif // HAVE_LANE_TRACKING
256 255
257 allocD->allocF(allocD->allocUD, s, sizeof(Lane), 0); 256 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
257#if LUAJIT_FLAVOR == 0
258 {
259 AllocatorDefinition* const allocD = &s->U->protected_allocator.definition;
260 allocD->allocF(allocD->allocUD, s, sizeof(Lane), 0);
261 }
262#else // LUAJIT_FLAVOR
263 free(s);
264#endif // LUAJIT_FLAVOR
258} 265}
259 266
260/* 267/*
@@ -1054,7 +1061,6 @@ LUAG_FUNC( lane_new)
1054#define FIXED_ARGS 7 1061#define FIXED_ARGS 7
1055 int const nargs = lua_gettop(L) - FIXED_ARGS; 1062 int const nargs = lua_gettop(L) - FIXED_ARGS;
1056 Universe* U = universe_get( L); 1063 Universe* U = universe_get( L);
1057 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
1058 ASSERT_L( nargs >= 0); 1064 ASSERT_L( nargs >= 0);
1059 1065
1060 // public Lanes API accepts a generic range -3/+3 1066 // public Lanes API accepts a generic range -3/+3
@@ -1224,7 +1230,15 @@ LUAG_FUNC( lane_new)
1224 // 1230 //
1225 // a Lane full userdata needs a single uservalue 1231 // a Lane full userdata needs a single uservalue
1226 ud = lua_newuserdatauv( L, sizeof( Lane*), 1); // func libs priority globals package required gc_cb lane 1232 ud = lua_newuserdatauv( L, sizeof( Lane*), 1); // func libs priority globals package required gc_cb lane
1227 s = *ud = (Lane*) allocD->allocF( allocD->allocUD, NULL, 0, sizeof(Lane)); 1233 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
1234#if LUAJIT_FLAVOR == 0
1235 {
1236 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
1237 s = *ud = (Lane*)allocD->allocF(allocD->allocUD, NULL, 0, sizeof(Lane));
1238 }
1239#else // LUAJIT_FLAVOR
1240 s = *ud = (Lane*) malloc(sizeof(Lane));
1241#endif // LUAJIT_FLAVOR
1228 if( s == NULL) 1242 if( s == NULL)
1229 { 1243 {
1230 return luaL_error( L, "could not create lane: out of memory"); 1244 return luaL_error( L, "could not create lane: out of memory");
diff --git a/src/linda.c b/src/linda.c
index 21b38fe..4149e71 100644
--- a/src/linda.c
+++ b/src/linda.c
@@ -758,9 +758,6 @@ LUAG_FUNC( linda_towatch)
758*/ 758*/
759static void* linda_id( lua_State* L, DeepOp op_) 759static void* linda_id( lua_State* L, DeepOp op_)
760{ 760{
761 Universe* const U = universe_get(L);
762 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
763
764 switch( op_) 761 switch( op_)
765 { 762 {
766 case eDO_new: 763 case eDO_new:
@@ -797,7 +794,17 @@ static void* linda_id( lua_State* L, DeepOp op_)
797 * One can use any memory allocation scheme. 794 * One can use any memory allocation scheme.
798 * just don't use L's allocF because we don't know which state will get the honor of GCing the linda 795 * just don't use L's allocF because we don't know which state will get the honor of GCing the linda
799 */ 796 */
800 s = (struct s_Linda*) allocD->allocF( allocD->allocUD, NULL, 0, sizeof(struct s_Linda) + name_len); // terminating 0 is already included 797 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
798#if LUAJIT_FLAVOR == 0
799 {
800 Universe* const U = universe_get(L);
801 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
802
803 s = (struct s_Linda*)allocD->allocF(allocD->allocUD, NULL, 0, sizeof(struct s_Linda) + name_len); // terminating 0 is already included
804 }
805#else // LUAJIT_FLAVOR
806 s = (struct s_Linda*)malloc(sizeof(struct s_Linda) + name_len); // terminating 0 is already included
807#endif // LUAJIT_FLAVOR
801 if( s) 808 if( s)
802 { 809 {
803 s->prelude.magic.value = DEEP_VERSION.value; 810 s->prelude.magic.value = DEEP_VERSION.value;
@@ -830,7 +837,17 @@ static void* linda_id( lua_State* L, DeepOp op_)
830 // There aren't any lanes waiting on these lindas, since all proxies have been gc'ed. Right? 837 // There aren't any lanes waiting on these lindas, since all proxies have been gc'ed. Right?
831 SIGNAL_FREE( &linda->read_happened); 838 SIGNAL_FREE( &linda->read_happened);
832 SIGNAL_FREE( &linda->write_happened); 839 SIGNAL_FREE( &linda->write_happened);
833 allocD->allocF( allocD->allocUD, linda, sizeof(struct s_Linda) + strlen(linda->name), 0); 840 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
841#if LUAJIT_FLAVOR == 0
842 {
843 Universe* const U = universe_get(L);
844 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
845
846 allocD->allocF(allocD->allocUD, linda, sizeof(struct s_Linda) + strlen(linda->name), 0);
847 }
848#else // LUAJIT_FLAVOR
849 free(linda);
850#endif // LUAJIT_FLAVOR
834 return NULL; 851 return NULL;
835 } 852 }
836 853
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h
index b67a7c4..dac89d1 100644
--- a/src/macros_and_utils.h
+++ b/src/macros_and_utils.h
@@ -99,4 +99,14 @@ extern char const* debugspew_indent;
99 99
100#define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L) 100#define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L)
101 101
102#if defined(LUA_JITLIBNAME)
103#if (defined(__x86_64__) || defined(_M_X64) || defined(__LP64__))
104#define LUAJIT_FLAVOR 64
105#else // 64 bits
106#define LUAJIT_FLAVOR 32
107#endif // 64 bits
108#else // LUA_JITLIBNAME
109#define LUAJIT_FLAVOR 0
110#endif // LUA_JITLIBNAME
111
102#endif // MACROS_AND_UTILS_H 112#endif // MACROS_AND_UTILS_H
diff --git a/src/state.c b/src/state.c
index c6c04d7..a2de5cb 100644
--- a/src/state.c
+++ b/src/state.c
@@ -129,31 +129,31 @@ static int require_lanes_core( lua_State* L)
129static const luaL_Reg libs[] = 129static const luaL_Reg libs[] =
130{ 130{
131 { LUA_LOADLIBNAME, luaopen_package}, 131 { LUA_LOADLIBNAME, luaopen_package},
132{ LUA_TABLIBNAME, luaopen_table}, 132 { LUA_TABLIBNAME, luaopen_table},
133{ LUA_STRLIBNAME, luaopen_string}, 133 { LUA_STRLIBNAME, luaopen_string},
134{ LUA_MATHLIBNAME, luaopen_math}, 134 { LUA_MATHLIBNAME, luaopen_math},
135#ifndef PLATFORM_XBOX // no os/io libs on xbox 135#ifndef PLATFORM_XBOX // no os/io libs on xbox
136{ LUA_OSLIBNAME, luaopen_os}, 136 { LUA_OSLIBNAME, luaopen_os},
137{ LUA_IOLIBNAME, luaopen_io}, 137 { LUA_IOLIBNAME, luaopen_io},
138#endif // PLATFORM_XBOX 138#endif // PLATFORM_XBOX
139#if LUA_VERSION_NUM >= 503 139#if LUA_VERSION_NUM >= 503
140{ LUA_UTF8LIBNAME, luaopen_utf8}, 140 { LUA_UTF8LIBNAME, luaopen_utf8},
141#endif 141#endif
142#if LUA_VERSION_NUM >= 502 142#if LUA_VERSION_NUM >= 502
143#ifdef luaopen_bit32 143#ifdef luaopen_bit32
144{ LUA_BITLIBNAME, luaopen_bit32}, 144 { LUA_BITLIBNAME, luaopen_bit32},
145#endif 145#endif
146{ LUA_COLIBNAME, luaopen_coroutine}, // Lua 5.2: coroutine is no longer a part of base! 146 { LUA_COLIBNAME, luaopen_coroutine}, // Lua 5.2: coroutine is no longer a part of base!
147#else // LUA_VERSION_NUM 147#else // LUA_VERSION_NUM
148{ LUA_COLIBNAME, NULL}, // Lua 5.1: part of base package 148 { LUA_COLIBNAME, NULL}, // Lua 5.1: part of base package
149#endif // LUA_VERSION_NUM 149#endif // LUA_VERSION_NUM
150{ LUA_DBLIBNAME, luaopen_debug}, 150 { LUA_DBLIBNAME, luaopen_debug},
151#if defined LUA_JITLIBNAME // building against LuaJIT headers, add some LuaJIT-specific libs 151#if LUAJIT_FLAVOR != 0 // building against LuaJIT headers, add some LuaJIT-specific libs
152//#pragma message( "supporting JIT base libs") 152//#pragma message( "supporting JIT base libs")
153{ LUA_BITLIBNAME, luaopen_bit}, 153 { LUA_BITLIBNAME, luaopen_bit},
154{ LUA_JITLIBNAME, luaopen_jit}, 154 { LUA_JITLIBNAME, luaopen_jit},
155{ LUA_FFILIBNAME, luaopen_ffi}, 155 { LUA_FFILIBNAME, luaopen_ffi},
156#endif // LUA_JITLIBNAME 156#endif // LUAJIT_FLAVOR
157 157
158{ LUA_DBLIBNAME, luaopen_debug}, 158{ LUA_DBLIBNAME, luaopen_debug},
159{ "lanes.core", require_lanes_core}, // So that we can open it like any base library (possible since we have access to the init function) 159{ "lanes.core", require_lanes_core}, // So that we can open it like any base library (possible since we have access to the init function)
@@ -249,6 +249,10 @@ void initialize_on_state_create( Universe* U, lua_State* L)
249lua_State* create_state( Universe* U, lua_State* from_) 249lua_State* create_state( Universe* U, lua_State* from_)
250{ 250{
251 lua_State* L; 251 lua_State* L;
252#if LUAJIT_FLAVOR == 64
253 // for some reason, LuaJIT 64 bits does not support creating a state with lua_newstate...
254 L = luaL_newstate();
255#else // LUAJIT_FLAVOR == 64
252 if( U->provide_allocator != NULL) // we have a function we can call to obtain an allocator 256 if( U->provide_allocator != NULL) // we have a function we can call to obtain an allocator
253 { 257 {
254 lua_pushcclosure( from_, U->provide_allocator, 0); 258 lua_pushcclosure( from_, U->provide_allocator, 0);
@@ -264,6 +268,7 @@ lua_State* create_state( Universe* U, lua_State* from_)
264 // reuse the allocator provided when the master state was created 268 // reuse the allocator provided when the master state was created
265 L = lua_newstate( U->protected_allocator.definition.allocF, U->protected_allocator.definition.allocUD); 269 L = lua_newstate( U->protected_allocator.definition.allocF, U->protected_allocator.definition.allocUD);
266 } 270 }
271#endif // LUAJIT_FLAVOR == 64
267 272
268 if( L == NULL) 273 if( L == NULL)
269 { 274 {
diff --git a/src/uniquekey.h b/src/uniquekey.h
index ff3d45d..a72426c 100644
--- a/src/uniquekey.h
+++ b/src/uniquekey.h
@@ -1,7 +1,7 @@
1#if !defined __LANES_UNIQUEKEY_H__ 1#if !defined __LANES_UNIQUEKEY_H__
2#define __LANES_UNIQUEKEY_H__ 1 2#define __LANES_UNIQUEKEY_H__ 1
3 3
4#include "lualib.h" 4#include "macros_and_utils.h"
5 5
6// Lua light userdata can hold a pointer. 6// Lua light userdata can hold a pointer.
7struct s_UniqueKey 7struct s_UniqueKey
@@ -10,11 +10,11 @@ struct s_UniqueKey
10}; 10};
11typedef struct s_UniqueKey UniqueKey; 11typedef struct s_UniqueKey UniqueKey;
12 12
13#if defined(LUA_JITLIBNAME) && (defined(__x86_64__) || defined(_M_X64) || defined(__LP64__)) // building against LuaJIT headers, light userdata is restricted to 47 significant bits. 13#if LUAJIT_FLAVOR == 64 // building against LuaJIT headers for 64 bits, light userdata is restricted to 47 significant bits, because LuaJIT uses the other bits for internal optimizations
14#define MAKE_UNIQUE_KEY( p_) ((void*)((ptrdiff_t)(p_) & 0x7fffffffffffull)) 14#define MAKE_UNIQUE_KEY( p_) ((void*)((ptrdiff_t)(p_) & 0x7fffffffffffull))
15#else // LUA_JITLIBNAME 15#else // LUAJIT_FLAVOR
16#define MAKE_UNIQUE_KEY( p_) ((void*)(ptrdiff_t)(p_)) 16#define MAKE_UNIQUE_KEY( p_) ((void*)(ptrdiff_t)(p_))
17#endif // LUA_JITLIBNAME 17#endif // LUAJIT_FLAVOR
18 18
19#define DECLARE_UNIQUE_KEY( name_) UniqueKey name_ 19#define DECLARE_UNIQUE_KEY( name_) UniqueKey name_
20#define DECLARE_CONST_UNIQUE_KEY( name_, p_) UniqueKey const name_ = { MAKE_UNIQUE_KEY( p_)} 20#define DECLARE_CONST_UNIQUE_KEY( name_, p_) UniqueKey const name_ = { MAKE_UNIQUE_KEY( p_)}