cowdata.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint64_t)
  48. // Silence a false positive warning (see GH-52119).
  49. #if defined(__GNUC__) && !defined(__clang__)
  50. #pragma GCC diagnostic push
  51. #pragma GCC diagnostic ignored "-Wplacement-new"
  52. #endif
  53. template <class T>
  54. class CowData {
  55. template <class TV>
  56. friend class Vector;
  57. template <class TV, class VV>
  58. friend class VMap;
  59. template <class TS>
  60. friend class CharStringT;
  61. public:
  62. typedef int64_t Size;
  63. typedef uint64_t USize;
  64. static constexpr USize MAX_INT = INT64_MAX;
  65. private:
  66. // Function to find the next power of 2 to an integer.
  67. static _FORCE_INLINE_ USize next_po2(USize x) {
  68. if (x == 0) {
  69. return 0;
  70. }
  71. --x;
  72. x |= x >> 1;
  73. x |= x >> 2;
  74. x |= x >> 4;
  75. x |= x >> 8;
  76. x |= x >> 16;
  77. if (sizeof(USize) == 8) {
  78. x |= x >> 32;
  79. }
  80. return ++x;
  81. }
  82. static constexpr USize ALLOC_PAD = sizeof(USize) * 2; // For size and atomic refcount.
  83. mutable T *_ptr = nullptr;
  84. // internal helpers
  85. _FORCE_INLINE_ SafeNumeric<USize> *_get_refcount() const {
  86. if (!_ptr) {
  87. return nullptr;
  88. }
  89. return reinterpret_cast<SafeNumeric<USize> *>(_ptr) - 2;
  90. }
  91. _FORCE_INLINE_ USize *_get_size() const {
  92. if (!_ptr) {
  93. return nullptr;
  94. }
  95. return reinterpret_cast<USize *>(_ptr) - 1;
  96. }
  97. _FORCE_INLINE_ USize _get_alloc_size(USize p_elements) const {
  98. return next_po2(p_elements * sizeof(T));
  99. }
  100. _FORCE_INLINE_ bool _get_alloc_size_checked(USize p_elements, USize *out) const {
  101. if (unlikely(p_elements == 0)) {
  102. *out = 0;
  103. return true;
  104. }
  105. #if defined(__GNUC__) && defined(IS_32_BIT)
  106. USize o;
  107. USize p;
  108. if (__builtin_mul_overflow(p_elements, sizeof(T), &o)) {
  109. *out = 0;
  110. return false;
  111. }
  112. *out = next_po2(o);
  113. if (__builtin_add_overflow(o, static_cast<USize>(32), &p)) {
  114. return false; // No longer allocated here.
  115. }
  116. #else
  117. // Speed is more important than correctness here, do the operations unchecked
  118. // and hope for the best.
  119. *out = _get_alloc_size(p_elements);
  120. #endif
  121. return *out;
  122. }
  123. void _unref(void *p_data);
  124. void _ref(const CowData *p_from);
  125. void _ref(const CowData &p_from);
  126. USize _copy_on_write();
  127. public:
  128. void operator=(const CowData<T> &p_from) { _ref(p_from); }
  129. _FORCE_INLINE_ T *ptrw() {
  130. _copy_on_write();
  131. return _ptr;
  132. }
  133. _FORCE_INLINE_ const T *ptr() const {
  134. return _ptr;
  135. }
  136. _FORCE_INLINE_ Size size() const {
  137. USize *size = (USize *)_get_size();
  138. if (size) {
  139. return *size;
  140. } else {
  141. return 0;
  142. }
  143. }
  144. _FORCE_INLINE_ void clear() { resize(0); }
  145. _FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; }
  146. _FORCE_INLINE_ void set(Size p_index, const T &p_elem) {
  147. ERR_FAIL_INDEX(p_index, size());
  148. _copy_on_write();
  149. _ptr[p_index] = p_elem;
  150. }
  151. _FORCE_INLINE_ T &get_m(Size p_index) {
  152. CRASH_BAD_INDEX(p_index, size());
  153. _copy_on_write();
  154. return _ptr[p_index];
  155. }
  156. _FORCE_INLINE_ const T &get(Size p_index) const {
  157. CRASH_BAD_INDEX(p_index, size());
  158. return _ptr[p_index];
  159. }
  160. template <bool p_ensure_zero = false>
  161. Error resize(Size p_size);
  162. _FORCE_INLINE_ void remove_at(Size p_index) {
  163. ERR_FAIL_INDEX(p_index, size());
  164. T *p = ptrw();
  165. Size len = size();
  166. for (Size i = p_index; i < len - 1; i++) {
  167. p[i] = p[i + 1];
  168. }
  169. resize(len - 1);
  170. }
  171. Error insert(Size p_pos, const T &p_val) {
  172. ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER);
  173. resize(size() + 1);
  174. for (Size i = (size() - 1); i > p_pos; i--) {
  175. set(i, get(i - 1));
  176. }
  177. set(p_pos, p_val);
  178. return OK;
  179. }
  180. Size find(const T &p_val, Size p_from = 0) const;
  181. Size rfind(const T &p_val, Size p_from = -1) const;
  182. Size count(const T &p_val) const;
  183. _FORCE_INLINE_ CowData() {}
  184. _FORCE_INLINE_ ~CowData();
  185. _FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); };
  186. };
  187. template <class T>
  188. void CowData<T>::_unref(void *p_data) {
  189. if (!p_data) {
  190. return;
  191. }
  192. SafeNumeric<USize> *refc = _get_refcount();
  193. if (refc->decrement() > 0) {
  194. return; // still in use
  195. }
  196. // clean up
  197. if (!std::is_trivially_destructible<T>::value) {
  198. USize *count = _get_size();
  199. T *data = (T *)(count + 1);
  200. for (USize i = 0; i < *count; ++i) {
  201. // call destructors
  202. data[i].~T();
  203. }
  204. }
  205. // free mem
  206. Memory::free_static(((uint8_t *)p_data) - ALLOC_PAD, false);
  207. }
  208. template <class T>
  209. typename CowData<T>::USize CowData<T>::_copy_on_write() {
  210. if (!_ptr) {
  211. return 0;
  212. }
  213. SafeNumeric<USize> *refc = _get_refcount();
  214. USize rc = refc->get();
  215. if (unlikely(rc > 1)) {
  216. /* in use by more than me */
  217. USize current_size = *_get_size();
  218. USize *mem_new = (USize *)Memory::alloc_static(_get_alloc_size(current_size) + ALLOC_PAD, false);
  219. mem_new += 2;
  220. new (mem_new - 2) SafeNumeric<USize>(1); //refcount
  221. *(mem_new - 1) = current_size; //size
  222. T *_data = (T *)(mem_new);
  223. // initialize new elements
  224. if (std::is_trivially_copyable<T>::value) {
  225. memcpy(mem_new, _ptr, current_size * sizeof(T));
  226. } else {
  227. for (USize i = 0; i < current_size; i++) {
  228. memnew_placement(&_data[i], T(_ptr[i]));
  229. }
  230. }
  231. _unref(_ptr);
  232. _ptr = _data;
  233. rc = 1;
  234. }
  235. return rc;
  236. }
  237. template <class T>
  238. template <bool p_ensure_zero>
  239. Error CowData<T>::resize(Size p_size) {
  240. ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
  241. Size current_size = size();
  242. if (p_size == current_size) {
  243. return OK;
  244. }
  245. if (p_size == 0) {
  246. // wants to clean up
  247. _unref(_ptr);
  248. _ptr = nullptr;
  249. return OK;
  250. }
  251. // possibly changing size, copy on write
  252. USize rc = _copy_on_write();
  253. USize current_alloc_size = _get_alloc_size(current_size);
  254. USize alloc_size;
  255. ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
  256. if (p_size > current_size) {
  257. if (alloc_size != current_alloc_size) {
  258. if (current_size == 0) {
  259. // alloc from scratch
  260. USize *ptr = (USize *)Memory::alloc_static(alloc_size + ALLOC_PAD, false);
  261. ptr += 2;
  262. ERR_FAIL_NULL_V(ptr, ERR_OUT_OF_MEMORY);
  263. *(ptr - 1) = 0; //size, currently none
  264. new (ptr - 2) SafeNumeric<USize>(1); //refcount
  265. _ptr = (T *)ptr;
  266. } else {
  267. USize *_ptrnew = (USize *)Memory::realloc_static(((uint8_t *)_ptr) - ALLOC_PAD, alloc_size + ALLOC_PAD, false);
  268. ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY);
  269. _ptrnew += 2;
  270. new (_ptrnew - 2) SafeNumeric<USize>(rc); //refcount
  271. _ptr = (T *)(_ptrnew);
  272. }
  273. }
  274. // construct the newly created elements
  275. if (!std::is_trivially_constructible<T>::value) {
  276. for (Size i = *_get_size(); i < p_size; i++) {
  277. memnew_placement(&_ptr[i], T);
  278. }
  279. } else if (p_ensure_zero) {
  280. memset((void *)(_ptr + current_size), 0, (p_size - current_size) * sizeof(T));
  281. }
  282. *_get_size() = p_size;
  283. } else if (p_size < current_size) {
  284. if (!std::is_trivially_destructible<T>::value) {
  285. // deinitialize no longer needed elements
  286. for (USize i = p_size; i < *_get_size(); i++) {
  287. T *t = &_ptr[i];
  288. t->~T();
  289. }
  290. }
  291. if (alloc_size != current_alloc_size) {
  292. USize *_ptrnew = (USize *)Memory::realloc_static(((uint8_t *)_ptr) - ALLOC_PAD, alloc_size + ALLOC_PAD, false);
  293. ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY);
  294. _ptrnew += 2;
  295. new (_ptrnew - 2) SafeNumeric<USize>(rc); //refcount
  296. _ptr = (T *)(_ptrnew);
  297. }
  298. *_get_size() = p_size;
  299. }
  300. return OK;
  301. }
  302. template <class T>
  303. typename CowData<T>::Size CowData<T>::find(const T &p_val, Size p_from) const {
  304. Size ret = -1;
  305. if (p_from < 0 || size() == 0) {
  306. return ret;
  307. }
  308. for (Size i = p_from; i < size(); i++) {
  309. if (get(i) == p_val) {
  310. ret = i;
  311. break;
  312. }
  313. }
  314. return ret;
  315. }
  316. template <class T>
  317. typename CowData<T>::Size CowData<T>::rfind(const T &p_val, Size p_from) const {
  318. const Size s = size();
  319. if (p_from < 0) {
  320. p_from = s + p_from;
  321. }
  322. if (p_from < 0 || p_from >= s) {
  323. p_from = s - 1;
  324. }
  325. for (Size i = p_from; i >= 0; i--) {
  326. if (get(i) == p_val) {
  327. return i;
  328. }
  329. }
  330. return -1;
  331. }
  332. template <class T>
  333. typename CowData<T>::Size CowData<T>::count(const T &p_val) const {
  334. Size amount = 0;
  335. for (Size i = 0; i < size(); i++) {
  336. if (get(i) == p_val) {
  337. amount++;
  338. }
  339. }
  340. return amount;
  341. }
  342. template <class T>
  343. void CowData<T>::_ref(const CowData *p_from) {
  344. _ref(*p_from);
  345. }
  346. template <class T>
  347. void CowData<T>::_ref(const CowData &p_from) {
  348. if (_ptr == p_from._ptr) {
  349. return; // self assign, do nothing.
  350. }
  351. _unref(_ptr);
  352. _ptr = nullptr;
  353. if (!p_from._ptr) {
  354. return; // nothing to do
  355. }
  356. if (p_from._get_refcount()->conditional_increment() > 0) { // could reference
  357. _ptr = p_from._ptr;
  358. }
  359. }
  360. template <class T>
  361. CowData<T>::~CowData() {
  362. _unref(_ptr);
  363. }
  364. #if defined(__GNUC__) && !defined(__clang__)
  365. #pragma GCC diagnostic pop
  366. #endif
  367. } // namespace godot
  368. #endif // GODOT_COWDATA_HPP