2023-01-10 14:26:54 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* pool_vector.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2019-02-12 12:30:56 +00:00
|
|
|
#ifndef POOL_VECTOR_H
|
|
|
|
#define POOL_VECTOR_H
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/memory.h"
|
2021-01-31 12:34:42 +00:00
|
|
|
#include "core/os/mutex.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/rw_lock.h"
|
|
|
|
#include "core/pool_allocator.h"
|
|
|
|
#include "core/safe_refcount.h"
|
|
|
|
#include "core/ustring.h"
|
2017-01-07 21:25:37 +00:00
|
|
|
|
|
|
|
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;
|
2021-01-31 12:34:42 +00:00
|
|
|
SafeNumeric<uint32_t> lock;
|
2017-01-07 21:25:37 +00:00
|
|
|
void *mem;
|
|
|
|
PoolAllocator::ID pool_id;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
Alloc *free_list;
|
|
|
|
|
2018-12-08 20:07:33 +00:00
|
|
|
Alloc() :
|
|
|
|
lock(0),
|
2021-05-04 14:00:45 +00:00
|
|
|
mem(nullptr),
|
2018-12-08 20:07:33 +00:00
|
|
|
pool_id(POOL_ALLOCATOR_INVALID_ID),
|
|
|
|
size(0),
|
2021-05-04 14:00:45 +00:00
|
|
|
free_list(nullptr) {
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2017-01-07 21:25:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Alloc *allocs;
|
|
|
|
static Alloc *free_list;
|
|
|
|
static uint32_t alloc_count;
|
|
|
|
static uint32_t allocs_used;
|
2021-01-27 09:43:02 +00:00
|
|
|
static Mutex alloc_mutex;
|
2017-01-07 21:25:37 +00:00
|
|
|
static size_t total_memory;
|
|
|
|
static size_t max_memory;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static void setup(uint32_t p_max_allocs = (1 << 16));
|
2017-01-07 21:25:37 +00:00
|
|
|
static void cleanup();
|
|
|
|
};
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T>
|
2017-01-07 21:25:37 +00:00
|
|
|
class PoolVector {
|
|
|
|
MemoryPool::Alloc *alloc;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
void _copy_on_write() {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!alloc) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
// 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
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-16 00:39:16 +00:00
|
|
|
// Refcount should not be zero, otherwise it's a misuse of COW
|
2021-05-05 10:44:11 +00:00
|
|
|
if (alloc->refcount.get() == 1) {
|
2017-01-07 21:25:37 +00:00
|
|
|
return; //nothing to do
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
//must allocate something
|
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (MemoryPool::allocs_used == MemoryPool::alloc_count) {
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_MSG("All memory pool allocations are in use, can't COW.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
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++;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
//copy the alloc data
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->size = old_alloc->size;
|
2017-01-07 21:25:37 +00:00
|
|
|
alloc->refcount.init();
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->pool_id = POOL_ALLOCATOR_INVALID_ID;
|
2021-01-31 12:34:42 +00:00
|
|
|
alloc->lock.set(0);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 15:44:50 +00:00
|
|
|
MemoryPool::total_memory += alloc->size;
|
|
|
|
if (MemoryPool::total_memory > MemoryPool::max_memory) {
|
|
|
|
MemoryPool::max_memory = MemoryPool::total_memory;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-01-07 21:25:37 +00:00
|
|
|
#endif
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
if (MemoryPool::memory_pool) {
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->mem = memalloc(alloc->size);
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-08 05:04:53 +00:00
|
|
|
{
|
|
|
|
Write w;
|
|
|
|
w._ref(alloc);
|
|
|
|
Read r;
|
|
|
|
r._ref(old_alloc);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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]));
|
2017-01-08 05:04:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 17:40:37 +00:00
|
|
|
if (old_alloc->refcount.unref()) {
|
2018-07-18 14:24:33 +00:00
|
|
|
//this should never happen but..
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
MemoryPool::total_memory -= old_alloc->size;
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
#endif
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-08 05:04:53 +00:00
|
|
|
{
|
|
|
|
Write w;
|
|
|
|
w._ref(old_alloc);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int cur_elements = old_alloc->size / sizeof(T);
|
|
|
|
T *elems = (T *)w.ptr();
|
|
|
|
for (int i = 0; i < cur_elements; i++) {
|
2017-01-08 05:04:53 +00:00
|
|
|
elems[i].~T();
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
if (MemoryPool::memory_pool) {
|
|
|
|
//resize memory pool
|
|
|
|
//if none, create
|
|
|
|
//if some resize
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
memfree(old_alloc->mem);
|
2021-05-04 14:00:45 +00:00
|
|
|
old_alloc->mem = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
old_alloc->size = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
old_alloc->free_list = MemoryPool::free_list;
|
|
|
|
MemoryPool::free_list = old_alloc;
|
2017-01-07 21:25:37 +00:00
|
|
|
MemoryPool::allocs_used--;
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-02-12 12:30:56 +00:00
|
|
|
void _reference(const PoolVector &p_pool_vector) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (alloc == p_pool_vector.alloc) {
|
2017-01-07 21:25:37 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
_unreference();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-02-12 12:30:56 +00:00
|
|
|
if (!p_pool_vector.alloc) {
|
2017-01-07 21:25:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-02-12 12:30:56 +00:00
|
|
|
if (p_pool_vector.alloc->refcount.ref()) {
|
|
|
|
alloc = p_pool_vector.alloc;
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _unreference() {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!alloc) {
|
2017-01-07 21:25:37 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-01-07 21:25:37 +00:00
|
|
|
|
2018-10-06 20:20:41 +00:00
|
|
|
if (!alloc->refcount.unref()) {
|
2021-05-04 14:00:45 +00:00
|
|
|
alloc = nullptr;
|
2016-03-08 23:00:52 +00:00
|
|
|
return;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
//must be disposed!
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
int cur_elements = alloc->size / sizeof(T);
|
2017-06-16 00:39:16 +00:00
|
|
|
|
|
|
|
// 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);
|
2017-01-08 05:04:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < cur_elements; i++) {
|
2017-01-07 21:25:37 +00:00
|
|
|
w[i].~T();
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
MemoryPool::total_memory -= alloc->size;
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
#endif
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
if (MemoryPool::memory_pool) {
|
|
|
|
//resize memory pool
|
|
|
|
//if none, create
|
|
|
|
//if some resize
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
memfree(alloc->mem);
|
2021-05-04 14:00:45 +00:00
|
|
|
alloc->mem = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->size = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->free_list = MemoryPool::free_list;
|
|
|
|
MemoryPool::free_list = alloc;
|
2017-01-07 21:25:37 +00:00
|
|
|
MemoryPool::allocs_used--;
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
alloc = nullptr;
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
public:
|
|
|
|
class Access {
|
2017-03-05 15:44:50 +00:00
|
|
|
friend class PoolVector;
|
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
protected:
|
|
|
|
MemoryPool::Alloc *alloc;
|
2017-03-05 15:44:50 +00:00
|
|
|
T *mem;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
_FORCE_INLINE_ void _ref(MemoryPool::Alloc *p_alloc) {
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc = p_alloc;
|
2017-01-07 21:25:37 +00:00
|
|
|
if (alloc) {
|
2021-01-31 12:34:42 +00:00
|
|
|
if (alloc->lock.increment() == 1) {
|
2017-01-07 21:25:37 +00:00
|
|
|
if (MemoryPool::memory_pool) {
|
|
|
|
//lock it and get mem
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mem = (T *)alloc->mem;
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
_FORCE_INLINE_ void _unref() {
|
|
|
|
if (alloc) {
|
2021-01-31 12:34:42 +00:00
|
|
|
if (alloc->lock.decrement() == 0) {
|
2017-01-07 21:25:37 +00:00
|
|
|
if (MemoryPool::memory_pool) {
|
|
|
|
//put mem back
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
mem = nullptr;
|
|
|
|
alloc = nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
Access() {
|
2021-05-04 14:00:45 +00:00
|
|
|
alloc = nullptr;
|
|
|
|
mem = nullptr;
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
public:
|
|
|
|
virtual ~Access() {
|
|
|
|
_unref();
|
|
|
|
}
|
2019-07-05 17:08:43 +00:00
|
|
|
|
|
|
|
void release() {
|
|
|
|
_unref();
|
|
|
|
}
|
2017-01-07 21:25:37 +00:00
|
|
|
};
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
class Read : public Access {
|
|
|
|
public:
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ const T &operator[](int p_index) const { return this->mem[p_index]; }
|
2017-01-07 21:25:37 +00:00
|
|
|
_FORCE_INLINE_ const T *ptr() const { return this->mem; }
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void operator=(const Read &p_read) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (this->alloc == p_read.alloc) {
|
2017-01-07 21:25:37 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-01-07 21:25:37 +00:00
|
|
|
this->_unref();
|
|
|
|
this->_ref(p_read.alloc);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Read(const Read &p_read) {
|
2017-01-07 21:25:37 +00:00
|
|
|
this->_ref(p_read.alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
Read() {}
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
class Write : public Access {
|
2014-02-10 01:10:30 +00:00
|
|
|
public:
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ T &operator[](int p_index) const { return this->mem[p_index]; }
|
2017-01-07 21:25:37 +00:00
|
|
|
_FORCE_INLINE_ T *ptr() const { return this->mem; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void operator=(const Write &p_read) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (this->alloc == p_read.alloc) {
|
2017-01-07 21:25:37 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-01-07 21:25:37 +00:00
|
|
|
this->_unref();
|
|
|
|
this->_ref(p_read.alloc);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Write(const Write &p_read) {
|
2017-01-07 21:25:37 +00:00
|
|
|
this->_ref(p_read.alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
Write() {}
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Read read() const {
|
|
|
|
Read r;
|
2017-01-07 21:25:37 +00:00
|
|
|
if (alloc) {
|
|
|
|
r._ref(alloc);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
return r;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
Write write() {
|
|
|
|
Write w;
|
2017-01-07 21:25:37 +00:00
|
|
|
if (alloc) {
|
2020-09-18 12:09:51 +00:00
|
|
|
_copy_on_write(); //make sure there is only one being accessed
|
2017-01-07 21:25:37 +00:00
|
|
|
w._ref(alloc);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
return w;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class MC>
|
|
|
|
void fill_with(const MC &p_mc) {
|
|
|
|
int c = p_mc.size();
|
2014-02-10 01:10:30 +00:00
|
|
|
resize(c);
|
2017-03-05 15:44:50 +00:00
|
|
|
Write w = write();
|
|
|
|
int idx = 0;
|
|
|
|
for (const typename MC::Element *E = p_mc.front(); E; E = E->next()) {
|
|
|
|
w[idx++] = E->get();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove(int p_index) {
|
|
|
|
int s = size();
|
|
|
|
ERR_FAIL_INDEX(p_index, s);
|
|
|
|
Write w = write();
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = p_index; i < s - 1; i++) {
|
|
|
|
w[i] = w[i + 1];
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
w = Write();
|
2017-03-05 15:44:50 +00:00
|
|
|
resize(s - 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 11:47:49 +00:00
|
|
|
int find(const T &p_val, int p_from = 0) const {
|
|
|
|
const int s = size();
|
|
|
|
const Read r = read();
|
|
|
|
|
|
|
|
if (p_from < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = p_from; i < s; i++) {
|
|
|
|
if (r[i] == p_val) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rfind(const T &p_val, int p_from = -1) const {
|
|
|
|
const int s = size();
|
|
|
|
const Read r = read();
|
|
|
|
|
|
|
|
if (p_from < 0) {
|
|
|
|
p_from = s + p_from;
|
|
|
|
}
|
|
|
|
if (p_from < 0 || p_from >= s) {
|
|
|
|
p_from = s - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = p_from; i >= 0; i--) {
|
|
|
|
if (r[i] == p_val) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int count(const T &p_val) const {
|
|
|
|
const int s = size();
|
|
|
|
const Read r = read();
|
|
|
|
int amount = 0;
|
|
|
|
for (int i = 0; i < s; i++) {
|
|
|
|
if (r[i] == p_val) {
|
|
|
|
amount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has(const T &p_val) const {
|
|
|
|
return find(p_val) != -1;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
inline int size() const;
|
2019-10-31 15:27:32 +00:00
|
|
|
inline bool empty() const;
|
2014-02-10 01:10:30 +00:00
|
|
|
T get(int p_index) const;
|
2017-03-05 15:44:50 +00:00
|
|
|
void set(int p_index, const T &p_val);
|
2022-04-22 10:45:34 +00:00
|
|
|
void fill(const T &p_val);
|
2017-03-05 15:44:50 +00:00
|
|
|
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) {
|
2014-02-10 01:10:30 +00:00
|
|
|
int ds = p_arr.size();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (ds == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
int bs = size();
|
2017-03-05 15:44:50 +00:00
|
|
|
resize(bs + ds);
|
2014-02-10 01:10:30 +00:00
|
|
|
Write w = write();
|
|
|
|
Read r = p_arr.read();
|
2021-05-05 10:44:11 +00:00
|
|
|
for (int i = 0; i < ds; i++) {
|
2017-03-05 15:44:50 +00:00
|
|
|
w[bs + i] = r[i];
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<T> subarray(int p_from, int p_to) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_from < 0) {
|
|
|
|
p_from = size() + p_from;
|
2016-07-17 23:43:10 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_to < 0) {
|
|
|
|
p_to = size() + p_to;
|
2016-07-17 23:43:10 +00:00
|
|
|
}
|
2017-05-26 19:11:16 +00:00
|
|
|
|
2019-06-24 07:22:15 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_from, size(), PoolVector<T>());
|
|
|
|
ERR_FAIL_INDEX_V(p_to, size(), PoolVector<T>());
|
2016-07-17 23:43:10 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<T> slice;
|
2017-03-05 15:44:50 +00:00
|
|
|
int span = 1 + p_to - p_from;
|
2016-07-17 23:43:10 +00:00
|
|
|
slice.resize(span);
|
|
|
|
Read r = read();
|
|
|
|
Write w = slice.write();
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < span; ++i) {
|
|
|
|
w[i] = r[p_from + i];
|
2016-07-17 23:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return slice;
|
|
|
|
}
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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);
|
2015-02-14 15:09:52 +00:00
|
|
|
{
|
|
|
|
Write w = write();
|
2021-05-05 10:44:11 +00:00
|
|
|
for (int i = s; i > p_pos; i--) {
|
2017-03-05 15:44:50 +00:00
|
|
|
w[i] = w[i - 1];
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
w[p_pos] = p_val;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-07-16 11:52:05 +00:00
|
|
|
String join(String delimiter) const {
|
2017-02-23 18:41:10 +00:00
|
|
|
String rs = "";
|
|
|
|
int s = size();
|
|
|
|
Read r = read();
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < s; i++) {
|
2017-02-23 18:41:10 +00:00
|
|
|
rs += r[i] + delimiter;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
rs.erase(rs.length() - delimiter.length(), delimiter.length());
|
2017-02-23 18:41:10 +00:00
|
|
|
return rs;
|
|
|
|
}
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2021-01-31 12:34:42 +00:00
|
|
|
bool is_locked() const { return alloc && alloc->lock.get() > 0; }
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-07-20 06:09:57 +00:00
|
|
|
inline T operator[](int p_index) const;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Error resize(int p_size);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2016-05-16 17:06:41 +00:00
|
|
|
void invert();
|
2021-11-04 13:30:12 +00:00
|
|
|
void sort();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-02-12 12:30:56 +00:00
|
|
|
void operator=(const PoolVector &p_pool_vector) { _reference(p_pool_vector); }
|
2021-05-04 14:00:45 +00:00
|
|
|
PoolVector() { alloc = nullptr; }
|
2019-02-12 12:30:56 +00:00
|
|
|
PoolVector(const PoolVector &p_pool_vector) {
|
2021-05-04 14:00:45 +00:00
|
|
|
alloc = nullptr;
|
2019-02-12 12:30:56 +00:00
|
|
|
_reference(p_pool_vector);
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2017-01-07 21:25:37 +00:00
|
|
|
~PoolVector() { _unreference(); }
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T>
|
2017-01-07 21:25:37 +00:00
|
|
|
int PoolVector<T>::size() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return alloc ? alloc->size / sizeof(T) : 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 15:27:32 +00:00
|
|
|
template <class T>
|
|
|
|
bool PoolVector<T>::empty() const {
|
|
|
|
return alloc ? alloc->size == 0 : true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T>
|
2017-01-07 21:25:37 +00:00
|
|
|
T PoolVector<T>::get(int p_index) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return operator[](p_index);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T>
|
|
|
|
void PoolVector<T>::set(int p_index, const T &p_val) {
|
2019-07-07 00:43:34 +00:00
|
|
|
ERR_FAIL_INDEX(p_index, size());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Write w = write();
|
2017-03-05 15:44:50 +00:00
|
|
|
w[p_index] = p_val;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 10:45:34 +00:00
|
|
|
template <class T>
|
|
|
|
void PoolVector<T>::fill(const T &p_val) {
|
|
|
|
Write w = write();
|
|
|
|
for (int i = 0; i < size(); i++) {
|
|
|
|
w[i] = p_val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T>
|
|
|
|
void PoolVector<T>::push_back(const T &p_val) {
|
|
|
|
resize(size() + 1);
|
|
|
|
set(size() - 1, p_val);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T>
|
2019-07-20 06:09:57 +00:00
|
|
|
T PoolVector<T>::operator[](int p_index) const {
|
2017-05-26 19:11:16 +00:00
|
|
|
CRASH_BAD_INDEX(p_index, size());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Read r = read();
|
|
|
|
return r[p_index];
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T>
|
2017-01-07 21:25:37 +00:00
|
|
|
Error PoolVector<T>::resize(int p_size) {
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(p_size < 0, ERR_INVALID_PARAMETER, "Size of PoolVector cannot be negative.");
|
2019-06-24 07:22:15 +00:00
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
if (alloc == nullptr) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_size == 0) {
|
2017-01-07 21:25:37 +00:00
|
|
|
return OK; //nothing to do here
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
//must allocate something
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (MemoryPool::allocs_used == MemoryPool::alloc_count) {
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "All memory pool allocations are in use.");
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
//take one from the free list
|
|
|
|
alloc = MemoryPool::free_list;
|
|
|
|
MemoryPool::free_list = alloc->free_list;
|
|
|
|
//increment the used counter
|
|
|
|
MemoryPool::allocs_used++;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
//cleanup the alloc
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->size = 0;
|
2017-01-07 21:25:37 +00:00
|
|
|
alloc->refcount.init();
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->pool_id = POOL_ALLOCATOR_INVALID_ID;
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
|
|
|
|
} else {
|
2021-01-31 12:34:42 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(alloc->lock.get() > 0, ERR_LOCKED, "Can't resize PoolVector if locked."); //can't resize if locked!
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
size_t new_size = sizeof(T) * p_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (alloc->size == new_size) {
|
2017-01-07 21:25:37 +00:00
|
|
|
return OK; //nothing to do
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_size == 0) {
|
2017-01-07 21:25:37 +00:00
|
|
|
_unreference();
|
|
|
|
return OK;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
_copy_on_write(); // make it unique
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
MemoryPool::total_memory -= alloc->size;
|
|
|
|
MemoryPool::total_memory += new_size;
|
|
|
|
if (MemoryPool::total_memory > MemoryPool::max_memory) {
|
|
|
|
MemoryPool::max_memory = MemoryPool::total_memory;
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
#endif
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
int cur_elements = alloc->size / sizeof(T);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_size > cur_elements) {
|
2017-01-07 21:25:37 +00:00
|
|
|
if (MemoryPool::memory_pool) {
|
|
|
|
//resize memory pool
|
|
|
|
//if none, create
|
|
|
|
//if some resize
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (alloc->size == 0) {
|
|
|
|
alloc->mem = memalloc(new_size);
|
2017-01-07 21:25:37 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->mem = memrealloc(alloc->mem, new_size);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->size = new_size;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
Write w = write();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = cur_elements; i < p_size; i++) {
|
|
|
|
memnew_placement(&w[i], T);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
} else {
|
|
|
|
{
|
2017-01-08 05:04:53 +00:00
|
|
|
Write w = write();
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = p_size; i < cur_elements; i++) {
|
2017-01-07 21:25:37 +00:00
|
|
|
w[i].~T();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
if (MemoryPool::memory_pool) {
|
|
|
|
//resize memory pool
|
|
|
|
//if none, create
|
|
|
|
//if some resize
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (new_size == 0) {
|
|
|
|
memfree(alloc->mem);
|
2021-05-04 14:00:45 +00:00
|
|
|
alloc->mem = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->size = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->free_list = MemoryPool::free_list;
|
|
|
|
MemoryPool::free_list = alloc;
|
2017-01-07 21:25:37 +00:00
|
|
|
MemoryPool::allocs_used--;
|
2021-01-27 09:43:02 +00:00
|
|
|
MemoryPool::alloc_mutex.unlock();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
alloc->mem = memrealloc(alloc->mem, new_size);
|
|
|
|
alloc->size = new_size;
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T>
|
2017-01-07 21:25:37 +00:00
|
|
|
void PoolVector<T>::invert() {
|
2016-05-16 17:06:41 +00:00
|
|
|
T temp;
|
|
|
|
Write w = write();
|
|
|
|
int s = size();
|
2017-03-05 15:44:50 +00:00
|
|
|
int half_s = s / 2;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < half_s; i++) {
|
2016-05-16 17:06:41 +00:00
|
|
|
temp = w[i];
|
2017-03-05 15:44:50 +00:00
|
|
|
w[i] = w[s - i - 1];
|
|
|
|
w[s - i - 1] = temp;
|
2016-05-16 17:06:41 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-11-04 13:30:12 +00:00
|
|
|
template <class T>
|
|
|
|
void PoolVector<T>::sort() {
|
|
|
|
int len = size();
|
|
|
|
if (len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Write w = write();
|
|
|
|
SortArray<T> sorter;
|
|
|
|
sorter.sort(w.ptr(), len);
|
|
|
|
}
|
|
|
|
|
2019-02-12 12:30:56 +00:00
|
|
|
#endif // POOL_VECTOR_H
|