Skip to content

Commit 3ac1590

Browse files
committed
Add memory_get_offset to simplify data offset constants.
1 parent 1cf573f commit 3ac1590

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
static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
5858
static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
@@ -85,8 +85,19 @@ class Memory {
8585
static uint64_t get_mem_available();
8686
static uint64_t get_mem_usage();
8787
static uint64_t get_mem_max_usage();
88+
89+
static constexpr size_t get_aligned_address(size_t p_address, size_t p_alignment);
8890
};
8991

92+
constexpr size_t Memory::get_aligned_address(size_t p_address, size_t p_alignment) {
93+
const size_t n_bytes_unaligned = p_address % p_alignment;
94+
return (n_bytes_unaligned == 0) ? p_address : (p_address + p_alignment - n_bytes_unaligned);
95+
}
96+
97+
inline constexpr size_t Memory::SIZE_OFFSET = 0;
98+
inline constexpr size_t Memory::ELEMENT_OFFSET = get_aligned_address(SIZE_OFFSET + sizeof(uint64_t), alignof(uint64_t));
99+
inline constexpr size_t Memory::DATA_OFFSET = get_aligned_address(ELEMENT_OFFSET + sizeof(uint64_t), alignof(max_align_t));
100+
90101
class DefaultAllocator {
91102
public:
92103
_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
@@ -77,8 +77,8 @@ class CowData {
7777
// Offset: ↑ REF_COUNT_OFFSET ↑ SIZE_OFFSET ↑ DATA_OFFSET
7878

7979
static constexpr size_t REF_COUNT_OFFSET = 0;
80-
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)));
81-
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)));
80+
static constexpr size_t SIZE_OFFSET = Memory::get_aligned_address(REF_COUNT_OFFSET + sizeof(SafeNumeric<USize>), alignof(USize));
81+
static constexpr size_t DATA_OFFSET = Memory::get_aligned_address(SIZE_OFFSET + sizeof(USize), alignof(max_align_t));
8282

8383
mutable T *_ptr = nullptr;
8484

0 commit comments

Comments
 (0)