Skip to content

Commit 51d1866

Browse files
authored
Merge pull request #2012 from Ivorforce/memory-2.0
Sync `Memory` with godot upstream.
2 parents 5cec5bb + 7a69def commit 51d1866

1 file changed

Lines changed: 26 additions & 24 deletions

File tree

‎include/godot_cpp/core/memory.hpp‎

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,37 @@ namespace godot {
6060

6161
class Wrapped;
6262

63-
class Memory {
64-
Memory();
63+
namespace Memory {
64+
constexpr size_t get_aligned_address(size_t p_address, size_t p_alignment) {
65+
const size_t n_bytes_unaligned = p_address % p_alignment;
66+
return (n_bytes_unaligned == 0) ? p_address : (p_address + p_alignment - n_bytes_unaligned);
67+
}
6568

66-
public:
6769
#if defined(__MINGW32__) && !defined(__MINGW64__)
68-
// Note: Using hardcoded value, since the value can end up different in different compile units on 32-bit windows
69-
// due to a compiler bug (see GH-113145)
70-
static constexpr size_t MAX_ALIGN = 16;
71-
static_assert(MAX_ALIGN % alignof(max_align_t) == 0);
70+
// Note: Using hardcoded value, since the value can end up different in different compile units on 32-bit windows
71+
// due to a compiler bug (see GH-113145)
72+
static constexpr size_t MAX_ALIGN = 16;
73+
static_assert(MAX_ALIGN % alignof(max_align_t) == 0);
7274
#else
73-
static constexpr size_t MAX_ALIGN = alignof(max_align_t);
75+
static constexpr size_t MAX_ALIGN = alignof(max_align_t);
7476
#endif
7577

76-
// Alignment: ↓ max_align_t ↓ uint64_t ↓ MAX_ALIGN
77-
// ┌─────────────────┬──┬────────────────┬──┬───────────...
78-
// │ uint64_t │░░│ uint64_t │░░│ T[]
79-
// │ alloc size │░░│ element count │░░│ data
80-
// └─────────────────┴──┴────────────────┴──┴───────────...
81-
// Offset: ↑ SIZE_OFFSET ↑ ELEMENT_OFFSET ↑ DATA_OFFSET
82-
// Note: "alloc size" is used and set by the engine and is never accessed or changed for the extension.
83-
84-
static constexpr size_t SIZE_OFFSET = 0;
85-
static constexpr size_t ELEMENT_OFFSET = ((SIZE_OFFSET + sizeof(uint64_t)) % alignof(uint64_t) == 0) ? (SIZE_OFFSET + sizeof(uint64_t)) : ((SIZE_OFFSET + sizeof(uint64_t)) + alignof(uint64_t) - ((SIZE_OFFSET + sizeof(uint64_t)) % alignof(uint64_t)));
86-
static constexpr size_t DATA_OFFSET = ((ELEMENT_OFFSET + sizeof(uint64_t)) % MAX_ALIGN == 0) ? (ELEMENT_OFFSET + sizeof(uint64_t)) : ((ELEMENT_OFFSET + sizeof(uint64_t)) + MAX_ALIGN - ((ELEMENT_OFFSET + sizeof(uint64_t)) % MAX_ALIGN));
87-
88-
static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
89-
static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
90-
static void free_static(void *p_ptr, bool p_pad_align = false);
91-
};
78+
// Alignment: ↓ max_align_t ↓ uint64_t ↓ MAX_ALIGN
79+
// ┌─────────────────┬──┬────────────────┬──┬───────────...
80+
// │ uint64_t │░░│ uint64_t │░░│ T[]
81+
// │ alloc size │░░│ element count │░░│ data
82+
// └─────────────────┴──┴────────────────┴──┴───────────...
83+
// Offset: ↑ SIZE_OFFSET ↑ ELEMENT_OFFSET ↑ DATA_OFFSET
84+
// Note: "alloc size" is used and set by the engine and is never accessed or changed for the extension.
85+
86+
inline constexpr size_t SIZE_OFFSET = 0;
87+
inline constexpr size_t ELEMENT_OFFSET = get_aligned_address(SIZE_OFFSET + sizeof(uint64_t), alignof(uint64_t));
88+
inline constexpr size_t DATA_OFFSET = get_aligned_address(ELEMENT_OFFSET + sizeof(uint64_t), MAX_ALIGN);
89+
90+
void *alloc_static(size_t p_bytes, bool p_pad_align = false);
91+
void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
92+
void free_static(void *p_ptr, bool p_pad_align = false);
93+
}; //namespace Memory
9294

9395
template <typename T, std::enable_if_t<!std::is_base_of<::godot::Wrapped, T>::value, bool> = true>
9496
_ALWAYS_INLINE_ void _pre_initialize() {}

0 commit comments

Comments
 (0)