1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:07:45 +00:00

Kernel: Only deallocate memory when alloc succeeds

Also make AllocationHeader acquisition from pointers more verbose
This commit is contained in:
Hendiadyoin1 2021-07-01 15:47:22 +02:00 committed by Andreas Kling
parent 5f6c513610
commit 3694b8b690

View file

@ -29,6 +29,15 @@ class Heap {
static_assert(CHUNK_SIZE >= sizeof(AllocationHeader)); static_assert(CHUNK_SIZE >= sizeof(AllocationHeader));
ALWAYS_INLINE AllocationHeader* allocation_header(void* ptr)
{
return (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
}
ALWAYS_INLINE const AllocationHeader* allocation_header(const void* ptr) const
{
return (const AllocationHeader*)((((const u8*)ptr) - sizeof(AllocationHeader)));
}
static size_t calculate_chunks(size_t memory_size) static size_t calculate_chunks(size_t memory_size)
{ {
return (sizeof(u8) * memory_size) / (sizeof(u8) * CHUNK_SIZE + 1); return (sizeof(u8) * memory_size) / (sizeof(u8) * CHUNK_SIZE + 1);
@ -91,7 +100,7 @@ public:
{ {
if (!ptr) if (!ptr)
return; return;
auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader))); auto* a = allocation_header(ptr);
VERIFY((u8*)a >= m_chunks && (u8*)ptr < m_chunks + m_total_chunks * CHUNK_SIZE); VERIFY((u8*)a >= m_chunks && (u8*)ptr < m_chunks + m_total_chunks * CHUNK_SIZE);
FlatPtr start = ((FlatPtr)a - (FlatPtr)m_chunks) / CHUNK_SIZE; FlatPtr start = ((FlatPtr)a - (FlatPtr)m_chunks) / CHUNK_SIZE;
@ -115,7 +124,7 @@ public:
if (!ptr) if (!ptr)
return h.allocate(new_size); return h.allocate(new_size);
auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader))); auto* a = allocation_header(ptr);
VERIFY((u8*)a >= m_chunks && (u8*)ptr < m_chunks + m_total_chunks * CHUNK_SIZE); VERIFY((u8*)a >= m_chunks && (u8*)ptr < m_chunks + m_total_chunks * CHUNK_SIZE);
VERIFY((u8*)a + a->allocation_size_in_chunks * CHUNK_SIZE <= m_chunks + m_total_chunks * CHUNK_SIZE); VERIFY((u8*)a + a->allocation_size_in_chunks * CHUNK_SIZE <= m_chunks + m_total_chunks * CHUNK_SIZE);
@ -125,9 +134,10 @@ public:
return ptr; return ptr;
auto* new_ptr = h.allocate(new_size); auto* new_ptr = h.allocate(new_size);
if (new_ptr) if (new_ptr) {
__builtin_memcpy(new_ptr, ptr, min(old_size, new_size)); __builtin_memcpy(new_ptr, ptr, min(old_size, new_size));
deallocate(ptr); deallocate(ptr);
}
return new_ptr; return new_ptr;
} }
@ -138,7 +148,7 @@ public:
bool contains(const void* ptr) const bool contains(const void* ptr) const
{ {
const auto* a = (const AllocationHeader*)((((const u8*)ptr) - sizeof(AllocationHeader))); const auto* a = allocation_header(ptr);
if ((const u8*)a < m_chunks) if ((const u8*)a < m_chunks)
return false; return false;
if ((const u8*)ptr >= m_chunks + m_total_chunks * CHUNK_SIZE) if ((const u8*)ptr >= m_chunks + m_total_chunks * CHUNK_SIZE)