diff options
-rw-r--r-- | AK/SimpleMalloc.cpp | 256 | ||||
-rw-r--r-- | AK/SimpleMalloc.h | 15 | ||||
-rw-r--r-- | AK/kmalloc.cpp | 87 | ||||
-rw-r--r-- | AK/kmalloc.h | 7 | ||||
-rw-r--r-- | Kernel/Ext2FileSystem.cpp | 1 | ||||
-rw-r--r-- | Kernel/MemoryManager.cpp | 1 | ||||
-rw-r--r-- | Kernel/VirtualFileSystem.cpp | 1 |
7 files changed, 0 insertions, 368 deletions
diff --git a/AK/SimpleMalloc.cpp b/AK/SimpleMalloc.cpp deleted file mode 100644 index 295e841922..0000000000 --- a/AK/SimpleMalloc.cpp +++ /dev/null @@ -1,256 +0,0 @@ -#include "SimpleMalloc.h" -#include "Assertions.h" -#include "Types.h" -#include <sys/mman.h> -#include <cstring> -#include <cstdio> - -namespace SimpleMalloc { - -class AllocationBitmap { -public: - static AllocationBitmap wrap(byte* data, unsigned size) - { - return AllocationBitmap(data, size); - } - - ~AllocationBitmap() - { - } - - unsigned size() const { return m_size; } - bool get(unsigned index) const - { - ASSERT(index < m_size); - return 0 != (m_data[index / 8] & (1u << (index % 8))); - } - void set(unsigned index, bool value) const - { - ASSERT(index < m_size); - if (value) - m_data[index / 8] |= static_cast<byte>((1u << (index % 8))); - else - m_data[index / 8] &= static_cast<byte>(~(1u << (index % 8))); - } - -private: - AllocationBitmap(byte* data, unsigned size) - : m_data(data) - , m_size(size) - { - } - - byte* m_data { nullptr }; - unsigned m_size { 0 }; -}; - -template<dword chunkSize> -class ChunkAllocator { -public: - void initialize(byte* base) - { - m_base = base; - m_free = capacity_in_allocations(); - dump(); - } - - static constexpr dword capacity_in_allocations() - { - return 1048576 / chunkSize; - } - - static constexpr dword capacity_in_bytes() - { - return capacity_in_allocations() * chunkSize; - } - - byte* allocate() - { - auto bitmap = this->bitmap(); - for (dword i = 0; i < capacity_in_allocations(); ++i) { - if (!bitmap.get(i)) { - bitmap.set(i, true); - --m_free; - return pointer_to_chunk(i); - } - } - return nullptr; - } - - void dump() const - { - printf("ChunkAllocator<%u> @ %p, free: %u\n", chunkSize, m_base, m_free); - } - - void free(byte* ptr) - { - ASSERT(is_in_allocator(ptr)); - auto bitmap = this->bitmap(); - auto chunk_index = chunk_index_from_pointer(ptr); - ASSERT(bitmap.get(chunk_index)); - bitmap.set(chunk_index, false); - ++m_free; - } - - bool is_in_allocator(byte* ptr) - { - return ptr >= pointer_to_chunk(0) && ptr <= address_after_this_allocator(); - } - - dword chunk_index_from_pointer(byte* ptr) - { - return (ptr - pointer_to_chunk(0)) / chunkSize; - } - - byte* pointer_to_chunk(dword index) - { - return m_base + size_of_allocation_bitmap_in_bytes() + (index * chunkSize); - } - - AllocationBitmap bitmap() - { - return AllocationBitmap::wrap(m_base, capacity_in_allocations()); - } - - static constexpr dword size_of_allocation_bitmap_in_bytes() - { - return capacity_in_allocations() / 8; - } - - byte* address_after_this_allocator() const - { - return m_base + size_of_allocation_bitmap_in_bytes() + capacity_in_bytes(); - } - - dword number_of_free_chunks() const - { - return m_free; - } - -private: - byte* m_base { nullptr }; - dword m_free { capacity_in_allocations() }; -}; - -struct Allocator { - void initialize(); - void initialize_if_needed(); - void dump(); - - ChunkAllocator<8> alloc8; - ChunkAllocator<16> alloc16; - ChunkAllocator<4096> alloc4096; - ChunkAllocator<16384> alloc16384; - - byte* space; - bool initialized { false }; -}; - -static Allocator allocator; - -void Allocator::initialize_if_needed() -{ - if (initialized) - return; - initialize(); - initialized = true; -} - -void Allocator::initialize() -{ - space = (byte*)mmap((void*)0x20000000, 32 * MB, PROT_WRITE | PROT_READ | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - ASSERT(space != MAP_FAILED); - alloc8.initialize(space + 0x10000); - alloc16.initialize(alloc8.address_after_this_allocator()); - alloc4096.initialize(alloc16.address_after_this_allocator()); - alloc16384.initialize(alloc4096.address_after_this_allocator()); -} - -void Allocator::dump() -{ - alloc8.dump(); - alloc16.dump(); - alloc4096.dump(); - alloc16384.dump(); -} - -void initialize() -{ - allocator.initialize(); -} - -void dump() -{ - allocator.dump(); -} - -byte* allocate(dword size) -{ - if (!size) - return nullptr; - allocator.initialize_if_needed(); - if (size <= 8) { - if (auto* ptr = allocator.alloc8.allocate()) - return ptr; - } - if (size <= 16) { - if (auto* ptr = allocator.alloc16.allocate()) - return ptr; - } - if (size <= 4096) { - if (auto* ptr = allocator.alloc4096.allocate()) - return ptr; - } - if (size <= 16384) { - if (auto* ptr = allocator.alloc16384.allocate()) - return ptr; - } - printf("SimpleMalloc: unsupported alloc size: %u\n", size); - ASSERT_NOT_REACHED(); - return nullptr; -} - -byte* allocate_zeroed(dword size) -{ - auto* ptr = allocate(size); - if (!ptr) - return nullptr; - memset(ptr, 0, size); - return ptr; -} - -byte* reallocate(byte* ptr, dword size) -{ - // FIXME; - (void) ptr; - (void) size; - ASSERT_NOT_REACHED(); - return nullptr; -} - -void free(byte* ptr) -{ - if (!ptr) - return; - allocator.initialize_if_needed(); - if (allocator.alloc8.is_in_allocator(ptr)) { - allocator.alloc8.free(ptr); - return; - } - if (allocator.alloc16.is_in_allocator(ptr)) { - allocator.alloc16.free(ptr); - return; - } - if (allocator.alloc4096.is_in_allocator(ptr)) { - allocator.alloc4096.free(ptr); - return; - } - if (allocator.alloc16384.is_in_allocator(ptr)) { - allocator.alloc16384.free(ptr); - return; - } - ASSERT_NOT_REACHED(); -} - -} - diff --git a/AK/SimpleMalloc.h b/AK/SimpleMalloc.h deleted file mode 100644 index 84b214533b..0000000000 --- a/AK/SimpleMalloc.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include "Types.h" - -namespace SimpleMalloc { - -void initialize(); -void dump(); -byte* allocate(dword); -byte* allocate_zeroed(dword); -void free(byte*); -byte* reallocate(byte*, dword); - -} - diff --git a/AK/kmalloc.cpp b/AK/kmalloc.cpp index e08c7fd8cb..f51bfd2910 100644 --- a/AK/kmalloc.cpp +++ b/AK/kmalloc.cpp @@ -1,22 +1,9 @@ #include "kmalloc.h" #ifndef SERENITY -#include <cstdio> #include <cstdlib> #endif -#if defined(SERENITY) && defined(USERLAND) -#define USE_SYSTEM_MALLOC -#endif - -#define USE_SYSTEM_MALLOC - -#ifndef USE_SYSTEM_MALLOC -#include "SimpleMalloc.h" -#endif - -#ifdef USE_SYSTEM_MALLOC - extern "C" { void* kcalloc(size_t nmemb, size_t size) @@ -45,77 +32,3 @@ void* kmalloc_eternal(size_t size) } } - -#else - -extern "C" { - -void* kcalloc(size_t nmemb, size_t size) -{ - if (!nmemb || !size) - return nullptr; - return SimpleMalloc::allocate_zeroed(nmemb * size); -} - -void* kmalloc(size_t size) -{ - if (!size) - return nullptr; - return SimpleMalloc::allocate(size); -} - -void* kmalloc_eternal(size_t size) -{ - if (!size) - return nullptr; - return SimpleMalloc::allocate(size); -} - -void kfree(void* ptr) -{ - if (!ptr) - return; - SimpleMalloc::free((byte*)ptr); -} - -void* krealloc(void* ptr, size_t size) -{ - if (!ptr) - return ptr; - return SimpleMalloc::reallocate((byte*)ptr, size); -} - -} - -void* operator new(std::size_t size) -{ - return kmalloc(size); -} - -void* operator new[](std::size_t size) -{ - return kmalloc(size); -} - -void operator delete(void* ptr) -{ - return kfree(ptr); -} - -void operator delete[](void* ptr) -{ - return kfree(ptr); -} - -void operator delete(void* ptr, size_t) -{ - return kfree(ptr); -} - -void operator delete[](void* ptr, size_t) -{ - return kfree(ptr); -} - -#endif - diff --git a/AK/kmalloc.h b/AK/kmalloc.h index e1bd7126f7..3b28b0a4f4 100644 --- a/AK/kmalloc.h +++ b/AK/kmalloc.h @@ -27,11 +27,6 @@ void kfree(void* ptr); } -#ifdef KERNEL -inline void* operator new(size_t, void* p) { return p; } -inline void* operator new[](size_t, void* p) { return p; } -#else - inline void* operator new(size_t size) { return kmalloc(size); @@ -53,5 +48,3 @@ inline void operator delete[](void* ptr) } #endif - -#endif diff --git a/Kernel/Ext2FileSystem.cpp b/Kernel/Ext2FileSystem.cpp index c1a91da68f..e39d8c52f9 100644 --- a/Kernel/Ext2FileSystem.cpp +++ b/Kernel/Ext2FileSystem.cpp @@ -4,7 +4,6 @@ #include "RTC.h" #include <AK/Bitmap.h> #include <AK/StdLibExtras.h> -#include <AK/kmalloc.h> #include <AK/ktime.h> #include <AK/kstdio.h> #include <AK/BufferStream.h> diff --git a/Kernel/MemoryManager.cpp b/Kernel/MemoryManager.cpp index e348f5561d..03b95f07c3 100644 --- a/Kernel/MemoryManager.cpp +++ b/Kernel/MemoryManager.cpp @@ -1,7 +1,6 @@ #include "MemoryManager.h" #include <AK/Assertions.h> #include <AK/kstdio.h> -#include <AK/kmalloc.h> #include "i386.h" #include "StdLib.h" #include "Process.h" diff --git a/Kernel/VirtualFileSystem.cpp b/Kernel/VirtualFileSystem.cpp index fa26e8c1a0..9cbf8be20a 100644 --- a/Kernel/VirtualFileSystem.cpp +++ b/Kernel/VirtualFileSystem.cpp @@ -3,7 +3,6 @@ #include "FileSystem.h" #include <AK/FileSystemPath.h> #include <AK/StringBuilder.h> -#include <AK/kmalloc.h> #include <AK/kstdio.h> #include <AK/ktime.h> #include "CharacterDevice.h" |