Skip to content

Commit

Permalink
Merge pull request #94137 from Craig-Stoneham/master
Browse files Browse the repository at this point in the history
Improve template class conditionals with constexpr (code style)
  • Loading branch information
akien-mga committed Aug 26, 2024
2 parents ce44c91 + 604df4f commit 8019cdb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
24 changes: 12 additions & 12 deletions core/templates/paged_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PagedAllocator {
public:
template <typename... Args>
T *alloc(Args &&...p_args) {
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.lock();
}
if (unlikely(allocs_available == 0)) {
Expand All @@ -76,21 +76,21 @@ class PagedAllocator {

allocs_available--;
T *alloc = available_pool[allocs_available >> page_shift][allocs_available & page_mask];
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.unlock();
}
memnew_placement(alloc, T(p_args...));
return alloc;
}

void free(T *p_mem) {
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.lock();
}
p_mem->~T();
available_pool[allocs_available >> page_shift][allocs_available & page_mask] = p_mem;
allocs_available++;
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.unlock();
}
}
Expand Down Expand Up @@ -120,36 +120,36 @@ class PagedAllocator {

public:
void reset(bool p_allow_unfreed = false) {
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.lock();
}
_reset(p_allow_unfreed);
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.unlock();
}
}

bool is_configured() const {
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.lock();
}
bool result = page_size > 0;
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.unlock();
}
return result;
}

void configure(uint32_t p_page_size) {
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.lock();
}
ERR_FAIL_COND(page_pool != nullptr); // Safety check.
ERR_FAIL_COND(p_page_size == 0);
page_size = nearest_power_of_2_templated(p_page_size);
page_mask = page_size - 1;
page_shift = get_shift_from_power_of_2(page_size);
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.unlock();
}
}
Expand All @@ -161,7 +161,7 @@ class PagedAllocator {
}

~PagedAllocator() {
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.lock();
}
bool leaked = allocs_available < pages_allocated * page_size;
Expand All @@ -172,7 +172,7 @@ class PagedAllocator {
} else {
_reset(false);
}
if (thread_safe) {
if constexpr (thread_safe) {
spin_lock.unlock();
}
}
Expand Down
40 changes: 20 additions & 20 deletions core/templates/rid_owner.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class RID_Alloc : public RID_AllocBase {
mutable SpinLock spin_lock;

_FORCE_INLINE_ RID _allocate_rid() {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.lock();
}

Expand Down Expand Up @@ -128,7 +128,7 @@ class RID_Alloc : public RID_AllocBase {

alloc_count++;

if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}

Expand Down Expand Up @@ -156,14 +156,14 @@ class RID_Alloc : public RID_AllocBase {
if (p_rid == RID()) {
return nullptr;
}
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.lock();
}

uint64_t id = p_rid.get_id();
uint32_t idx = uint32_t(id & 0xFFFFFFFF);
if (unlikely(idx >= max_alloc)) {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
return nullptr;
Expand All @@ -176,14 +176,14 @@ class RID_Alloc : public RID_AllocBase {

if (unlikely(p_initialize)) {
if (unlikely(!(validator_chunks[idx_chunk][idx_element] & 0x80000000))) {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
ERR_FAIL_V_MSG(nullptr, "Initializing already initialized RID");
}

if (unlikely((validator_chunks[idx_chunk][idx_element] & 0x7FFFFFFF) != validator)) {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
ERR_FAIL_V_MSG(nullptr, "Attempting to initialize the wrong RID");
Expand All @@ -192,7 +192,7 @@ class RID_Alloc : public RID_AllocBase {
validator_chunks[idx_chunk][idx_element] &= 0x7FFFFFFF; //initialized

} else if (unlikely(validator_chunks[idx_chunk][idx_element] != validator)) {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
if ((validator_chunks[idx_chunk][idx_element] & 0x80000000) && validator_chunks[idx_chunk][idx_element] != 0xFFFFFFFF) {
Expand All @@ -203,7 +203,7 @@ class RID_Alloc : public RID_AllocBase {

T *ptr = &chunks[idx_chunk][idx_element];

if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}

Expand All @@ -221,14 +221,14 @@ class RID_Alloc : public RID_AllocBase {
}

_FORCE_INLINE_ bool owns(const RID &p_rid) const {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.lock();
}

uint64_t id = p_rid.get_id();
uint32_t idx = uint32_t(id & 0xFFFFFFFF);
if (unlikely(idx >= max_alloc)) {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
return false;
Expand All @@ -241,22 +241,22 @@ class RID_Alloc : public RID_AllocBase {

bool owned = (validator != 0x7FFFFFFF) && (validator_chunks[idx_chunk][idx_element] & 0x7FFFFFFF) == validator;

if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}

return owned;
}

_FORCE_INLINE_ void free(const RID &p_rid) {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.lock();
}

uint64_t id = p_rid.get_id();
uint32_t idx = uint32_t(id & 0xFFFFFFFF);
if (unlikely(idx >= max_alloc)) {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
ERR_FAIL();
Expand All @@ -267,12 +267,12 @@ class RID_Alloc : public RID_AllocBase {

uint32_t validator = uint32_t(id >> 32);
if (unlikely(validator_chunks[idx_chunk][idx_element] & 0x80000000)) {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
ERR_FAIL_MSG("Attempted to free an uninitialized or invalid RID.");
} else if (unlikely(validator_chunks[idx_chunk][idx_element] != validator)) {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
ERR_FAIL();
Expand All @@ -284,7 +284,7 @@ class RID_Alloc : public RID_AllocBase {
alloc_count--;
free_list_chunks[alloc_count / elements_in_chunk][alloc_count % elements_in_chunk] = idx;

if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
}
Expand All @@ -293,7 +293,7 @@ class RID_Alloc : public RID_AllocBase {
return alloc_count;
}
void get_owned_list(List<RID> *p_owned) const {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.lock();
}
for (size_t i = 0; i < max_alloc; i++) {
Expand All @@ -302,14 +302,14 @@ class RID_Alloc : public RID_AllocBase {
p_owned->push_back(_make_from_id((validator << 32) | i));
}
}
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
}

//used for fast iteration in the elements or RIDs
void fill_owned_buffer(RID *p_rid_buffer) const {
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.lock();
}
uint32_t idx = 0;
Expand All @@ -320,7 +320,7 @@ class RID_Alloc : public RID_AllocBase {
idx++;
}
}
if (THREAD_SAFE) {
if constexpr (THREAD_SAFE) {
spin_lock.unlock();
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/templates/sort_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ class SortArray {

while (true) {
while (compare(p_array[p_first], p_pivot)) {
if (Validate) {
if constexpr (Validate) {
ERR_BAD_COMPARE(p_first == unmodified_last - 1);
}
p_first++;
}
p_last--;
while (compare(p_pivot, p_array[p_last])) {
if (Validate) {
if constexpr (Validate) {
ERR_BAD_COMPARE(p_last == unmodified_first);
}
p_last--;
Expand Down Expand Up @@ -251,7 +251,7 @@ class SortArray {
inline void unguarded_linear_insert(int64_t p_last, T p_value, T *p_array) const {
int64_t next = p_last - 1;
while (compare(p_value, p_array[next])) {
if (Validate) {
if constexpr (Validate) {
ERR_BAD_COMPARE(next == 0);
}
p_array[p_last] = p_array[next];
Expand Down

0 comments on commit 8019cdb

Please sign in to comment.