cowdata.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*************************************************************************/
  2. /* cowdata.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_COWDATA_HPP
  31. #define GODOT_COWDATA_HPP
  32. #include <godot_cpp/classes/global_constants.hpp>
  33. #include <godot_cpp/core/class_db.hpp>
  34. #include <godot_cpp/core/error_macros.hpp>
  35. #include <godot_cpp/core/math.hpp>
  36. #include <godot_cpp/core/memory.hpp>
  37. #include <godot_cpp/templates/safe_refcount.hpp>
  38. #include <cstring>
  39. namespace godot {
  40. template <class T>
  41. class Vector;
  42. template <class T, class V>
  43. class VMap;
  44. // Silence a false positive warning (see GH-52119).
  45. #if defined(__GNUC__) && !defined(__clang__)
  46. #pragma GCC diagnostic push
  47. #pragma GCC diagnostic ignored "-Wplacement-new"
  48. #endif
  49. template <class T>
  50. class CowData {
  51. template <class TV>
  52. friend class Vector;
  53. template <class TV, class VV>
  54. friend class VMap;
  55. private:
  56. mutable T *_ptr = nullptr;
  57. // internal helpers
  58. _FORCE_INLINE_ SafeNumeric<uint32_t> *_get_refcount() const {
  59. if (!_ptr) {
  60. return nullptr;
  61. }
  62. return reinterpret_cast<SafeNumeric<uint32_t> *>(_ptr) - 2;
  63. }
  64. _FORCE_INLINE_ uint32_t *_get_size() const {
  65. if (!_ptr) {
  66. return nullptr;
  67. }
  68. return reinterpret_cast<uint32_t *>(_ptr) - 1;
  69. }
  70. _FORCE_INLINE_ T *_get_data() const {
  71. if (!_ptr) {
  72. return nullptr;
  73. }
  74. return reinterpret_cast<T *>(_ptr);
  75. }
  76. _FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const {
  77. return Math::next_power_of_2(p_elements * sizeof(T));
  78. }
  79. _FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const {
  80. #if defined(__GNUC__)
  81. size_t o;
  82. size_t p;
  83. if (__builtin_mul_overflow(p_elements, sizeof(T), &o)) {
  84. *out = 0;
  85. return false;
  86. }
  87. *out = Math::next_power_of_2(o);
  88. if (__builtin_add_overflow(o, static_cast<size_t>(32), &p)) {
  89. return false; // No longer allocated here.
  90. }
  91. return true;
  92. #else
  93. // Speed is more important than correctness here, do the operations unchecked
  94. // and hope for the best.
  95. *out = _get_alloc_size(p_elements);
  96. return true;
  97. #endif
  98. }
  99. void _unref(void *p_data);
  100. void _ref(const CowData *p_from);
  101. void _ref(const CowData &p_from);
  102. uint32_t _copy_on_write();
  103. public:
  104. void operator=(const CowData<T> &p_from) { _ref(p_from); }
  105. _FORCE_INLINE_ T *ptrw() {
  106. _copy_on_write();
  107. return (T *)_get_data();
  108. }
  109. _FORCE_INLINE_ const T *ptr() const {
  110. return _get_data();
  111. }
  112. _FORCE_INLINE_ int size() const {
  113. uint32_t *size = (uint32_t *)_get_size();
  114. if (size) {
  115. return *size;
  116. } else {
  117. return 0;
  118. }
  119. }
  120. _FORCE_INLINE_ void clear() { resize(0); }
  121. _FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; }
  122. _FORCE_INLINE_ void set(int p_index, const T &p_elem) {
  123. ERR_FAIL_INDEX(p_index, size());
  124. _copy_on_write();
  125. _get_data()[p_index] = p_elem;
  126. }
  127. _FORCE_INLINE_ T &get_m(int p_index) {
  128. CRASH_BAD_INDEX(p_index, size());
  129. _copy_on_write();
  130. return _get_data()[p_index];
  131. }
  132. _FORCE_INLINE_ const T &get(int p_index) const {
  133. CRASH_BAD_INDEX(p_index, size());
  134. return _get_data()[p_index];
  135. }
  136. Error resize(int p_size);
  137. _FORCE_INLINE_ void remove_at(int p_index) {
  138. ERR_FAIL_INDEX(p_index, size());
  139. T *p = ptrw();
  140. int len = size();
  141. for (int i = p_index; i < len - 1; i++) {
  142. p[i] = p[i + 1];
  143. }
  144. resize(len - 1);
  145. }
  146. Error insert(int p_pos, const T &p_val) {
  147. ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER);
  148. resize(size() + 1);
  149. for (int i = (size() - 1); i > p_pos; i--) {
  150. set(i, get(i - 1));
  151. }
  152. set(p_pos, p_val);
  153. return OK;
  154. }
  155. int find(const T &p_val, int p_from = 0) const;
  156. _FORCE_INLINE_ CowData() {}
  157. _FORCE_INLINE_ ~CowData();
  158. _FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); }
  159. };
  160. template <class T>
  161. void CowData<T>::_unref(void *p_data) {
  162. if (!p_data) {
  163. return;
  164. }
  165. SafeNumeric<uint32_t> *refc = _get_refcount();
  166. if (refc->decrement() > 0) {
  167. return; // still in use
  168. }
  169. // clean up
  170. if (!__has_trivial_destructor(T)) {
  171. uint32_t *count = _get_size();
  172. T *data = (T *)(count + 1);
  173. for (uint32_t i = 0; i < *count; ++i) {
  174. // call destructors
  175. data[i].~T();
  176. }
  177. }
  178. // free mem
  179. Memory::free_static((uint8_t *)p_data);
  180. }
  181. template <class T>
  182. uint32_t CowData<T>::_copy_on_write() {
  183. if (!_ptr) {
  184. return 0;
  185. }
  186. SafeNumeric<uint32_t> *refc = _get_refcount();
  187. uint32_t rc = refc->get();
  188. if (unlikely(rc > 1)) {
  189. /* in use by more than me */
  190. uint32_t current_size = *_get_size();
  191. uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size));
  192. new (mem_new - 2) SafeNumeric<uint32_t>(1); // refcount
  193. *(mem_new - 1) = current_size; // size
  194. T *_data = (T *)(mem_new);
  195. // initialize new elements
  196. if (__has_trivial_copy(T)) {
  197. memcpy(mem_new, _ptr, current_size * sizeof(T));
  198. } else {
  199. for (uint32_t i = 0; i < current_size; i++) {
  200. memnew_placement(&_data[i], T(_get_data()[i]));
  201. }
  202. }
  203. _unref(_ptr);
  204. _ptr = _data;
  205. rc = 1;
  206. }
  207. return rc;
  208. }
  209. template <class T>
  210. Error CowData<T>::resize(int p_size) {
  211. ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
  212. int current_size = size();
  213. if (p_size == current_size) {
  214. return OK;
  215. }
  216. if (p_size == 0) {
  217. // wants to clean up
  218. _unref(_ptr);
  219. _ptr = nullptr;
  220. return OK;
  221. }
  222. // possibly changing size, copy on write
  223. uint32_t rc = _copy_on_write();
  224. size_t current_alloc_size = _get_alloc_size(current_size);
  225. size_t alloc_size;
  226. ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
  227. if (p_size > current_size) {
  228. if (alloc_size != current_alloc_size) {
  229. if (current_size == 0) {
  230. // alloc from scratch
  231. uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size);
  232. ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY);
  233. *(ptr - 1) = 0; // size, currently none
  234. new (ptr - 2) SafeNumeric<uint32_t>(1); // refcount
  235. _ptr = (T *)ptr;
  236. } else {
  237. uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size);
  238. ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
  239. new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); // refcount
  240. _ptr = (T *)(_ptrnew);
  241. }
  242. }
  243. // construct the newly created elements
  244. if (!__has_trivial_constructor(T)) {
  245. T *elems = _get_data();
  246. for (int i = *_get_size(); i < p_size; i++) {
  247. memnew_placement(&elems[i], T);
  248. }
  249. }
  250. *_get_size() = p_size;
  251. } else if (p_size < current_size) {
  252. if (!__has_trivial_destructor(T)) {
  253. // deinitialize no longer needed elements
  254. for (uint32_t i = p_size; i < *_get_size(); i++) {
  255. T *t = &_get_data()[i];
  256. t->~T();
  257. }
  258. }
  259. if (alloc_size != current_alloc_size) {
  260. uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size);
  261. ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
  262. new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); // refcount
  263. _ptr = (T *)(_ptrnew);
  264. }
  265. *_get_size() = p_size;
  266. }
  267. return OK;
  268. }
  269. template <class T>
  270. int CowData<T>::find(const T &p_val, int p_from) const {
  271. int ret = -1;
  272. if (p_from < 0 || size() == 0) {
  273. return ret;
  274. }
  275. for (int i = p_from; i < size(); i++) {
  276. if (get(i) == p_val) {
  277. ret = i;
  278. break;
  279. }
  280. }
  281. return ret;
  282. }
  283. template <class T>
  284. void CowData<T>::_ref(const CowData *p_from) {
  285. _ref(*p_from);
  286. }
  287. template <class T>
  288. void CowData<T>::_ref(const CowData &p_from) {
  289. if (_ptr == p_from._ptr) {
  290. return; // self assign, do nothing.
  291. }
  292. _unref(_ptr);
  293. _ptr = nullptr;
  294. if (!p_from._ptr) {
  295. return; // nothing to do
  296. }
  297. if (p_from._get_refcount()->conditional_increment() > 0) { // could reference
  298. _ptr = p_from._ptr;
  299. }
  300. }
  301. template <class T>
  302. CowData<T>::~CowData() {
  303. _unref(_ptr);
  304. }
  305. #if defined(__GNUC__) && !defined(__clang__)
  306. #pragma GCC diagnostic pop
  307. #endif
  308. } // namespace godot
  309. #endif // GODOT_COWDATA_HPP