2020-12-23 17:52:58 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* paged_allocator.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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#ifndef PAGED_ALLOCATOR_H
|
|
|
|
#define PAGED_ALLOCATOR_H
|
|
|
|
|
2022-05-20 14:28:44 +02:00
|
|
|
#include "core/core_globals.h"
|
2020-12-23 17:52:58 +01:00
|
|
|
#include "core/os/memory.h"
|
|
|
|
#include "core/os/spin_lock.h"
|
2022-05-20 14:28:44 +02:00
|
|
|
#include "core/string/ustring.h"
|
2020-12-23 17:52:58 +01:00
|
|
|
#include "core/typedefs.h"
|
|
|
|
|
2021-05-21 09:58:01 +02:00
|
|
|
#include <type_traits>
|
2022-05-20 14:28:44 +02:00
|
|
|
#include <typeinfo>
|
2021-05-21 09:58:01 +02:00
|
|
|
|
2023-12-28 19:31:28 +01:00
|
|
|
template <class T, bool thread_safe = false, uint32_t DEFAULT_PAGE_SIZE = 4096>
|
2020-12-23 17:52:58 +01:00
|
|
|
class PagedAllocator {
|
|
|
|
T **page_pool = nullptr;
|
|
|
|
T ***available_pool = nullptr;
|
|
|
|
uint32_t pages_allocated = 0;
|
|
|
|
uint32_t allocs_available = 0;
|
|
|
|
|
|
|
|
uint32_t page_shift = 0;
|
|
|
|
uint32_t page_mask = 0;
|
|
|
|
uint32_t page_size = 0;
|
|
|
|
SpinLock spin_lock;
|
|
|
|
|
|
|
|
public:
|
2021-08-14 09:00:58 +02:00
|
|
|
template <class... Args>
|
2023-12-20 12:01:39 +01:00
|
|
|
T *alloc(Args &&...p_args) {
|
2020-12-23 17:52:58 +01:00
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.lock();
|
|
|
|
}
|
|
|
|
if (unlikely(allocs_available == 0)) {
|
|
|
|
uint32_t pages_used = pages_allocated;
|
|
|
|
|
|
|
|
pages_allocated++;
|
|
|
|
page_pool = (T **)memrealloc(page_pool, sizeof(T *) * pages_allocated);
|
|
|
|
available_pool = (T ***)memrealloc(available_pool, sizeof(T **) * pages_allocated);
|
|
|
|
|
|
|
|
page_pool[pages_used] = (T *)memalloc(sizeof(T) * page_size);
|
|
|
|
available_pool[pages_used] = (T **)memalloc(sizeof(T *) * page_size);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < page_size; i++) {
|
|
|
|
available_pool[0][i] = &page_pool[pages_used][i];
|
|
|
|
}
|
|
|
|
allocs_available += page_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
allocs_available--;
|
|
|
|
T *alloc = available_pool[allocs_available >> page_shift][allocs_available & page_mask];
|
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
2021-08-14 09:00:58 +02:00
|
|
|
memnew_placement(alloc, T(p_args...));
|
2020-12-23 17:52:58 +01:00
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(T *p_mem) {
|
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.lock();
|
|
|
|
}
|
|
|
|
p_mem->~T();
|
|
|
|
available_pool[allocs_available >> page_shift][allocs_available & page_mask] = p_mem;
|
2022-03-06 12:57:09 +01:00
|
|
|
allocs_available++;
|
2020-12-23 17:52:58 +01:00
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 12:01:39 +01:00
|
|
|
template <class... Args>
|
|
|
|
T *new_allocation(Args &&...p_args) { return alloc(p_args...); }
|
|
|
|
void delete_allocation(T *p_mem) { free(p_mem); }
|
|
|
|
|
2023-05-05 19:55:29 +02:00
|
|
|
private:
|
|
|
|
void _reset(bool p_allow_unfreed) {
|
2024-02-02 15:43:21 +01:00
|
|
|
if (!p_allow_unfreed || !std::is_trivially_destructible_v<T>) {
|
2021-05-21 09:58:01 +02:00
|
|
|
ERR_FAIL_COND(allocs_available < pages_allocated * page_size);
|
|
|
|
}
|
2020-12-23 17:52:58 +01:00
|
|
|
if (pages_allocated) {
|
|
|
|
for (uint32_t i = 0; i < pages_allocated; i++) {
|
|
|
|
memfree(page_pool[i]);
|
|
|
|
memfree(available_pool[i]);
|
|
|
|
}
|
|
|
|
memfree(page_pool);
|
|
|
|
memfree(available_pool);
|
|
|
|
page_pool = nullptr;
|
|
|
|
available_pool = nullptr;
|
|
|
|
pages_allocated = 0;
|
|
|
|
allocs_available = 0;
|
|
|
|
}
|
|
|
|
}
|
2023-05-05 19:55:29 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
void reset(bool p_allow_unfreed = false) {
|
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.lock();
|
|
|
|
}
|
|
|
|
_reset(p_allow_unfreed);
|
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 17:52:58 +01:00
|
|
|
bool is_configured() const {
|
2023-05-05 19:55:29 +02:00
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.lock();
|
|
|
|
}
|
|
|
|
bool result = page_size > 0;
|
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
|
|
|
return result;
|
2020-12-23 17:52:58 +01:00
|
|
|
}
|
|
|
|
|
2020-12-24 18:08:03 +01:00
|
|
|
void configure(uint32_t p_page_size) {
|
2023-05-05 19:55:29 +02:00
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.lock();
|
|
|
|
}
|
2023-09-28 15:42:55 +02:00
|
|
|
ERR_FAIL_COND(page_pool != nullptr); // Safety check.
|
2020-12-23 17:52:58 +01:00
|
|
|
ERR_FAIL_COND(p_page_size == 0);
|
|
|
|
page_size = nearest_power_of_2_templated(p_page_size);
|
|
|
|
page_mask = page_size - 1;
|
|
|
|
page_shift = get_shift_from_power_of_2(page_size);
|
2023-05-05 19:55:29 +02:00
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.unlock();
|
|
|
|
}
|
2020-12-23 17:52:58 +01:00
|
|
|
}
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
// Power of 2 recommended because of alignment with OS page sizes.
|
|
|
|
// Even if element is bigger, it's still a multiple and gets rounded to amount of pages.
|
|
|
|
PagedAllocator(uint32_t p_page_size = DEFAULT_PAGE_SIZE) {
|
2020-12-24 18:08:03 +01:00
|
|
|
configure(p_page_size);
|
2020-12-23 17:52:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~PagedAllocator() {
|
2023-05-05 19:55:29 +02:00
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.lock();
|
|
|
|
}
|
|
|
|
bool leaked = allocs_available < pages_allocated * page_size;
|
|
|
|
if (leaked) {
|
2022-05-20 14:28:44 +02:00
|
|
|
if (CoreGlobals::leak_reporting_enabled) {
|
2023-05-05 19:55:29 +02:00
|
|
|
ERR_PRINT(String("Pages in use exist at exit in PagedAllocator: ") + String(typeid(T).name()));
|
2022-05-20 14:28:44 +02:00
|
|
|
}
|
2023-05-05 19:55:29 +02:00
|
|
|
} else {
|
|
|
|
_reset(false);
|
|
|
|
}
|
|
|
|
if (thread_safe) {
|
|
|
|
spin_lock.unlock();
|
2022-05-20 14:28:44 +02:00
|
|
|
}
|
2020-12-23 17:52:58 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PAGED_ALLOCATOR_H
|