pool_allocator.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*************************************************************************/
  2. /* pool_allocator.cpp */
  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. #include "pool_allocator.h"
  30. #include "error_macros.h"
  31. #include "core/os/os.h"
  32. #include "os/memory.h"
  33. #include "os/copymem.h"
  34. #include "print_string.h"
  35. #include <assert.h>
  36. #define COMPACT_CHUNK( m_entry , m_to_pos ) \
  37. do { \
  38. void *_dst=&((unsigned char*)pool)[m_to_pos]; \
  39. void *_src=&((unsigned char*)pool)[(m_entry).pos]; \
  40. movemem(_dst,_src,aligned((m_entry).len)); \
  41. (m_entry).pos=m_to_pos; \
  42. } while (0);
  43. void PoolAllocator::mt_lock() const {
  44. }
  45. void PoolAllocator::mt_unlock() const {
  46. }
  47. bool PoolAllocator::get_free_entry(EntryArrayPos* p_pos) {
  48. if (entry_count==entry_max)
  49. return false;
  50. for (int i=0;i<entry_max;i++) {
  51. if (entry_array[i].len==0) {
  52. *p_pos=i;
  53. return true;
  54. }
  55. }
  56. ERR_PRINT("Out of memory Chunks!");
  57. return false; //
  58. }
  59. /**
  60. * Find a hole
  61. * @param p_pos The hole is behind the block pointed by this variable upon return. if pos==entry_count, then allocate at end
  62. * @param p_for_size hole size
  63. * @return false if hole found, true if no hole found
  64. */
  65. bool PoolAllocator::find_hole(EntryArrayPos *p_pos, int p_for_size) {
  66. /* position where previous entry ends. Defaults to zero (begin of pool) */
  67. int prev_entry_end_pos=0;
  68. for (int i=0;i<entry_count;i++) {
  69. Entry &entry=entry_array[ entry_indices[ i ] ];
  70. /* determine hole size to previous entry */
  71. int hole_size=entry.pos-prev_entry_end_pos;
  72. /* detemine if what we want fits in that hole */
  73. if (hole_size>=p_for_size) {
  74. *p_pos=i;
  75. return true;
  76. }
  77. /* prepare for next one */
  78. prev_entry_end_pos=entry_end(entry);
  79. }
  80. /* No holes between entrys, check at the end..*/
  81. if ( (pool_size-prev_entry_end_pos)>=p_for_size ) {
  82. *p_pos=entry_count;
  83. return true;
  84. }
  85. return false;
  86. }
  87. void PoolAllocator::compact(int p_up_to) {
  88. uint32_t prev_entry_end_pos=0;
  89. if (p_up_to<0)
  90. p_up_to=entry_count;
  91. for (int i=0;i<p_up_to;i++) {
  92. Entry &entry=entry_array[ entry_indices[ i ] ];
  93. /* determine hole size to previous entry */
  94. int hole_size=entry.pos-prev_entry_end_pos;
  95. /* if we can compact, do it */
  96. if (hole_size>0 && !entry.lock) {
  97. COMPACT_CHUNK(entry,prev_entry_end_pos);
  98. }
  99. /* prepare for next one */
  100. prev_entry_end_pos=entry_end(entry);
  101. }
  102. }
  103. void PoolAllocator::compact_up(int p_from) {
  104. uint32_t next_entry_end_pos=pool_size; // - static_area_size;
  105. for (int i=entry_count-1;i>=p_from;i--) {
  106. Entry &entry=entry_array[ entry_indices[ i ] ];
  107. /* determine hole size to nextious entry */
  108. int hole_size=next_entry_end_pos-(entry.pos+aligned(entry.len));
  109. /* if we can compact, do it */
  110. if (hole_size>0 && !entry.lock) {
  111. COMPACT_CHUNK(entry,(next_entry_end_pos-aligned(entry.len)));
  112. }
  113. /* prepare for next one */
  114. next_entry_end_pos=entry.pos;
  115. }
  116. }
  117. bool PoolAllocator::find_entry_index(EntryIndicesPos *p_map_pos,Entry *p_entry) {
  118. EntryArrayPos entry_pos=entry_max;
  119. for (int i=0;i<entry_count;i++) {
  120. if (&entry_array[ entry_indices[ i ] ]==p_entry) {
  121. entry_pos=i;
  122. break;
  123. }
  124. }
  125. if (entry_pos==entry_max)
  126. return false;
  127. *p_map_pos=entry_pos;
  128. return true;
  129. }
  130. PoolAllocator::ID PoolAllocator::alloc(int p_size) {
  131. ERR_FAIL_COND_V(p_size<1,POOL_ALLOCATOR_INVALID_ID);
  132. #ifdef DEBUG_ENABLED
  133. if (p_size > free_mem) OS::get_singleton()->debug_break();
  134. #endif
  135. ERR_FAIL_COND_V(p_size>free_mem,POOL_ALLOCATOR_INVALID_ID);
  136. mt_lock();
  137. if (entry_count==entry_max) {
  138. mt_unlock();
  139. ERR_PRINT("entry_count==entry_max");
  140. return POOL_ALLOCATOR_INVALID_ID;
  141. }
  142. int size_to_alloc=aligned(p_size);
  143. EntryIndicesPos new_entry_indices_pos;
  144. if (!find_hole(&new_entry_indices_pos, size_to_alloc)) {
  145. /* No hole could be found, try compacting mem */
  146. compact();
  147. /* Then search again */
  148. if (!find_hole(&new_entry_indices_pos, size_to_alloc)) {
  149. mt_unlock();
  150. ERR_PRINT("memory can't be compacted further");
  151. return POOL_ALLOCATOR_INVALID_ID;
  152. }
  153. }
  154. EntryArrayPos new_entry_array_pos;
  155. bool found_free_entry=get_free_entry(&new_entry_array_pos);
  156. if (!found_free_entry) {
  157. mt_unlock();
  158. ERR_FAIL_COND_V( !found_free_entry , POOL_ALLOCATOR_INVALID_ID );
  159. }
  160. /* move all entry indices up, make room for this one */
  161. for (int i=entry_count;i>new_entry_indices_pos;i-- ) {
  162. entry_indices[i]=entry_indices[i-1];
  163. }
  164. entry_indices[new_entry_indices_pos]=new_entry_array_pos;
  165. entry_count++;
  166. Entry &entry=entry_array[ entry_indices[ new_entry_indices_pos ] ];
  167. entry.len=p_size;
  168. entry.pos=(new_entry_indices_pos==0)?0:entry_end(entry_array[ entry_indices[ new_entry_indices_pos-1 ] ]); //alloc either at begining or end of previous
  169. entry.lock=0;
  170. entry.check=(check_count++)&CHECK_MASK;
  171. free_mem-=size_to_alloc;
  172. if (free_mem<free_mem_peak)
  173. free_mem_peak=free_mem;
  174. ID retval = (entry_indices[ new_entry_indices_pos ]<<CHECK_BITS)|entry.check;
  175. mt_unlock();
  176. //ERR_FAIL_COND_V( (uintptr_t)get(retval)%align != 0, retval );
  177. return retval;
  178. }
  179. PoolAllocator::Entry * PoolAllocator::get_entry(ID p_mem) {
  180. unsigned int check=p_mem&CHECK_MASK;
  181. int entry=p_mem>>CHECK_BITS;
  182. ERR_FAIL_INDEX_V(entry,entry_max,NULL);
  183. ERR_FAIL_COND_V(entry_array[entry].check!=check,NULL);
  184. ERR_FAIL_COND_V(entry_array[entry].len==0,NULL);
  185. return &entry_array[entry];
  186. }
  187. const PoolAllocator::Entry * PoolAllocator::get_entry(ID p_mem) const {
  188. unsigned int check=p_mem&CHECK_MASK;
  189. int entry=p_mem>>CHECK_BITS;
  190. ERR_FAIL_INDEX_V(entry,entry_max,NULL);
  191. ERR_FAIL_COND_V(entry_array[entry].check!=check,NULL);
  192. ERR_FAIL_COND_V(entry_array[entry].len==0,NULL);
  193. return &entry_array[entry];
  194. }
  195. void PoolAllocator::free(ID p_mem) {
  196. mt_lock();
  197. Entry *e=get_entry(p_mem);
  198. if (!e) {
  199. mt_unlock();
  200. ERR_PRINT("!e");
  201. return;
  202. }
  203. if (e->lock) {
  204. mt_unlock();
  205. ERR_PRINT("e->lock");
  206. return;
  207. }
  208. EntryIndicesPos entry_indices_pos;
  209. bool index_found = find_entry_index(&entry_indices_pos,e);
  210. if (!index_found) {
  211. mt_unlock();
  212. ERR_FAIL_COND(!index_found);
  213. }
  214. for (int i=entry_indices_pos;i<(entry_count-1);i++) {
  215. entry_indices[ i ] = entry_indices[ i+1 ];
  216. }
  217. entry_count--;
  218. free_mem+=aligned(e->len);
  219. e->clear();
  220. mt_unlock();
  221. }
  222. int PoolAllocator::get_size(ID p_mem) const {
  223. int size;
  224. mt_lock();
  225. const Entry *e=get_entry(p_mem);
  226. if (!e) {
  227. mt_unlock();
  228. ERR_PRINT("!e");
  229. return 0;
  230. }
  231. size=e->len;
  232. mt_unlock();
  233. return size;
  234. }
  235. Error PoolAllocator::resize(ID p_mem,int p_new_size) {
  236. mt_lock();
  237. Entry *e=get_entry(p_mem);
  238. if (!e) {
  239. mt_unlock();
  240. ERR_FAIL_COND_V(!e,ERR_INVALID_PARAMETER);
  241. }
  242. if (needs_locking && e->lock) {
  243. mt_unlock();
  244. ERR_FAIL_COND_V(e->lock,ERR_ALREADY_IN_USE);
  245. }
  246. int alloc_size = aligned(p_new_size);
  247. if (aligned(e->len)==alloc_size) {
  248. e->len=p_new_size;
  249. mt_unlock();
  250. return OK;
  251. } else if (e->len>(uint32_t)p_new_size) {
  252. free_mem += aligned(e->len);
  253. free_mem -= alloc_size;
  254. e->len=p_new_size;
  255. mt_unlock();
  256. return OK;
  257. }
  258. //p_new_size = align(p_new_size)
  259. int _total = pool_size; // - static_area_size;
  260. int _free = free_mem; // - static_area_size;
  261. if ((_free + aligned(e->len)) - alloc_size < 0) {
  262. mt_unlock();
  263. ERR_FAIL_V( ERR_OUT_OF_MEMORY );
  264. };
  265. EntryIndicesPos entry_indices_pos;
  266. bool index_found = find_entry_index(&entry_indices_pos,e);
  267. if (!index_found) {
  268. mt_unlock();
  269. ERR_FAIL_COND_V(!index_found,ERR_BUG);
  270. }
  271. //no need to move stuff around, it fits before the next block
  272. int next_pos;
  273. if (entry_indices_pos+1 == entry_count) {
  274. next_pos = pool_size; // - static_area_size;
  275. } else {
  276. next_pos = entry_array[entry_indices[entry_indices_pos+1]].pos;
  277. };
  278. if ((next_pos - e->pos) > alloc_size) {
  279. free_mem+=aligned(e->len);
  280. e->len=p_new_size;
  281. free_mem-=alloc_size;
  282. mt_unlock();
  283. return OK;
  284. }
  285. //it doesn't fit, compact around BEFORE current index (make room behind)
  286. compact(entry_indices_pos+1);
  287. if ((next_pos - e->pos) > alloc_size) {
  288. //now fits! hooray!
  289. free_mem+=aligned(e->len);
  290. e->len=p_new_size;
  291. free_mem-=alloc_size;
  292. mt_unlock();
  293. if (free_mem<free_mem_peak)
  294. free_mem_peak=free_mem;
  295. return OK;
  296. }
  297. //STILL doesn't fit, compact around AFTER current index (make room after)
  298. compact_up(entry_indices_pos+1);
  299. if ((entry_array[entry_indices[entry_indices_pos+1]].pos - e->pos) > alloc_size) {
  300. //now fits! hooray!
  301. free_mem+=aligned(e->len);
  302. e->len=p_new_size;
  303. free_mem-=alloc_size;
  304. mt_unlock();
  305. if (free_mem<free_mem_peak)
  306. free_mem_peak=free_mem;
  307. return OK;
  308. }
  309. mt_unlock();
  310. ERR_FAIL_V(ERR_OUT_OF_MEMORY);
  311. }
  312. Error PoolAllocator::lock(ID p_mem) {
  313. if (!needs_locking)
  314. return OK;
  315. mt_lock();
  316. Entry *e=get_entry(p_mem);
  317. if (!e) {
  318. mt_unlock();
  319. ERR_PRINT("!e");
  320. return ERR_INVALID_PARAMETER;
  321. }
  322. e->lock++;
  323. mt_unlock();
  324. return OK;
  325. }
  326. bool PoolAllocator::is_locked(ID p_mem) const {
  327. if (!needs_locking)
  328. return false;
  329. mt_lock();
  330. const Entry *e=((PoolAllocator*)(this))->get_entry(p_mem);
  331. if (!e) {
  332. mt_unlock();
  333. ERR_PRINT("!e");
  334. return false;
  335. }
  336. bool locked = e->lock;
  337. mt_unlock();
  338. return locked;
  339. }
  340. const void *PoolAllocator::get(ID p_mem) const {
  341. if (!needs_locking) {
  342. const Entry *e=get_entry(p_mem);
  343. ERR_FAIL_COND_V(!e,NULL);
  344. return &pool[e->pos];
  345. }
  346. mt_lock();
  347. const Entry *e=get_entry(p_mem);
  348. if (!e) {
  349. mt_unlock();
  350. ERR_FAIL_COND_V(!e,NULL);
  351. }
  352. if (e->lock==0) {
  353. mt_unlock();
  354. ERR_PRINT( "e->lock == 0" );
  355. return NULL;
  356. }
  357. if (e->pos<0 || (int)e->pos>=pool_size) {
  358. mt_unlock();
  359. ERR_PRINT("e->pos<0 || e->pos>=pool_size");
  360. return NULL;
  361. }
  362. const void *ptr=&pool[e->pos];
  363. mt_unlock();
  364. return ptr;
  365. }
  366. void *PoolAllocator::get(ID p_mem) {
  367. if (!needs_locking) {
  368. Entry *e=get_entry(p_mem);
  369. if (!e) {
  370. ERR_FAIL_COND_V(!e,NULL);
  371. };
  372. return &pool[e->pos];
  373. }
  374. mt_lock();
  375. Entry *e=get_entry(p_mem);
  376. if (!e) {
  377. mt_unlock();
  378. ERR_FAIL_COND_V(!e,NULL);
  379. }
  380. if (e->lock==0) {
  381. //assert(0);
  382. mt_unlock();
  383. ERR_PRINT( "e->lock == 0" );
  384. return NULL;
  385. }
  386. if (e->pos<0 || (int)e->pos>=pool_size) {
  387. mt_unlock();
  388. ERR_PRINT("e->pos<0 || e->pos>=pool_size");
  389. return NULL;
  390. }
  391. void *ptr=&pool[e->pos];
  392. mt_unlock();
  393. return ptr;
  394. }
  395. void PoolAllocator::unlock(ID p_mem) {
  396. if (!needs_locking)
  397. return;
  398. mt_lock();
  399. Entry *e=get_entry(p_mem);
  400. if (e->lock == 0 ) {
  401. mt_unlock();
  402. ERR_PRINT( "e->lock == 0" );
  403. return;
  404. }
  405. e->lock--;
  406. mt_unlock();
  407. }
  408. int PoolAllocator::get_used_mem() const {
  409. return pool_size-free_mem;
  410. }
  411. int PoolAllocator::get_free_peak() {
  412. return free_mem_peak;
  413. }
  414. int PoolAllocator::get_free_mem() {
  415. return free_mem;
  416. }
  417. void PoolAllocator::create_pool(void * p_mem,int p_size,int p_max_entries) {
  418. pool=(uint8_t*)p_mem;
  419. pool_size=p_size;
  420. entry_array = memnew_arr( Entry, p_max_entries );
  421. entry_indices = memnew_arr( int, p_max_entries );
  422. entry_max = p_max_entries;
  423. entry_count=0;
  424. free_mem=p_size;
  425. free_mem_peak=p_size;
  426. check_count=0;
  427. }
  428. PoolAllocator::PoolAllocator(int p_size,bool p_needs_locking,int p_max_entries) {
  429. mem_ptr=Memory::alloc_static( p_size,"PoolAllocator()");
  430. ERR_FAIL_COND(!mem_ptr);
  431. align=1;
  432. create_pool(mem_ptr,p_size,p_max_entries);
  433. needs_locking=p_needs_locking;
  434. }
  435. PoolAllocator::PoolAllocator(void * p_mem,int p_size, int p_align ,bool p_needs_locking,int p_max_entries) {
  436. if (p_align > 1) {
  437. uint8_t *mem8=(uint8_t*)p_mem;
  438. uint64_t ofs = (uint64_t)mem8;
  439. if (ofs%p_align) {
  440. int dif = p_align-(ofs%p_align);
  441. mem8+=p_align-(ofs%p_align);
  442. p_size -= dif;
  443. p_mem = (void*)mem8;
  444. };
  445. };
  446. create_pool( p_mem,p_size,p_max_entries);
  447. needs_locking=p_needs_locking;
  448. align=p_align;
  449. mem_ptr=NULL;
  450. }
  451. PoolAllocator::PoolAllocator(int p_align,int p_size,bool p_needs_locking,int p_max_entries) {
  452. ERR_FAIL_COND(p_align<1);
  453. mem_ptr=Memory::alloc_static( p_size+p_align,"PoolAllocator()");
  454. uint8_t *mem8=(uint8_t*)mem_ptr;
  455. uint64_t ofs = (uint64_t)mem8;
  456. if (ofs%p_align)
  457. mem8+=p_align-(ofs%p_align);
  458. create_pool( mem8 ,p_size,p_max_entries);
  459. needs_locking=p_needs_locking;
  460. align=p_align;
  461. }
  462. PoolAllocator::~PoolAllocator() {
  463. if (mem_ptr)
  464. Memory::free_static( mem_ptr );
  465. memdelete_arr( entry_array );
  466. memdelete_arr( entry_indices );
  467. }