rid_owner.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /**************************************************************************/
  2. /* rid_owner.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef RID_OWNER_H
  31. #define RID_OWNER_H
  32. #include "core/os/memory.h"
  33. #include "core/os/mutex.h"
  34. #include "core/string/print_string.h"
  35. #include "core/templates/hash_set.h"
  36. #include "core/templates/list.h"
  37. #include "core/templates/oa_hash_map.h"
  38. #include "core/templates/rid.h"
  39. #include "core/templates/safe_refcount.h"
  40. #include <stdio.h>
  41. #include <typeinfo>
  42. class RID_AllocBase {
  43. static SafeNumeric<uint64_t> base_id;
  44. protected:
  45. static RID _make_from_id(uint64_t p_id) {
  46. RID rid;
  47. rid._id = p_id;
  48. return rid;
  49. }
  50. static RID _gen_rid() {
  51. return _make_from_id(_gen_id());
  52. }
  53. friend struct VariantUtilityFunctions;
  54. static uint64_t _gen_id() {
  55. return base_id.increment();
  56. }
  57. public:
  58. virtual ~RID_AllocBase() {}
  59. };
  60. template <typename T, bool THREAD_SAFE = false>
  61. class RID_Alloc : public RID_AllocBase {
  62. struct Chunk {
  63. T data;
  64. uint32_t validator;
  65. };
  66. Chunk **chunks = nullptr;
  67. uint32_t **free_list_chunks = nullptr;
  68. uint32_t elements_in_chunk;
  69. uint32_t max_alloc = 0;
  70. uint32_t alloc_count = 0;
  71. uint32_t chunk_limit = 0;
  72. const char *description = nullptr;
  73. mutable Mutex mutex;
  74. _FORCE_INLINE_ RID _allocate_rid() {
  75. if constexpr (THREAD_SAFE) {
  76. mutex.lock();
  77. }
  78. if (alloc_count == max_alloc) {
  79. //allocate a new chunk
  80. uint32_t chunk_count = alloc_count == 0 ? 0 : (max_alloc / elements_in_chunk);
  81. if (THREAD_SAFE && chunk_count == chunk_limit) {
  82. mutex.unlock();
  83. if (description != nullptr) {
  84. ERR_FAIL_V_MSG(RID(), vformat("Element limit for RID of type '%s' reached.", String(description)));
  85. } else {
  86. ERR_FAIL_V_MSG(RID(), "Element limit reached.");
  87. }
  88. }
  89. //grow chunks
  90. if constexpr (!THREAD_SAFE) {
  91. chunks = (Chunk **)memrealloc(chunks, sizeof(Chunk *) * (chunk_count + 1));
  92. }
  93. chunks[chunk_count] = (Chunk *)memalloc(sizeof(Chunk) * elements_in_chunk); //but don't initialize
  94. //grow free lists
  95. if constexpr (!THREAD_SAFE) {
  96. free_list_chunks = (uint32_t **)memrealloc(free_list_chunks, sizeof(uint32_t *) * (chunk_count + 1));
  97. }
  98. free_list_chunks[chunk_count] = (uint32_t *)memalloc(sizeof(uint32_t) * elements_in_chunk);
  99. //initialize
  100. for (uint32_t i = 0; i < elements_in_chunk; i++) {
  101. // Don't initialize chunk.
  102. chunks[chunk_count][i].validator = 0xFFFFFFFF;
  103. free_list_chunks[chunk_count][i] = alloc_count + i;
  104. }
  105. max_alloc += elements_in_chunk;
  106. }
  107. uint32_t free_index = free_list_chunks[alloc_count / elements_in_chunk][alloc_count % elements_in_chunk];
  108. uint32_t free_chunk = free_index / elements_in_chunk;
  109. uint32_t free_element = free_index % elements_in_chunk;
  110. uint32_t validator = (uint32_t)(_gen_id() & 0x7FFFFFFF);
  111. CRASH_COND_MSG(validator == 0x7FFFFFFF, "Overflow in RID validator");
  112. uint64_t id = validator;
  113. id <<= 32;
  114. id |= free_index;
  115. chunks[free_chunk][free_element].validator = validator;
  116. chunks[free_chunk][free_element].validator |= 0x80000000; //mark uninitialized bit
  117. alloc_count++;
  118. if constexpr (THREAD_SAFE) {
  119. mutex.unlock();
  120. }
  121. return _make_from_id(id);
  122. }
  123. public:
  124. RID make_rid() {
  125. RID rid = _allocate_rid();
  126. initialize_rid(rid);
  127. return rid;
  128. }
  129. RID make_rid(const T &p_value) {
  130. RID rid = _allocate_rid();
  131. initialize_rid(rid, p_value);
  132. return rid;
  133. }
  134. //allocate but don't initialize, use initialize_rid afterwards
  135. RID allocate_rid() {
  136. return _allocate_rid();
  137. }
  138. _FORCE_INLINE_ T *get_or_null(const RID &p_rid, bool p_initialize = false) {
  139. if (p_rid == RID()) {
  140. return nullptr;
  141. }
  142. uint64_t id = p_rid.get_id();
  143. uint32_t idx = uint32_t(id & 0xFFFFFFFF);
  144. if (unlikely(idx >= max_alloc)) {
  145. return nullptr;
  146. }
  147. uint32_t idx_chunk = idx / elements_in_chunk;
  148. uint32_t idx_element = idx % elements_in_chunk;
  149. uint32_t validator = uint32_t(id >> 32);
  150. Chunk &c = chunks[idx_chunk][idx_element];
  151. if (unlikely(p_initialize)) {
  152. if (unlikely(!(c.validator & 0x80000000))) {
  153. ERR_FAIL_V_MSG(nullptr, "Initializing already initialized RID");
  154. }
  155. if (unlikely((c.validator & 0x7FFFFFFF) != validator)) {
  156. ERR_FAIL_V_MSG(nullptr, "Attempting to initialize the wrong RID");
  157. }
  158. c.validator &= 0x7FFFFFFF; //initialized
  159. } else if (unlikely(c.validator != validator)) {
  160. if ((c.validator & 0x80000000) && c.validator != 0xFFFFFFFF) {
  161. ERR_FAIL_V_MSG(nullptr, "Attempting to use an uninitialized RID");
  162. }
  163. return nullptr;
  164. }
  165. T *ptr = &c.data;
  166. return ptr;
  167. }
  168. void initialize_rid(RID p_rid) {
  169. T *mem = get_or_null(p_rid, true);
  170. ERR_FAIL_NULL(mem);
  171. memnew_placement(mem, T);
  172. }
  173. void initialize_rid(RID p_rid, const T &p_value) {
  174. T *mem = get_or_null(p_rid, true);
  175. ERR_FAIL_NULL(mem);
  176. memnew_placement(mem, T(p_value));
  177. }
  178. _FORCE_INLINE_ bool owns(const RID &p_rid) const {
  179. if constexpr (THREAD_SAFE) {
  180. mutex.lock();
  181. }
  182. uint64_t id = p_rid.get_id();
  183. uint32_t idx = uint32_t(id & 0xFFFFFFFF);
  184. if (unlikely(idx >= max_alloc)) {
  185. if constexpr (THREAD_SAFE) {
  186. mutex.unlock();
  187. }
  188. return false;
  189. }
  190. uint32_t idx_chunk = idx / elements_in_chunk;
  191. uint32_t idx_element = idx % elements_in_chunk;
  192. uint32_t validator = uint32_t(id >> 32);
  193. bool owned = (validator != 0x7FFFFFFF) && (chunks[idx_chunk][idx_element].validator & 0x7FFFFFFF) == validator;
  194. if constexpr (THREAD_SAFE) {
  195. mutex.unlock();
  196. }
  197. return owned;
  198. }
  199. _FORCE_INLINE_ void free(const RID &p_rid) {
  200. if constexpr (THREAD_SAFE) {
  201. mutex.lock();
  202. }
  203. uint64_t id = p_rid.get_id();
  204. uint32_t idx = uint32_t(id & 0xFFFFFFFF);
  205. if (unlikely(idx >= max_alloc)) {
  206. if constexpr (THREAD_SAFE) {
  207. mutex.unlock();
  208. }
  209. ERR_FAIL();
  210. }
  211. uint32_t idx_chunk = idx / elements_in_chunk;
  212. uint32_t idx_element = idx % elements_in_chunk;
  213. uint32_t validator = uint32_t(id >> 32);
  214. if (unlikely(chunks[idx_chunk][idx_element].validator & 0x80000000)) {
  215. if constexpr (THREAD_SAFE) {
  216. mutex.unlock();
  217. }
  218. ERR_FAIL_MSG("Attempted to free an uninitialized or invalid RID");
  219. } else if (unlikely(chunks[idx_chunk][idx_element].validator != validator)) {
  220. if constexpr (THREAD_SAFE) {
  221. mutex.unlock();
  222. }
  223. ERR_FAIL();
  224. }
  225. chunks[idx_chunk][idx_element].data.~T();
  226. chunks[idx_chunk][idx_element].validator = 0xFFFFFFFF; // go invalid
  227. alloc_count--;
  228. free_list_chunks[alloc_count / elements_in_chunk][alloc_count % elements_in_chunk] = idx;
  229. if constexpr (THREAD_SAFE) {
  230. mutex.unlock();
  231. }
  232. }
  233. _FORCE_INLINE_ uint32_t get_rid_count() const {
  234. return alloc_count;
  235. }
  236. void get_owned_list(List<RID> *p_owned) const {
  237. if constexpr (THREAD_SAFE) {
  238. mutex.lock();
  239. }
  240. for (size_t i = 0; i < max_alloc; i++) {
  241. uint64_t validator = chunks[i / elements_in_chunk][i % elements_in_chunk].validator;
  242. if (validator != 0xFFFFFFFF) {
  243. p_owned->push_back(_make_from_id((validator << 32) | i));
  244. }
  245. }
  246. if constexpr (THREAD_SAFE) {
  247. mutex.unlock();
  248. }
  249. }
  250. //used for fast iteration in the elements or RIDs
  251. void fill_owned_buffer(RID *p_rid_buffer) const {
  252. if constexpr (THREAD_SAFE) {
  253. mutex.lock();
  254. }
  255. uint32_t idx = 0;
  256. for (size_t i = 0; i < max_alloc; i++) {
  257. uint64_t validator = chunks[i / elements_in_chunk][i % elements_in_chunk].validator;
  258. if (validator != 0xFFFFFFFF) {
  259. p_rid_buffer[idx] = _make_from_id((validator << 32) | i);
  260. idx++;
  261. }
  262. }
  263. if constexpr (THREAD_SAFE) {
  264. mutex.unlock();
  265. }
  266. }
  267. void set_description(const char *p_descrption) {
  268. description = p_descrption;
  269. }
  270. RID_Alloc(uint32_t p_target_chunk_byte_size = 65536, uint32_t p_maximum_number_of_elements = 262144) {
  271. elements_in_chunk = sizeof(T) > p_target_chunk_byte_size ? 1 : (p_target_chunk_byte_size / sizeof(T));
  272. if constexpr (THREAD_SAFE) {
  273. chunk_limit = (p_maximum_number_of_elements / elements_in_chunk) + 1;
  274. chunks = (Chunk **)memalloc(sizeof(Chunk *) * chunk_limit);
  275. free_list_chunks = (uint32_t **)memalloc(sizeof(uint32_t *) * chunk_limit);
  276. }
  277. }
  278. ~RID_Alloc() {
  279. if (alloc_count) {
  280. print_error(vformat("ERROR: %d RID allocations of type '%s' were leaked at exit.",
  281. alloc_count, description ? description : typeid(T).name()));
  282. for (size_t i = 0; i < max_alloc; i++) {
  283. uint64_t validator = chunks[i / elements_in_chunk][i % elements_in_chunk].validator;
  284. if (validator & 0x80000000) {
  285. continue; //uninitialized
  286. }
  287. if (validator != 0xFFFFFFFF) {
  288. chunks[i / elements_in_chunk][i % elements_in_chunk].data.~T();
  289. }
  290. }
  291. }
  292. uint32_t chunk_count = max_alloc / elements_in_chunk;
  293. for (uint32_t i = 0; i < chunk_count; i++) {
  294. memfree(chunks[i]);
  295. memfree(free_list_chunks[i]);
  296. }
  297. if (chunks) {
  298. memfree(chunks);
  299. memfree(free_list_chunks);
  300. }
  301. }
  302. };
  303. template <typename T, bool THREAD_SAFE = false>
  304. class RID_PtrOwner {
  305. RID_Alloc<T *, THREAD_SAFE> alloc;
  306. public:
  307. _FORCE_INLINE_ RID make_rid(T *p_ptr) {
  308. return alloc.make_rid(p_ptr);
  309. }
  310. _FORCE_INLINE_ RID allocate_rid() {
  311. return alloc.allocate_rid();
  312. }
  313. _FORCE_INLINE_ void initialize_rid(RID p_rid, T *p_ptr) {
  314. alloc.initialize_rid(p_rid, p_ptr);
  315. }
  316. _FORCE_INLINE_ T *get_or_null(const RID &p_rid) {
  317. T **ptr = alloc.get_or_null(p_rid);
  318. if (unlikely(!ptr)) {
  319. return nullptr;
  320. }
  321. return *ptr;
  322. }
  323. _FORCE_INLINE_ void replace(const RID &p_rid, T *p_new_ptr) {
  324. T **ptr = alloc.get_or_null(p_rid);
  325. ERR_FAIL_NULL(ptr);
  326. *ptr = p_new_ptr;
  327. }
  328. _FORCE_INLINE_ bool owns(const RID &p_rid) const {
  329. return alloc.owns(p_rid);
  330. }
  331. _FORCE_INLINE_ void free(const RID &p_rid) {
  332. alloc.free(p_rid);
  333. }
  334. _FORCE_INLINE_ uint32_t get_rid_count() const {
  335. return alloc.get_rid_count();
  336. }
  337. _FORCE_INLINE_ void get_owned_list(List<RID> *p_owned) const {
  338. return alloc.get_owned_list(p_owned);
  339. }
  340. void fill_owned_buffer(RID *p_rid_buffer) const {
  341. alloc.fill_owned_buffer(p_rid_buffer);
  342. }
  343. void set_description(const char *p_descrption) {
  344. alloc.set_description(p_descrption);
  345. }
  346. RID_PtrOwner(uint32_t p_target_chunk_byte_size = 65536, uint32_t p_maximum_number_of_elements = 262144) :
  347. alloc(p_target_chunk_byte_size, p_maximum_number_of_elements) {}
  348. };
  349. template <typename T, bool THREAD_SAFE = false>
  350. class RID_Owner {
  351. RID_Alloc<T, THREAD_SAFE> alloc;
  352. public:
  353. _FORCE_INLINE_ RID make_rid() {
  354. return alloc.make_rid();
  355. }
  356. _FORCE_INLINE_ RID make_rid(const T &p_ptr) {
  357. return alloc.make_rid(p_ptr);
  358. }
  359. _FORCE_INLINE_ RID allocate_rid() {
  360. return alloc.allocate_rid();
  361. }
  362. _FORCE_INLINE_ void initialize_rid(RID p_rid) {
  363. alloc.initialize_rid(p_rid);
  364. }
  365. _FORCE_INLINE_ void initialize_rid(RID p_rid, const T &p_ptr) {
  366. alloc.initialize_rid(p_rid, p_ptr);
  367. }
  368. _FORCE_INLINE_ T *get_or_null(const RID &p_rid) {
  369. return alloc.get_or_null(p_rid);
  370. }
  371. _FORCE_INLINE_ bool owns(const RID &p_rid) const {
  372. return alloc.owns(p_rid);
  373. }
  374. _FORCE_INLINE_ void free(const RID &p_rid) {
  375. alloc.free(p_rid);
  376. }
  377. _FORCE_INLINE_ uint32_t get_rid_count() const {
  378. return alloc.get_rid_count();
  379. }
  380. _FORCE_INLINE_ void get_owned_list(List<RID> *p_owned) const {
  381. return alloc.get_owned_list(p_owned);
  382. }
  383. void fill_owned_buffer(RID *p_rid_buffer) const {
  384. alloc.fill_owned_buffer(p_rid_buffer);
  385. }
  386. void set_description(const char *p_descrption) {
  387. alloc.set_description(p_descrption);
  388. }
  389. RID_Owner(uint32_t p_target_chunk_byte_size = 65536, uint32_t p_maximum_number_of_elements = 262144) :
  390. alloc(p_target_chunk_byte_size, p_maximum_number_of_elements) {}
  391. };
  392. #endif // RID_OWNER_H