2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-03-11 15:26:02 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2018-10-16 11:01:38 +02:00
|
|
|
/*
|
|
|
|
* Really really *really* Q&D malloc() and free() implementations
|
|
|
|
* just to get going. Don't ever let anyone see this shit. :^)
|
|
|
|
*/
|
|
|
|
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <AK/Assertions.h>
|
2020-08-29 16:41:30 -06:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2019-04-06 14:29:29 +02:00
|
|
|
#include <AK/Types.h>
|
2021-03-11 15:26:02 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2020-08-29 16:41:30 -06:00
|
|
|
#include <Kernel/Heap/Heap.h>
|
2020-02-09 16:47:15 +02:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/KSyms.h>
|
2021-02-14 09:30:31 +01:00
|
|
|
#include <Kernel/Panic.h>
|
2021-05-13 13:09:00 +02:00
|
|
|
#include <Kernel/PerformanceManager.h>
|
2021-06-22 17:40:16 +02:00
|
|
|
#include <Kernel/Sections.h>
|
2020-06-05 22:01:30 -06:00
|
|
|
#include <Kernel/SpinLock.h>
|
2020-05-16 12:00:04 +02:00
|
|
|
#include <Kernel/StdLib.h>
|
2020-08-24 16:38:20 -06:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
2018-10-16 11:01:38 +02:00
|
|
|
|
2020-02-22 14:18:34 +01:00
|
|
|
#define CHUNK_SIZE 32
|
2020-08-29 16:41:30 -06:00
|
|
|
#define POOL_SIZE (2 * MiB)
|
2021-06-28 13:45:57 +02:00
|
|
|
#define ETERNAL_RANGE_SIZE (3 * MiB)
|
2020-08-21 21:55:08 -06:00
|
|
|
|
AK+Kernel: Make fallible allocations compiler-agnostic
In standard C++, operators `new` and `new[]` are guaranteed to return a
valid (non-null) pointer and throw an exception if the allocation
couldn't be performed. Based on this, compilers did not check the
returned pointer before attempting to use them for object construction.
To avoid this, the allocator operators were changed to be `noexcept` in
PR #7026, which made GCC emit the desired null checks. Unfortunately,
this is a non-standard feature which meant that Clang would not accept
these function definitions, as it did not match its expected
declaration.
To make compiling using Clang possible, the special "nothrow" versions
of `new` are implemented in this commit. These take a tag type of
`std::nothrow_t` (used for disambiguating from placement new/etc.), and
are allowed by the standard to return null. There is a global variable,
`std::nothrow`, declared with this type, which is also exported into the
global namespace.
To perform fallible allocations, the following syntax should be used:
```cpp
auto ptr = new (nothrow) T;
```
As we don't support exceptions in the kernel, the only way of uphold the
"throwing" new's guarantee is to abort if the allocation couldn't be
performed. Once we have proper OOM handling in the kernel, this should
only be used for critical allocations, where we wouldn't be able to
recover from allocation failures anyway.
2021-06-20 09:39:20 +02:00
|
|
|
namespace std {
|
|
|
|
const nothrow_t nothrow;
|
|
|
|
}
|
|
|
|
|
2020-11-01 13:11:31 -07:00
|
|
|
static RecursiveSpinLock s_lock; // needs to be recursive because of dump_backtrace()
|
|
|
|
|
2020-12-30 17:00:49 -07:00
|
|
|
static void kmalloc_allocate_backup_memory();
|
|
|
|
|
2020-08-29 16:41:30 -06:00
|
|
|
struct KmallocGlobalHeap {
|
|
|
|
struct ExpandGlobalHeap {
|
|
|
|
KmallocGlobalHeap& m_global_heap;
|
|
|
|
|
|
|
|
ExpandGlobalHeap(KmallocGlobalHeap& global_heap)
|
|
|
|
: m_global_heap(global_heap)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:31:56 -06:00
|
|
|
bool m_adding { false };
|
2020-08-29 16:41:30 -06:00
|
|
|
bool add_memory(size_t allocation_request)
|
|
|
|
{
|
|
|
|
if (!MemoryManager::is_initialized()) {
|
2021-03-11 15:26:02 +01:00
|
|
|
if constexpr (KMALLOC_DEBUG) {
|
|
|
|
dmesgln("kmalloc: Cannot expand heap before MM is initialized!");
|
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_adding);
|
2020-09-04 15:31:56 -06:00
|
|
|
TemporaryChange change(m_adding, true);
|
2020-08-29 16:41:30 -06:00
|
|
|
// At this point we have very little memory left. Any attempt to
|
|
|
|
// kmalloc() could fail, so use our backup memory first, so we
|
|
|
|
// can't really reliably allocate even a new region of memory.
|
|
|
|
// This is why we keep a backup region, which we can
|
|
|
|
auto region = move(m_global_heap.m_backup_memory);
|
|
|
|
if (!region) {
|
2020-09-04 15:31:56 -06:00
|
|
|
// Be careful to not log too much here. We don't want to trigger
|
|
|
|
// any further calls to kmalloc(). We're already out of memory
|
|
|
|
// and don't have any backup memory, either!
|
2021-03-11 15:26:02 +01:00
|
|
|
if constexpr (KMALLOC_DEBUG) {
|
|
|
|
dmesgln("kmalloc: Cannot expand heap: no backup memory");
|
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:31:56 -06:00
|
|
|
// At this point we should have at least enough memory from the
|
|
|
|
// backup region to be able to log properly
|
2021-03-11 15:26:02 +01:00
|
|
|
if constexpr (KMALLOC_DEBUG) {
|
|
|
|
dmesgln("kmalloc: Adding memory to heap at {}, bytes: {}", region->vaddr(), region->size());
|
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
|
|
|
|
auto& subheap = m_global_heap.m_heap.add_subheap(region->vaddr().as_ptr(), region->size());
|
|
|
|
m_global_heap.m_subheap_memory.append(region.release_nonnull());
|
|
|
|
|
|
|
|
// Since we pulled in our backup heap, make sure we allocate another
|
|
|
|
// backup heap before returning. Otherwise we potentially lose
|
|
|
|
// the ability to expand the heap next time we get called.
|
|
|
|
ScopeGuard guard([&]() {
|
2020-12-30 17:00:49 -07:00
|
|
|
// We may need to defer allocating backup memory because the
|
|
|
|
// heap expansion may have been triggered while holding some
|
|
|
|
// other spinlock. If the expansion happens to need the same
|
|
|
|
// spinlock we would deadlock. So, if we're in any lock, defer
|
|
|
|
Processor::current().deferred_call_queue(kmalloc_allocate_backup_memory);
|
2020-08-29 16:41:30 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Now that we added our backup memory, check if the backup heap
|
|
|
|
// was big enough to likely satisfy the request
|
|
|
|
if (subheap.free_bytes() < allocation_request) {
|
|
|
|
// Looks like we probably need more
|
2021-02-14 09:57:19 +01:00
|
|
|
size_t memory_size = page_round_up(decltype(m_global_heap.m_heap)::calculate_memory_for_bytes(allocation_request));
|
2020-09-18 09:49:51 +02:00
|
|
|
// Add some more to the new heap. We're already using it for other
|
2020-09-04 15:31:56 -06:00
|
|
|
// allocations not including the original allocation_request
|
|
|
|
// that triggered heap expansion. If we don't allocate
|
|
|
|
memory_size += 1 * MiB;
|
2021-02-14 01:25:22 +01:00
|
|
|
region = MM.allocate_kernel_region(memory_size, "kmalloc subheap", Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
|
2020-08-29 16:41:30 -06:00
|
|
|
if (region) {
|
2021-03-09 20:26:32 +01:00
|
|
|
dbgln("kmalloc: Adding even more memory to heap at {}, bytes: {}", region->vaddr(), region->size());
|
2020-08-29 16:41:30 -06:00
|
|
|
|
|
|
|
m_global_heap.m_heap.add_subheap(region->vaddr().as_ptr(), region->size());
|
|
|
|
m_global_heap.m_subheap_memory.append(region.release_nonnull());
|
|
|
|
} else {
|
2021-03-09 20:26:32 +01:00
|
|
|
dbgln("kmalloc: Could not expand heap to satisfy allocation of {} bytes", allocation_request);
|
2020-08-29 16:41:30 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool remove_memory(void* memory)
|
|
|
|
{
|
|
|
|
// This is actually relatively unlikely to happen, because it requires that all
|
|
|
|
// allocated memory in a subheap to be freed. Only then the subheap can be removed...
|
|
|
|
for (size_t i = 0; i < m_global_heap.m_subheap_memory.size(); i++) {
|
|
|
|
if (m_global_heap.m_subheap_memory[i].vaddr().as_ptr() == memory) {
|
|
|
|
auto region = m_global_heap.m_subheap_memory.take(i);
|
2020-09-01 16:04:23 -06:00
|
|
|
if (!m_global_heap.m_backup_memory) {
|
2021-03-11 15:26:02 +01:00
|
|
|
if constexpr (KMALLOC_DEBUG) {
|
|
|
|
dmesgln("kmalloc: Using removed memory as backup: {}, bytes: {}", region->vaddr(), region->size());
|
|
|
|
}
|
2020-09-01 16:04:23 -06:00
|
|
|
m_global_heap.m_backup_memory = move(region);
|
2020-11-01 13:11:31 -07:00
|
|
|
} else {
|
2021-03-11 15:26:02 +01:00
|
|
|
if constexpr (KMALLOC_DEBUG) {
|
|
|
|
dmesgln("kmalloc: Queue removing memory from heap at {}, bytes: {}", region->vaddr(), region->size());
|
|
|
|
}
|
2020-11-01 13:11:31 -07:00
|
|
|
Processor::deferred_call_queue([this, region = move(region)]() mutable {
|
|
|
|
// We need to defer freeing the region to prevent a potential
|
|
|
|
// deadlock since we are still holding the kmalloc lock
|
|
|
|
// We don't really need to do anything other than holding
|
|
|
|
// onto the region. Unless we already used the backup
|
|
|
|
// memory, in which case we want to use the region as the
|
|
|
|
// new backup.
|
|
|
|
ScopedSpinLock lock(s_lock);
|
|
|
|
if (!m_global_heap.m_backup_memory) {
|
2021-03-11 15:26:02 +01:00
|
|
|
if constexpr (KMALLOC_DEBUG) {
|
|
|
|
dmesgln("kmalloc: Queued memory region at {}, bytes: {} will be used as new backup", region->vaddr(), region->size());
|
|
|
|
}
|
2020-11-01 13:11:31 -07:00
|
|
|
m_global_heap.m_backup_memory = move(region);
|
|
|
|
} else {
|
2021-03-11 15:26:02 +01:00
|
|
|
if constexpr (KMALLOC_DEBUG) {
|
|
|
|
dmesgln("kmalloc: Queued memory region at {}, bytes: {} will be freed now", region->vaddr(), region->size());
|
|
|
|
}
|
2020-11-01 13:11:31 -07:00
|
|
|
}
|
|
|
|
});
|
2020-09-01 16:04:23 -06:00
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:26:02 +01:00
|
|
|
if constexpr (KMALLOC_DEBUG) {
|
|
|
|
dmesgln("kmalloc: Cannot remove memory from heap: {}", VirtualAddress(memory));
|
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef ExpandableHeap<CHUNK_SIZE, KMALLOC_SCRUB_BYTE, KFREE_SCRUB_BYTE, ExpandGlobalHeap> HeapType;
|
|
|
|
|
|
|
|
HeapType m_heap;
|
|
|
|
NonnullOwnPtrVector<Region> m_subheap_memory;
|
|
|
|
OwnPtr<Region> m_backup_memory;
|
|
|
|
|
|
|
|
KmallocGlobalHeap(u8* memory, size_t memory_size)
|
|
|
|
: m_heap(memory, memory_size, ExpandGlobalHeap(*this))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void allocate_backup_memory()
|
|
|
|
{
|
|
|
|
if (m_backup_memory)
|
|
|
|
return;
|
2021-02-14 01:25:22 +01:00
|
|
|
m_backup_memory = MM.allocate_kernel_region(1 * MiB, "kmalloc subheap", Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
|
2020-08-29 16:41:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t backup_memory_bytes() const
|
|
|
|
{
|
|
|
|
return m_backup_memory ? m_backup_memory->size() : 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-14 17:49:05 +01:00
|
|
|
READONLY_AFTER_INIT static KmallocGlobalHeap* g_kmalloc_global;
|
2021-07-01 11:29:28 +02:00
|
|
|
alignas(KmallocGlobalHeap) static u8 g_kmalloc_global_heap[sizeof(KmallocGlobalHeap)];
|
2020-08-29 16:41:30 -06:00
|
|
|
|
2021-01-20 17:49:55 +01:00
|
|
|
// Treat the heap as logically separate from .bss
|
|
|
|
__attribute__((section(".heap"))) static u8 kmalloc_eternal_heap[ETERNAL_RANGE_SIZE];
|
|
|
|
__attribute__((section(".heap"))) static u8 kmalloc_pool_heap[POOL_SIZE];
|
2020-08-24 16:38:20 -06:00
|
|
|
|
2020-08-29 16:41:30 -06:00
|
|
|
static size_t g_kmalloc_bytes_eternal = 0;
|
|
|
|
static size_t g_kmalloc_call_count;
|
|
|
|
static size_t g_kfree_call_count;
|
2021-05-13 13:09:00 +02:00
|
|
|
static size_t g_nested_kfree_calls;
|
2019-04-15 23:58:48 +02:00
|
|
|
bool g_dump_kmalloc_stacks;
|
2019-04-15 19:43:12 +02:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
static u8* s_next_eternal_ptr;
|
2021-02-14 17:49:05 +01:00
|
|
|
READONLY_AFTER_INIT static u8* s_end_of_eternal_range;
|
2018-11-02 20:41:58 +01:00
|
|
|
|
2020-12-30 17:00:49 -07:00
|
|
|
static void kmalloc_allocate_backup_memory()
|
|
|
|
{
|
|
|
|
g_kmalloc_global->allocate_backup_memory();
|
|
|
|
}
|
|
|
|
|
2020-08-29 16:41:30 -06:00
|
|
|
void kmalloc_enable_expand()
|
|
|
|
{
|
|
|
|
g_kmalloc_global->allocate_backup_memory();
|
|
|
|
}
|
|
|
|
|
2021-05-14 04:09:06 -07:00
|
|
|
static inline void kmalloc_verify_nospinlock_held()
|
|
|
|
{
|
|
|
|
// Catch bad callers allocating under spinlock.
|
|
|
|
if constexpr (KMALLOC_VERIFY_NO_SPINLOCK_HELD) {
|
|
|
|
VERIFY(!Processor::current().in_critical());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 18:41:50 +01:00
|
|
|
UNMAP_AFTER_INIT void kmalloc_init()
|
2018-10-16 11:01:38 +02:00
|
|
|
{
|
2021-01-20 17:49:55 +01:00
|
|
|
// Zero out heap since it's placed after end_of_kernel_bss.
|
|
|
|
memset(kmalloc_eternal_heap, 0, sizeof(kmalloc_eternal_heap));
|
|
|
|
memset(kmalloc_pool_heap, 0, sizeof(kmalloc_pool_heap));
|
|
|
|
g_kmalloc_global = new (g_kmalloc_global_heap) KmallocGlobalHeap(kmalloc_pool_heap, sizeof(kmalloc_pool_heap));
|
2018-10-16 11:01:38 +02:00
|
|
|
|
2020-08-29 16:41:30 -06:00
|
|
|
s_lock.initialize();
|
2018-10-31 23:19:15 +01:00
|
|
|
|
2021-01-20 17:49:55 +01:00
|
|
|
s_next_eternal_ptr = kmalloc_eternal_heap;
|
2021-06-28 13:45:57 +02:00
|
|
|
s_end_of_eternal_range = s_next_eternal_ptr + sizeof(kmalloc_eternal_heap);
|
2018-10-31 23:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void* kmalloc_eternal(size_t size)
|
|
|
|
{
|
2021-05-14 04:09:06 -07:00
|
|
|
kmalloc_verify_nospinlock_held();
|
|
|
|
|
2020-11-01 08:50:03 -07:00
|
|
|
size = round_up_to_power_of_two(size, sizeof(void*));
|
|
|
|
|
2020-06-05 22:01:30 -06:00
|
|
|
ScopedSpinLock lock(s_lock);
|
2018-10-31 23:19:15 +01:00
|
|
|
void* ptr = s_next_eternal_ptr;
|
|
|
|
s_next_eternal_ptr += size;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(s_next_eternal_ptr < s_end_of_eternal_range);
|
2020-05-16 10:37:31 +02:00
|
|
|
g_kmalloc_bytes_eternal += size;
|
2018-10-31 23:19:15 +01:00
|
|
|
return ptr;
|
2018-10-16 11:01:38 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:13:17 +01:00
|
|
|
void* kmalloc(size_t size)
|
2018-10-16 11:01:38 +02:00
|
|
|
{
|
2021-05-14 04:09:06 -07:00
|
|
|
kmalloc_verify_nospinlock_held();
|
2020-06-05 22:01:30 -06:00
|
|
|
ScopedSpinLock lock(s_lock);
|
2019-04-15 19:43:12 +02:00
|
|
|
++g_kmalloc_call_count;
|
2018-10-24 00:51:19 +02:00
|
|
|
|
2020-04-08 13:30:50 +02:00
|
|
|
if (g_dump_kmalloc_stacks && Kernel::g_kernel_symbols_available) {
|
2021-01-10 15:17:54 +01:00
|
|
|
dbgln("kmalloc({})", size);
|
2020-02-16 01:27:42 +01:00
|
|
|
Kernel::dump_backtrace();
|
2019-04-15 23:58:48 +02:00
|
|
|
}
|
|
|
|
|
2020-08-29 16:41:30 -06:00
|
|
|
void* ptr = g_kmalloc_global->m_heap.allocate(size);
|
|
|
|
if (!ptr) {
|
2021-02-22 02:35:36 +03:30
|
|
|
PANIC("kmalloc: Out of memory (requested size: {})", size);
|
2018-10-16 11:01:38 +02:00
|
|
|
}
|
|
|
|
|
2021-05-30 16:24:53 +02:00
|
|
|
Thread* current_thread = Thread::current();
|
|
|
|
if (!current_thread)
|
|
|
|
current_thread = Processor::idle_thread();
|
|
|
|
if (current_thread)
|
|
|
|
PerformanceManager::add_kmalloc_perf_event(*current_thread, size, (FlatPtr)ptr);
|
2021-05-13 13:09:00 +02:00
|
|
|
|
2020-08-29 16:41:30 -06:00
|
|
|
return ptr;
|
2018-10-16 11:01:38 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 13:19:33 +02:00
|
|
|
void kfree_sized(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
(void)size;
|
|
|
|
return kfree(ptr);
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:01:30 -06:00
|
|
|
void kfree(void* ptr)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
2021-05-14 04:09:06 -07:00
|
|
|
kmalloc_verify_nospinlock_held();
|
2020-06-05 22:01:30 -06:00
|
|
|
ScopedSpinLock lock(s_lock);
|
2020-08-29 16:41:30 -06:00
|
|
|
++g_kfree_call_count;
|
2021-05-13 13:09:00 +02:00
|
|
|
++g_nested_kfree_calls;
|
|
|
|
|
|
|
|
if (g_nested_kfree_calls == 1) {
|
2021-05-30 16:24:53 +02:00
|
|
|
Thread* current_thread = Thread::current();
|
|
|
|
if (!current_thread)
|
|
|
|
current_thread = Processor::idle_thread();
|
|
|
|
if (current_thread)
|
|
|
|
PerformanceManager::add_kfree_perf_event(*current_thread, 0, (FlatPtr)ptr);
|
2021-05-13 13:09:00 +02:00
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
|
|
|
|
g_kmalloc_global->m_heap.deallocate(ptr);
|
2021-05-13 13:09:00 +02:00
|
|
|
--g_nested_kfree_calls;
|
2020-06-05 22:01:30 -06:00
|
|
|
}
|
|
|
|
|
2021-05-15 10:06:41 +02:00
|
|
|
size_t kmalloc_good_size(size_t size)
|
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2021-07-15 12:08:35 +02:00
|
|
|
[[gnu::malloc, gnu::alloc_size(1), gnu::alloc_align(2)]] static void* kmalloc_aligned_cxx(size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
VERIFY(alignment <= 4096);
|
|
|
|
void* ptr = kmalloc(size + alignment + sizeof(ptrdiff_t));
|
|
|
|
size_t max_addr = (size_t)ptr + alignment;
|
|
|
|
void* aligned_ptr = (void*)(max_addr - (max_addr % alignment));
|
|
|
|
((ptrdiff_t*)aligned_ptr)[-1] = (ptrdiff_t)((u8*)aligned_ptr - (u8*)ptr);
|
|
|
|
return aligned_ptr;
|
|
|
|
}
|
|
|
|
|
AK+Kernel: Make fallible allocations compiler-agnostic
In standard C++, operators `new` and `new[]` are guaranteed to return a
valid (non-null) pointer and throw an exception if the allocation
couldn't be performed. Based on this, compilers did not check the
returned pointer before attempting to use them for object construction.
To avoid this, the allocator operators were changed to be `noexcept` in
PR #7026, which made GCC emit the desired null checks. Unfortunately,
this is a non-standard feature which meant that Clang would not accept
these function definitions, as it did not match its expected
declaration.
To make compiling using Clang possible, the special "nothrow" versions
of `new` are implemented in this commit. These take a tag type of
`std::nothrow_t` (used for disambiguating from placement new/etc.), and
are allowed by the standard to return null. There is a global variable,
`std::nothrow`, declared with this type, which is also exported into the
global namespace.
To perform fallible allocations, the following syntax should be used:
```cpp
auto ptr = new (nothrow) T;
```
As we don't support exceptions in the kernel, the only way of uphold the
"throwing" new's guarantee is to abort if the allocation couldn't be
performed. Once we have proper OOM handling in the kernel, this should
only be used for critical allocations, where we wouldn't be able to
recover from allocation failures anyway.
2021-06-20 09:39:20 +02:00
|
|
|
void* operator new(size_t size)
|
|
|
|
{
|
|
|
|
void* ptr = kmalloc(size);
|
|
|
|
VERIFY(ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new(size_t size, const std::nothrow_t&) noexcept
|
2018-10-16 11:01:38 +02:00
|
|
|
{
|
|
|
|
return kmalloc(size);
|
|
|
|
}
|
|
|
|
|
2021-07-15 12:08:35 +02:00
|
|
|
void* operator new(size_t size, std::align_val_t al)
|
|
|
|
{
|
|
|
|
void* ptr = kmalloc_aligned_cxx(size, (size_t)al);
|
|
|
|
VERIFY(ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new(size_t size, std::align_val_t al, const std::nothrow_t&) noexcept
|
|
|
|
{
|
|
|
|
return kmalloc_aligned_cxx(size, (size_t)al);
|
|
|
|
}
|
|
|
|
|
AK+Kernel: Make fallible allocations compiler-agnostic
In standard C++, operators `new` and `new[]` are guaranteed to return a
valid (non-null) pointer and throw an exception if the allocation
couldn't be performed. Based on this, compilers did not check the
returned pointer before attempting to use them for object construction.
To avoid this, the allocator operators were changed to be `noexcept` in
PR #7026, which made GCC emit the desired null checks. Unfortunately,
this is a non-standard feature which meant that Clang would not accept
these function definitions, as it did not match its expected
declaration.
To make compiling using Clang possible, the special "nothrow" versions
of `new` are implemented in this commit. These take a tag type of
`std::nothrow_t` (used for disambiguating from placement new/etc.), and
are allowed by the standard to return null. There is a global variable,
`std::nothrow`, declared with this type, which is also exported into the
global namespace.
To perform fallible allocations, the following syntax should be used:
```cpp
auto ptr = new (nothrow) T;
```
As we don't support exceptions in the kernel, the only way of uphold the
"throwing" new's guarantee is to abort if the allocation couldn't be
performed. Once we have proper OOM handling in the kernel, this should
only be used for critical allocations, where we wouldn't be able to
recover from allocation failures anyway.
2021-06-20 09:39:20 +02:00
|
|
|
void* operator new[](size_t size)
|
|
|
|
{
|
|
|
|
void* ptr = kmalloc(size);
|
|
|
|
VERIFY(ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new[](size_t size, const std::nothrow_t&) noexcept
|
2018-10-16 11:01:38 +02:00
|
|
|
{
|
|
|
|
return kmalloc(size);
|
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
|
2021-07-11 13:25:42 +02:00
|
|
|
void operator delete(void*) noexcept
|
2021-05-11 01:17:04 -07:00
|
|
|
{
|
2021-07-11 13:25:42 +02:00
|
|
|
// All deletes in kernel code should have a known size.
|
|
|
|
VERIFY_NOT_REACHED();
|
2021-05-11 01:17:04 -07:00
|
|
|
}
|
|
|
|
|
2021-07-11 13:19:33 +02:00
|
|
|
void operator delete(void* ptr, size_t size) noexcept
|
2021-05-11 01:17:04 -07:00
|
|
|
{
|
2021-07-11 13:19:33 +02:00
|
|
|
return kfree_sized(ptr, size);
|
2021-05-11 01:17:04 -07:00
|
|
|
}
|
|
|
|
|
2021-07-15 12:08:35 +02:00
|
|
|
void operator delete(void* ptr, size_t, std::align_val_t) noexcept
|
|
|
|
{
|
|
|
|
return kfree_aligned(ptr);
|
|
|
|
}
|
|
|
|
|
2021-07-11 13:25:42 +02:00
|
|
|
void operator delete[](void*) noexcept
|
2021-05-11 01:17:04 -07:00
|
|
|
{
|
2021-07-11 13:25:42 +02:00
|
|
|
// All deletes in kernel code should have a known size.
|
|
|
|
VERIFY_NOT_REACHED();
|
2021-05-11 01:17:04 -07:00
|
|
|
}
|
|
|
|
|
2021-07-11 13:19:33 +02:00
|
|
|
void operator delete[](void* ptr, size_t size) noexcept
|
2021-05-11 01:17:04 -07:00
|
|
|
{
|
2021-07-11 13:19:33 +02:00
|
|
|
return kfree_sized(ptr, size);
|
2021-05-11 01:17:04 -07:00
|
|
|
}
|
|
|
|
|
2020-08-29 16:41:30 -06:00
|
|
|
void get_kmalloc_stats(kmalloc_stats& stats)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(s_lock);
|
|
|
|
stats.bytes_allocated = g_kmalloc_global->m_heap.allocated_bytes();
|
|
|
|
stats.bytes_free = g_kmalloc_global->m_heap.free_bytes() + g_kmalloc_global->backup_memory_bytes();
|
|
|
|
stats.bytes_eternal = g_kmalloc_bytes_eternal;
|
|
|
|
stats.kmalloc_call_count = g_kmalloc_call_count;
|
|
|
|
stats.kfree_call_count = g_kfree_call_count;
|
|
|
|
}
|