Skip to content

Commit 5fda924

Browse files
committed
Merge pull request godotengine#100145 from Ivorforce/memory-offset-func
Add `mem_aligned_address` to simplify data offset constants.
2 parents 5088a93 + 3ac1590 commit 5fda924

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

core/os/memory.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class Memory {
5050
// └─────────────────┴──┴────────────────┴──┴───────────...
5151
// Offset: ↑ SIZE_OFFSET ↑ ELEMENT_OFFSET ↑ DATA_OFFSET
5252

53-
static constexpr size_t SIZE_OFFSET = 0;
54-
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)));
55-
static constexpr size_t DATA_OFFSET = ((ELEMENT_OFFSET + sizeof(uint64_t)) % alignof(max_align_t) == 0) ? (ELEMENT_OFFSET + sizeof(uint64_t)) : ((ELEMENT_OFFSET + sizeof(uint64_t)) + alignof(max_align_t) - ((ELEMENT_OFFSET + sizeof(uint64_t)) % alignof(max_align_t)));
53+
static const size_t SIZE_OFFSET;
54+
static const size_t ELEMENT_OFFSET;
55+
static const size_t DATA_OFFSET;
5656

5757
template <bool p_ensure_zero = false>
5858
static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
@@ -87,8 +87,19 @@ class Memory {
8787
static uint64_t get_mem_available();
8888
static uint64_t get_mem_usage();
8989
static uint64_t get_mem_max_usage();
90+
91+
static constexpr size_t get_aligned_address(size_t p_address, size_t p_alignment);
9092
};
9193

94+
constexpr size_t Memory::get_aligned_address(size_t p_address, size_t p_alignment) {
95+
const size_t n_bytes_unaligned = p_address % p_alignment;
96+
return (n_bytes_unaligned == 0) ? p_address : (p_address + p_alignment - n_bytes_unaligned);
97+
}
98+
99+
inline constexpr size_t Memory::SIZE_OFFSET = 0;
100+
inline constexpr size_t Memory::ELEMENT_OFFSET = get_aligned_address(SIZE_OFFSET + sizeof(uint64_t), alignof(uint64_t));
101+
inline constexpr size_t Memory::DATA_OFFSET = get_aligned_address(ELEMENT_OFFSET + sizeof(uint64_t), alignof(max_align_t));
102+
92103
class DefaultAllocator {
93104
public:
94105
_FORCE_INLINE_ static void *alloc(size_t p_memory) { return Memory::alloc_static(p_memory, false); }

core/templates/cowdata.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class CowData {
6060
// Offset: ↑ REF_COUNT_OFFSET ↑ SIZE_OFFSET ↑ DATA_OFFSET
6161

6262
static constexpr size_t REF_COUNT_OFFSET = 0;
63-
static constexpr size_t SIZE_OFFSET = ((REF_COUNT_OFFSET + sizeof(SafeNumeric<USize>)) % alignof(USize) == 0) ? (REF_COUNT_OFFSET + sizeof(SafeNumeric<USize>)) : ((REF_COUNT_OFFSET + sizeof(SafeNumeric<USize>)) + alignof(USize) - ((REF_COUNT_OFFSET + sizeof(SafeNumeric<USize>)) % alignof(USize)));
64-
static constexpr size_t DATA_OFFSET = ((SIZE_OFFSET + sizeof(USize)) % alignof(max_align_t) == 0) ? (SIZE_OFFSET + sizeof(USize)) : ((SIZE_OFFSET + sizeof(USize)) + alignof(max_align_t) - ((SIZE_OFFSET + sizeof(USize)) % alignof(max_align_t)));
63+
static constexpr size_t SIZE_OFFSET = Memory::get_aligned_address(REF_COUNT_OFFSET + sizeof(SafeNumeric<USize>), alignof(USize));
64+
static constexpr size_t DATA_OFFSET = Memory::get_aligned_address(SIZE_OFFSET + sizeof(USize), alignof(max_align_t));
6565

6666
mutable T *_ptr = nullptr;
6767

0 commit comments

Comments
 (0)