pool_allocator.cpp 14 KB

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