rid_owner.h 13 KB

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