mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:27:35 +00:00
malloc: Use a Vector with inline capacity for the big block recyclers.
This commit is contained in:
parent
959c8f287c
commit
f5234660f6
1 changed files with 7 additions and 2 deletions
|
@ -10,6 +10,8 @@
|
||||||
// FIXME: Thread safety.
|
// FIXME: Thread safety.
|
||||||
|
|
||||||
//#define MALLOC_DEBUG
|
//#define MALLOC_DEBUG
|
||||||
|
#define RECYCLE_BIG_ALLOCATIONS
|
||||||
|
|
||||||
#define MALLOC_SCRUB_BYTE 0x85
|
#define MALLOC_SCRUB_BYTE 0x85
|
||||||
#define FREE_SCRUB_BYTE 0x82
|
#define FREE_SCRUB_BYTE 0x82
|
||||||
#define MAGIC_PAGE_HEADER 0x42657274
|
#define MAGIC_PAGE_HEADER 0x42657274
|
||||||
|
@ -85,7 +87,7 @@ struct Allocator {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BigAllocator {
|
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];
|
static Allocator g_allocators[num_size_classes];
|
||||||
|
@ -110,7 +112,6 @@ static BigAllocator* big_allocator_for_size(size_t size)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
size_t malloc_good_size(size_t size)
|
size_t malloc_good_size(size_t size)
|
||||||
|
@ -146,12 +147,14 @@ void* malloc(size_t size)
|
||||||
|
|
||||||
if (!allocator) {
|
if (!allocator) {
|
||||||
size_t real_size = PAGE_ROUND_UP(sizeof(BigAllocationBlock) + size);
|
size_t real_size = PAGE_ROUND_UP(sizeof(BigAllocationBlock) + size);
|
||||||
|
#ifdef RECYCLE_BIG_ALLOCATIONS
|
||||||
if (auto* allocator = big_allocator_for_size(real_size)) {
|
if (auto* allocator = big_allocator_for_size(real_size)) {
|
||||||
if (!allocator->blocks.is_empty()) {
|
if (!allocator->blocks.is_empty()) {
|
||||||
auto* block = allocator->blocks.take_last();
|
auto* block = allocator->blocks.take_last();
|
||||||
return &block->m_slot[0];
|
return &block->m_slot[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
auto* block = (BigAllocationBlock*)os_alloc(real_size);
|
auto* block = (BigAllocationBlock*)os_alloc(real_size);
|
||||||
char buffer[64];
|
char buffer[64];
|
||||||
snprintf(buffer, sizeof(buffer), "malloc: BigAllocationBlock(%u)", real_size);
|
snprintf(buffer, sizeof(buffer), "malloc: BigAllocationBlock(%u)", real_size);
|
||||||
|
@ -205,12 +208,14 @@ void free(void* ptr)
|
||||||
|
|
||||||
if (magic == MAGIC_BIGALLOC_HEADER) {
|
if (magic == MAGIC_BIGALLOC_HEADER) {
|
||||||
auto* block = (BigAllocationBlock*)page_base;
|
auto* block = (BigAllocationBlock*)page_base;
|
||||||
|
#ifdef RECYCLE_BIG_ALLOCATIONS
|
||||||
if (auto* allocator = big_allocator_for_size(block->m_size)) {
|
if (auto* allocator = big_allocator_for_size(block->m_size)) {
|
||||||
if (allocator->blocks.size() < number_of_big_blocks_to_keep_around_per_size_class) {
|
if (allocator->blocks.size() < number_of_big_blocks_to_keep_around_per_size_class) {
|
||||||
allocator->blocks.append(block);
|
allocator->blocks.append(block);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
os_free(block, block->m_size);
|
os_free(block, block->m_size);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue