Skip to content

Commit 4ab9a1c

Browse files
committed
Workaround alignment issue in Linux 32-bit platforms.
Apparently geting alignment right pre-C++17 is not easy. Well, 2^32 fits entirely in Lua "double" 2^53 so it's fine to assume alignment of 1. Fixes #1916.
1 parent 78e7c44 commit 4ab9a1c

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/common/runtime.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
// C++
3131
#include <algorithm>
3232
#include <iostream>
33+
#include <cstdint>
3334
#include <cstdio>
3435
#include <cstddef>
3536
#include <cmath>
@@ -136,16 +137,25 @@ static ObjectKey luax_computeloveobjectkey(lua_State *L, love::Object *object)
136137
// use more than 53 bits if their alignment is guaranteed to be more than 1.
137138
// For example an alignment requirement of 8 means we can shift the
138139
// pointer's bits by 3.
139-
const size_t minalign = LOVE_ALIGNOF(std::max_align_t);
140+
#if UINTPTR_MAX == 0xffffffff
141+
// https://github.com/love2d/love/issues/1916
142+
// This appears to be ABI violation on 32-bit platforms. However it seems
143+
// there's no reliable way to get the correct alignment pre-C++17. Consider
144+
// that 32-bit still fits in 2^53 range, it's perfectly fine to assume
145+
// alignment of 1.
146+
constexpr size_t minalign = 1;
147+
#else
148+
constexpr size_t minalign = LOVE_ALIGNOF(std::max_align_t);
149+
#endif
140150
uintptr_t key = (uintptr_t) object;
141151

142152
if ((key & (minalign - 1)) != 0)
143153
{
144154
luaL_error(L, "Cannot push love object to Lua: unexpected alignment "
145-
"(pointer is %p but alignment should be %d)", object, minalign);
155+
"(pointer is %p but alignment should be %d)", object, (int) minalign);
146156
}
147157

148-
static const size_t shift = (size_t) log2(LOVE_ALIGNOF(std::max_align_t));
158+
static const size_t shift = (size_t) log2(minalign);
149159

150160
key >>= shift;
151161

0 commit comments

Comments
 (0)