rid_owner.hpp 13 KB

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