dvector.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*************************************************************************/
  2. /* dvector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 DVECTOR_H
  31. #define DVECTOR_H
  32. #include "os/memory.h"
  33. /**
  34. @author Juan Linietsky <reduzio@gmail.com>
  35. */
  36. extern Mutex *dvector_lock;
  37. template <class T>
  38. class DVector {
  39. mutable MID mem;
  40. void copy_on_write() {
  41. if (!mem.is_valid())
  42. return;
  43. if (dvector_lock)
  44. dvector_lock->lock();
  45. MID_Lock lock(mem);
  46. if (*(int *)lock.data() == 1) {
  47. // one reference, means no refcount changes
  48. if (dvector_lock)
  49. dvector_lock->unlock();
  50. return;
  51. }
  52. MID new_mem = dynalloc(mem.get_size());
  53. if (!new_mem.is_valid()) {
  54. if (dvector_lock)
  55. dvector_lock->unlock();
  56. ERR_FAIL_COND(new_mem.is_valid()); // out of memory
  57. }
  58. MID_Lock dst_lock(new_mem);
  59. int *rc = (int *)dst_lock.data();
  60. *rc = 1;
  61. T *dst = (T *)(rc + 1);
  62. T *src = (T *)((int *)lock.data() + 1);
  63. int count = (mem.get_size() - sizeof(int)) / sizeof(T);
  64. for (int i = 0; i < count; i++) {
  65. memnew_placement(&dst[i], T(src[i]));
  66. }
  67. (*(int *)lock.data())--;
  68. // unlock all
  69. dst_lock = MID_Lock();
  70. lock = MID_Lock();
  71. mem = new_mem;
  72. if (dvector_lock)
  73. dvector_lock->unlock();
  74. }
  75. void reference(const DVector &p_dvector) {
  76. unreference();
  77. if (dvector_lock)
  78. dvector_lock->lock();
  79. if (!p_dvector.mem.is_valid()) {
  80. if (dvector_lock)
  81. dvector_lock->unlock();
  82. return;
  83. }
  84. MID_Lock lock(p_dvector.mem);
  85. int *rc = (int *)lock.data();
  86. (*rc)++;
  87. lock = MID_Lock();
  88. mem = p_dvector.mem;
  89. if (dvector_lock)
  90. dvector_lock->unlock();
  91. }
  92. void unreference() {
  93. if (dvector_lock)
  94. dvector_lock->lock();
  95. if (!mem.is_valid()) {
  96. if (dvector_lock)
  97. dvector_lock->unlock();
  98. return;
  99. }
  100. MID_Lock lock(mem);
  101. int *rc = (int *)lock.data();
  102. (*rc)--;
  103. if (*rc == 0) {
  104. // no one else using it, destruct
  105. T *t = (T *)(rc + 1);
  106. int count = (mem.get_size() - sizeof(int)) / sizeof(T);
  107. for (int i = 0; i < count; i++) {
  108. t[i].~T();
  109. }
  110. }
  111. lock = MID_Lock();
  112. mem = MID();
  113. if (dvector_lock)
  114. dvector_lock->unlock();
  115. }
  116. public:
  117. class Read {
  118. friend class DVector;
  119. MID_Lock lock;
  120. const T *mem;
  121. public:
  122. _FORCE_INLINE_ const T &operator[](int p_index) const { return mem[p_index]; }
  123. _FORCE_INLINE_ const T *ptr() const { return mem; }
  124. Read() { mem = NULL; }
  125. };
  126. class Write {
  127. friend class DVector;
  128. MID_Lock lock;
  129. T *mem;
  130. public:
  131. _FORCE_INLINE_ T &operator[](int p_index) { return mem[p_index]; }
  132. _FORCE_INLINE_ T *ptr() { return mem; }
  133. Write() { mem = NULL; }
  134. };
  135. Read read() const {
  136. Read r;
  137. if (mem.is_valid()) {
  138. r.lock = MID_Lock(mem);
  139. r.mem = (const T *)((int *)r.lock.data() + 1);
  140. }
  141. return r;
  142. }
  143. Write write() {
  144. Write w;
  145. if (mem.is_valid()) {
  146. copy_on_write();
  147. w.lock = MID_Lock(mem);
  148. w.mem = (T *)((int *)w.lock.data() + 1);
  149. }
  150. return w;
  151. }
  152. template <class MC>
  153. void fill_with(const MC &p_mc) {
  154. int c = p_mc.size();
  155. resize(c);
  156. Write w = write();
  157. int idx = 0;
  158. for (const typename MC::Element *E = p_mc.front(); E; E = E->next()) {
  159. w[idx++] = E->get();
  160. }
  161. }
  162. void remove(int p_index) {
  163. int s = size();
  164. ERR_FAIL_INDEX(p_index, s);
  165. Write w = write();
  166. for (int i = p_index; i < s - 1; i++) {
  167. w[i] = w[i + 1];
  168. };
  169. w = Write();
  170. resize(s - 1);
  171. }
  172. inline int size() const;
  173. T get(int p_index) const;
  174. void set(int p_index, const T &p_val);
  175. void push_back(const T &p_val);
  176. void append(const T &p_val) { push_back(p_val); }
  177. void append_array(const DVector<T> &p_arr) {
  178. int ds = p_arr.size();
  179. if (ds == 0)
  180. return;
  181. int bs = size();
  182. resize(bs + ds);
  183. Write w = write();
  184. Read r = p_arr.read();
  185. for (int i = 0; i < ds; i++)
  186. w[bs + i] = r[i];
  187. }
  188. Error insert(int p_pos, const T &p_val) {
  189. int s = size();
  190. ERR_FAIL_INDEX_V(p_pos, s + 1, ERR_INVALID_PARAMETER);
  191. resize(s + 1);
  192. {
  193. Write w = write();
  194. for (int i = s; i > p_pos; i--)
  195. w[i] = w[i - 1];
  196. w[p_pos] = p_val;
  197. }
  198. return OK;
  199. }
  200. bool is_locked() const { return mem.is_locked(); }
  201. inline const T operator[](int p_index) const;
  202. Error resize(int p_size);
  203. void invert();
  204. void operator=(const DVector &p_dvector) { reference(p_dvector); }
  205. DVector() {}
  206. DVector(const DVector &p_dvector) { reference(p_dvector); }
  207. ~DVector() { unreference(); }
  208. };
  209. template <class T>
  210. int DVector<T>::size() const {
  211. return mem.is_valid() ? ((mem.get_size() - sizeof(int)) / sizeof(T)) : 0;
  212. }
  213. template <class T>
  214. T DVector<T>::get(int p_index) const {
  215. return operator[](p_index);
  216. }
  217. template <class T>
  218. void DVector<T>::set(int p_index, const T &p_val) {
  219. if (p_index < 0 || p_index >= size()) {
  220. ERR_FAIL_COND(p_index < 0 || p_index >= size());
  221. }
  222. Write w = write();
  223. w[p_index] = p_val;
  224. }
  225. template <class T>
  226. void DVector<T>::push_back(const T &p_val) {
  227. resize(size() + 1);
  228. set(size() - 1, p_val);
  229. }
  230. template <class T>
  231. const T DVector<T>::operator[](int p_index) const {
  232. PRAY_BAD_INDEX(p_index, size(), T);
  233. Read r = read();
  234. return r[p_index];
  235. }
  236. template <class T>
  237. Error DVector<T>::resize(int p_size) {
  238. if (dvector_lock)
  239. dvector_lock->lock();
  240. bool same = p_size == size();
  241. if (dvector_lock)
  242. dvector_lock->unlock();
  243. // no further locking is necesary because we are supposed to own the only copy of this (using copy on write)
  244. if (same)
  245. return OK;
  246. if (p_size == 0) {
  247. unreference();
  248. return OK;
  249. }
  250. copy_on_write(); // make it unique
  251. ERR_FAIL_COND_V(mem.is_locked(), ERR_LOCKED); // if after copy on write, memory is locked, fail.
  252. if (p_size > size()) {
  253. int oldsize = size();
  254. MID_Lock lock;
  255. if (oldsize == 0) {
  256. mem = dynalloc(p_size * sizeof(T) + sizeof(int));
  257. lock = MID_Lock(mem);
  258. int *rc = ((int *)lock.data());
  259. *rc = 1;
  260. } else {
  261. if (dynrealloc(mem, p_size * sizeof(T) + sizeof(int)) != OK) {
  262. ERR_FAIL_V(ERR_OUT_OF_MEMORY); // out of memory
  263. }
  264. lock = MID_Lock(mem);
  265. }
  266. T *t = (T *)((int *)lock.data() + 1);
  267. for (int i = oldsize; i < p_size; i++) {
  268. memnew_placement(&t[i], T);
  269. }
  270. lock = MID_Lock(); // clear
  271. } else {
  272. int oldsize = size();
  273. MID_Lock lock(mem);
  274. T *t = (T *)((int *)lock.data() + 1);
  275. for (int i = p_size; i < oldsize; i++) {
  276. t[i].~T();
  277. }
  278. lock = MID_Lock(); // clear
  279. if (dynrealloc(mem, p_size * sizeof(T) + sizeof(int)) != OK) {
  280. ERR_FAIL_V(ERR_OUT_OF_MEMORY); // wtf error
  281. }
  282. }
  283. return OK;
  284. }
  285. template <class T>
  286. void DVector<T>::invert() {
  287. T temp;
  288. Write w = write();
  289. int s = size();
  290. int half_s = s / 2;
  291. for (int i = 0; i < half_s; i++) {
  292. temp = w[i];
  293. w[i] = w[s - i - 1];
  294. w[s - i - 1] = temp;
  295. }
  296. }
  297. #endif