cowdata.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*************************************************************************/
  2. /* cowdata.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/os/memory.h"
  33. #include "core/safe_refcount.h"
  34. template <class T>
  35. class Vector;
  36. class String;
  37. class CharString;
  38. template <class T, class V>
  39. class VMap;
  40. template <class T>
  41. class CowData {
  42. template <class TV>
  43. friend class Vector;
  44. friend class String;
  45. friend class CharString;
  46. template <class TV, class VV>
  47. friend class VMap;
  48. private:
  49. mutable T *_ptr;
  50. // internal helpers
  51. _FORCE_INLINE_ uint32_t *_get_refcount() const {
  52. if (!_ptr)
  53. return NULL;
  54. return reinterpret_cast<uint32_t *>(_ptr) - 2;
  55. }
  56. _FORCE_INLINE_ uint32_t *_get_size() const {
  57. if (!_ptr)
  58. return NULL;
  59. return reinterpret_cast<uint32_t *>(_ptr) - 1;
  60. }
  61. _FORCE_INLINE_ T *_get_data() const {
  62. if (!_ptr)
  63. return NULL;
  64. return reinterpret_cast<T *>(_ptr);
  65. }
  66. _FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const {
  67. //return nearest_power_of_2_templated(p_elements*sizeof(T)+sizeof(SafeRefCount)+sizeof(int));
  68. return next_power_of_2(p_elements * sizeof(T));
  69. }
  70. _FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const {
  71. #if defined(_add_overflow) && defined(_mul_overflow)
  72. size_t o;
  73. size_t p;
  74. if (_mul_overflow(p_elements, sizeof(T), &o)) {
  75. *out = 0;
  76. return false;
  77. }
  78. *out = next_power_of_2(o);
  79. if (_add_overflow(o, static_cast<size_t>(32), &p)) return false; //no longer allocated here
  80. return true;
  81. #else
  82. // Speed is more important than correctness here, do the operations unchecked
  83. // and hope the best
  84. *out = _get_alloc_size(p_elements);
  85. return true;
  86. #endif
  87. }
  88. void _unref(void *p_data);
  89. void _ref(const CowData *p_from);
  90. void _ref(const CowData &p_from);
  91. void _copy_on_write();
  92. public:
  93. void operator=(const CowData<T> &p_from) { _ref(p_from); }
  94. _FORCE_INLINE_ T *ptrw() {
  95. _copy_on_write();
  96. return (T *)_get_data();
  97. }
  98. _FORCE_INLINE_ const T *ptr() const {
  99. return _get_data();
  100. }
  101. _FORCE_INLINE_ int size() const {
  102. uint32_t *size = (uint32_t *)_get_size();
  103. if (size)
  104. return *size;
  105. else
  106. return 0;
  107. }
  108. _FORCE_INLINE_ void clear() { resize(0); }
  109. _FORCE_INLINE_ bool empty() const { return _ptr == 0; }
  110. _FORCE_INLINE_ void set(int p_index, const T &p_elem) {
  111. CRASH_BAD_INDEX(p_index, size());
  112. _copy_on_write();
  113. _get_data()[p_index] = p_elem;
  114. }
  115. _FORCE_INLINE_ T &get_m(int p_index) {
  116. CRASH_BAD_INDEX(p_index, size());
  117. _copy_on_write();
  118. return _get_data()[p_index];
  119. }
  120. _FORCE_INLINE_ const T &get(int p_index) const {
  121. CRASH_BAD_INDEX(p_index, size());
  122. return _get_data()[p_index];
  123. }
  124. Error resize(int p_size);
  125. _FORCE_INLINE_ void remove(int p_index) {
  126. ERR_FAIL_INDEX(p_index, size());
  127. T *p = ptrw();
  128. int len = size();
  129. for (int i = p_index; i < len - 1; i++) {
  130. p[i] = p[i + 1];
  131. };
  132. resize(len - 1);
  133. };
  134. Error insert(int p_pos, const T &p_val) {
  135. ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER);
  136. resize(size() + 1);
  137. for (int i = (size() - 1); i > p_pos; i--)
  138. set(i, get(i - 1));
  139. set(p_pos, p_val);
  140. return OK;
  141. };
  142. _FORCE_INLINE_ CowData();
  143. _FORCE_INLINE_ ~CowData();
  144. _FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); };
  145. };
  146. template <class T>
  147. void CowData<T>::_unref(void *p_data) {
  148. if (!p_data)
  149. return;
  150. uint32_t *refc = _get_refcount();
  151. if (atomic_decrement(refc) > 0)
  152. return; // still in use
  153. // clean up
  154. uint32_t *count = _get_size();
  155. T *data = (T *)(count + 1);
  156. for (uint32_t i = 0; i < *count; ++i) {
  157. // call destructors
  158. data[i].~T();
  159. }
  160. // free mem
  161. Memory::free_static((uint8_t *)p_data, true);
  162. }
  163. template <class T>
  164. void CowData<T>::_copy_on_write() {
  165. if (!_ptr)
  166. return;
  167. uint32_t *refc = _get_refcount();
  168. if (unlikely(*refc > 1)) {
  169. /* in use by more than me */
  170. uint32_t current_size = *_get_size();
  171. uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true);
  172. *(mem_new - 2) = 1; //refcount
  173. *(mem_new - 1) = current_size; //size
  174. T *_data = (T *)(mem_new);
  175. // initialize new elements
  176. for (uint32_t i = 0; i < current_size; i++) {
  177. memnew_placement(&_data[i], T(_get_data()[i]));
  178. }
  179. _unref(_ptr);
  180. _ptr = _data;
  181. }
  182. }
  183. template <class T>
  184. Error CowData<T>::resize(int p_size) {
  185. ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
  186. if (p_size == size())
  187. return OK;
  188. if (p_size == 0) {
  189. // wants to clean up
  190. _unref(_ptr);
  191. _ptr = NULL;
  192. return OK;
  193. }
  194. // possibly changing size, copy on write
  195. _copy_on_write();
  196. size_t alloc_size;
  197. ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
  198. if (p_size > size()) {
  199. if (size() == 0) {
  200. // alloc from scratch
  201. uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true);
  202. ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY);
  203. *(ptr - 1) = 0; //size, currently none
  204. *(ptr - 2) = 1; //refcount
  205. _ptr = (T *)ptr;
  206. } else {
  207. void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true);
  208. ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
  209. _ptr = (T *)(_ptrnew);
  210. }
  211. // construct the newly created elements
  212. T *elems = _get_data();
  213. for (int i = *_get_size(); i < p_size; i++) {
  214. memnew_placement(&elems[i], T);
  215. }
  216. *_get_size() = p_size;
  217. } else if (p_size < size()) {
  218. // deinitialize no longer needed elements
  219. for (uint32_t i = p_size; i < *_get_size(); i++) {
  220. T *t = &_get_data()[i];
  221. t->~T();
  222. }
  223. void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true);
  224. ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
  225. _ptr = (T *)(_ptrnew);
  226. *_get_size() = p_size;
  227. }
  228. return OK;
  229. }
  230. template <class T>
  231. void CowData<T>::_ref(const CowData *p_from) {
  232. _ref(*p_from);
  233. }
  234. template <class T>
  235. void CowData<T>::_ref(const CowData &p_from) {
  236. if (_ptr == p_from._ptr)
  237. return; // self assign, do nothing.
  238. _unref(_ptr);
  239. _ptr = NULL;
  240. if (!p_from._ptr)
  241. return; //nothing to do
  242. if (atomic_conditional_increment(p_from._get_refcount()) > 0) { // could reference
  243. _ptr = p_from._ptr;
  244. }
  245. }
  246. template <class T>
  247. CowData<T>::CowData() {
  248. _ptr = NULL;
  249. }
  250. template <class T>
  251. CowData<T>::~CowData() {
  252. _unref(_ptr);
  253. }
  254. #endif /* COW_H_ */