pool_allocator.cpp 14 KB

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