pool_allocator.cpp 14 KB

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