cowdata.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 (unlikely(p_elements == 0)) {
  79. *out = 0;
  80. return true;
  81. }
  82. #if defined(__GNUC__)
  83. size_t o;
  84. size_t p;
  85. if (__builtin_mul_overflow(p_elements, sizeof(T), &o)) {
  86. *out = 0;
  87. return false;
  88. }
  89. *out = next_power_of_2(o);
  90. if (__builtin_add_overflow(o, static_cast<size_t>(32), &p)) {
  91. return false; // No longer allocated here.
  92. }
  93. #else
  94. // Speed is more important than correctness here, do the operations unchecked
  95. // and hope for the best.
  96. *out = _get_alloc_size(p_elements);
  97. #endif
  98. return *out;
  99. }
  100. void _unref(void *p_data);
  101. void _ref(const CowData *p_from);
  102. void _ref(const CowData &p_from);
  103. uint32_t _copy_on_write();
  104. public:
  105. void operator=(const CowData<T> &p_from) { _ref(p_from); }
  106. _FORCE_INLINE_ T *ptrw() {
  107. _copy_on_write();
  108. return _ptr;
  109. }
  110. _FORCE_INLINE_ const T *ptr() const {
  111. return _ptr;
  112. }
  113. _FORCE_INLINE_ int size() const {
  114. uint32_t *size = (uint32_t *)_get_size();
  115. if (size) {
  116. return *size;
  117. } else {
  118. return 0;
  119. }
  120. }
  121. _FORCE_INLINE_ void clear() { resize(0); }
  122. _FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; }
  123. _FORCE_INLINE_ void set(int p_index, const T &p_elem) {
  124. ERR_FAIL_INDEX(p_index, size());
  125. _copy_on_write();
  126. _ptr[p_index] = p_elem;
  127. }
  128. _FORCE_INLINE_ T &get_m(int p_index) {
  129. CRASH_BAD_INDEX(p_index, size());
  130. _copy_on_write();
  131. return _ptr[p_index];
  132. }
  133. _FORCE_INLINE_ const T &get(int p_index) const {
  134. CRASH_BAD_INDEX(p_index, size());
  135. return _ptr[p_index];
  136. }
  137. template <bool p_ensure_zero = false>
  138. Error resize(int p_size);
  139. _FORCE_INLINE_ void remove_at(int p_index) {
  140. ERR_FAIL_INDEX(p_index, size());
  141. T *p = ptrw();
  142. int len = size();
  143. for (int i = p_index; i < len - 1; i++) {
  144. p[i] = p[i + 1];
  145. }
  146. resize(len - 1);
  147. }
  148. Error insert(int p_pos, const T &p_val) {
  149. ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER);
  150. resize(size() + 1);
  151. for (int i = (size() - 1); i > p_pos; i--) {
  152. set(i, get(i - 1));
  153. }
  154. set(p_pos, p_val);
  155. return OK;
  156. }
  157. int find(const T &p_val, int p_from = 0) const;
  158. int rfind(const T &p_val, int p_from = -1) const;
  159. int count(const T &p_val) const;
  160. _FORCE_INLINE_ CowData() {}
  161. _FORCE_INLINE_ ~CowData();
  162. _FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); };
  163. };
  164. template <class T>
  165. void CowData<T>::_unref(void *p_data) {
  166. if (!p_data) {
  167. return;
  168. }
  169. SafeNumeric<uint32_t> *refc = _get_refcount();
  170. if (refc->decrement() > 0) {
  171. return; // still in use
  172. }
  173. // clean up
  174. if (!std::is_trivially_destructible<T>::value) {
  175. uint32_t *count = _get_size();
  176. T *data = (T *)(count + 1);
  177. for (uint32_t i = 0; i < *count; ++i) {
  178. // call destructors
  179. data[i].~T();
  180. }
  181. }
  182. // free mem
  183. Memory::free_static((uint8_t *)p_data, true);
  184. }
  185. template <class T>
  186. uint32_t CowData<T>::_copy_on_write() {
  187. if (!_ptr) {
  188. return 0;
  189. }
  190. SafeNumeric<uint32_t> *refc = _get_refcount();
  191. uint32_t rc = refc->get();
  192. if (unlikely(rc > 1)) {
  193. /* in use by more than me */
  194. uint32_t current_size = *_get_size();
  195. uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true);
  196. new (mem_new - 2) SafeNumeric<uint32_t>(1); //refcount
  197. *(mem_new - 1) = current_size; //size
  198. T *_data = (T *)(mem_new);
  199. // initialize new elements
  200. if (std::is_trivially_copyable<T>::value) {
  201. memcpy(mem_new, _ptr, current_size * sizeof(T));
  202. } else {
  203. for (uint32_t i = 0; i < current_size; i++) {
  204. memnew_placement(&_data[i], T(_ptr[i]));
  205. }
  206. }
  207. _unref(_ptr);
  208. _ptr = _data;
  209. rc = 1;
  210. }
  211. return rc;
  212. }
  213. template <class T>
  214. template <bool p_ensure_zero>
  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. for (int i = *_get_size(); i < p_size; i++) {
  251. memnew_placement(&_ptr[i], T);
  252. }
  253. } else if (p_ensure_zero) {
  254. memset((void *)(_ptr + current_size), 0, (p_size - current_size) * sizeof(T));
  255. }
  256. *_get_size() = p_size;
  257. } else if (p_size < current_size) {
  258. if (!std::is_trivially_destructible<T>::value) {
  259. // deinitialize no longer needed elements
  260. for (uint32_t i = p_size; i < *_get_size(); i++) {
  261. T *t = &_ptr[i];
  262. t->~T();
  263. }
  264. }
  265. if (alloc_size != current_alloc_size) {
  266. uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true);
  267. ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
  268. new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); //refcount
  269. _ptr = (T *)(_ptrnew);
  270. }
  271. *_get_size() = p_size;
  272. }
  273. return OK;
  274. }
  275. template <class T>
  276. int CowData<T>::find(const T &p_val, int p_from) const {
  277. int ret = -1;
  278. if (p_from < 0 || size() == 0) {
  279. return ret;
  280. }
  281. for (int i = p_from; i < size(); i++) {
  282. if (get(i) == p_val) {
  283. ret = i;
  284. break;
  285. }
  286. }
  287. return ret;
  288. }
  289. template <class T>
  290. int CowData<T>::rfind(const T &p_val, int p_from) const {
  291. const int s = size();
  292. if (p_from < 0) {
  293. p_from = s + p_from;
  294. }
  295. if (p_from < 0 || p_from >= s) {
  296. p_from = s - 1;
  297. }
  298. for (int i = p_from; i >= 0; i--) {
  299. if (get(i) == p_val) {
  300. return i;
  301. }
  302. }
  303. return -1;
  304. }
  305. template <class T>
  306. int CowData<T>::count(const T &p_val) const {
  307. int amount = 0;
  308. for (int i = 0; i < size(); i++) {
  309. if (get(i) == p_val) {
  310. amount++;
  311. }
  312. }
  313. return amount;
  314. }
  315. template <class T>
  316. void CowData<T>::_ref(const CowData *p_from) {
  317. _ref(*p_from);
  318. }
  319. template <class T>
  320. void CowData<T>::_ref(const CowData &p_from) {
  321. if (_ptr == p_from._ptr) {
  322. return; // self assign, do nothing.
  323. }
  324. _unref(_ptr);
  325. _ptr = nullptr;
  326. if (!p_from._ptr) {
  327. return; //nothing to do
  328. }
  329. if (p_from._get_refcount()->conditional_increment() > 0) { // could reference
  330. _ptr = p_from._ptr;
  331. }
  332. }
  333. template <class T>
  334. CowData<T>::~CowData() {
  335. _unref(_ptr);
  336. }
  337. #if defined(__GNUC__) && !defined(__clang__)
  338. #pragma GCC diagnostic pop
  339. #endif
  340. #endif // COWDATA_H