mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:07:45 +00:00
Kernel: Serialize debug output
This commit is contained in:
parent
1da7fea602
commit
3ac6d31b45
5 changed files with 177 additions and 20 deletions
|
@ -28,6 +28,7 @@
|
|||
#include <AK/Memory.h>
|
||||
#include <Kernel/Heap/SlabAllocator.h>
|
||||
#include <Kernel/Heap/kmalloc.h>
|
||||
#include <Kernel/SpinLock.h>
|
||||
#include <Kernel/VM/Region.h>
|
||||
|
||||
#define SANITIZE_SLABS
|
||||
|
@ -50,22 +51,22 @@ public:
|
|||
}
|
||||
slabs[0].next = nullptr;
|
||||
m_freelist = &slabs[slab_count - 1];
|
||||
m_num_allocated = 0;
|
||||
m_num_free = slab_count;
|
||||
m_num_allocated.store(0, AK::MemoryOrder::memory_order_release);
|
||||
m_num_free.store(slab_count, AK::MemoryOrder::memory_order_release);
|
||||
}
|
||||
|
||||
constexpr size_t slab_size() const { return templated_slab_size; }
|
||||
|
||||
void* alloc()
|
||||
{
|
||||
InterruptDisabler disabler;
|
||||
ScopedSpinLock lock(m_lock);
|
||||
if (!m_freelist)
|
||||
return kmalloc(slab_size());
|
||||
ASSERT(m_freelist);
|
||||
void* ptr = m_freelist;
|
||||
m_freelist = m_freelist->next;
|
||||
++m_num_allocated;
|
||||
--m_num_free;
|
||||
m_num_allocated.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
|
||||
m_num_free.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
|
||||
#ifdef SANITIZE_SLABS
|
||||
memset(ptr, SLAB_ALLOC_SCRUB_BYTE, slab_size());
|
||||
#endif
|
||||
|
@ -74,7 +75,7 @@ public:
|
|||
|
||||
void dealloc(void* ptr)
|
||||
{
|
||||
InterruptDisabler disabler;
|
||||
ScopedSpinLock lock(m_lock);
|
||||
ASSERT(ptr);
|
||||
if (ptr < m_base || ptr >= m_end) {
|
||||
kfree(ptr);
|
||||
|
@ -86,12 +87,12 @@ public:
|
|||
memset(((FreeSlab*)ptr)->padding, SLAB_DEALLOC_SCRUB_BYTE, sizeof(FreeSlab::padding));
|
||||
#endif
|
||||
m_freelist = (FreeSlab*)ptr;
|
||||
++m_num_allocated;
|
||||
--m_num_free;
|
||||
m_num_allocated.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
|
||||
m_num_free.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
|
||||
}
|
||||
|
||||
size_t num_allocated() const { return m_num_allocated; }
|
||||
size_t num_free() const { return m_num_free; }
|
||||
size_t num_allocated() const { return m_num_allocated.load(AK::MemoryOrder::memory_order_consume); }
|
||||
size_t num_free() const { return m_num_free.load(AK::MemoryOrder::memory_order_consume); }
|
||||
|
||||
private:
|
||||
struct FreeSlab {
|
||||
|
@ -101,10 +102,11 @@ private:
|
|||
|
||||
// NOTE: These are not default-initialized to prevent an init-time constructor from overwriting them
|
||||
FreeSlab* m_freelist;
|
||||
size_t m_num_allocated;
|
||||
size_t m_num_free;
|
||||
Atomic<size_t> m_num_allocated;
|
||||
Atomic<size_t> m_num_free;
|
||||
void* m_base;
|
||||
void* m_end;
|
||||
SpinLock<u32> m_lock;
|
||||
|
||||
static_assert(sizeof(FreeSlab) == templated_slab_size);
|
||||
};
|
||||
|
|
|
@ -38,6 +38,7 @@
|
|||
#include <Kernel/KSyms.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/Scheduler.h>
|
||||
#include <Kernel/SpinLock.h>
|
||||
#include <Kernel/StdLib.h>
|
||||
|
||||
#define SANITIZE_KMALLOC
|
||||
|
@ -66,10 +67,13 @@ bool g_dump_kmalloc_stacks;
|
|||
static u8* s_next_eternal_ptr;
|
||||
static u8* s_end_of_eternal_range;
|
||||
|
||||
static SpinLock s_lock;
|
||||
|
||||
void kmalloc_init()
|
||||
{
|
||||
memset(&alloc_map, 0, sizeof(alloc_map));
|
||||
memset((void*)BASE_PHYSICAL, 0, POOL_SIZE);
|
||||
s_lock.initialize();
|
||||
|
||||
g_kmalloc_bytes_eternal = 0;
|
||||
g_kmalloc_bytes_allocated = 0;
|
||||
|
@ -81,6 +85,7 @@ void kmalloc_init()
|
|||
|
||||
void* kmalloc_eternal(size_t size)
|
||||
{
|
||||
ScopedSpinLock lock(s_lock);
|
||||
void* ptr = s_next_eternal_ptr;
|
||||
s_next_eternal_ptr += size;
|
||||
ASSERT(s_next_eternal_ptr < s_end_of_eternal_range);
|
||||
|
@ -129,7 +134,7 @@ inline void* kmalloc_allocate(size_t first_chunk, size_t chunks_needed)
|
|||
|
||||
void* kmalloc_impl(size_t size)
|
||||
{
|
||||
Kernel::InterruptDisabler disabler;
|
||||
ScopedSpinLock lock(s_lock);
|
||||
++g_kmalloc_call_count;
|
||||
|
||||
if (g_dump_kmalloc_stacks && Kernel::g_kernel_symbols_available) {
|
||||
|
@ -168,12 +173,8 @@ void* kmalloc_impl(size_t size)
|
|||
return kmalloc_allocate(first_chunk.value(), chunks_needed);
|
||||
}
|
||||
|
||||
void kfree(void* ptr)
|
||||
static inline void kfree_impl(void* ptr)
|
||||
{
|
||||
if (!ptr)
|
||||
return;
|
||||
|
||||
Kernel::InterruptDisabler disabler;
|
||||
++g_kfree_call_count;
|
||||
|
||||
auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
|
||||
|
@ -190,12 +191,21 @@ void kfree(void* ptr)
|
|||
#endif
|
||||
}
|
||||
|
||||
void kfree(void* ptr)
|
||||
{
|
||||
if (!ptr)
|
||||
return;
|
||||
|
||||
ScopedSpinLock lock(s_lock);
|
||||
kfree_impl(ptr);
|
||||
}
|
||||
|
||||
void* krealloc(void* ptr, size_t new_size)
|
||||
{
|
||||
if (!ptr)
|
||||
return kmalloc(new_size);
|
||||
|
||||
Kernel::InterruptDisabler disabler;
|
||||
ScopedSpinLock lock(s_lock);
|
||||
|
||||
auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
|
||||
size_t old_size = a->allocation_size_in_chunks * CHUNK_SIZE;
|
||||
|
@ -205,7 +215,7 @@ void* krealloc(void* ptr, size_t new_size)
|
|||
|
||||
auto* new_ptr = kmalloc(new_size);
|
||||
memcpy(new_ptr, ptr, min(old_size, new_size));
|
||||
kfree(ptr);
|
||||
kfree_impl(ptr);
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue