-
Notifications
You must be signed in to change notification settings - Fork 5.3k
lua: align the allocated memory #6599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,8 @@ namespace Lua { | |
| */ | ||
| #define DECLARE_LUA_FUNCTION_EX(Class, Name, Index) \ | ||
| static int static_##Name(lua_State* state) { \ | ||
| Class* object = static_cast<Class*>(luaL_checkudata(state, Index, typeid(Class).name())); \ | ||
| Class* object = ::Envoy::Extensions::Filters::Common::Lua::alignAndCast<Class>( \ | ||
| luaL_checkudata(state, Index, typeid(Class).name())); \ | ||
| object->checkDead(state); \ | ||
| return object->Name(state); \ | ||
| } \ | ||
|
|
@@ -60,6 +61,32 @@ namespace Lua { | |
| */ | ||
| #define DECLARE_LUA_CLOSURE(Class, Name) DECLARE_LUA_FUNCTION_EX(Class, Name, lua_upvalueindex(1)) | ||
|
|
||
| /** | ||
| * Calculate the maximum space needed to be aligned. | ||
| */ | ||
| template <typename T> constexpr size_t maximumSpaceNeededToAlign() { | ||
| // The allocated memory can be misaligned up to `alignof(T) - 1` bytes. Adding it to the size to | ||
| // allocate. | ||
| return sizeof(T) + alignof(T) - 1; | ||
| } | ||
|
|
||
| template <typename T> inline T* alignAndCast(void* mem) { | ||
| size_t size = maximumSpaceNeededToAlign<T>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: const or just inline the call below.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4th parameter of |
||
| return static_cast<T*>(std::align(alignof(T), sizeof(T), mem, size)); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new user data and assign its metatable. | ||
| */ | ||
| template <typename T> inline T* allocateLuaUserData(lua_State* state) { | ||
| void* mem = lua_newuserdata(state, maximumSpaceNeededToAlign<T>()); | ||
| luaL_getmetatable(state, typeid(T).name()); | ||
| ASSERT(lua_istable(state, -1)); | ||
| lua_setmetatable(state, -2); | ||
|
|
||
| return alignAndCast<T>(mem); | ||
| } | ||
|
|
||
| /** | ||
| * This is the base class for all C++ objects that we expose out to Lua. The goal is to hide as | ||
| * much ugliness as possible. In general, to use this, do the following: | ||
|
|
@@ -90,14 +117,9 @@ template <class T> class BaseLuaObject : protected Logger::Loggable<Logger::Id:: | |
| */ | ||
| template <typename... ConstructorArgs> | ||
| static std::pair<T*, lua_State*> create(lua_State* state, ConstructorArgs&&... args) { | ||
| // Create a new user data and assign its metatable. | ||
| void* mem = lua_newuserdata(state, sizeof(T)); | ||
| luaL_getmetatable(state, typeid(T).name()); | ||
| ASSERT(lua_istable(state, -1)); | ||
| lua_setmetatable(state, -2); | ||
|
|
||
| // Memory is allocated via Lua and it is raw. We use placement new to run the constructor. | ||
| ENVOY_LOG(trace, "creating {} at {}", typeid(T).name(), mem); | ||
| T* mem = allocateLuaUserData<T>(state); | ||
| ENVOY_LOG(trace, "creating {} at {}", typeid(T).name(), static_cast<void*>(mem)); | ||
| return {new (mem) T(std::forward<ConstructorArgs>(args)...), state}; | ||
| } | ||
|
|
||
|
|
@@ -119,7 +141,7 @@ template <class T> class BaseLuaObject : protected Logger::Loggable<Logger::Id:: | |
| // manually because the memory is raw and was allocated by Lua. | ||
| to_register.push_back( | ||
| {"__gc", [](lua_State* state) { | ||
| T* object = static_cast<T*>(luaL_checkudata(state, 1, typeid(T).name())); | ||
| T* object = alignAndCast<T>(luaL_checkudata(state, 1, typeid(T).name())); | ||
| ENVOY_LOG(trace, "destroying {} at {}", typeid(T).name(), static_cast<void*>(object)); | ||
| object->~T(); | ||
| return 0; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,6 +285,7 @@ accessors | |
| acls | ||
| addr | ||
| agg | ||
| alignof | ||
| alloc | ||
| alloca | ||
| allocator | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.