cowdata.hpp 9.7 KB

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