cowdata.h 9.3 KB

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