2023-09-23 02:26:23 +03:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static consteval size_t log_size_t(size_t value, size_t base)
|
|
|
|
{
|
|
|
|
size_t result = 0;
|
|
|
|
while (value /= base)
|
|
|
|
result++;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr size_t s_malloc_pool_size_initial = 4096;
|
|
|
|
static constexpr size_t s_malloc_pool_size_multiplier = 2;
|
|
|
|
static constexpr size_t s_malloc_pool_count = sizeof(size_t) * 8 - log_size_t(s_malloc_pool_size_initial, s_malloc_pool_size_multiplier);
|
2023-12-14 23:42:25 +02:00
|
|
|
static constexpr size_t s_malloc_default_align = alignof(max_align_t);
|
|
|
|
// This is indirectly smallest allowed allocation
|
|
|
|
static constexpr size_t s_malloc_shrink_threshold = 64;
|
2023-09-23 02:26:23 +03:00
|
|
|
|
|
|
|
struct malloc_node_t
|
|
|
|
{
|
2023-12-14 23:42:25 +02:00
|
|
|
// TODO: these two pointers could be put into data region
|
|
|
|
malloc_node_t* prev_free;
|
|
|
|
malloc_node_t* next_free;
|
|
|
|
size_t size;
|
2023-09-23 02:26:23 +03:00
|
|
|
bool allocated;
|
2023-12-14 15:14:55 +02:00
|
|
|
bool last;
|
2023-12-14 23:42:25 +02:00
|
|
|
alignas(s_malloc_default_align) uint8_t data[0];
|
2023-09-23 02:26:23 +03:00
|
|
|
|
|
|
|
size_t data_size() const { return size - sizeof(malloc_node_t); }
|
|
|
|
malloc_node_t* next() { return (malloc_node_t*)(data + data_size()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct malloc_pool_t
|
|
|
|
{
|
|
|
|
uint8_t* start;
|
|
|
|
size_t size;
|
2023-12-14 15:14:55 +02:00
|
|
|
|
2023-12-14 23:42:25 +02:00
|
|
|
malloc_node_t* free_list;
|
2023-12-14 15:14:55 +02:00
|
|
|
|
|
|
|
uint8_t* end() { return start + size; }
|
|
|
|
bool contains(malloc_node_t* node) { return start <= (uint8_t*)node && (uint8_t*)node < end(); }
|
2023-09-23 02:26:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static malloc_pool_t s_malloc_pools[s_malloc_pool_count];
|
|
|
|
|
|
|
|
void init_malloc()
|
|
|
|
{
|
|
|
|
size_t pool_size = s_malloc_pool_size_initial;
|
|
|
|
for (size_t i = 0; i < s_malloc_pool_count; i++)
|
|
|
|
{
|
|
|
|
s_malloc_pools[i].start = nullptr;
|
|
|
|
s_malloc_pools[i].size = pool_size;
|
2023-12-14 23:42:25 +02:00
|
|
|
s_malloc_pools[i].free_list = nullptr;;
|
2023-09-23 02:26:23 +03:00
|
|
|
pool_size *= s_malloc_pool_size_multiplier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool allocate_pool(size_t pool_index)
|
|
|
|
{
|
|
|
|
auto& pool = s_malloc_pools[pool_index];
|
|
|
|
assert(pool.start == nullptr);
|
|
|
|
|
|
|
|
// allocate memory for pool
|
2023-09-29 10:38:08 +03:00
|
|
|
void* new_pool = mmap(nullptr, pool.size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
|
|
|
if (new_pool == MAP_FAILED)
|
2023-09-23 02:26:23 +03:00
|
|
|
return false;
|
|
|
|
|
2023-09-29 10:38:08 +03:00
|
|
|
pool.start = (uint8_t*)new_pool;
|
|
|
|
|
2023-09-23 02:26:23 +03:00
|
|
|
// initialize pool to single unallocated node
|
|
|
|
auto* node = (malloc_node_t*)pool.start;
|
|
|
|
node->allocated = false;
|
|
|
|
node->size = pool.size;
|
2023-12-14 15:14:55 +02:00
|
|
|
node->last = true;
|
2023-12-14 23:42:25 +02:00
|
|
|
node->prev_free = nullptr;
|
|
|
|
node->next_free = nullptr;
|
2023-12-14 15:14:55 +02:00
|
|
|
|
2023-12-14 23:42:25 +02:00
|
|
|
pool.free_list = node;
|
2023-09-23 02:26:23 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-14 23:42:25 +02:00
|
|
|
static void remove_node_from_pool_free_list(malloc_pool_t& pool, malloc_node_t* node)
|
|
|
|
{
|
|
|
|
if (node == pool.free_list)
|
|
|
|
{
|
|
|
|
pool.free_list = pool.free_list->next_free;
|
|
|
|
if (pool.free_list)
|
|
|
|
pool.free_list->prev_free = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (node->next_free)
|
|
|
|
node->next_free->prev_free = node->prev_free;
|
|
|
|
if (node->prev_free)
|
|
|
|
node->prev_free->next_free = node->next_free;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 02:26:23 +03:00
|
|
|
static void* allocate_from_pool(size_t pool_index, size_t size)
|
|
|
|
{
|
|
|
|
assert(size % s_malloc_default_align == 0);
|
|
|
|
|
|
|
|
auto& pool = s_malloc_pools[pool_index];
|
|
|
|
assert(pool.start != nullptr);
|
|
|
|
|
2023-12-14 23:42:25 +02:00
|
|
|
if (!pool.free_list)
|
2023-12-14 15:14:55 +02:00
|
|
|
return nullptr;
|
2023-09-23 02:26:23 +03:00
|
|
|
|
2023-12-14 23:42:25 +02:00
|
|
|
for (auto* node = pool.free_list; node; node = node->next_free)
|
2023-09-23 02:26:23 +03:00
|
|
|
{
|
2023-12-14 23:42:25 +02:00
|
|
|
assert(!node->allocated);
|
2023-09-23 02:26:23 +03:00
|
|
|
|
2023-12-14 23:42:25 +02:00
|
|
|
// merge nodes right after current one
|
|
|
|
while (!node->last && !node->next()->allocated)
|
2023-09-23 02:26:23 +03:00
|
|
|
{
|
2023-12-14 23:42:25 +02:00
|
|
|
auto* next = node->next();
|
|
|
|
remove_node_from_pool_free_list(pool, next);
|
|
|
|
node->last = next->last;
|
|
|
|
node->size += next->size;
|
2023-09-23 02:26:23 +03:00
|
|
|
}
|
2023-12-14 23:42:25 +02:00
|
|
|
|
2023-09-23 02:26:23 +03:00
|
|
|
if (node->data_size() < size)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
node->allocated = true;
|
2023-12-14 23:42:25 +02:00
|
|
|
remove_node_from_pool_free_list(pool, node);
|
2023-12-14 15:14:55 +02:00
|
|
|
|
2023-09-23 02:26:23 +03:00
|
|
|
// shrink node if needed
|
2023-12-14 23:42:25 +02:00
|
|
|
if (node->data_size() - size >= sizeof(malloc_node_t) + s_malloc_shrink_threshold)
|
2023-09-23 02:26:23 +03:00
|
|
|
{
|
|
|
|
uint8_t* node_end = (uint8_t*)node->next();
|
|
|
|
|
|
|
|
node->size = sizeof(malloc_node_t) + size;
|
|
|
|
|
|
|
|
auto* next = node->next();
|
|
|
|
next->allocated = false;
|
|
|
|
next->size = node_end - (uint8_t*)next;
|
2023-12-14 15:14:55 +02:00
|
|
|
next->last = node->last;
|
|
|
|
|
|
|
|
node->last = false;
|
|
|
|
|
2023-12-14 23:42:25 +02:00
|
|
|
// insert excess node to free list
|
|
|
|
if (pool.free_list)
|
|
|
|
pool.free_list->prev_free = next;
|
|
|
|
next->next_free = pool.free_list;
|
|
|
|
next->prev_free = nullptr;
|
|
|
|
pool.free_list = next;
|
2023-09-23 02:26:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return node->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static malloc_node_t* node_from_data_pointer(void* data_pointer)
|
|
|
|
{
|
|
|
|
return (malloc_node_t*)((uint8_t*)data_pointer - sizeof(malloc_node_t));
|
|
|
|
}
|
|
|
|
|
2023-12-14 15:14:55 +02:00
|
|
|
static malloc_pool_t& pool_from_node(malloc_node_t* node)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < s_malloc_pool_count; i++)
|
|
|
|
if (s_malloc_pools[i].start && s_malloc_pools[i].contains(node))
|
|
|
|
return s_malloc_pools[i];
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2023-09-23 02:26:23 +03:00
|
|
|
void* malloc(size_t size)
|
|
|
|
{
|
|
|
|
// align size to s_malloc_default_align boundary
|
|
|
|
if (size_t ret = size % s_malloc_default_align)
|
|
|
|
size += s_malloc_default_align - ret;
|
|
|
|
|
|
|
|
// find the first pool with size atleast size
|
|
|
|
size_t first_usable_pool = 0;
|
2023-09-28 21:03:43 +03:00
|
|
|
while (s_malloc_pools[first_usable_pool].size - sizeof(malloc_node_t) < size)
|
2023-09-23 02:26:23 +03:00
|
|
|
first_usable_pool++;
|
|
|
|
// first_usable_pool = ceil(log(size/s_malloc_smallest_pool, s_malloc_pool_size_mult))
|
|
|
|
|
|
|
|
// try to find any already existing pools that we can allocate in
|
|
|
|
for (size_t i = first_usable_pool; i < s_malloc_pool_count; i++)
|
|
|
|
if (s_malloc_pools[i].start != nullptr)
|
|
|
|
if (void* ret = allocate_from_pool(i, size))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
// allocate new pool
|
|
|
|
for (size_t i = first_usable_pool; i < s_malloc_pool_count; i++)
|
|
|
|
{
|
|
|
|
if (s_malloc_pools[i].start != nullptr)
|
|
|
|
continue;
|
|
|
|
if (!allocate_pool(i))
|
|
|
|
break;
|
2023-09-28 21:03:43 +03:00
|
|
|
// NOTE: always works since we just created the pool
|
2023-09-23 02:26:23 +03:00
|
|
|
return allocate_from_pool(i, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = ENOMEM;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* realloc(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
if (ptr == nullptr)
|
|
|
|
return malloc(size);
|
|
|
|
|
|
|
|
// align size to s_malloc_default_align boundary
|
|
|
|
if (size_t ret = size % s_malloc_default_align)
|
|
|
|
size += s_malloc_default_align - ret;
|
|
|
|
|
|
|
|
auto* node = node_from_data_pointer(ptr);
|
|
|
|
size_t oldsize = node->data_size();
|
|
|
|
|
|
|
|
if (oldsize == size)
|
|
|
|
return ptr;
|
|
|
|
|
2023-12-14 23:42:25 +02:00
|
|
|
// TODO: try to shrink or expand allocation
|
2023-09-23 02:26:23 +03:00
|
|
|
|
|
|
|
// allocate new pointer
|
|
|
|
void* new_ptr = malloc(size);
|
|
|
|
if (new_ptr == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// move data to the new pointer
|
|
|
|
size_t bytes_to_copy = oldsize < size ? oldsize : size;
|
|
|
|
memcpy(new_ptr, ptr, bytes_to_copy);
|
|
|
|
free(ptr);
|
|
|
|
|
|
|
|
return new_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(void* ptr)
|
|
|
|
{
|
|
|
|
if (ptr == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto* node = node_from_data_pointer(ptr);
|
|
|
|
|
|
|
|
node->allocated = false;
|
2023-12-14 23:42:25 +02:00
|
|
|
|
|
|
|
auto& pool = pool_from_node(node);
|
|
|
|
|
|
|
|
// merge nodes right after freed one
|
|
|
|
while (!node->last && !node->next()->allocated)
|
2023-12-14 15:14:55 +02:00
|
|
|
{
|
2023-12-14 23:42:25 +02:00
|
|
|
auto* next = node->next();
|
|
|
|
remove_node_from_pool_free_list(pool, next);
|
|
|
|
node->last = next->last;
|
|
|
|
node->size += next->size;
|
2023-12-14 15:14:55 +02:00
|
|
|
}
|
|
|
|
|
2023-12-14 23:42:25 +02:00
|
|
|
// add node to free list
|
|
|
|
if (pool.free_list)
|
|
|
|
pool.free_list->prev_free = node;
|
|
|
|
node->prev_free = nullptr;
|
|
|
|
node->next_free = pool.free_list;
|
|
|
|
pool.free_list = node;
|
2023-09-23 02:26:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void* calloc(size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
size_t total = nmemb * size;
|
|
|
|
if (size != 0 && total / size != nmemb)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
void* ptr = malloc(total);
|
|
|
|
if (ptr == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
memset(ptr, 0, total);
|
|
|
|
return ptr;
|
|
|
|
}
|