2
0

cowdata.h 9.4 KB

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