summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-05-18 22:26:01 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-05-18 22:26:01 +0200
commitf5234660f6e7f0e505dd897bb67e1fedd954dd83 (patch)
tree7da8eba5d620c921166cb2d7d99f1845e216ee17
parent959c8f287c8794f71df52781c30f5fe6e1275612 (diff)
downloadserenity-f5234660f6e7f0e505dd897bb67e1fedd954dd83.zip
malloc: Use a Vector with inline capacity for the big block recyclers.
-rw-r--r--LibC/malloc.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/LibC/malloc.cpp b/LibC/malloc.cpp
index a2e91fa854..4ebd7cbefb 100644
--- a/LibC/malloc.cpp
+++ b/LibC/malloc.cpp
@@ -10,6 +10,8 @@
// FIXME: Thread safety.
//#define MALLOC_DEBUG
+#define RECYCLE_BIG_ALLOCATIONS
+
#define MALLOC_SCRUB_BYTE 0x85
#define FREE_SCRUB_BYTE 0x82
#define MAGIC_PAGE_HEADER 0x42657274
@@ -85,7 +87,7 @@ struct Allocator {
};
struct BigAllocator {
- Vector<BigAllocationBlock*> blocks;
+ Vector<BigAllocationBlock*, number_of_big_blocks_to_keep_around_per_size_class> blocks;
};
static Allocator g_allocators[num_size_classes];
@@ -110,7 +112,6 @@ static BigAllocator* big_allocator_for_size(size_t size)
return nullptr;
}
-
extern "C" {
size_t malloc_good_size(size_t size)
@@ -146,12 +147,14 @@ void* malloc(size_t size)
if (!allocator) {
size_t real_size = PAGE_ROUND_UP(sizeof(BigAllocationBlock) + size);
+#ifdef RECYCLE_BIG_ALLOCATIONS
if (auto* allocator = big_allocator_for_size(real_size)) {
if (!allocator->blocks.is_empty()) {
auto* block = allocator->blocks.take_last();
return &block->m_slot[0];
}
}
+#endif
auto* block = (BigAllocationBlock*)os_alloc(real_size);
char buffer[64];
snprintf(buffer, sizeof(buffer), "malloc: BigAllocationBlock(%u)", real_size);
@@ -205,12 +208,14 @@ void free(void* ptr)
if (magic == MAGIC_BIGALLOC_HEADER) {
auto* block = (BigAllocationBlock*)page_base;
+#ifdef RECYCLE_BIG_ALLOCATIONS
if (auto* allocator = big_allocator_for_size(block->m_size)) {
if (allocator->blocks.size() < number_of_big_blocks_to_keep_around_per_size_class) {
allocator->blocks.append(block);
return;
}
}
+#endif
os_free(block, block->m_size);
return;
}