dvector.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*************************************************************************/
  2. /* dvector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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/copymem.h"
  32. #include "os/memory.h"
  33. #include "os/rw_lock.h"
  34. #include "pool_allocator.h"
  35. #include "safe_refcount.h"
  36. #include "ustring.h"
  37. struct MemoryPool {
  38. //avoid accessing these directly, must be public for template access
  39. static PoolAllocator *memory_pool;
  40. static uint8_t *pool_memory;
  41. static size_t *pool_size;
  42. struct Alloc {
  43. SafeRefCount refcount;
  44. uint32_t lock;
  45. void *mem;
  46. PoolAllocator::ID pool_id;
  47. size_t size;
  48. Alloc *free_list;
  49. Alloc() {
  50. mem = NULL;
  51. lock = 0;
  52. pool_id = POOL_ALLOCATOR_INVALID_ID;
  53. size = 0;
  54. free_list = NULL;
  55. }
  56. };
  57. static Alloc *allocs;
  58. static Alloc *free_list;
  59. static uint32_t alloc_count;
  60. static uint32_t allocs_used;
  61. static Mutex *alloc_mutex;
  62. static size_t total_memory;
  63. static size_t max_memory;
  64. static void setup(uint32_t p_max_allocs = (1 << 16));
  65. static void cleanup();
  66. };
  67. /**
  68. @author Juan Linietsky <[email protected]>
  69. */
  70. template <class T>
  71. class PoolVector {
  72. MemoryPool::Alloc *alloc;
  73. void _copy_on_write() {
  74. if (!alloc)
  75. return;
  76. // ERR_FAIL_COND(alloc->lock>0); should not be illegal to lock this for copy on write, as it's a copy on write after all
  77. if (alloc->refcount.get() == 1)
  78. return; //nothing to do
  79. //must allocate something
  80. MemoryPool::alloc_mutex->lock();
  81. if (MemoryPool::allocs_used == MemoryPool::alloc_count) {
  82. MemoryPool::alloc_mutex->unlock();
  83. ERR_EXPLAINC("All memory pool allocations are in use, can't COW.");
  84. ERR_FAIL();
  85. }
  86. MemoryPool::Alloc *old_alloc = alloc;
  87. //take one from the free list
  88. alloc = MemoryPool::free_list;
  89. MemoryPool::free_list = alloc->free_list;
  90. //increment the used counter
  91. MemoryPool::allocs_used++;
  92. //copy the alloc data
  93. alloc->size = old_alloc->size;
  94. alloc->refcount.init();
  95. alloc->pool_id = POOL_ALLOCATOR_INVALID_ID;
  96. alloc->lock = 0;
  97. #ifdef DEBUG_ENABLED
  98. MemoryPool::total_memory += alloc->size;
  99. if (MemoryPool::total_memory > MemoryPool::max_memory) {
  100. MemoryPool::max_memory = MemoryPool::total_memory;
  101. }
  102. #endif
  103. MemoryPool::alloc_mutex->unlock();
  104. if (MemoryPool::memory_pool) {
  105. } else {
  106. alloc->mem = memalloc(alloc->size);
  107. }
  108. {
  109. Write w;
  110. w._ref(alloc);
  111. Read r;
  112. r._ref(old_alloc);
  113. int cur_elements = alloc->size / sizeof(T);
  114. T *dst = (T *)w.ptr();
  115. const T *src = (const T *)r.ptr();
  116. for (int i = 0; i < cur_elements; i++) {
  117. memnew_placement(&dst[i], T(src[i]));
  118. }
  119. }
  120. if (old_alloc->refcount.unref() == true) {
  121. //this should never happen but..
  122. #ifdef DEBUG_ENABLED
  123. MemoryPool::alloc_mutex->lock();
  124. MemoryPool::total_memory -= old_alloc->size;
  125. MemoryPool::alloc_mutex->unlock();
  126. #endif
  127. {
  128. Write w;
  129. w._ref(old_alloc);
  130. int cur_elements = old_alloc->size / sizeof(T);
  131. T *elems = (T *)w.ptr();
  132. for (int i = 0; i < cur_elements; i++) {
  133. elems[i].~T();
  134. }
  135. }
  136. if (MemoryPool::memory_pool) {
  137. //resize memory pool
  138. //if none, create
  139. //if some resize
  140. } else {
  141. memfree(old_alloc->mem);
  142. old_alloc->mem = NULL;
  143. old_alloc->size = 0;
  144. MemoryPool::alloc_mutex->lock();
  145. old_alloc->free_list = MemoryPool::free_list;
  146. MemoryPool::free_list = old_alloc;
  147. MemoryPool::allocs_used--;
  148. MemoryPool::alloc_mutex->unlock();
  149. }
  150. }
  151. }
  152. void _reference(const PoolVector &p_dvector) {
  153. if (alloc == p_dvector.alloc)
  154. return;
  155. _unreference();
  156. if (!p_dvector.alloc) {
  157. return;
  158. }
  159. if (p_dvector.alloc->refcount.ref()) {
  160. alloc = p_dvector.alloc;
  161. }
  162. }
  163. void _unreference() {
  164. if (!alloc)
  165. return;
  166. if (alloc->refcount.unref() == false) {
  167. alloc = NULL;
  168. return;
  169. }
  170. //must be disposed!
  171. {
  172. int cur_elements = alloc->size / sizeof(T);
  173. Write w = write();
  174. for (int i = 0; i < cur_elements; i++) {
  175. w[i].~T();
  176. }
  177. }
  178. #ifdef DEBUG_ENABLED
  179. MemoryPool::alloc_mutex->lock();
  180. MemoryPool::total_memory -= alloc->size;
  181. MemoryPool::alloc_mutex->unlock();
  182. #endif
  183. if (MemoryPool::memory_pool) {
  184. //resize memory pool
  185. //if none, create
  186. //if some resize
  187. } else {
  188. memfree(alloc->mem);
  189. alloc->mem = NULL;
  190. alloc->size = 0;
  191. MemoryPool::alloc_mutex->lock();
  192. alloc->free_list = MemoryPool::free_list;
  193. MemoryPool::free_list = alloc;
  194. MemoryPool::allocs_used--;
  195. MemoryPool::alloc_mutex->unlock();
  196. }
  197. alloc = NULL;
  198. }
  199. public:
  200. class Access {
  201. friend class PoolVector;
  202. protected:
  203. MemoryPool::Alloc *alloc;
  204. T *mem;
  205. _FORCE_INLINE_ void _ref(MemoryPool::Alloc *p_alloc) {
  206. alloc = p_alloc;
  207. if (alloc) {
  208. if (atomic_increment(&alloc->lock) == 1) {
  209. if (MemoryPool::memory_pool) {
  210. //lock it and get mem
  211. }
  212. }
  213. mem = (T *)alloc->mem;
  214. }
  215. }
  216. _FORCE_INLINE_ void _unref() {
  217. if (alloc) {
  218. if (atomic_decrement(&alloc->lock) == 0) {
  219. if (MemoryPool::memory_pool) {
  220. //put mem back
  221. }
  222. }
  223. mem = NULL;
  224. alloc = NULL;
  225. }
  226. }
  227. Access() {
  228. alloc = NULL;
  229. mem = NULL;
  230. }
  231. public:
  232. virtual ~Access() {
  233. _unref();
  234. }
  235. };
  236. class Read : public Access {
  237. public:
  238. _FORCE_INLINE_ const T &operator[](int p_index) const { return this->mem[p_index]; }
  239. _FORCE_INLINE_ const T *ptr() const { return this->mem; }
  240. void operator=(const Read &p_read) {
  241. if (this->alloc == p_read.alloc)
  242. return;
  243. this->_unref();
  244. this->_ref(p_read.alloc);
  245. }
  246. Read(const Read &p_read) {
  247. this->_ref(p_read.alloc);
  248. }
  249. Read() {}
  250. };
  251. class Write : public Access {
  252. public:
  253. _FORCE_INLINE_ T &operator[](int p_index) const { return this->mem[p_index]; }
  254. _FORCE_INLINE_ T *ptr() const { return this->mem; }
  255. void operator=(const Write &p_read) {
  256. if (this->alloc == p_read.alloc)
  257. return;
  258. this->_unref();
  259. this->_ref(p_read.alloc);
  260. }
  261. Write(const Write &p_read) {
  262. this->_ref(p_read.alloc);
  263. }
  264. Write() {}
  265. };
  266. Read read() const {
  267. Read r;
  268. if (alloc) {
  269. r._ref(alloc);
  270. }
  271. return r;
  272. }
  273. Write write() {
  274. Write w;
  275. if (alloc) {
  276. _copy_on_write(); //make sure there is only one being acessed
  277. w._ref(alloc);
  278. }
  279. return w;
  280. }
  281. template <class MC>
  282. void fill_with(const MC &p_mc) {
  283. int c = p_mc.size();
  284. resize(c);
  285. Write w = write();
  286. int idx = 0;
  287. for (const typename MC::Element *E = p_mc.front(); E; E = E->next()) {
  288. w[idx++] = E->get();
  289. }
  290. }
  291. void remove(int p_index) {
  292. int s = size();
  293. ERR_FAIL_INDEX(p_index, s);
  294. Write w = write();
  295. for (int i = p_index; i < s - 1; i++) {
  296. w[i] = w[i + 1];
  297. };
  298. w = Write();
  299. resize(s - 1);
  300. }
  301. inline int size() const;
  302. T get(int p_index) const;
  303. void set(int p_index, const T &p_val);
  304. void push_back(const T &p_val);
  305. void append(const T &p_val) { push_back(p_val); }
  306. void append_array(const PoolVector<T> &p_arr) {
  307. int ds = p_arr.size();
  308. if (ds == 0)
  309. return;
  310. int bs = size();
  311. resize(bs + ds);
  312. Write w = write();
  313. Read r = p_arr.read();
  314. for (int i = 0; i < ds; i++)
  315. w[bs + i] = r[i];
  316. }
  317. PoolVector<T> subarray(int p_from, int p_to) {
  318. if (p_from < 0) {
  319. p_from = size() + p_from;
  320. }
  321. if (p_to < 0) {
  322. p_to = size() + p_to;
  323. }
  324. if (p_from < 0 || p_from >= size()) {
  325. PoolVector<T> &aux = *((PoolVector<T> *)0); // nullreturn
  326. ERR_FAIL_COND_V(p_from < 0 || p_from >= size(), aux)
  327. }
  328. if (p_to < 0 || p_to >= size()) {
  329. PoolVector<T> &aux = *((PoolVector<T> *)0); // nullreturn
  330. ERR_FAIL_COND_V(p_to < 0 || p_to >= size(), aux)
  331. }
  332. PoolVector<T> slice;
  333. int span = 1 + p_to - p_from;
  334. slice.resize(span);
  335. Read r = read();
  336. Write w = slice.write();
  337. for (int i = 0; i < span; ++i) {
  338. w[i] = r[p_from + i];
  339. }
  340. return slice;
  341. }
  342. Error insert(int p_pos, const T &p_val) {
  343. int s = size();
  344. ERR_FAIL_INDEX_V(p_pos, s + 1, ERR_INVALID_PARAMETER);
  345. resize(s + 1);
  346. {
  347. Write w = write();
  348. for (int i = s; i > p_pos; i--)
  349. w[i] = w[i - 1];
  350. w[p_pos] = p_val;
  351. }
  352. return OK;
  353. }
  354. String join(String delimiter) {
  355. String rs = "";
  356. int s = size();
  357. Read r = read();
  358. for (int i = 0; i < s; i++) {
  359. rs += r[i] + delimiter;
  360. }
  361. rs.erase(rs.length() - delimiter.length(), delimiter.length());
  362. return rs;
  363. }
  364. bool is_locked() const { return alloc && alloc->lock > 0; }
  365. inline const T operator[](int p_index) const;
  366. Error resize(int p_size);
  367. void invert();
  368. void operator=(const PoolVector &p_dvector) { _reference(p_dvector); }
  369. PoolVector() { alloc = NULL; }
  370. PoolVector(const PoolVector &p_dvector) {
  371. alloc = NULL;
  372. _reference(p_dvector);
  373. }
  374. ~PoolVector() { _unreference(); }
  375. };
  376. template <class T>
  377. int PoolVector<T>::size() const {
  378. return alloc ? alloc->size / sizeof(T) : 0;
  379. }
  380. template <class T>
  381. T PoolVector<T>::get(int p_index) const {
  382. return operator[](p_index);
  383. }
  384. template <class T>
  385. void PoolVector<T>::set(int p_index, const T &p_val) {
  386. if (p_index < 0 || p_index >= size()) {
  387. ERR_FAIL_COND(p_index < 0 || p_index >= size());
  388. }
  389. Write w = write();
  390. w[p_index] = p_val;
  391. }
  392. template <class T>
  393. void PoolVector<T>::push_back(const T &p_val) {
  394. resize(size() + 1);
  395. set(size() - 1, p_val);
  396. }
  397. template <class T>
  398. const T PoolVector<T>::operator[](int p_index) const {
  399. if (p_index < 0 || p_index >= size()) {
  400. T &aux = *((T *)0); //nullreturn
  401. ERR_FAIL_COND_V(p_index < 0 || p_index >= size(), aux);
  402. }
  403. Read r = read();
  404. return r[p_index];
  405. }
  406. template <class T>
  407. Error PoolVector<T>::resize(int p_size) {
  408. if (alloc == NULL) {
  409. if (p_size == 0)
  410. return OK; //nothing to do here
  411. //must allocate something
  412. MemoryPool::alloc_mutex->lock();
  413. if (MemoryPool::allocs_used == MemoryPool::alloc_count) {
  414. MemoryPool::alloc_mutex->unlock();
  415. ERR_EXPLAINC("All memory pool allocations are in use.");
  416. ERR_FAIL_V(ERR_OUT_OF_MEMORY);
  417. }
  418. //take one from the free list
  419. alloc = MemoryPool::free_list;
  420. MemoryPool::free_list = alloc->free_list;
  421. //increment the used counter
  422. MemoryPool::allocs_used++;
  423. //cleanup the alloc
  424. alloc->size = 0;
  425. alloc->refcount.init();
  426. alloc->pool_id = POOL_ALLOCATOR_INVALID_ID;
  427. MemoryPool::alloc_mutex->unlock();
  428. } else {
  429. ERR_FAIL_COND_V(alloc->lock > 0, ERR_LOCKED); //can't resize if locked!
  430. }
  431. size_t new_size = sizeof(T) * p_size;
  432. if (alloc->size == new_size)
  433. return OK; //nothing to do
  434. if (p_size == 0) {
  435. _unreference();
  436. return OK;
  437. }
  438. _copy_on_write(); // make it unique
  439. #ifdef DEBUG_ENABLED
  440. MemoryPool::alloc_mutex->lock();
  441. MemoryPool::total_memory -= alloc->size;
  442. MemoryPool::total_memory += new_size;
  443. if (MemoryPool::total_memory > MemoryPool::max_memory) {
  444. MemoryPool::max_memory = MemoryPool::total_memory;
  445. }
  446. MemoryPool::alloc_mutex->unlock();
  447. #endif
  448. int cur_elements = alloc->size / sizeof(T);
  449. if (p_size > cur_elements) {
  450. if (MemoryPool::memory_pool) {
  451. //resize memory pool
  452. //if none, create
  453. //if some resize
  454. } else {
  455. if (alloc->size == 0) {
  456. alloc->mem = memalloc(new_size);
  457. } else {
  458. alloc->mem = memrealloc(alloc->mem, new_size);
  459. }
  460. }
  461. alloc->size = new_size;
  462. Write w = write();
  463. for (int i = cur_elements; i < p_size; i++) {
  464. memnew_placement(&w[i], T);
  465. }
  466. } else {
  467. {
  468. Write w = write();
  469. for (int i = p_size; i < cur_elements; i++) {
  470. w[i].~T();
  471. }
  472. }
  473. if (MemoryPool::memory_pool) {
  474. //resize memory pool
  475. //if none, create
  476. //if some resize
  477. } else {
  478. if (new_size == 0) {
  479. memfree(alloc->mem);
  480. alloc->mem = NULL;
  481. alloc->size = 0;
  482. MemoryPool::alloc_mutex->lock();
  483. alloc->free_list = MemoryPool::free_list;
  484. MemoryPool::free_list = alloc;
  485. MemoryPool::allocs_used--;
  486. MemoryPool::alloc_mutex->unlock();
  487. } else {
  488. alloc->mem = memrealloc(alloc->mem, new_size);
  489. alloc->size = new_size;
  490. }
  491. }
  492. }
  493. return OK;
  494. }
  495. template <class T>
  496. void PoolVector<T>::invert() {
  497. T temp;
  498. Write w = write();
  499. int s = size();
  500. int half_s = s / 2;
  501. for (int i = 0; i < half_s; i++) {
  502. temp = w[i];
  503. w[i] = w[s - i - 1];
  504. w[s - i - 1] = temp;
  505. }
  506. }
  507. #endif