cowdata.hpp 9.7 KB

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