From 1a7c9fc74b63e9754a11203ea757bcee74f2004e Mon Sep 17 00:00:00 2001 From: Smertig Date: Mon, 10 May 2021 11:39:09 +0300 Subject: [PATCH] Add constexpr alignment stuff --- include/sol/stack_core.hpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/include/sol/stack_core.hpp b/include/sol/stack_core.hpp index 8fbea4b2..c80c564a 100644 --- a/include/sol/stack_core.hpp +++ b/include/sol/stack_core.hpp @@ -78,7 +78,7 @@ namespace sol { #endif } - inline std::uintptr_t align(std::size_t alignment, std::uintptr_t ptr, std::size_t& space) { + constexpr std::uintptr_t align(std::size_t alignment, std::uintptr_t ptr, std::size_t& space) { // this handles arbitrary alignments... // make this into a power-of-2-only? // actually can't: this is a C++14-compatible framework, @@ -94,13 +94,13 @@ namespace sol { return reinterpret_cast(align(alignment, reinterpret_cast(ptr), space)); } - inline std::uintptr_t align_one(std::size_t alignment, std::size_t size, std::uintptr_t target_alignment) { + constexpr std::uintptr_t align_one(std::size_t alignment, std::size_t size, std::uintptr_t target_alignment) { std::size_t space = (std::numeric_limits::max)(); return align(alignment, target_alignment, space) + size; } template - std::size_t aligned_space_for(std::uintptr_t alignment) { + constexpr std::size_t aligned_space_for(std::uintptr_t alignment) { std::uintptr_t start = alignment; (void)detail::swallow { int {}, (alignment = align_one(std::alignment_of_v, sizeof(Args), alignment), int {})... }; return static_cast(alignment - start); @@ -219,8 +219,8 @@ namespace sol { T** pointerpointer = static_cast(alloc_newuserdata(L, sizeof(T*))); return pointerpointer; } - static const std::size_t initial_size = aligned_space_for(0x0); - static const std::size_t misaligned_size = aligned_space_for(0x1); + constexpr std::size_t initial_size = aligned_space_for(0x0); + constexpr std::size_t misaligned_size = aligned_space_for(0x1); std::size_t allocated_size = initial_size; void* unadjusted = alloc_newuserdata(L, initial_size); @@ -331,8 +331,8 @@ namespace sol { compilers are optimized for aligned reads/writes (and clang will barf UBsan errors on us for not being aligned) */ - static const std::size_t initial_size = aligned_space_for(0x0); - static const std::size_t misaligned_size = aligned_space_for(0x1); + constexpr std::size_t initial_size = aligned_space_for(0x0); + constexpr std::size_t misaligned_size = aligned_space_for(0x1); void* pointer_adjusted; void* data_adjusted; @@ -382,8 +382,8 @@ namespace sol { return mem; } - static const std::size_t initial_size = aligned_space_for(0x0); - static const std::size_t misaligned_size = aligned_space_for(0x1); + constexpr std::size_t initial_size = aligned_space_for(0x0); + constexpr std::size_t misaligned_size = aligned_space_for(0x1); void* pointer_adjusted; void* dx_adjusted; @@ -450,8 +450,8 @@ namespace sol { return pointer; } - static const std::size_t initial_size = aligned_space_for(0x0); - static const std::size_t misaligned_size = aligned_space_for(0x1); + constexpr std::size_t initial_size = aligned_space_for(0x0); + constexpr std::size_t misaligned_size = aligned_space_for(0x1); std::size_t allocated_size = initial_size; void* unadjusted = alloc_newuserdata(L, allocated_size);