rid_owner.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /**************************************************************************/
  2. /* rid_owner.hpp */
  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. #pragma once
  31. #include <godot_cpp/core/memory.hpp>
  32. #include <godot_cpp/godot.hpp>
  33. #include <godot_cpp/templates/list.hpp>
  34. #include <godot_cpp/templates/spin_lock.hpp>
  35. #include <godot_cpp/variant/utility_functions.hpp>
  36. #include <cstdio>
  37. #include <typeinfo>
  38. namespace godot {
  39. template <typename T, bool THREAD_SAFE = false>
  40. class RID_Alloc {
  41. T **chunks = nullptr;
  42. uint32_t **free_list_chunks = nullptr;
  43. uint32_t **validator_chunks = nullptr;
  44. uint32_t elements_in_chunk;
  45. uint32_t max_alloc = 0;
  46. uint32_t alloc_count = 0;
  47. const char *description = nullptr;
  48. SpinLock spin_lock;
  49. _FORCE_INLINE_ RID _allocate_rid() {
  50. if (THREAD_SAFE) {
  51. spin_lock.lock();
  52. }
  53. if (alloc_count == max_alloc) {
  54. // allocate a new chunk
  55. uint32_t chunk_count = alloc_count == 0 ? 0 : (max_alloc / elements_in_chunk);
  56. // grow chunks
  57. chunks = (T **)memrealloc(chunks, sizeof(T *) * (chunk_count + 1));
  58. chunks[chunk_count] = (T *)memalloc(sizeof(T) * elements_in_chunk); // but don't initialize
  59. // grow validators
  60. validator_chunks = (uint32_t **)memrealloc(validator_chunks, sizeof(uint32_t *) * (chunk_count + 1));
  61. validator_chunks[chunk_count] = (uint32_t *)memalloc(sizeof(uint32_t) * elements_in_chunk);
  62. // grow free lists
  63. free_list_chunks = (uint32_t **)memrealloc(free_list_chunks, sizeof(uint32_t *) * (chunk_count + 1));
  64. free_list_chunks[chunk_count] = (uint32_t *)memalloc(sizeof(uint32_t) * elements_in_chunk);
  65. // initialize
  66. for (uint32_t i = 0; i < elements_in_chunk; i++) {
  67. // Don't initialize chunk.
  68. validator_chunks[chunk_count][i] = 0xFFFFFFFF;
  69. free_list_chunks[chunk_count][i] = alloc_count + i;
  70. }
  71. max_alloc += elements_in_chunk;
  72. }
  73. uint32_t free_index = free_list_chunks[alloc_count / elements_in_chunk][alloc_count % elements_in_chunk];
  74. uint32_t free_chunk = free_index / elements_in_chunk;
  75. uint32_t free_element = free_index % elements_in_chunk;
  76. uint32_t validator = (uint32_t)(UtilityFunctions::rid_allocate_id() & 0x7FFFFFFF);
  77. uint64_t id = validator;
  78. id <<= 32;
  79. id |= free_index;
  80. validator_chunks[free_chunk][free_element] = validator;
  81. validator_chunks[free_chunk][free_element] |= 0x80000000; // mark uninitialized bit
  82. alloc_count++;
  83. if (THREAD_SAFE) {
  84. spin_lock.unlock();
  85. }
  86. return UtilityFunctions::rid_from_int64(id);
  87. }
  88. public:
  89. RID make_rid() {
  90. RID rid = _allocate_rid();
  91. initialize_rid(rid);
  92. return rid;
  93. }
  94. RID make_rid(const T &p_value) {
  95. RID rid = _allocate_rid();
  96. initialize_rid(rid, p_value);
  97. return rid;
  98. }
  99. // allocate but don't initialize, use initialize_rid afterwards
  100. RID allocate_rid() {
  101. return _allocate_rid();
  102. }
  103. _FORCE_INLINE_ T *get_or_null(const RID &p_rid, bool p_initialize = false) {
  104. if (p_rid == RID()) {
  105. return nullptr;
  106. }
  107. if (THREAD_SAFE) {
  108. spin_lock.lock();
  109. }
  110. uint64_t id = p_rid.get_id();
  111. uint32_t idx = uint32_t(id & 0xFFFFFFFF);
  112. if (unlikely(idx >= max_alloc)) {
  113. if (THREAD_SAFE) {
  114. spin_lock.unlock();
  115. }
  116. return nullptr;
  117. }
  118. uint32_t idx_chunk = idx / elements_in_chunk;
  119. uint32_t idx_element = idx % elements_in_chunk;
  120. uint32_t validator = uint32_t(id >> 32);
  121. if (unlikely(p_initialize)) {
  122. if (unlikely(!(validator_chunks[idx_chunk][idx_element] & 0x80000000))) {
  123. if (THREAD_SAFE) {
  124. spin_lock.unlock();
  125. }
  126. ERR_FAIL_V_MSG(nullptr, "Initializing already initialized RID");
  127. }
  128. if (unlikely((validator_chunks[idx_chunk][idx_element] & 0x7FFFFFFF) != validator)) {
  129. if (THREAD_SAFE) {
  130. spin_lock.unlock();
  131. }
  132. ERR_FAIL_V_MSG(nullptr, "Attempting to initialize the wrong RID");
  133. return nullptr;
  134. }
  135. validator_chunks[idx_chunk][idx_element] &= 0x7FFFFFFF; // initialized
  136. } else if (unlikely(validator_chunks[idx_chunk][idx_element] != validator)) {
  137. if (THREAD_SAFE) {
  138. spin_lock.unlock();
  139. }
  140. if ((validator_chunks[idx_chunk][idx_element] & 0x80000000) && validator_chunks[idx_chunk][idx_element] != 0xFFFFFFFF) {
  141. ERR_FAIL_V_MSG(nullptr, "Attempting to use an uninitialized RID");
  142. }
  143. return nullptr;
  144. }
  145. T *ptr = &chunks[idx_chunk][idx_element];
  146. if (THREAD_SAFE) {
  147. spin_lock.unlock();
  148. }
  149. return ptr;
  150. }
  151. void initialize_rid(RID p_rid) {
  152. T *mem = get_or_null(p_rid, true);
  153. ERR_FAIL_NULL(mem);
  154. memnew_placement(mem, T);
  155. }
  156. void initialize_rid(RID p_rid, const T &p_value) {
  157. T *mem = get_or_null(p_rid, true);
  158. ERR_FAIL_NULL(mem);
  159. memnew_placement(mem, T(p_value));
  160. }
  161. _FORCE_INLINE_ bool owns(const RID &p_rid) {
  162. if (THREAD_SAFE) {
  163. spin_lock.lock();
  164. }
  165. uint64_t id = p_rid.get_id();
  166. uint32_t idx = uint32_t(id & 0xFFFFFFFF);
  167. if (unlikely(idx >= max_alloc)) {
  168. if (THREAD_SAFE) {
  169. spin_lock.unlock();
  170. }
  171. return false;
  172. }
  173. uint32_t idx_chunk = idx / elements_in_chunk;
  174. uint32_t idx_element = idx % elements_in_chunk;
  175. uint32_t validator = uint32_t(id >> 32);
  176. bool owned = (validator_chunks[idx_chunk][idx_element] & 0x7FFFFFFF) == validator;
  177. if (THREAD_SAFE) {
  178. spin_lock.unlock();
  179. }
  180. return owned;
  181. }
  182. _FORCE_INLINE_ void free(const RID &p_rid) {
  183. if (THREAD_SAFE) {
  184. spin_lock.lock();
  185. }
  186. uint64_t id = p_rid.get_id();
  187. uint32_t idx = uint32_t(id & 0xFFFFFFFF);
  188. if (unlikely(idx >= max_alloc)) {
  189. if (THREAD_SAFE) {
  190. spin_lock.unlock();
  191. }
  192. ERR_FAIL();
  193. }
  194. uint32_t idx_chunk = idx / elements_in_chunk;
  195. uint32_t idx_element = idx % elements_in_chunk;
  196. uint32_t validator = uint32_t(id >> 32);
  197. if (unlikely(validator_chunks[idx_chunk][idx_element] & 0x80000000)) {
  198. if (THREAD_SAFE) {
  199. spin_lock.unlock();
  200. }
  201. ERR_FAIL_MSG("Attempted to free an uninitialized or invalid RID");
  202. } else if (unlikely(validator_chunks[idx_chunk][idx_element] != validator)) {
  203. if (THREAD_SAFE) {
  204. spin_lock.unlock();
  205. }
  206. ERR_FAIL();
  207. }
  208. chunks[idx_chunk][idx_element].~T();
  209. validator_chunks[idx_chunk][idx_element] = 0xFFFFFFFF; // go invalid
  210. alloc_count--;
  211. free_list_chunks[alloc_count / elements_in_chunk][alloc_count % elements_in_chunk] = idx;
  212. if (THREAD_SAFE) {
  213. spin_lock.unlock();
  214. }
  215. }
  216. _FORCE_INLINE_ uint32_t get_rid_count() const {
  217. return alloc_count;
  218. }
  219. void get_owned_list(List<RID> *p_owned) {
  220. if (THREAD_SAFE) {
  221. spin_lock.lock();
  222. }
  223. for (size_t i = 0; i < max_alloc; i++) {
  224. uint64_t validator = validator_chunks[i / elements_in_chunk][i % elements_in_chunk];
  225. if (validator != 0xFFFFFFFF) {
  226. p_owned->push_back(UtilityFunctions::rid_from_int64((validator << 32) | i));
  227. }
  228. }
  229. if (THREAD_SAFE) {
  230. spin_lock.unlock();
  231. }
  232. }
  233. // used for fast iteration in the elements or RIDs
  234. void fill_owned_buffer(RID *p_rid_buffer) {
  235. if (THREAD_SAFE) {
  236. spin_lock.lock();
  237. }
  238. uint32_t idx = 0;
  239. for (size_t i = 0; i < max_alloc; i++) {
  240. uint64_t validator = validator_chunks[i / elements_in_chunk][i % elements_in_chunk];
  241. if (validator != 0xFFFFFFFF) {
  242. p_rid_buffer[idx] = UtilityFunctions::rid_from_int64((validator << 32) | i);
  243. idx++;
  244. }
  245. }
  246. if (THREAD_SAFE) {
  247. spin_lock.unlock();
  248. }
  249. }
  250. void set_description(const char *p_descrption) {
  251. description = p_descrption;
  252. }
  253. RID_Alloc(uint32_t p_target_chunk_byte_size = 65536) {
  254. elements_in_chunk = sizeof(T) > p_target_chunk_byte_size ? 1 : (p_target_chunk_byte_size / sizeof(T));
  255. }
  256. ~RID_Alloc() {
  257. if (alloc_count) {
  258. if (description) {
  259. printf("ERROR: %d RID allocations of type '%s' were leaked at exit.", alloc_count, description);
  260. } else {
  261. #ifdef NO_SAFE_CAST
  262. printf("ERROR: %d RID allocations of type 'unknown' were leaked at exit.", alloc_count);
  263. #else
  264. printf("ERROR: %d RID allocations of type '%s' were leaked at exit.", alloc_count, typeid(T).name());
  265. #endif
  266. }
  267. for (size_t i = 0; i < max_alloc; i++) {
  268. uint64_t validator = validator_chunks[i / elements_in_chunk][i % elements_in_chunk];
  269. if (validator & 0x80000000) {
  270. continue; // uninitialized
  271. }
  272. if (validator != 0xFFFFFFFF) {
  273. chunks[i / elements_in_chunk][i % elements_in_chunk].~T();
  274. }
  275. }
  276. }
  277. uint32_t chunk_count = max_alloc / elements_in_chunk;
  278. for (uint32_t i = 0; i < chunk_count; i++) {
  279. memfree(chunks[i]);
  280. memfree(validator_chunks[i]);
  281. memfree(free_list_chunks[i]);
  282. }
  283. if (chunks) {
  284. memfree(chunks);
  285. memfree(free_list_chunks);
  286. memfree(validator_chunks);
  287. }
  288. }
  289. };
  290. template <typename T, bool THREAD_SAFE = false>
  291. class RID_PtrOwner {
  292. RID_Alloc<T *, THREAD_SAFE> alloc;
  293. public:
  294. _FORCE_INLINE_ RID make_rid(T *p_ptr) {
  295. return alloc.make_rid(p_ptr);
  296. }
  297. _FORCE_INLINE_ RID allocate_rid() {
  298. return alloc.allocate_rid();
  299. }
  300. _FORCE_INLINE_ void initialize_rid(RID p_rid, T *p_ptr) {
  301. alloc.initialize_rid(p_rid, p_ptr);
  302. }
  303. _FORCE_INLINE_ T *get_or_null(const RID &p_rid) {
  304. T **ptr = alloc.get_or_null(p_rid);
  305. if (unlikely(!ptr)) {
  306. return nullptr;
  307. }
  308. return *ptr;
  309. }
  310. _FORCE_INLINE_ void replace(const RID &p_rid, T *p_new_ptr) {
  311. T **ptr = alloc.get_or_null(p_rid);
  312. ERR_FAIL_NULL(ptr);
  313. *ptr = p_new_ptr;
  314. }
  315. _FORCE_INLINE_ bool owns(const RID &p_rid) {
  316. return alloc.owns(p_rid);
  317. }
  318. _FORCE_INLINE_ void free(const RID &p_rid) {
  319. alloc.free(p_rid);
  320. }
  321. _FORCE_INLINE_ uint32_t get_rid_count() const {
  322. return alloc.get_rid_count();
  323. }
  324. _FORCE_INLINE_ void get_owned_list(List<RID> *p_owned) {
  325. return alloc.get_owned_list(p_owned);
  326. }
  327. void fill_owned_buffer(RID *p_rid_buffer) {
  328. alloc.fill_owned_buffer(p_rid_buffer);
  329. }
  330. void set_description(const char *p_descrption) {
  331. alloc.set_description(p_descrption);
  332. }
  333. RID_PtrOwner(uint32_t p_target_chunk_byte_size = 65536) :
  334. alloc(p_target_chunk_byte_size) {}
  335. };
  336. template <typename T, bool THREAD_SAFE = false>
  337. class RID_Owner {
  338. RID_Alloc<T, THREAD_SAFE> alloc;
  339. public:
  340. _FORCE_INLINE_ RID make_rid() {
  341. return alloc.make_rid();
  342. }
  343. _FORCE_INLINE_ RID make_rid(const T &p_ptr) {
  344. return alloc.make_rid(p_ptr);
  345. }
  346. _FORCE_INLINE_ RID allocate_rid() {
  347. return alloc.allocate_rid();
  348. }
  349. _FORCE_INLINE_ void initialize_rid(RID p_rid) {
  350. alloc.initialize_rid(p_rid);
  351. }
  352. _FORCE_INLINE_ void initialize_rid(RID p_rid, const T &p_ptr) {
  353. alloc.initialize_rid(p_rid, p_ptr);
  354. }
  355. _FORCE_INLINE_ T *get_or_null(const RID &p_rid) {
  356. return alloc.get_or_null(p_rid);
  357. }
  358. _FORCE_INLINE_ bool owns(const RID &p_rid) {
  359. return alloc.owns(p_rid);
  360. }
  361. _FORCE_INLINE_ void free(const RID &p_rid) {
  362. alloc.free(p_rid);
  363. }
  364. _FORCE_INLINE_ uint32_t get_rid_count() const {
  365. return alloc.get_rid_count();
  366. }
  367. _FORCE_INLINE_ void get_owned_list(List<RID> *p_owned) {
  368. return alloc.get_owned_list(p_owned);
  369. }
  370. void fill_owned_buffer(RID *p_rid_buffer) {
  371. alloc.fill_owned_buffer(p_rid_buffer);
  372. }
  373. void set_description(const char *p_descrption) {
  374. alloc.set_description(p_descrption);
  375. }
  376. RID_Owner(uint32_t p_target_chunk_byte_size = 65536) :
  377. alloc(p_target_chunk_byte_size) {}
  378. };
  379. } // namespace godot