cowdata.h 10 KB

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