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>
|
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-08-22 01:37:17 +02:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-08-06 10:45:34 +02:00
|
|
|
#include <Kernel/Memory/MemoryManager.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-05-16 12:00:04 +02:00
|
|
|
#include <Kernel/StdLib.h>
|
2018-10-16 11:01:38 +02:00
|
|
|
|
2021-10-26 10:37:36 +02:00
|
|
|
#if ARCH(I386)
|
|
|
|
static constexpr size_t CHUNK_SIZE = 32;
|
|
|
|
#else
|
|
|
|
static constexpr size_t CHUNK_SIZE = 64;
|
|
|
|
#endif
|
|
|
|
|
2020-08-29 16:41:30 -06:00
|
|
|
#define POOL_SIZE (2 * MiB)
|
2021-07-16 20:59:42 +02:00
|
|
|
#define ETERNAL_RANGE_SIZE (4 * 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;
|
|
|
|
}
|
|
|
|
|
2021-08-22 01:37:17 +02:00
|
|
|
static RecursiveSpinlock s_lock; // needs to be recursive because of dump_backtrace()
|
2020-11-01 13:11:31 -07:00
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
struct KmallocSubheap {
|
|
|
|
KmallocSubheap(u8* base, size_t size)
|
|
|
|
: allocator(base, size)
|
|
|
|
{
|
|
|
|
}
|
2020-12-30 17:00:49 -07:00
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
IntrusiveListNode<KmallocSubheap> list_node;
|
|
|
|
Heap<CHUNK_SIZE, KMALLOC_SCRUB_BYTE, KFREE_SCRUB_BYTE> allocator;
|
|
|
|
};
|
2020-08-29 16:41:30 -06:00
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
struct KmallocGlobalData {
|
|
|
|
KmallocGlobalData(u8* initial_heap, size_t initial_heap_size)
|
|
|
|
{
|
|
|
|
add_subheap(initial_heap, initial_heap_size);
|
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
void add_subheap(u8* storage, size_t storage_size)
|
|
|
|
{
|
2021-12-26 01:25:02 +01:00
|
|
|
dbgln("Adding kmalloc subheap @ {} with size {}", storage, storage_size);
|
2021-12-25 17:23:18 +01:00
|
|
|
auto* subheap = new (storage) KmallocSubheap(storage + PAGE_SIZE, storage_size - PAGE_SIZE);
|
|
|
|
subheaps.append(*subheap);
|
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
void* allocate(size_t size)
|
|
|
|
{
|
|
|
|
VERIFY(!expansion_in_progress);
|
2020-08-29 16:41:30 -06:00
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
for (auto& subheap : subheaps) {
|
|
|
|
if (auto* ptr = subheap.allocator.allocate(size))
|
|
|
|
return ptr;
|
2020-08-29 16:41:30 -06:00
|
|
|
}
|
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
if (!try_expand()) {
|
|
|
|
PANIC("OOM when trying to expand kmalloc heap.");
|
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
return allocate(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(void* ptr)
|
|
|
|
{
|
|
|
|
VERIFY(!expansion_in_progress);
|
|
|
|
|
|
|
|
for (auto& subheap : subheaps) {
|
|
|
|
if (subheap.allocator.contains(ptr)) {
|
|
|
|
subheap.allocator.deallocate(ptr);
|
|
|
|
return;
|
2021-03-11 15:26:02 +01:00
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
}
|
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
PANIC("Bogus pointer {:p} passed to kfree()", ptr);
|
|
|
|
}
|
2020-08-29 16:41:30 -06:00
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
size_t allocated_bytes() const
|
2020-08-29 16:41:30 -06:00
|
|
|
{
|
2021-12-25 17:23:18 +01:00
|
|
|
size_t total = 0;
|
|
|
|
for (auto const& subheap : subheaps)
|
|
|
|
total += subheap.allocator.allocated_bytes();
|
|
|
|
return total;
|
2020-08-29 16:41:30 -06:00
|
|
|
}
|
2021-12-25 17:23:18 +01:00
|
|
|
|
|
|
|
size_t free_bytes() const
|
2020-08-29 16:41:30 -06:00
|
|
|
{
|
2021-12-25 17:23:18 +01:00
|
|
|
size_t total = 0;
|
|
|
|
for (auto const& subheap : subheaps)
|
|
|
|
total += subheap.allocator.free_bytes();
|
|
|
|
return total;
|
2020-08-29 16:41:30 -06:00
|
|
|
}
|
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
bool try_expand()
|
2020-08-29 16:41:30 -06:00
|
|
|
{
|
2021-12-25 17:23:18 +01:00
|
|
|
VERIFY(!expansion_in_progress);
|
|
|
|
TemporaryChange change(expansion_in_progress, true);
|
|
|
|
|
|
|
|
auto new_subheap_base = expansion_data->next_virtual_address;
|
|
|
|
size_t new_subheap_size = 1 * MiB;
|
|
|
|
|
|
|
|
if (!expansion_data->virtual_range.contains(new_subheap_base, new_subheap_size)) {
|
|
|
|
// FIXME: Dare to return false and allow kmalloc() to fail!
|
|
|
|
PANIC("Out of address space when expanding kmalloc heap.");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto physical_pages_or_error = MM.commit_user_physical_pages(new_subheap_size / PAGE_SIZE);
|
|
|
|
if (physical_pages_or_error.is_error()) {
|
|
|
|
// FIXME: Dare to return false!
|
|
|
|
PANIC("Out of physical pages when expanding kmalloc heap.");
|
|
|
|
}
|
|
|
|
auto physical_pages = physical_pages_or_error.release_value();
|
|
|
|
|
|
|
|
expansion_data->next_virtual_address = expansion_data->next_virtual_address.offset(new_subheap_size);
|
|
|
|
|
2021-12-25 19:55:52 +01:00
|
|
|
auto cpu_supports_nx = Processor::current().has_feature(CPUFeature::NX);
|
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
SpinlockLocker mm_locker(Memory::s_mm_lock);
|
|
|
|
SpinlockLocker pd_locker(MM.kernel_page_directory().get_lock());
|
|
|
|
|
|
|
|
for (auto vaddr = new_subheap_base; !physical_pages.is_empty(); vaddr = vaddr.offset(PAGE_SIZE)) {
|
|
|
|
// FIXME: We currently leak physical memory when mapping it into the kmalloc heap.
|
|
|
|
auto& page = physical_pages.take_one().leak_ref();
|
2021-12-26 02:42:49 +01:00
|
|
|
auto* pte = MM.pte(MM.kernel_page_directory(), vaddr);
|
|
|
|
VERIFY(pte);
|
2021-12-25 17:23:18 +01:00
|
|
|
pte->set_physical_page_base(page.paddr().get());
|
|
|
|
pte->set_global(true);
|
|
|
|
pte->set_user_allowed(false);
|
|
|
|
pte->set_writable(true);
|
2021-12-25 19:55:52 +01:00
|
|
|
if (cpu_supports_nx)
|
|
|
|
pte->set_execute_disabled(true);
|
2021-12-25 17:23:18 +01:00
|
|
|
pte->set_present(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
MM.flush_tlb(&MM.kernel_page_directory(), new_subheap_base, new_subheap_size / PAGE_SIZE);
|
|
|
|
|
|
|
|
add_subheap(new_subheap_base.as_ptr(), new_subheap_size);
|
|
|
|
return true;
|
2020-08-29 16:41:30 -06:00
|
|
|
}
|
2021-12-25 17:23:18 +01:00
|
|
|
|
2021-12-26 02:42:49 +01:00
|
|
|
void enable_expansion()
|
|
|
|
{
|
|
|
|
// FIXME: This range can be much bigger on 64-bit, but we need to figure something out for 32-bit.
|
|
|
|
auto virtual_range = MM.kernel_page_directory().range_allocator().try_allocate_anywhere(64 * MiB, 1 * MiB);
|
|
|
|
|
|
|
|
expansion_data = KmallocGlobalData::ExpansionData {
|
|
|
|
.virtual_range = virtual_range.value(),
|
|
|
|
.next_virtual_address = virtual_range.value().base(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make sure the entire kmalloc VM range is backed by page tables.
|
|
|
|
// This avoids having to deal with lazy page table allocation during heap expansion.
|
|
|
|
SpinlockLocker mm_locker(Memory::s_mm_lock);
|
|
|
|
SpinlockLocker pd_locker(MM.kernel_page_directory().get_lock());
|
|
|
|
for (auto vaddr = virtual_range.value().base(); vaddr < virtual_range.value().end(); vaddr = vaddr.offset(PAGE_SIZE)) {
|
|
|
|
MM.ensure_pte(MM.kernel_page_directory(), vaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
struct ExpansionData {
|
|
|
|
Memory::VirtualRange virtual_range;
|
|
|
|
VirtualAddress next_virtual_address;
|
|
|
|
};
|
|
|
|
Optional<ExpansionData> expansion_data;
|
|
|
|
|
|
|
|
IntrusiveList<&KmallocSubheap::list_node> subheaps;
|
|
|
|
|
|
|
|
bool expansion_in_progress { false };
|
2020-08-29 16:41:30 -06:00
|
|
|
};
|
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
READONLY_AFTER_INIT static KmallocGlobalData* g_kmalloc_global;
|
|
|
|
alignas(KmallocGlobalData) static u8 g_kmalloc_global_heap[sizeof(KmallocGlobalData)];
|
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-08-29 16:41:30 -06:00
|
|
|
void kmalloc_enable_expand()
|
|
|
|
{
|
2021-12-26 02:42:49 +01:00
|
|
|
g_kmalloc_global->enable_expansion();
|
2020-08-29 16:41:30 -06:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-08-10 01:16:08 +02:00
|
|
|
VERIFY(!Processor::in_critical());
|
2021-05-14 04:09:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
2021-12-25 17:23:18 +01:00
|
|
|
g_kmalloc_global = new (g_kmalloc_global_heap) KmallocGlobalData(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*));
|
|
|
|
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker 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();
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker 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
|
|
|
}
|
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
void* ptr = g_kmalloc_global->allocate(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();
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker 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
|
|
|
|
2021-12-25 17:23:18 +01:00
|
|
|
g_kmalloc_global->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));
|
2021-08-13 12:39:14 +02:00
|
|
|
if (ptr == nullptr)
|
|
|
|
return nullptr;
|
2021-07-15 12:08:35 +02:00
|
|
|
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)
|
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(s_lock);
|
2021-12-25 17:23:18 +01:00
|
|
|
stats.bytes_allocated = g_kmalloc_global->allocated_bytes();
|
|
|
|
stats.bytes_free = g_kmalloc_global->free_bytes();
|
2020-08-29 16:41:30 -06:00
|
|
|
stats.bytes_eternal = g_kmalloc_bytes_eternal;
|
|
|
|
stats.kmalloc_call_count = g_kmalloc_call_count;
|
|
|
|
stats.kfree_call_count = g_kfree_call_count;
|
|
|
|
}
|