pooled_list.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**************************************************************************/
  2. /* pooled_list.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 POOLED_LIST_H
  31. #define POOLED_LIST_H
  32. // Simple template to provide a pool with O(1) allocate and free.
  33. // The freelist could alternatively be a linked list placed within the unused elements
  34. // to use less memory, however a separate freelist is probably more cache friendly.
  35. // NOTE : Take great care when using this with non POD types. The construction and destruction
  36. // is done in the LocalVector, NOT as part of the pool. So requesting a new item does not guarantee
  37. // a constructor is run, and free does not guarantee a destructor.
  38. // You should generally handle clearing
  39. // an item explicitly after a request, as it may contain 'leftovers'.
  40. // This is by design for fastest use in the BVH. If you want a more general pool
  41. // that does call constructors / destructors on request / free, this should probably be
  42. // a separate template.
  43. // The zero_on_first_request feature is optional and is useful for e.g. pools of handles,
  44. // which may use a ref count which we want to be initialized to zero the first time a handle is created,
  45. // but left alone on subsequent allocations (as will typically be incremented).
  46. // Note that there is no function to compact the pool - this would
  47. // invalidate any existing pool IDs held externally.
  48. // Compaction can be done but would rely on a more complex method
  49. // of preferentially giving out lower IDs in the freelist first.
  50. #include "core/templates/local_vector.h"
  51. template <typename T, typename U = uint32_t, bool force_trivial = false, bool zero_on_first_request = false>
  52. class PooledList {
  53. LocalVector<T, U, force_trivial> list;
  54. LocalVector<U, U, true> freelist;
  55. // not all list members are necessarily used
  56. U _used_size;
  57. public:
  58. PooledList() {
  59. _used_size = 0;
  60. }
  61. // Use with care, in most cases you should make sure to
  62. // free all elements first (i.e. _used_size would be zero),
  63. // although it could also be used without this as an optimization
  64. // in some cases.
  65. void clear() {
  66. list.clear();
  67. freelist.clear();
  68. _used_size = 0;
  69. }
  70. uint64_t estimate_memory_use() const {
  71. return ((uint64_t)list.size() * sizeof(T)) + ((uint64_t)freelist.size() * sizeof(U));
  72. }
  73. const T &operator[](U p_index) const {
  74. return list[p_index];
  75. }
  76. T &operator[](U p_index) {
  77. return list[p_index];
  78. }
  79. // To be explicit in a pool there is a distinction
  80. // between the number of elements that are currently
  81. // in use, and the number of elements that have been reserved.
  82. // Using size() would be vague.
  83. U used_size() const { return _used_size; }
  84. U reserved_size() const { return list.size(); }
  85. T *request(U &r_id) {
  86. _used_size++;
  87. if (freelist.size()) {
  88. // pop from freelist
  89. int new_size = freelist.size() - 1;
  90. r_id = freelist[new_size];
  91. freelist.resize(new_size);
  92. return &list[r_id];
  93. }
  94. r_id = list.size();
  95. list.resize(r_id + 1);
  96. static_assert((!zero_on_first_request) || (__is_pod(T)), "zero_on_first_request requires trivial type");
  97. if constexpr (zero_on_first_request && __is_pod(T)) {
  98. list[r_id] = {};
  99. }
  100. return &list[r_id];
  101. }
  102. void free(const U &p_id) {
  103. // should not be on free list already
  104. ERR_FAIL_UNSIGNED_INDEX(p_id, list.size());
  105. freelist.push_back(p_id);
  106. ERR_FAIL_COND_MSG(!_used_size, "_used_size has become out of sync, have you double freed an item?");
  107. _used_size--;
  108. }
  109. };
  110. // a pooled list which automatically keeps a list of the active members
  111. template <typename T, typename U = uint32_t, bool force_trivial = false, bool zero_on_first_request = false>
  112. class TrackedPooledList {
  113. public:
  114. U pool_used_size() const { return _pool.used_size(); }
  115. U pool_reserved_size() const { return _pool.reserved_size(); }
  116. U active_size() const { return _active_list.size(); }
  117. // use with care, see the earlier notes in the PooledList clear()
  118. void clear() {
  119. _pool.clear();
  120. _active_list.clear();
  121. _active_map.clear();
  122. }
  123. U get_active_id(U p_index) const {
  124. return _active_list[p_index];
  125. }
  126. const T &get_active(U p_index) const {
  127. return _pool[get_active_id(p_index)];
  128. }
  129. T &get_active(U p_index) {
  130. return _pool[get_active_id(p_index)];
  131. }
  132. const T &operator[](U p_index) const {
  133. return _pool[p_index];
  134. }
  135. T &operator[](U p_index) {
  136. return _pool[p_index];
  137. }
  138. T *request(U &r_id) {
  139. T *item = _pool.request(r_id);
  140. // add to the active list
  141. U active_list_id = _active_list.size();
  142. _active_list.push_back(r_id);
  143. // expand the active map (this should be in sync with the pool list
  144. if (_pool.used_size() > _active_map.size()) {
  145. _active_map.resize(_pool.used_size());
  146. }
  147. // store in the active map
  148. _active_map[r_id] = active_list_id;
  149. return item;
  150. }
  151. void free(const U &p_id) {
  152. _pool.free(p_id);
  153. // remove from the active list.
  154. U list_id = _active_map[p_id];
  155. // zero the _active map to detect bugs (only in debug?)
  156. _active_map[p_id] = -1;
  157. _active_list.remove_unordered(list_id);
  158. // keep the replacement in sync with the correct list Id
  159. if (list_id < _active_list.size()) {
  160. // which pool id has been replaced in the active list
  161. U replacement_id = _active_list[list_id];
  162. // keep that replacements map up to date with the new position
  163. _active_map[replacement_id] = list_id;
  164. }
  165. }
  166. const LocalVector<U, U> &get_active_list() const { return _active_list; }
  167. private:
  168. PooledList<T, U, force_trivial, zero_on_first_request> _pool;
  169. LocalVector<U, U> _active_map;
  170. LocalVector<U, U> _active_list;
  171. };
  172. #endif // POOLED_LIST_H