2019-11-05 11:01:00 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* rid_owner.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
2020-02-11 13:01:43 +00:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2019-11-05 11:01:00 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
#ifndef RID_OWNER_H
|
|
|
|
#define RID_OWNER_H
|
|
|
|
|
2020-04-14 03:05:21 +00:00
|
|
|
#include "core/os/memory.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/os/spin_lock.h"
|
|
|
|
#include "core/string/print_string.h"
|
2022-05-19 15:00:06 +00:00
|
|
|
#include "core/templates/hash_set.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/templates/list.h"
|
|
|
|
#include "core/templates/oa_hash_map.h"
|
|
|
|
#include "core/templates/rid.h"
|
|
|
|
#include "core/templates/safe_refcount.h"
|
2020-05-12 15:01:17 +00:00
|
|
|
|
2019-07-29 21:19:31 +00:00
|
|
|
#include <stdio.h>
|
2019-06-24 19:13:06 +00:00
|
|
|
#include <typeinfo>
|
2019-06-10 15:38:51 +00:00
|
|
|
|
|
|
|
class RID_AllocBase {
|
2021-02-10 18:22:13 +00:00
|
|
|
static SafeNumeric<uint64_t> base_id;
|
2019-06-10 15:38:51 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static RID _make_from_id(uint64_t p_id) {
|
|
|
|
RID rid;
|
|
|
|
rid._id = p_id;
|
|
|
|
return rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RID _gen_rid() {
|
|
|
|
return _make_from_id(_gen_id());
|
|
|
|
}
|
|
|
|
|
2021-08-23 23:57:52 +00:00
|
|
|
friend struct VariantUtilityFunctions;
|
|
|
|
|
|
|
|
static uint64_t _gen_id() {
|
|
|
|
return base_id.increment();
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
public:
|
|
|
|
virtual ~RID_AllocBase() {}
|
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
template <typename T, bool THREAD_SAFE = false>
|
2019-06-10 15:38:51 +00:00
|
|
|
class RID_Alloc : public RID_AllocBase {
|
2020-05-12 15:01:17 +00:00
|
|
|
T **chunks = nullptr;
|
|
|
|
uint32_t **free_list_chunks = nullptr;
|
|
|
|
uint32_t **validator_chunks = nullptr;
|
2019-06-10 15:38:51 +00:00
|
|
|
|
|
|
|
uint32_t elements_in_chunk;
|
2020-05-12 15:01:17 +00:00
|
|
|
uint32_t max_alloc = 0;
|
|
|
|
uint32_t alloc_count = 0;
|
2019-06-10 15:38:51 +00:00
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
const char *description = nullptr;
|
2019-06-10 15:38:51 +00:00
|
|
|
|
2022-08-05 09:59:58 +00:00
|
|
|
mutable SpinLock spin_lock;
|
2019-07-29 15:59:18 +00:00
|
|
|
|
2021-06-29 13:58:28 +00:00
|
|
|
_FORCE_INLINE_ RID _allocate_rid() {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.lock();
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
if (alloc_count == max_alloc) {
|
|
|
|
//allocate a new chunk
|
2019-06-19 20:03:19 +00:00
|
|
|
uint32_t chunk_count = alloc_count == 0 ? 0 : (max_alloc / elements_in_chunk);
|
2019-06-10 15:38:51 +00:00
|
|
|
|
|
|
|
//grow chunks
|
|
|
|
chunks = (T **)memrealloc(chunks, sizeof(T *) * (chunk_count + 1));
|
|
|
|
chunks[chunk_count] = (T *)memalloc(sizeof(T) * elements_in_chunk); //but don't initialize
|
|
|
|
|
|
|
|
//grow validators
|
|
|
|
validator_chunks = (uint32_t **)memrealloc(validator_chunks, sizeof(uint32_t *) * (chunk_count + 1));
|
|
|
|
validator_chunks[chunk_count] = (uint32_t *)memalloc(sizeof(uint32_t) * elements_in_chunk);
|
|
|
|
//grow free lists
|
|
|
|
free_list_chunks = (uint32_t **)memrealloc(free_list_chunks, sizeof(uint32_t *) * (chunk_count + 1));
|
|
|
|
free_list_chunks[chunk_count] = (uint32_t *)memalloc(sizeof(uint32_t) * elements_in_chunk);
|
|
|
|
|
|
|
|
//initialize
|
|
|
|
for (uint32_t i = 0; i < elements_in_chunk; i++) {
|
Fix various typos with codespell
Found via `codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,fave,findn,hist,inout,leapyear,lod,nd,numer,ois,ony,paket,seeked,sinc,switchs,te,uint`
2021-07-07 15:17:32 +00:00
|
|
|
// Don't initialize chunk.
|
2019-06-10 15:38:51 +00:00
|
|
|
validator_chunks[chunk_count][i] = 0xFFFFFFFF;
|
|
|
|
free_list_chunks[chunk_count][i] = alloc_count + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_alloc += elements_in_chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t free_index = free_list_chunks[alloc_count / elements_in_chunk][alloc_count % elements_in_chunk];
|
|
|
|
|
|
|
|
uint32_t free_chunk = free_index / elements_in_chunk;
|
|
|
|
uint32_t free_element = free_index % elements_in_chunk;
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
uint32_t validator = (uint32_t)(_gen_id() & 0x7FFFFFFF);
|
2023-03-10 16:25:28 +00:00
|
|
|
CRASH_COND_MSG(validator == 0x7FFFFFFF, "Overflow in RID validator");
|
2019-06-10 15:38:51 +00:00
|
|
|
uint64_t id = validator;
|
|
|
|
id <<= 32;
|
|
|
|
id |= free_index;
|
|
|
|
|
|
|
|
validator_chunks[free_chunk][free_element] = validator;
|
2021-02-09 16:19:03 +00:00
|
|
|
|
2021-06-29 13:58:28 +00:00
|
|
|
validator_chunks[free_chunk][free_element] |= 0x80000000; //mark uninitialized bit
|
2021-02-09 16:19:03 +00:00
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
alloc_count++;
|
|
|
|
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
return _make_from_id(id);
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
public:
|
2021-06-29 13:58:28 +00:00
|
|
|
RID make_rid() {
|
|
|
|
RID rid = _allocate_rid();
|
|
|
|
initialize_rid(rid);
|
|
|
|
return rid;
|
|
|
|
}
|
2021-02-09 16:19:03 +00:00
|
|
|
RID make_rid(const T &p_value) {
|
2021-06-29 13:58:28 +00:00
|
|
|
RID rid = _allocate_rid();
|
|
|
|
initialize_rid(rid, p_value);
|
|
|
|
return rid;
|
2021-02-09 16:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//allocate but don't initialize, use initialize_rid afterwards
|
|
|
|
RID allocate_rid() {
|
2021-06-29 13:58:28 +00:00
|
|
|
return _allocate_rid();
|
2021-02-09 16:19:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 17:08:41 +00:00
|
|
|
_FORCE_INLINE_ T *get_or_null(const RID &p_rid, bool p_initialize = false) {
|
2021-02-09 16:19:03 +00:00
|
|
|
if (p_rid == RID()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.lock();
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
uint64_t id = p_rid.get_id();
|
|
|
|
uint32_t idx = uint32_t(id & 0xFFFFFFFF);
|
2019-06-24 19:13:06 +00:00
|
|
|
if (unlikely(idx >= max_alloc)) {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-08-05 12:18:13 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2019-06-10 15:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t idx_chunk = idx / elements_in_chunk;
|
|
|
|
uint32_t idx_element = idx % elements_in_chunk;
|
|
|
|
|
|
|
|
uint32_t validator = uint32_t(id >> 32);
|
2021-02-09 16:19:03 +00:00
|
|
|
|
|
|
|
if (unlikely(p_initialize)) {
|
|
|
|
if (unlikely(!(validator_chunks[idx_chunk][idx_element] & 0x80000000))) {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2021-02-09 16:19:03 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
ERR_FAIL_V_MSG(nullptr, "Initializing already initialized RID");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely((validator_chunks[idx_chunk][idx_element] & 0x7FFFFFFF) != validator)) {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2021-02-09 16:19:03 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
ERR_FAIL_V_MSG(nullptr, "Attempting to initialize the wrong RID");
|
|
|
|
}
|
|
|
|
|
|
|
|
validator_chunks[idx_chunk][idx_element] &= 0x7FFFFFFF; //initialized
|
|
|
|
|
|
|
|
} else if (unlikely(validator_chunks[idx_chunk][idx_element] != validator)) {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-08-05 12:18:13 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
2021-07-06 21:56:28 +00:00
|
|
|
if ((validator_chunks[idx_chunk][idx_element] & 0x80000000) && validator_chunks[idx_chunk][idx_element] != 0xFFFFFFFF) {
|
2021-02-09 16:19:03 +00:00
|
|
|
ERR_FAIL_V_MSG(nullptr, "Attempting to use an uninitialized RID");
|
|
|
|
}
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2019-06-10 15:38:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
T *ptr = &chunks[idx_chunk][idx_element];
|
|
|
|
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
2019-06-10 15:38:51 +00:00
|
|
|
}
|
2021-06-29 13:58:28 +00:00
|
|
|
void initialize_rid(RID p_rid) {
|
2021-09-29 17:08:41 +00:00
|
|
|
T *mem = get_or_null(p_rid, true);
|
2023-09-09 14:11:33 +00:00
|
|
|
ERR_FAIL_NULL(mem);
|
2021-06-29 13:58:28 +00:00
|
|
|
memnew_placement(mem, T);
|
|
|
|
}
|
2021-02-09 16:19:03 +00:00
|
|
|
void initialize_rid(RID p_rid, const T &p_value) {
|
2021-09-29 17:08:41 +00:00
|
|
|
T *mem = get_or_null(p_rid, true);
|
2023-09-09 14:11:33 +00:00
|
|
|
ERR_FAIL_NULL(mem);
|
2021-02-09 16:19:03 +00:00
|
|
|
memnew_placement(mem, T(p_value));
|
|
|
|
}
|
2019-06-10 15:38:51 +00:00
|
|
|
|
2022-08-05 09:59:58 +00:00
|
|
|
_FORCE_INLINE_ bool owns(const RID &p_rid) const {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.lock();
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
uint64_t id = p_rid.get_id();
|
|
|
|
uint32_t idx = uint32_t(id & 0xFFFFFFFF);
|
2019-06-24 19:13:06 +00:00
|
|
|
if (unlikely(idx >= max_alloc)) {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
2019-06-10 15:38:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t idx_chunk = idx / elements_in_chunk;
|
|
|
|
uint32_t idx_element = idx % elements_in_chunk;
|
|
|
|
|
|
|
|
uint32_t validator = uint32_t(id >> 32);
|
2019-07-29 15:59:18 +00:00
|
|
|
|
2023-03-10 16:25:28 +00:00
|
|
|
bool owned = (validator != 0x7FFFFFFF) && (validator_chunks[idx_chunk][idx_element] & 0x7FFFFFFF) == validator;
|
2019-07-29 15:59:18 +00:00
|
|
|
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return owned;
|
2019-06-10 15:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void free(const RID &p_rid) {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.lock();
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
uint64_t id = p_rid.get_id();
|
|
|
|
uint32_t idx = uint32_t(id & 0xFFFFFFFF);
|
2019-08-05 12:18:13 +00:00
|
|
|
if (unlikely(idx >= max_alloc)) {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-08-05 12:18:13 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
2019-06-10 15:38:51 +00:00
|
|
|
|
|
|
|
uint32_t idx_chunk = idx / elements_in_chunk;
|
|
|
|
uint32_t idx_element = idx % elements_in_chunk;
|
|
|
|
|
|
|
|
uint32_t validator = uint32_t(id >> 32);
|
2021-02-09 16:19:03 +00:00
|
|
|
if (unlikely(validator_chunks[idx_chunk][idx_element] & 0x80000000)) {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2021-02-09 16:19:03 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
2024-02-23 19:49:13 +00:00
|
|
|
ERR_FAIL_MSG("Attempted to free an uninitialized or invalid RID.");
|
2021-02-09 16:19:03 +00:00
|
|
|
} else if (unlikely(validator_chunks[idx_chunk][idx_element] != validator)) {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-08-05 12:18:13 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
2019-06-10 15:38:51 +00:00
|
|
|
|
|
|
|
chunks[idx_chunk][idx_element].~T();
|
|
|
|
validator_chunks[idx_chunk][idx_element] = 0xFFFFFFFF; // go invalid
|
|
|
|
|
|
|
|
alloc_count--;
|
2019-06-19 20:03:19 +00:00
|
|
|
free_list_chunks[alloc_count / elements_in_chunk][alloc_count % elements_in_chunk] = idx;
|
2019-07-29 15:59:18 +00:00
|
|
|
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
2019-06-10 15:38:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 01:54:32 +00:00
|
|
|
_FORCE_INLINE_ uint32_t get_rid_count() const {
|
|
|
|
return alloc_count;
|
|
|
|
}
|
2022-08-05 09:59:58 +00:00
|
|
|
void get_owned_list(List<RID> *p_owned) const {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 15:59:18 +00:00
|
|
|
spin_lock.lock();
|
|
|
|
}
|
2022-02-05 10:59:34 +00:00
|
|
|
for (size_t i = 0; i < max_alloc; i++) {
|
|
|
|
uint64_t validator = validator_chunks[i / elements_in_chunk][i % elements_in_chunk];
|
|
|
|
if (validator != 0xFFFFFFFF) {
|
|
|
|
p_owned->push_back(_make_from_id((validator << 32) | i));
|
|
|
|
}
|
2019-07-29 21:19:31 +00:00
|
|
|
}
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-07-29 21:19:31 +00:00
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 10:59:34 +00:00
|
|
|
//used for fast iteration in the elements or RIDs
|
2022-08-05 09:59:58 +00:00
|
|
|
void fill_owned_buffer(RID *p_rid_buffer) const {
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-10-20 19:31:21 +00:00
|
|
|
spin_lock.lock();
|
|
|
|
}
|
2022-02-05 10:59:34 +00:00
|
|
|
uint32_t idx = 0;
|
2019-10-20 19:31:21 +00:00
|
|
|
for (size_t i = 0; i < max_alloc; i++) {
|
|
|
|
uint64_t validator = validator_chunks[i / elements_in_chunk][i % elements_in_chunk];
|
|
|
|
if (validator != 0xFFFFFFFF) {
|
2022-02-05 10:59:34 +00:00
|
|
|
p_rid_buffer[idx] = _make_from_id((validator << 32) | i);
|
|
|
|
idx++;
|
2019-10-20 19:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-09 16:02:21 +00:00
|
|
|
if constexpr (THREAD_SAFE) {
|
2019-10-20 19:31:21 +00:00
|
|
|
spin_lock.unlock();
|
2019-06-10 15:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_description(const char *p_descrption) {
|
|
|
|
description = p_descrption;
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:58:28 +00:00
|
|
|
RID_Alloc(uint32_t p_target_chunk_byte_size = 65536) {
|
2019-06-10 15:38:51 +00:00
|
|
|
elements_in_chunk = sizeof(T) > p_target_chunk_byte_size ? 1 : (p_target_chunk_byte_size / sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
~RID_Alloc() {
|
|
|
|
if (alloc_count) {
|
2022-10-03 09:18:27 +00:00
|
|
|
print_error(vformat("ERROR: %d RID allocations of type '%s' were leaked at exit.",
|
|
|
|
alloc_count, description ? description : typeid(T).name()));
|
2019-06-10 15:38:51 +00:00
|
|
|
|
2019-10-20 19:31:21 +00:00
|
|
|
for (size_t i = 0; i < max_alloc; i++) {
|
|
|
|
uint64_t validator = validator_chunks[i / elements_in_chunk][i % elements_in_chunk];
|
2021-06-14 04:29:41 +00:00
|
|
|
if (validator & 0x80000000) {
|
|
|
|
continue; //uninitialized
|
|
|
|
}
|
2019-10-20 19:31:21 +00:00
|
|
|
if (validator != 0xFFFFFFFF) {
|
|
|
|
chunks[i / elements_in_chunk][i % elements_in_chunk].~T();
|
|
|
|
}
|
2019-06-10 15:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 19:13:06 +00:00
|
|
|
uint32_t chunk_count = max_alloc / elements_in_chunk;
|
2019-06-10 15:38:51 +00:00
|
|
|
for (uint32_t i = 0; i < chunk_count; i++) {
|
|
|
|
memfree(chunks[i]);
|
|
|
|
memfree(validator_chunks[i]);
|
|
|
|
memfree(free_list_chunks[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunks) {
|
|
|
|
memfree(chunks);
|
|
|
|
memfree(free_list_chunks);
|
|
|
|
memfree(validator_chunks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
template <typename T, bool THREAD_SAFE = false>
|
2019-06-10 15:38:51 +00:00
|
|
|
class RID_PtrOwner {
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Alloc<T *, THREAD_SAFE> alloc;
|
2019-06-10 15:38:51 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
_FORCE_INLINE_ RID make_rid(T *p_ptr) {
|
|
|
|
return alloc.make_rid(p_ptr);
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
_FORCE_INLINE_ RID allocate_rid() {
|
|
|
|
return alloc.allocate_rid();
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void initialize_rid(RID p_rid, T *p_ptr) {
|
|
|
|
alloc.initialize_rid(p_rid, p_ptr);
|
|
|
|
}
|
|
|
|
|
2021-09-29 17:08:41 +00:00
|
|
|
_FORCE_INLINE_ T *get_or_null(const RID &p_rid) {
|
|
|
|
T **ptr = alloc.get_or_null(p_rid);
|
2019-06-10 15:38:51 +00:00
|
|
|
if (unlikely(!ptr)) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2019-06-10 15:38:51 +00:00
|
|
|
}
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
_FORCE_INLINE_ void replace(const RID &p_rid, T *p_new_ptr) {
|
2021-09-29 17:08:41 +00:00
|
|
|
T **ptr = alloc.get_or_null(p_rid);
|
2023-09-09 14:11:33 +00:00
|
|
|
ERR_FAIL_NULL(ptr);
|
2021-02-09 16:19:03 +00:00
|
|
|
*ptr = p_new_ptr;
|
|
|
|
}
|
|
|
|
|
2022-08-05 09:59:58 +00:00
|
|
|
_FORCE_INLINE_ bool owns(const RID &p_rid) const {
|
2019-06-10 15:38:51 +00:00
|
|
|
return alloc.owns(p_rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void free(const RID &p_rid) {
|
|
|
|
alloc.free(p_rid);
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:52:58 +00:00
|
|
|
_FORCE_INLINE_ uint32_t get_rid_count() const {
|
|
|
|
return alloc.get_rid_count();
|
|
|
|
}
|
|
|
|
|
2022-08-05 09:59:58 +00:00
|
|
|
_FORCE_INLINE_ void get_owned_list(List<RID> *p_owned) const {
|
2019-06-10 15:38:51 +00:00
|
|
|
return alloc.get_owned_list(p_owned);
|
|
|
|
}
|
|
|
|
|
2022-08-05 09:59:58 +00:00
|
|
|
void fill_owned_buffer(RID *p_rid_buffer) const {
|
2022-02-05 10:59:34 +00:00
|
|
|
alloc.fill_owned_buffer(p_rid_buffer);
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
void set_description(const char *p_descrption) {
|
|
|
|
alloc.set_description(p_descrption);
|
|
|
|
}
|
2020-12-23 16:52:58 +00:00
|
|
|
|
2021-06-29 13:58:28 +00:00
|
|
|
RID_PtrOwner(uint32_t p_target_chunk_byte_size = 65536) :
|
2019-06-10 15:38:51 +00:00
|
|
|
alloc(p_target_chunk_byte_size) {}
|
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
template <typename T, bool THREAD_SAFE = false>
|
2019-06-10 15:38:51 +00:00
|
|
|
class RID_Owner {
|
2019-07-29 15:59:18 +00:00
|
|
|
RID_Alloc<T, THREAD_SAFE> alloc;
|
2019-06-10 15:38:51 +00:00
|
|
|
|
|
|
|
public:
|
2021-06-29 13:58:28 +00:00
|
|
|
_FORCE_INLINE_ RID make_rid() {
|
|
|
|
return alloc.make_rid();
|
|
|
|
}
|
2019-06-10 15:38:51 +00:00
|
|
|
_FORCE_INLINE_ RID make_rid(const T &p_ptr) {
|
|
|
|
return alloc.make_rid(p_ptr);
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
_FORCE_INLINE_ RID allocate_rid() {
|
|
|
|
return alloc.allocate_rid();
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:58:28 +00:00
|
|
|
_FORCE_INLINE_ void initialize_rid(RID p_rid) {
|
|
|
|
alloc.initialize_rid(p_rid);
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
_FORCE_INLINE_ void initialize_rid(RID p_rid, const T &p_ptr) {
|
|
|
|
alloc.initialize_rid(p_rid, p_ptr);
|
|
|
|
}
|
|
|
|
|
2021-09-29 17:08:41 +00:00
|
|
|
_FORCE_INLINE_ T *get_or_null(const RID &p_rid) {
|
|
|
|
return alloc.get_or_null(p_rid);
|
2019-06-10 15:38:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 09:59:58 +00:00
|
|
|
_FORCE_INLINE_ bool owns(const RID &p_rid) const {
|
2019-06-10 15:38:51 +00:00
|
|
|
return alloc.owns(p_rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void free(const RID &p_rid) {
|
|
|
|
alloc.free(p_rid);
|
|
|
|
}
|
|
|
|
|
2019-07-05 01:54:32 +00:00
|
|
|
_FORCE_INLINE_ uint32_t get_rid_count() const {
|
|
|
|
return alloc.get_rid_count();
|
|
|
|
}
|
|
|
|
|
2022-08-05 09:59:58 +00:00
|
|
|
_FORCE_INLINE_ void get_owned_list(List<RID> *p_owned) const {
|
2019-06-10 15:38:51 +00:00
|
|
|
return alloc.get_owned_list(p_owned);
|
|
|
|
}
|
2022-08-05 09:59:58 +00:00
|
|
|
void fill_owned_buffer(RID *p_rid_buffer) const {
|
2022-02-05 10:59:34 +00:00
|
|
|
alloc.fill_owned_buffer(p_rid_buffer);
|
|
|
|
}
|
2019-06-10 15:38:51 +00:00
|
|
|
|
|
|
|
void set_description(const char *p_descrption) {
|
|
|
|
alloc.set_description(p_descrption);
|
|
|
|
}
|
2021-06-29 13:58:28 +00:00
|
|
|
RID_Owner(uint32_t p_target_chunk_byte_size = 65536) :
|
2019-06-10 15:38:51 +00:00
|
|
|
alloc(p_target_chunk_byte_size) {}
|
|
|
|
};
|
2020-05-12 15:01:17 +00:00
|
|
|
|
2019-06-10 15:38:51 +00:00
|
|
|
#endif // RID_OWNER_H
|