1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:48:11 +00:00

LibC: Implement a simple freelist-based malloc() with size classes.

It's not thread-safe yet, and there is lots of room for improvement.
Still it's a lot faster than the first-fit bitmap-based one it replaces.
This commit is contained in:
Andreas Kling 2019-05-02 02:23:39 +02:00
parent d3bd4fdcfe
commit 4291e96991
3 changed files with 230 additions and 173 deletions

View file

@ -16,178 +16,6 @@
extern "C" {
#define MALLOC_SCRUB_BYTE 0x85
#define FREE_SCRUB_BYTE 0x82
struct MallocHeader {
uint16_t first_chunk_index;
uint16_t chunk_count : 15;
bool is_mmap : 1;
size_t size;
};
#define CHUNK_SIZE 32
#define POOL_SIZE 4 * 1048576
static const size_t malloc_budget = POOL_SIZE;
static byte s_malloc_map[POOL_SIZE / CHUNK_SIZE / 8];
static byte* s_malloc_pool;
static uint32_t s_malloc_sum_alloc = 0;
static uint32_t s_malloc_sum_free = POOL_SIZE;
static bool s_log_malloc = false;
static bool s_scrub_malloc = true;
static bool s_scrub_free = true;
void* malloc(size_t size)
{
if (s_log_malloc)
dbgprintf("LibC: malloc(%u)\n", size);
if (size == 0)
return nullptr;
// We need space for the MallocHeader structure at the head of the block.
size_t real_size = size + sizeof(MallocHeader);
if (real_size >= PAGE_SIZE) {
auto* memory = mmap(nullptr, real_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
if (memory == MAP_FAILED) {
fprintf(stderr, "malloc() failed to mmap() for a %u-byte allocation: %s", size, strerror(errno));
volatile char* crashme = (char*)0xf007d00d;
*crashme = 0;
return nullptr;
}
auto* header = (MallocHeader*)(memory);
byte* ptr = ((byte*)header) + sizeof(MallocHeader);
header->chunk_count = 0;
header->first_chunk_index = 0;
header->size = real_size;
header->is_mmap = true;
return ptr;
}
if (s_malloc_sum_free < real_size) {
fprintf(stderr, "malloc(): Out of memory\ns_malloc_sum_free=%u, real_size=%u\n", s_malloc_sum_free, real_size);
ASSERT_NOT_REACHED();
}
size_t chunks_needed = real_size / CHUNK_SIZE;
if (real_size % CHUNK_SIZE)
chunks_needed++;
size_t chunks_here = 0;
size_t first_chunk = 0;
for (unsigned i = 0; i < (POOL_SIZE / CHUNK_SIZE / 8); ++i) {
if (s_malloc_map[i] == 0xff) {
// Skip over completely full bucket.
chunks_here = 0;
continue;
}
// FIXME: This scan can be optimized further with TZCNT.
for (unsigned j = 0; j < 8; ++j) {
if ((s_malloc_map[i] & (1<<j))) {
// This is in use, so restart chunks_here counter.
chunks_here = 0;
continue;
}
if (chunks_here == 0) {
// Mark where potential allocation starts.
first_chunk = i * 8 + j;
}
++chunks_here;
if (chunks_here == chunks_needed) {
auto* header = (MallocHeader*)(s_malloc_pool + (first_chunk * CHUNK_SIZE));
byte* ptr = ((byte*)header) + sizeof(MallocHeader);
header->chunk_count = chunks_needed;
header->first_chunk_index = first_chunk;
header->is_mmap = false;
header->size = size;
for (size_t k = first_chunk; k < (first_chunk + chunks_needed); ++k)
s_malloc_map[k / 8] |= 1 << (k % 8);
s_malloc_sum_alloc += header->chunk_count * CHUNK_SIZE;
s_malloc_sum_free -= header->chunk_count * CHUNK_SIZE;
if (s_scrub_malloc)
memset(ptr, MALLOC_SCRUB_BYTE, (header->chunk_count * CHUNK_SIZE) - sizeof(MallocHeader));
return ptr;
}
}
}
fprintf(stderr, "malloc(): Out of memory (no consecutive chunks found for size %u)\n", size);
volatile char* crashme = (char*)0xc007d00d;
*crashme = 0;
return nullptr;
}
void free(void* ptr)
{
if (!ptr)
return;
auto* header = (MallocHeader*)((((byte*)ptr) - sizeof(MallocHeader)));
if (header->is_mmap) {
int rc = munmap(header, header->size);
if (rc < 0)
fprintf(stderr, "free(): munmap(%p) for allocation %p with size %u failed: %s\n", header, ptr, header->size, strerror(errno));
return;
}
for (int i = header->first_chunk_index; i < (header->first_chunk_index + header->chunk_count); ++i)
s_malloc_map[i / 8] &= ~(1 << (i % 8));
s_malloc_sum_alloc -= header->chunk_count * CHUNK_SIZE;
s_malloc_sum_free += header->chunk_count * CHUNK_SIZE;
if (s_scrub_free)
memset(header, FREE_SCRUB_BYTE, header->chunk_count * CHUNK_SIZE);
}
void __malloc_init()
{
s_malloc_pool = (byte*)mmap(nullptr, malloc_budget, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
int rc = set_mmap_name(s_malloc_pool, malloc_budget, "malloc pool");
if (rc < 0)
perror("set_mmap_name failed");
if (getenv("LIBC_NOSCRUB_MALLOC"))
s_scrub_malloc = false;
if (getenv("LIBC_NOSCRUB_FREE"))
s_scrub_free = false;
if (getenv("LIBC_LOG_MALLOC"))
s_log_malloc = true;
}
void* calloc(size_t count, size_t size)
{
size_t new_size = count * size;
auto* ptr = malloc(new_size);
memset(ptr, 0, new_size);
return ptr;
}
void* realloc(void *ptr, size_t size)
{
if (!ptr)
return malloc(size);
auto* header = (MallocHeader*)((((byte*)ptr) - sizeof(MallocHeader)));
size_t old_size = header->size;
if (size == old_size)
return ptr;
auto* new_ptr = malloc(size);
memcpy(new_ptr, ptr, min(old_size, size));
free(ptr);
return new_ptr;
}
typedef void(*__atexit_handler)();
static int __atexit_handler_count = 0;
static __atexit_handler __atexit_handlers[32];