dvector.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*************************************************************************/
  2. /* dvector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef DVECTOR_H
  30. #define DVECTOR_H
  31. #include "os/memory.h"
  32. /**
  33. @author Juan Linietsky <reduzio@gmail.com>
  34. */
  35. extern Mutex* dvector_lock;
  36. template<class T>
  37. class DVector {
  38. mutable MID mem;
  39. void copy_on_write() {
  40. if (!mem.is_valid())
  41. return;
  42. if (dvector_lock)
  43. dvector_lock->lock();
  44. MID_Lock lock( mem );
  45. if ( *(int*)lock.data() == 1 ) {
  46. // one reference, means no refcount changes
  47. if (dvector_lock)
  48. dvector_lock->unlock();
  49. return;
  50. }
  51. MID new_mem= dynalloc( mem.get_size() );
  52. if (!new_mem.is_valid()) {
  53. if (dvector_lock)
  54. dvector_lock->unlock();
  55. ERR_FAIL_COND( new_mem.is_valid() ); // out of memory
  56. }
  57. MID_Lock dst_lock( new_mem );
  58. int *rc = (int*)dst_lock.data();
  59. *rc=1;
  60. T * dst = (T*)(rc + 1 );
  61. T * src =(T*) ((int*)lock.data() + 1 );
  62. int count = (mem.get_size() - sizeof(int)) / sizeof(T);
  63. for (int i=0;i<count;i++) {
  64. memnew_placement( &dst[i], T(src[i]) );
  65. }
  66. (*(int*)lock.data())--;
  67. // unlock all
  68. dst_lock=MID_Lock();
  69. lock=MID_Lock();
  70. mem=new_mem;
  71. if (dvector_lock)
  72. dvector_lock->unlock();
  73. }
  74. void reference( const DVector& p_dvector ) {
  75. unreference();
  76. if (dvector_lock)
  77. dvector_lock->lock();
  78. if (!p_dvector.mem.is_valid()) {
  79. if (dvector_lock)
  80. dvector_lock->unlock();
  81. return;
  82. }
  83. MID_Lock lock(p_dvector.mem);
  84. int * rc = (int*)lock.data();
  85. (*rc)++;
  86. lock = MID_Lock();
  87. mem=p_dvector.mem;
  88. if (dvector_lock)
  89. dvector_lock->unlock();
  90. }
  91. void unreference() {
  92. if (dvector_lock)
  93. dvector_lock->lock();
  94. if (!mem.is_valid()) {
  95. if (dvector_lock)
  96. dvector_lock->unlock();
  97. return;
  98. }
  99. MID_Lock lock(mem);
  100. int * rc = (int*)lock.data();
  101. (*rc)--;
  102. if (*rc==0) {
  103. // no one else using it, destruct
  104. T * t= (T*)(rc+1);
  105. int count = (mem.get_size() - sizeof(int)) / sizeof(T);
  106. for (int i=0;i<count;i++) {
  107. t[i].~T();
  108. }
  109. }
  110. lock = MID_Lock();
  111. mem = MID ();
  112. if (dvector_lock)
  113. dvector_lock->unlock();
  114. }
  115. public:
  116. class Read {
  117. friend class DVector;
  118. MID_Lock lock;
  119. const T * mem;
  120. public:
  121. _FORCE_INLINE_ const T& operator[](int p_index) const { return mem[p_index]; }
  122. _FORCE_INLINE_ const T *ptr() const { return mem; }
  123. Read() { mem=NULL; }
  124. };
  125. class Write {
  126. friend class DVector;
  127. MID_Lock lock;
  128. T * mem;
  129. public:
  130. _FORCE_INLINE_ T& operator[](int p_index) { return mem[p_index]; }
  131. _FORCE_INLINE_ T *ptr() { return mem; }
  132. Write() { mem=NULL; }
  133. };
  134. Read read() const {
  135. Read r;
  136. if (mem.is_valid()) {
  137. r.lock = MID_Lock( mem );
  138. r.mem = (const T*)((int*)r.lock.data()+1);
  139. }
  140. return r;
  141. }
  142. Write write() {
  143. Write w;
  144. if (mem.is_valid()) {
  145. copy_on_write();
  146. w.lock = MID_Lock( mem );
  147. w.mem = (T*)((int*)w.lock.data()+1);
  148. }
  149. return w;
  150. }
  151. template<class MC>
  152. void fill_with(const MC& p_mc) {
  153. int c=p_mc.size();
  154. resize(c);
  155. Write w=write();
  156. int idx=0;
  157. for(const typename MC::Element *E=p_mc.front();E;E=E->next()) {
  158. w[idx++]=E->get();
  159. }
  160. }
  161. void remove(int p_index) {
  162. int s = size();
  163. ERR_FAIL_INDEX(p_index, s);
  164. Write w = write();
  165. for (int i=p_index; i<s-1; i++) {
  166. w[i]=w[i+1];
  167. };
  168. w = Write();
  169. resize(s-1);
  170. }
  171. inline int size() const;
  172. T get(int p_index) const;
  173. void set(int p_index, const T& p_val);
  174. void push_back(const T& p_val);
  175. void append(const T& p_val) { push_back(p_val); }
  176. void append_array(const DVector<T>& p_arr) {
  177. int ds = p_arr.size();
  178. if (ds==0)
  179. return;
  180. int bs = size();
  181. resize( bs + ds);
  182. Write w = write();
  183. Read r = p_arr.read();
  184. for(int i=0;i<ds;i++)
  185. w[bs+i]=r[i];
  186. }
  187. Error insert(int p_pos,const T& p_val) {
  188. int s=size();
  189. ERR_FAIL_INDEX_V(p_pos,s+1,ERR_INVALID_PARAMETER);
  190. resize(s+1);
  191. {
  192. Write w = write();
  193. for (int i=s;i>p_pos;i--)
  194. w[i]=w[i-1];
  195. w[p_pos]=p_val;
  196. }
  197. return OK;
  198. }
  199. bool is_locked() const { return mem.is_locked(); }
  200. inline const T operator[](int p_index) const;
  201. Error resize(int p_size);
  202. void operator=(const DVector& p_dvector) { reference(p_dvector); }
  203. DVector() {}
  204. DVector(const DVector& p_dvector) { reference(p_dvector); }
  205. ~DVector() { unreference(); }
  206. };
  207. template<class T>
  208. int DVector<T>::size() const {
  209. return mem.is_valid() ? ((mem.get_size() - sizeof(int)) / sizeof(T) ) : 0;
  210. }
  211. template<class T>
  212. T DVector<T>::get(int p_index) const {
  213. return operator[](p_index);
  214. }
  215. template<class T>
  216. void DVector<T>::set(int p_index, const T& p_val) {
  217. if (p_index<0 || p_index>=size()) {
  218. ERR_FAIL_COND(p_index<0 || p_index>=size());
  219. }
  220. Write w = write();
  221. w[p_index]=p_val;
  222. }
  223. template<class T>
  224. void DVector<T>::push_back(const T& p_val) {
  225. resize( size() + 1 );
  226. set( size() -1, p_val );
  227. }
  228. template<class T>
  229. const T DVector<T>::operator[](int p_index) const {
  230. if (p_index<0 || p_index>=size()) {
  231. T& aux=*((T*)0); //nullreturn
  232. ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux);
  233. }
  234. Read r = read();
  235. return r[p_index];
  236. }
  237. template<class T>
  238. Error DVector<T>::resize(int p_size) {
  239. if (dvector_lock)
  240. dvector_lock->lock();
  241. bool same = p_size==size();
  242. if (dvector_lock)
  243. dvector_lock->unlock();
  244. // no further locking is necesary because we are supposed to own the only copy of this (using copy on write)
  245. if (same)
  246. return OK;
  247. if (p_size == 0 ) {
  248. unreference();
  249. return OK;
  250. }
  251. copy_on_write(); // make it unique
  252. ERR_FAIL_COND_V( mem.is_locked(), ERR_LOCKED ); // if after copy on write, memory is locked, fail.
  253. if (p_size > size() ) {
  254. int oldsize=size();
  255. MID_Lock lock;
  256. if (oldsize==0) {
  257. mem = dynalloc( p_size * sizeof(T) + sizeof(int) );
  258. lock=MID_Lock(mem);
  259. int *rc = ((int*)lock.data());
  260. *rc=1;
  261. } else {
  262. if (dynrealloc( mem, p_size * sizeof(T) + sizeof(int) )!=OK ) {
  263. ERR_FAIL_V(ERR_OUT_OF_MEMORY); // out of memory
  264. }
  265. lock=MID_Lock(mem);
  266. }
  267. T *t = (T*)((int*)lock.data() + 1);
  268. for (int i=oldsize;i<p_size;i++) {
  269. memnew_placement(&t[i], T );
  270. }
  271. lock = MID_Lock(); // clear
  272. } else {
  273. int oldsize=size();
  274. MID_Lock lock(mem);
  275. T *t = (T*)((int*)lock.data() + 1);
  276. for (int i=p_size;i<oldsize;i++) {
  277. t[i].~T();
  278. }
  279. lock = MID_Lock(); // clear
  280. if (dynrealloc( mem, p_size * sizeof(T) + sizeof(int) )!=OK ) {
  281. ERR_FAIL_V(ERR_OUT_OF_MEMORY); // wtf error
  282. }
  283. }
  284. return OK;
  285. }
  286. #endif