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 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
|
/*************************************************************************/
/* pool_vector.h */
/*************************************************************************/
/* 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. */
/*************************************************************************/
#ifndef POOL_VECTOR_H
#define POOL_VECTOR_H
#include "core/os/copymem.h"
#include "core/os/memory.h"
#include "core/os/rw_lock.h"
#include "core/pool_allocator.h"
#include "core/safe_refcount.h"
#include "core/ustring.h"
struct MemoryPool {
//avoid accessing these directly, must be public for template access
static PoolAllocator *memory_pool;
static uint8_t *pool_memory;
static size_t *pool_size;
struct Alloc {
SafeRefCount refcount;
uint32_t lock;
void *mem;
PoolAllocator::ID pool_id;
size_t size;
Alloc *free_list;
Alloc() :
lock(0),
mem(NULL),
pool_id(POOL_ALLOCATOR_INVALID_ID),
size(0),
free_list(NULL) {
}
};
static Alloc *allocs;
static Alloc *free_list;
static uint32_t alloc_count;
static uint32_t allocs_used;
static Mutex *alloc_mutex;
static size_t total_memory;
static size_t max_memory;
static void setup(uint32_t p_max_allocs = (1 << 16));
static void cleanup();
};
template <class T>
class PoolVector {
MemoryPool::Alloc *alloc;
void _copy_on_write() {
if (!alloc)
return;
// ERR_FAIL_COND(alloc->lock>0); should not be illegal to lock this for copy on write, as it's a copy on write after all
// Refcount should not be zero, otherwise it's a misuse of COW
if (alloc->refcount.get() == 1)
return; //nothing to do
//must allocate something
MemoryPool::alloc_mutex->lock();
if (MemoryPool::allocs_used == MemoryPool::alloc_count) {
MemoryPool::alloc_mutex->unlock();
ERR_FAIL_MSG("All memory pool allocations are in use, can't COW.");
}
MemoryPool::Alloc *old_alloc = alloc;
//take one from the free list
alloc = MemoryPool::free_list;
MemoryPool::free_list = alloc->free_list;
//increment the used counter
MemoryPool::allocs_used++;
//copy the alloc data
alloc->size = old_alloc->size;
alloc->refcount.init();
alloc->pool_id = POOL_ALLOCATOR_INVALID_ID;
alloc->lock = 0;
#ifdef DEBUG_ENABLED
MemoryPool::total_memory += alloc->size;
if (MemoryPool::total_memory > MemoryPool::max_memory) {
MemoryPool::max_memory = MemoryPool::total_memory;
}
#endif
MemoryPool::alloc_mutex->unlock();
if (MemoryPool::memory_pool) {
} else {
alloc->mem = memalloc(alloc->size);
}
{
Write w;
w._ref(alloc);
Read r;
r._ref(old_alloc);
int cur_elements = alloc->size / sizeof(T);
T *dst = (T *)w.ptr();
const T *src = (const T *)r.ptr();
for (int i = 0; i < cur_elements; i++) {
memnew_placement(&dst[i], T(src[i]));
}
}
if (old_alloc->refcount.unref()) {
//this should never happen but..
#ifdef DEBUG_ENABLED
MemoryPool::alloc_mutex->lock();
MemoryPool::total_memory -= old_alloc->size;
MemoryPool::alloc_mutex->unlock();
#endif
{
Write w;
w._ref(old_alloc);
int cur_elements = old_alloc->size / sizeof(T);
T *elems = (T *)w.ptr();
for (int i = 0; i < cur_elements; i++) {
elems[i].~T();
}
}
if (MemoryPool::memory_pool) {
//resize memory pool
//if none, create
//if some resize
} else {
memfree(old_alloc->mem);
old_alloc->mem = NULL;
old_alloc->size = 0;
MemoryPool::alloc_mutex->lock();
old_alloc->free_list = MemoryPool::free_list;
MemoryPool::free_list = old_alloc;
MemoryPool::allocs_used--;
MemoryPool::alloc_mutex->unlock();
}
}
}
void _reference(const PoolVector &p_pool_vector) {
if (alloc == p_pool_vector.alloc)
return;
_unreference();
if (!p_pool_vector.alloc) {
return;
}
if (p_pool_vector.alloc->refcount.ref()) {
alloc = p_pool_vector.alloc;
}
}
void _unreference() {
if (!alloc)
return;
if (!alloc->refcount.unref()) {
alloc = NULL;
return;
}
//must be disposed!
{
int cur_elements = alloc->size / sizeof(T);
// Don't use write() here because it could otherwise provoke COW,
// which is not desirable here because we are destroying the last reference anyways
Write w;
// Reference to still prevent other threads from touching the alloc
w._ref(alloc);
for (int i = 0; i < cur_elements; i++) {
w[i].~T();
}
}
#ifdef DEBUG_ENABLED
MemoryPool::alloc_mutex->lock();
MemoryPool::total_memory -= alloc->size;
MemoryPool::alloc_mutex->unlock();
#endif
if (MemoryPool::memory_pool) {
//resize memory pool
//if none, create
//if some resize
} else {
memfree(alloc->mem);
alloc->mem = NULL;
alloc->size = 0;
MemoryPool::alloc_mutex->lock();
alloc->free_list = MemoryPool::free_list;
MemoryPool::free_list = alloc;
MemoryPool::allocs_used--;
MemoryPool::alloc_mutex->unlock();
}
alloc = NULL;
}
public:
class Access {
friend class PoolVector;
protected:
MemoryPool::Alloc *alloc;
T *mem;
_FORCE_INLINE_ void _ref(MemoryPool::Alloc *p_alloc) {
alloc = p_alloc;
if (alloc) {
if (atomic_increment(&alloc->lock) == 1) {
if (MemoryPool::memory_pool) {
//lock it and get mem
}
}
mem = (T *)alloc->mem;
}
}
_FORCE_INLINE_ void _unref() {
if (alloc) {
if (atomic_decrement(&alloc->lock) == 0) {
if (MemoryPool::memory_pool) {
//put mem back
}
}
mem = NULL;
alloc = NULL;
}
}
Access() {
alloc = NULL;
mem = NULL;
}
public:
virtual ~Access() {
_unref();
}
void release() {
_unref();
}
};
class Read : public Access {
public:
_FORCE_INLINE_ const T &operator[](int p_index) const { return this->mem[p_index]; }
_FORCE_INLINE_ const T *ptr() const { return this->mem; }
void operator=(const Read &p_read) {
if (this->alloc == p_read.alloc)
return;
this->_unref();
this->_ref(p_read.alloc);
}
Read(const Read &p_read) {
this->_ref(p_read.alloc);
}
Read() {}
};
class Write : public Access {
public:
_FORCE_INLINE_ T &operator[](int p_index) const { return this->mem[p_index]; }
_FORCE_INLINE_ T *ptr() const { return this->mem; }
void operator=(const Write &p_read) {
if (this->alloc == p_read.alloc)
return;
this->_unref();
this->_ref(p_read.alloc);
}
Write(const Write &p_read) {
this->_ref(p_read.alloc);
}
Write() {}
};
Read read() const {
Read r;
if (alloc) {
r._ref(alloc);
}
return r;
}
Write write() {
Write w;
if (alloc) {
_copy_on_write(); //make sure there is only one being acessed
w._ref(alloc);
}
return w;
}
template <class MC>
void fill_with(const MC &p_mc) {
int c = p_mc.size();
resize(c);
Write w = write();
int idx = 0;
for (const typename MC::Element *E = p_mc.front(); E; E = E->next()) {
w[idx++] = E->get();
}
}
void remove(int p_index) {
int s = size();
ERR_FAIL_INDEX(p_index, s);
Write w = write();
for (int i = p_index; i < s - 1; i++) {
w[i] = w[i + 1];
};
w = Write();
resize(s - 1);
}
inline int size() const;
inline bool empty() const;
T get(int p_index) const;
void set(int p_index, const T &p_val);
void push_back(const T &p_val);
void append(const T &p_val) { push_back(p_val); }
void append_array(const PoolVector<T> &p_arr) {
int ds = p_arr.size();
if (ds == 0)
return;
int bs = size();
resize(bs + ds);
Write w = write();
Read r = p_arr.read();
for (int i = 0; i < ds; i++)
w[bs + i] = r[i];
}
PoolVector<T> subarray(int p_from, int p_to) {
if (p_from < 0) {
p_from = size() + p_from;
}
if (p_to < 0) {
p_to = size() + p_to;
}
ERR_FAIL_INDEX_V(p_from, size(), PoolVector<T>());
ERR_FAIL_INDEX_V(p_to, size(), PoolVector<T>());
PoolVector<T> slice;
int span = 1 + p_to - p_from;
slice.resize(span);
Read r = read();
Write w = slice.write();
for (int i = 0; i < span; ++i) {
w[i] = r[p_from + i];
}
return slice;
}
Error insert(int p_pos, const T &p_val) {
int s = size();
ERR_FAIL_INDEX_V(p_pos, s + 1, ERR_INVALID_PARAMETER);
resize(s + 1);
{
Write w = write();
for (int i = s; i > p_pos; i--)
w[i] = w[i - 1];
w[p_pos] = p_val;
}
return OK;
}
String join(String delimiter) {
String rs = "";
int s = size();
Read r = read();
for (int i = 0; i < s; i++) {
rs += r[i] + delimiter;
}
rs.erase(rs.length() - delimiter.length(), delimiter.length());
return rs;
}
bool is_locked() const { return alloc && alloc->lock > 0; }
inline T operator[](int p_index) const;
Error resize(int p_size);
void invert();
void operator=(const PoolVector &p_pool_vector) { _reference(p_pool_vector); }
PoolVector() { alloc = NULL; }
PoolVector(const PoolVector &p_pool_vector) {
alloc = NULL;
_reference(p_pool_vector);
}
~PoolVector() { _unreference(); }
};
template <class T>
int PoolVector<T>::size() const {
return alloc ? alloc->size / sizeof(T) : 0;
}
template <class T>
bool PoolVector<T>::empty() const {
return alloc ? alloc->size == 0 : true;
}
template <class T>
T PoolVector<T>::get(int p_index) const {
return operator[](p_index);
}
template <class T>
void PoolVector<T>::set(int p_index, const T &p_val) {
ERR_FAIL_INDEX(p_index, size());
Write w = write();
w[p_index] = p_val;
}
template <class T>
void PoolVector<T>::push_back(const T &p_val) {
resize(size() + 1);
set(size() - 1, p_val);
}
template <class T>
T PoolVector<T>::operator[](int p_index) const {
CRASH_BAD_INDEX(p_index, size());
Read r = read();
return r[p_index];
}
template <class T>
Error PoolVector<T>::resize(int p_size) {
ERR_FAIL_COND_V_MSG(p_size < 0, ERR_INVALID_PARAMETER, "Size of PoolVector cannot be negative.");
if (alloc == NULL) {
if (p_size == 0)
return OK; //nothing to do here
//must allocate something
MemoryPool::alloc_mutex->lock();
if (MemoryPool::allocs_used == MemoryPool::alloc_count) {
MemoryPool::alloc_mutex->unlock();
ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "All memory pool allocations are in use.");
}
//take one from the free list
alloc = MemoryPool::free_list;
MemoryPool::free_list = alloc->free_list;
//increment the used counter
MemoryPool::allocs_used++;
//cleanup the alloc
alloc->size = 0;
alloc->refcount.init();
alloc->pool_id = POOL_ALLOCATOR_INVALID_ID;
MemoryPool::alloc_mutex->unlock();
} else {
ERR_FAIL_COND_V_MSG(alloc->lock > 0, ERR_LOCKED, "Can't resize PoolVector if locked."); //can't resize if locked!
}
size_t new_size = sizeof(T) * p_size;
if (alloc->size == new_size)
return OK; //nothing to do
if (p_size == 0) {
_unreference();
return OK;
}
_copy_on_write(); // make it unique
#ifdef DEBUG_ENABLED
MemoryPool::alloc_mutex->lock();
MemoryPool::total_memory -= alloc->size;
MemoryPool::total_memory += new_size;
if (MemoryPool::total_memory > MemoryPool::max_memory) {
MemoryPool::max_memory = MemoryPool::total_memory;
}
MemoryPool::alloc_mutex->unlock();
#endif
int cur_elements = alloc->size / sizeof(T);
if (p_size > cur_elements) {
if (MemoryPool::memory_pool) {
//resize memory pool
//if none, create
//if some resize
} else {
if (alloc->size == 0) {
alloc->mem = memalloc(new_size);
} else {
alloc->mem = memrealloc(alloc->mem, new_size);
}
}
alloc->size = new_size;
Write w = write();
for (int i = cur_elements; i < p_size; i++) {
memnew_placement(&w[i], T);
}
} else {
{
Write w = write();
for (int i = p_size; i < cur_elements; i++) {
w[i].~T();
}
}
if (MemoryPool::memory_pool) {
//resize memory pool
//if none, create
//if some resize
} else {
if (new_size == 0) {
memfree(alloc->mem);
alloc->mem = NULL;
alloc->size = 0;
MemoryPool::alloc_mutex->lock();
alloc->free_list = MemoryPool::free_list;
MemoryPool::free_list = alloc;
MemoryPool::allocs_used--;
MemoryPool::alloc_mutex->unlock();
} else {
alloc->mem = memrealloc(alloc->mem, new_size);
alloc->size = new_size;
}
}
}
return OK;
}
template <class T>
void PoolVector<T>::invert() {
T temp;
Write w = write();
int s = size();
int half_s = s / 2;
for (int i = 0; i < half_s; i++) {
temp = w[i];
w[i] = w[s - i - 1];
w[s - i - 1] = temp;
}
}
#endif // POOL_VECTOR_H
|