cowdata.h 8.7 KB

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