2020-10-21 12:13:20 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* pooled_list.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-13 08:45:09 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-10-21 12:13:20 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// Simple template to provide a pool with O(1) allocate and free.
|
|
|
|
// The freelist could alternatively be a linked list placed within the unused elements
|
|
|
|
// to use less memory, however a separate freelist is probably more cache friendly.
|
|
|
|
|
|
|
|
// NOTE : Take great care when using this with non POD types. The construction and destruction
|
|
|
|
// is done in the LocalVector, NOT as part of the pool. So requesting a new item does not guarantee
|
|
|
|
// a constructor is run, and free does not guarantee a destructor.
|
|
|
|
// You should generally handle clearing
|
|
|
|
// an item explicitly after a request, as it may contain 'leftovers'.
|
|
|
|
// This is by design for fastest use in the BVH. If you want a more general pool
|
|
|
|
// that does call constructors / destructors on request / free, this should probably be
|
|
|
|
// a separate template.
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
// The zero_on_first_request feature is optional and is useful for e.g. pools of handles,
|
|
|
|
// which may use a ref count which we want to be initialized to zero the first time a handle is created,
|
|
|
|
// but left alone on subsequent allocations (as will typically be incremented).
|
|
|
|
|
|
|
|
// Note that there is no function to compact the pool - this would
|
|
|
|
// invalidate any existing pool IDs held externally.
|
|
|
|
// Compaction can be done but would rely on a more complex method
|
|
|
|
// of preferentially giving out lower IDs in the freelist first.
|
|
|
|
|
2020-10-21 12:13:20 +00:00
|
|
|
#include "core/local_vector.h"
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
template <class T, class U = uint32_t, bool force_trivial = false, bool zero_on_first_request = false>
|
2020-10-21 12:13:20 +00:00
|
|
|
class PooledList {
|
2021-11-09 12:00:07 +00:00
|
|
|
LocalVector<T, U, force_trivial> list;
|
|
|
|
LocalVector<U, U, true> freelist;
|
2020-10-21 12:13:20 +00:00
|
|
|
|
|
|
|
// not all list members are necessarily used
|
2021-11-09 12:00:07 +00:00
|
|
|
U _used_size;
|
2020-10-21 12:13:20 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
PooledList() {
|
|
|
|
_used_size = 0;
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
// Use with care, in most cases you should make sure to
|
|
|
|
// free all elements first (i.e. _used_size would be zero),
|
|
|
|
// although it could also be used without this as an optimization
|
|
|
|
// in some cases.
|
|
|
|
void clear() {
|
|
|
|
list.clear();
|
|
|
|
freelist.clear();
|
|
|
|
_used_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t estimate_memory_use() const {
|
|
|
|
return ((uint64_t)list.size() * sizeof(T)) + ((uint64_t)freelist.size() * sizeof(U));
|
2020-10-21 12:13:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
const T &operator[](U p_index) const {
|
2020-10-21 12:13:20 +00:00
|
|
|
return list[p_index];
|
|
|
|
}
|
2021-11-09 12:00:07 +00:00
|
|
|
T &operator[](U p_index) {
|
2020-10-21 12:13:20 +00:00
|
|
|
return list[p_index];
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
// To be explicit in a pool there is a distinction
|
|
|
|
// between the number of elements that are currently
|
|
|
|
// in use, and the number of elements that have been reserved.
|
|
|
|
// Using size() would be vague.
|
|
|
|
U used_size() const { return _used_size; }
|
|
|
|
U reserved_size() const { return list.size(); }
|
2020-10-21 12:13:20 +00:00
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
T *request(U &r_id) {
|
2020-10-21 12:13:20 +00:00
|
|
|
_used_size++;
|
|
|
|
|
|
|
|
if (freelist.size()) {
|
|
|
|
// pop from freelist
|
|
|
|
int new_size = freelist.size() - 1;
|
|
|
|
r_id = freelist[new_size];
|
|
|
|
freelist.resize(new_size);
|
2021-11-09 12:00:07 +00:00
|
|
|
|
2020-10-21 12:13:20 +00:00
|
|
|
return &list[r_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
r_id = list.size();
|
|
|
|
list.resize(r_id + 1);
|
2021-11-09 12:00:07 +00:00
|
|
|
|
|
|
|
static_assert((!zero_on_first_request) || (__is_pod(T)), "zero_on_first_request requires trivial type");
|
|
|
|
if (zero_on_first_request && __is_pod(T)) {
|
|
|
|
list[r_id] = {};
|
|
|
|
}
|
|
|
|
|
2020-10-21 12:13:20 +00:00
|
|
|
return &list[r_id];
|
|
|
|
}
|
2021-11-09 12:00:07 +00:00
|
|
|
void free(const U &p_id) {
|
2020-10-21 12:13:20 +00:00
|
|
|
// should not be on free list already
|
2021-11-09 12:00:07 +00:00
|
|
|
ERR_FAIL_UNSIGNED_INDEX(p_id, list.size());
|
2020-10-21 12:13:20 +00:00
|
|
|
freelist.push_back(p_id);
|
2021-11-09 12:00:07 +00:00
|
|
|
ERR_FAIL_COND_MSG(!_used_size, "_used_size has become out of sync, have you double freed an item?");
|
2020-10-21 12:13:20 +00:00
|
|
|
_used_size--;
|
|
|
|
}
|
|
|
|
};
|
2021-02-04 10:43:08 +00:00
|
|
|
|
|
|
|
// a pooled list which automatically keeps a list of the active members
|
2021-11-09 12:00:07 +00:00
|
|
|
template <class T, class U = uint32_t, bool force_trivial = false, bool zero_on_first_request = false>
|
2021-02-04 10:43:08 +00:00
|
|
|
class TrackedPooledList {
|
|
|
|
public:
|
2021-11-09 12:00:07 +00:00
|
|
|
U pool_used_size() const { return _pool.used_size(); }
|
|
|
|
U pool_reserved_size() const { return _pool.reserved_size(); }
|
|
|
|
U active_size() const { return _active_list.size(); }
|
|
|
|
|
|
|
|
// use with care, see the earlier notes in the PooledList clear()
|
|
|
|
void clear() {
|
|
|
|
_pool.clear();
|
|
|
|
_active_list.clear();
|
|
|
|
_active_map.clear();
|
|
|
|
}
|
2021-02-04 10:43:08 +00:00
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
U get_active_id(U p_index) const {
|
2021-02-04 10:43:08 +00:00
|
|
|
return _active_list[p_index];
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
const T &get_active(U p_index) const {
|
2021-02-04 10:43:08 +00:00
|
|
|
return _pool[get_active_id(p_index)];
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
T &get_active(U p_index) {
|
2021-02-04 10:43:08 +00:00
|
|
|
return _pool[get_active_id(p_index)];
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
const T &operator[](U p_index) const {
|
2021-02-04 10:43:08 +00:00
|
|
|
return _pool[p_index];
|
|
|
|
}
|
2021-11-09 12:00:07 +00:00
|
|
|
T &operator[](U p_index) {
|
2021-02-04 10:43:08 +00:00
|
|
|
return _pool[p_index];
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
T *request(U &r_id) {
|
2021-02-04 10:43:08 +00:00
|
|
|
T *item = _pool.request(r_id);
|
|
|
|
|
|
|
|
// add to the active list
|
2021-11-09 12:00:07 +00:00
|
|
|
U active_list_id = _active_list.size();
|
2021-02-04 10:43:08 +00:00
|
|
|
_active_list.push_back(r_id);
|
|
|
|
|
|
|
|
// expand the active map (this should be in sync with the pool list
|
2021-11-09 12:00:07 +00:00
|
|
|
if (_pool.used_size() > _active_map.size()) {
|
|
|
|
_active_map.resize(_pool.used_size());
|
2021-02-04 10:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// store in the active map
|
|
|
|
_active_map[r_id] = active_list_id;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
void free(const U &p_id) {
|
2021-02-04 10:43:08 +00:00
|
|
|
_pool.free(p_id);
|
|
|
|
|
|
|
|
// remove from the active list.
|
2021-11-09 12:00:07 +00:00
|
|
|
U list_id = _active_map[p_id];
|
2021-02-04 10:43:08 +00:00
|
|
|
|
|
|
|
// zero the _active map to detect bugs (only in debug?)
|
|
|
|
_active_map[p_id] = -1;
|
|
|
|
|
|
|
|
_active_list.remove_unordered(list_id);
|
|
|
|
|
|
|
|
// keep the replacement in sync with the correct list Id
|
2021-11-09 12:00:07 +00:00
|
|
|
if (list_id < _active_list.size()) {
|
2021-02-04 10:43:08 +00:00
|
|
|
// which pool id has been replaced in the active list
|
2021-11-09 12:00:07 +00:00
|
|
|
U replacement_id = _active_list[list_id];
|
2021-02-04 10:43:08 +00:00
|
|
|
|
|
|
|
// keep that replacements map up to date with the new position
|
|
|
|
_active_map[replacement_id] = list_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:00:07 +00:00
|
|
|
const LocalVector<U, U> &get_active_list() const { return _active_list; }
|
2021-08-05 14:44:43 +00:00
|
|
|
|
2021-02-04 10:43:08 +00:00
|
|
|
private:
|
2021-11-09 12:00:07 +00:00
|
|
|
PooledList<T, U, force_trivial, zero_on_first_request> _pool;
|
|
|
|
LocalVector<U, U> _active_map;
|
|
|
|
LocalVector<U, U> _active_list;
|
2021-02-04 10:43:08 +00:00
|
|
|
};
|