1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
|
/*************************************************************************/
/* pool_allocator.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "pool_allocator.h"
#include "core/error_macros.h"
#include "core/os/copymem.h"
#include "core/os/memory.h"
#include "core/os/os.h"
#include "core/print_string.h"
#include <assert.h>
#define COMPACT_CHUNK(m_entry, m_to_pos) \
do { \
void *_dst = &((unsigned char *)pool)[m_to_pos]; \
void *_src = &((unsigned char *)pool)[(m_entry).pos]; \
movemem(_dst, _src, aligned((m_entry).len)); \
(m_entry).pos = m_to_pos; \
} while (0);
void PoolAllocator::mt_lock() const {
}
void PoolAllocator::mt_unlock() const {
}
bool PoolAllocator::get_free_entry(EntryArrayPos *p_pos) {
if (entry_count == entry_max)
return false;
for (int i = 0; i < entry_max; i++) {
if (entry_array[i].len == 0) {
*p_pos = i;
return true;
}
}
ERR_PRINT("Out of memory Chunks!");
return false; //
}
/**
* Find a hole
* @param p_pos The hole is behind the block pointed by this variable upon return. if pos==entry_count, then allocate at end
* @param p_for_size hole size
* @return false if hole found, true if no hole found
*/
bool PoolAllocator::find_hole(EntryArrayPos *p_pos, int p_for_size) {
/* position where previous entry ends. Defaults to zero (begin of pool) */
int prev_entry_end_pos = 0;
for (int i = 0; i < entry_count; i++) {
Entry &entry = entry_array[entry_indices[i]];
/* determine hole size to previous entry */
int hole_size = entry.pos - prev_entry_end_pos;
/* determine if what we want fits in that hole */
if (hole_size >= p_for_size) {
*p_pos = i;
return true;
}
/* prepare for next one */
prev_entry_end_pos = entry_end(entry);
}
/* No holes between entries, check at the end..*/
if ((pool_size - prev_entry_end_pos) >= p_for_size) {
*p_pos = entry_count;
return true;
}
return false;
}
void PoolAllocator::compact(int p_up_to) {
uint32_t prev_entry_end_pos = 0;
if (p_up_to < 0)
p_up_to = entry_count;
for (int i = 0; i < p_up_to; i++) {
Entry &entry = entry_array[entry_indices[i]];
/* determine hole size to previous entry */
int hole_size = entry.pos - prev_entry_end_pos;
/* if we can compact, do it */
if (hole_size > 0 && !entry.lock) {
COMPACT_CHUNK(entry, prev_entry_end_pos);
}
/* prepare for next one */
prev_entry_end_pos = entry_end(entry);
}
}
void PoolAllocator::compact_up(int p_from) {
uint32_t next_entry_end_pos = pool_size; // - static_area_size;
for (int i = entry_count - 1; i >= p_from; i--) {
Entry &entry = entry_array[entry_indices[i]];
/* determine hole size to nextious entry */
int hole_size = next_entry_end_pos - (entry.pos + aligned(entry.len));
/* if we can compact, do it */
if (hole_size > 0 && !entry.lock) {
COMPACT_CHUNK(entry, (next_entry_end_pos - aligned(entry.len)));
}
/* prepare for next one */
next_entry_end_pos = entry.pos;
}
}
bool PoolAllocator::find_entry_index(EntryIndicesPos *p_map_pos, Entry *p_entry) {
EntryArrayPos entry_pos = entry_max;
for (int i = 0; i < entry_count; i++) {
if (&entry_array[entry_indices[i]] == p_entry) {
entry_pos = i;
break;
}
}
if (entry_pos == entry_max)
return false;
*p_map_pos = entry_pos;
return true;
}
PoolAllocator::ID PoolAllocator::alloc(int p_size) {
ERR_FAIL_COND_V(p_size < 1, POOL_ALLOCATOR_INVALID_ID);
#ifdef DEBUG_ENABLED
if (p_size > free_mem) OS::get_singleton()->debug_break();
#endif
ERR_FAIL_COND_V(p_size > free_mem, POOL_ALLOCATOR_INVALID_ID);
mt_lock();
if (entry_count == entry_max) {
mt_unlock();
ERR_PRINT("entry_count==entry_max");
return POOL_ALLOCATOR_INVALID_ID;
}
int size_to_alloc = aligned(p_size);
EntryIndicesPos new_entry_indices_pos;
if (!find_hole(&new_entry_indices_pos, size_to_alloc)) {
/* No hole could be found, try compacting mem */
compact();
/* Then search again */
if (!find_hole(&new_entry_indices_pos, size_to_alloc)) {
mt_unlock();
ERR_FAIL_V_MSG(POOL_ALLOCATOR_INVALID_ID, "Memory can't be compacted further.");
}
}
EntryArrayPos new_entry_array_pos;
bool found_free_entry = get_free_entry(&new_entry_array_pos);
if (!found_free_entry) {
mt_unlock();
ERR_FAIL_V_MSG(POOL_ALLOCATOR_INVALID_ID, "No free entry found in PoolAllocator.");
}
/* move all entry indices up, make room for this one */
for (int i = entry_count; i > new_entry_indices_pos; i--) {
entry_indices[i] = entry_indices[i - 1];
}
entry_indices[new_entry_indices_pos] = new_entry_array_pos;
entry_count++;
Entry &entry = entry_array[entry_indices[new_entry_indices_pos]];
entry.len = p_size;
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
entry.lock = 0;
entry.check = (check_count++) & CHECK_MASK;
free_mem -= size_to_alloc;
if (free_mem < free_mem_peak)
free_mem_peak = free_mem;
ID retval = (entry_indices[new_entry_indices_pos] << CHECK_BITS) | entry.check;
mt_unlock();
//ERR_FAIL_COND_V( (uintptr_t)get(retval)%align != 0, retval );
return retval;
}
PoolAllocator::Entry *PoolAllocator::get_entry(ID p_mem) {
unsigned int check = p_mem & CHECK_MASK;
int entry = p_mem >> CHECK_BITS;
ERR_FAIL_INDEX_V(entry, entry_max, NULL);
ERR_FAIL_COND_V(entry_array[entry].check != check, NULL);
ERR_FAIL_COND_V(entry_array[entry].len == 0, NULL);
return &entry_array[entry];
}
const PoolAllocator::Entry *PoolAllocator::get_entry(ID p_mem) const {
unsigned int check = p_mem & CHECK_MASK;
int entry = p_mem >> CHECK_BITS;
ERR_FAIL_INDEX_V(entry, entry_max, NULL);
ERR_FAIL_COND_V(entry_array[entry].check != check, NULL);
ERR_FAIL_COND_V(entry_array[entry].len == 0, NULL);
return &entry_array[entry];
}
void PoolAllocator::free(ID p_mem) {
mt_lock();
Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_PRINT("!e");
return;
}
if (e->lock) {
mt_unlock();
ERR_PRINT("e->lock");
return;
}
EntryIndicesPos entry_indices_pos;
bool index_found = find_entry_index(&entry_indices_pos, e);
if (!index_found) {
mt_unlock();
ERR_FAIL_COND(!index_found);
}
for (int i = entry_indices_pos; i < (entry_count - 1); i++) {
entry_indices[i] = entry_indices[i + 1];
}
entry_count--;
free_mem += aligned(e->len);
e->clear();
mt_unlock();
}
int PoolAllocator::get_size(ID p_mem) const {
int size;
mt_lock();
const Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_PRINT("!e");
return 0;
}
size = e->len;
mt_unlock();
return size;
}
Error PoolAllocator::resize(ID p_mem, int p_new_size) {
mt_lock();
Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_FAIL_COND_V(!e, ERR_INVALID_PARAMETER);
}
if (needs_locking && e->lock) {
mt_unlock();
ERR_FAIL_COND_V(e->lock, ERR_ALREADY_IN_USE);
}
uint32_t alloc_size = aligned(p_new_size);
if ((uint32_t)aligned(e->len) == alloc_size) {
e->len = p_new_size;
mt_unlock();
return OK;
} else if (e->len > (uint32_t)p_new_size) {
free_mem += aligned(e->len);
free_mem -= alloc_size;
e->len = p_new_size;
mt_unlock();
return OK;
}
//p_new_size = align(p_new_size)
int _free = free_mem; // - static_area_size;
if (uint32_t(_free + aligned(e->len)) < alloc_size) {
mt_unlock();
ERR_FAIL_V(ERR_OUT_OF_MEMORY);
};
EntryIndicesPos entry_indices_pos;
bool index_found = find_entry_index(&entry_indices_pos, e);
if (!index_found) {
mt_unlock();
ERR_FAIL_COND_V(!index_found, ERR_BUG);
}
//no need to move stuff around, it fits before the next block
uint32_t next_pos;
if (entry_indices_pos + 1 == entry_count) {
next_pos = pool_size; // - static_area_size;
} else {
next_pos = entry_array[entry_indices[entry_indices_pos + 1]].pos;
};
if ((next_pos - e->pos) > alloc_size) {
free_mem += aligned(e->len);
e->len = p_new_size;
free_mem -= alloc_size;
mt_unlock();
return OK;
}
//it doesn't fit, compact around BEFORE current index (make room behind)
compact(entry_indices_pos + 1);
if ((next_pos - e->pos) > alloc_size) {
//now fits! hooray!
free_mem += aligned(e->len);
e->len = p_new_size;
free_mem -= alloc_size;
mt_unlock();
if (free_mem < free_mem_peak)
free_mem_peak = free_mem;
return OK;
}
//STILL doesn't fit, compact around AFTER current index (make room after)
compact_up(entry_indices_pos + 1);
if ((entry_array[entry_indices[entry_indices_pos + 1]].pos - e->pos) > alloc_size) {
//now fits! hooray!
free_mem += aligned(e->len);
e->len = p_new_size;
free_mem -= alloc_size;
mt_unlock();
if (free_mem < free_mem_peak)
free_mem_peak = free_mem;
return OK;
}
mt_unlock();
ERR_FAIL_V(ERR_OUT_OF_MEMORY);
}
Error PoolAllocator::lock(ID p_mem) {
if (!needs_locking)
return OK;
mt_lock();
Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_PRINT("!e");
return ERR_INVALID_PARAMETER;
}
e->lock++;
mt_unlock();
return OK;
}
bool PoolAllocator::is_locked(ID p_mem) const {
if (!needs_locking)
return false;
mt_lock();
const Entry *e = ((PoolAllocator *)(this))->get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_PRINT("!e");
return false;
}
bool locked = e->lock;
mt_unlock();
return locked;
}
const void *PoolAllocator::get(ID p_mem) const {
if (!needs_locking) {
const Entry *e = get_entry(p_mem);
ERR_FAIL_COND_V(!e, NULL);
return &pool[e->pos];
}
mt_lock();
const Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_FAIL_COND_V(!e, NULL);
}
if (e->lock == 0) {
mt_unlock();
ERR_PRINT("e->lock == 0");
return NULL;
}
if ((int)e->pos >= pool_size) {
mt_unlock();
ERR_PRINT("e->pos<0 || e->pos>=pool_size");
return NULL;
}
const void *ptr = &pool[e->pos];
mt_unlock();
return ptr;
}
void *PoolAllocator::get(ID p_mem) {
if (!needs_locking) {
Entry *e = get_entry(p_mem);
ERR_FAIL_COND_V(!e, NULL);
return &pool[e->pos];
}
mt_lock();
Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_FAIL_COND_V(!e, NULL);
}
if (e->lock == 0) {
//assert(0);
mt_unlock();
ERR_PRINT("e->lock == 0");
return NULL;
}
if ((int)e->pos >= pool_size) {
mt_unlock();
ERR_PRINT("e->pos<0 || e->pos>=pool_size");
return NULL;
}
void *ptr = &pool[e->pos];
mt_unlock();
return ptr;
}
void PoolAllocator::unlock(ID p_mem) {
if (!needs_locking)
return;
mt_lock();
Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_FAIL_COND(!e);
}
if (e->lock == 0) {
mt_unlock();
ERR_PRINT("e->lock == 0");
return;
}
e->lock--;
mt_unlock();
}
int PoolAllocator::get_used_mem() const {
return pool_size - free_mem;
}
int PoolAllocator::get_free_peak() {
return free_mem_peak;
}
int PoolAllocator::get_free_mem() {
return free_mem;
}
void PoolAllocator::create_pool(void *p_mem, int p_size, int p_max_entries) {
pool = (uint8_t *)p_mem;
pool_size = p_size;
entry_array = memnew_arr(Entry, p_max_entries);
entry_indices = memnew_arr(int, p_max_entries);
entry_max = p_max_entries;
entry_count = 0;
free_mem = p_size;
free_mem_peak = p_size;
check_count = 0;
}
PoolAllocator::PoolAllocator(int p_size, bool p_needs_locking, int p_max_entries) {
mem_ptr = memalloc(p_size);
ERR_FAIL_COND(!mem_ptr);
align = 1;
create_pool(mem_ptr, p_size, p_max_entries);
needs_locking = p_needs_locking;
}
PoolAllocator::PoolAllocator(void *p_mem, int p_size, int p_align, bool p_needs_locking, int p_max_entries) {
if (p_align > 1) {
uint8_t *mem8 = (uint8_t *)p_mem;
uint64_t ofs = (uint64_t)mem8;
if (ofs % p_align) {
int dif = p_align - (ofs % p_align);
mem8 += p_align - (ofs % p_align);
p_size -= dif;
p_mem = (void *)mem8;
};
};
create_pool(p_mem, p_size, p_max_entries);
needs_locking = p_needs_locking;
align = p_align;
mem_ptr = NULL;
}
PoolAllocator::PoolAllocator(int p_align, int p_size, bool p_needs_locking, int p_max_entries) {
ERR_FAIL_COND(p_align < 1);
mem_ptr = Memory::alloc_static(p_size + p_align, true);
uint8_t *mem8 = (uint8_t *)mem_ptr;
uint64_t ofs = (uint64_t)mem8;
if (ofs % p_align)
mem8 += p_align - (ofs % p_align);
create_pool(mem8, p_size, p_max_entries);
needs_locking = p_needs_locking;
align = p_align;
}
PoolAllocator::~PoolAllocator() {
if (mem_ptr)
memfree(mem_ptr);
memdelete_arr(entry_array);
memdelete_arr(entry_indices);
}
|