HelenOS sources
This source file includes following definitions.
- heap_lock
- heap_unlock
- block_init
- block_check
- area_check
- area_create
- area_grow
- heap_shrink
- __malloc_init
- __malloc_fini
- split_mark
- malloc_area
- heap_grow_and_alloc
- malloc_internal
- malloc
- memalign
- realloc
- reallocarray
- free
- heap_check
#include <stdlib.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <as.h>
#include <align.h>
#include <macros.h>
#include <assert.h>
#include <errno.h>
#include <bitops.h>
#include <mem.h>
#include <stdlib.h>
#include <adt/gcdlcm.h>
#include <malloc.h>
#include "private/malloc.h"
#include "private/fibril.h"
#define HEAP_BLOCK_HEAD_MAGIC UINT32_C(0xBEEF0101)
#define HEAP_BLOCK_FOOT_MAGIC UINT32_C(0xBEEF0202)
#define HEAP_AREA_MAGIC UINT32_C(0xBEEFCAFE)
#define BASE_ALIGN 16
#define SHRINK_GRANULARITY (64 * PAGE_SIZE)
#define STRUCT_OVERHEAD \
(sizeof(heap_block_head_t) + sizeof(heap_block_foot_t))
#define GROSS_SIZE(size) ((size) + STRUCT_OVERHEAD)
#define NET_SIZE(size) ((size) - STRUCT_OVERHEAD)
#define AREA_OVERHEAD(size) \
(ALIGN_UP(GROSS_SIZE(size) + sizeof(heap_area_t), BASE_ALIGN))
#define AREA_FIRST_BLOCK_HEAD(area) \
(ALIGN_UP(((uintptr_t) (area)) + sizeof(heap_area_t), BASE_ALIGN))
#define AREA_LAST_BLOCK_FOOT(area) \
(((uintptr_t) (area)->end) - sizeof(heap_block_foot_t))
#define AREA_LAST_BLOCK_HEAD(area) \
((uintptr_t) BLOCK_HEAD(((heap_block_foot_t *) AREA_LAST_BLOCK_FOOT(area))))
#define BLOCK_HEAD(foot) \
((heap_block_head_t *) \
(((uintptr_t) (foot)) + sizeof(heap_block_foot_t) - (foot)->size))
#define BLOCK_FOOT(head) \
((heap_block_foot_t *) \
(((uintptr_t) (head)) + (head)->size - sizeof(heap_block_foot_t)))
typedef struct heap_area {
void *start;
void *end;
struct heap_area *prev;
struct heap_area *next;
uint32_t magic;
} heap_area_t;
typedef struct {
size_t size;
bool free;
heap_area_t *area;
uint32_t magic;
} heap_block_head_t;
typedef struct {
size_t size;
uint32_t magic;
} heap_block_foot_t;
static heap_area_t *first_heap_area = NULL;
static heap_area_t *last_heap_area = NULL;
static heap_block_head_t *next_fit = NULL;
static fibril_rmutex_t malloc_mutex;
#define malloc_assert(expr) safe_assert(expr)
static_assert(BASE_ALIGN >= alignof(heap_area_t), "");
static_assert(BASE_ALIGN >= alignof(heap_block_head_t), "");
static_assert(BASE_ALIGN >= alignof(heap_block_foot_t), "");
static_assert(BASE_ALIGN >= alignof(max_align_t), "");
static inline void heap_lock(void)
{
fibril_rmutex_lock(&malloc_mutex);
}
static inline void heap_unlock(void)
{
fibril_rmutex_unlock(&malloc_mutex);
}
static void block_init(void *addr, size_t size, bool free, heap_area_t *area)
{
heap_block_head_t *head = (heap_block_head_t *) addr;
head->size = size;
head->free = free;
head->area = area;
head->magic = HEAP_BLOCK_HEAD_MAGIC;
heap_block_foot_t *foot = BLOCK_FOOT(head);
foot->size = size;
foot->magic = HEAP_BLOCK_FOOT_MAGIC;
}
static void block_check(void *addr)
{
heap_block_head_t *head = (heap_block_head_t *) addr;
malloc_assert(head->magic == HEAP_BLOCK_HEAD_MAGIC);
heap_block_foot_t *foot = BLOCK_FOOT(head);
malloc_assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC);
malloc_assert(head->size == foot->size);
}
static void area_check(void *addr)
{
heap_area_t *area = (heap_area_t *) addr;
malloc_assert(area->magic == HEAP_AREA_MAGIC);
malloc_assert(addr == area->start);
malloc_assert(area->start < area->end);
malloc_assert(((uintptr_t) area->start % PAGE_SIZE) == 0);
malloc_assert(((uintptr_t) area->end % PAGE_SIZE) == 0);
}
static bool area_create(size_t size)
{
size_t asize = ALIGN_UP(size, PAGE_SIZE);
void *astart = as_area_create(AS_AREA_ANY, asize,
AS_AREA_WRITE | AS_AREA_READ | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
if (astart == AS_MAP_FAILED)
return false;
heap_area_t *area = (heap_area_t *) astart;
area->start = astart;
area->end = (void *) ((uintptr_t) astart + asize);
area->prev = NULL;
area->next = NULL;
area->magic = HEAP_AREA_MAGIC;
void *block = (void *) AREA_FIRST_BLOCK_HEAD(area);
size_t bsize = (size_t) (area->end - block);
block_init(block, bsize, true, area);
if (last_heap_area == NULL) {
first_heap_area = area;
last_heap_area = area;
} else {
area->prev = last_heap_area;
last_heap_area->next = area;
last_heap_area = area;
}
return true;
}
static bool area_grow(heap_area_t *area, size_t size)
{
if (size == 0)
return true;
area_check(area);
size_t gross_size = (size_t) (area->end - area->start) + size;
size_t asize = ALIGN_UP(gross_size, PAGE_SIZE);
void *end = (void *) ((uintptr_t) area->start + asize);
if (end < area->start)
return false;
errno_t ret = as_area_resize(area->start, asize, 0);
if (ret != EOK)
return false;
heap_block_head_t *last_head =
(heap_block_head_t *) AREA_LAST_BLOCK_HEAD(area);
if (last_head->free) {
size_t net_size = (size_t) (end - area->end) + last_head->size;
malloc_assert(net_size > 0);
block_init(last_head, net_size, true, area);
} else {
size_t net_size = (size_t) (end - area->end);
if (net_size > 0)
block_init(area->end, net_size, true, area);
}
area->end = end;
return true;
}
static void heap_shrink(heap_area_t *area)
{
area_check(area);
heap_block_foot_t *last_foot =
(heap_block_foot_t *) AREA_LAST_BLOCK_FOOT(area);
heap_block_head_t *last_head = BLOCK_HEAD(last_foot);
block_check((void *) last_head);
malloc_assert(last_head->area == area);
if (last_head->free) {
heap_block_head_t *first_head =
(heap_block_head_t *) AREA_FIRST_BLOCK_HEAD(area);
block_check((void *) first_head);
malloc_assert(first_head->area == area);
size_t shrink_size = ALIGN_DOWN(last_head->size, PAGE_SIZE);
if (first_head == last_head) {
heap_area_t *prev = area->prev;
heap_area_t *next = area->next;
if (prev != NULL) {
area_check(prev);
prev->next = next;
} else
first_heap_area = next;
if (next != NULL) {
area_check(next);
next->prev = prev;
} else
last_heap_area = prev;
as_area_destroy(area->start);
} else if (shrink_size >= SHRINK_GRANULARITY) {
size_t asize = (size_t) (area->end - area->start) - shrink_size;
void *end = (void *) ((uintptr_t) area->start + asize);
errno_t ret = as_area_resize(area->start, asize, 0);
if (ret != EOK)
abort();
area->end = end;
size_t excess = ((size_t) area->end) - ((size_t) last_head);
if (excess > 0) {
if (excess >= STRUCT_OVERHEAD) {
block_init((void *) last_head, excess, true, area);
} else {
heap_block_foot_t *prev_foot = (heap_block_foot_t *)
(((uintptr_t) last_head) - sizeof(heap_block_foot_t));
heap_block_head_t *prev_head = BLOCK_HEAD(prev_foot);
block_check((void *) prev_head);
block_init(prev_head, prev_head->size + excess,
prev_head->free, area);
}
}
}
}
next_fit = NULL;
}
void __malloc_init(void)
{
if (fibril_rmutex_initialize(&malloc_mutex) != EOK)
abort();
if (!area_create(PAGE_SIZE))
abort();
}
void __malloc_fini(void)
{
fibril_rmutex_destroy(&malloc_mutex);
}
static void split_mark(heap_block_head_t *cur, const size_t size)
{
malloc_assert(cur->size >= size);
size_t split_limit = GROSS_SIZE(size);
if (cur->size > split_limit) {
void *next = ((void *) cur) + size;
block_init(next, cur->size - size, true, cur->area);
block_init(cur, size, false, cur->area);
} else {
cur->free = false;
}
}
static void *malloc_area(heap_area_t *area, heap_block_head_t *first_block,
heap_block_head_t *final_block, size_t real_size, size_t falign)
{
area_check((void *) area);
malloc_assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));
malloc_assert((void *) first_block < area->end);
for (heap_block_head_t *cur = first_block; (void *) cur < area->end;
cur = (heap_block_head_t *) (((void *) cur) + cur->size)) {
block_check(cur);
if ((final_block != NULL) && (cur == final_block))
break;
if ((cur->free) && (cur->size >= real_size)) {
void *addr = (void *)
((uintptr_t) cur + sizeof(heap_block_head_t));
void *aligned = (void *)
ALIGN_UP((uintptr_t) addr, falign);
if (addr == aligned) {
split_mark(cur, real_size);
next_fit = cur;
return addr;
} else {
size_t excess = (size_t) (aligned - addr);
if (cur->size >= real_size + excess) {
if ((void *) cur > (void *) AREA_FIRST_BLOCK_HEAD(area)) {
heap_block_foot_t *prev_foot = (heap_block_foot_t *)
((void *) cur - sizeof(heap_block_foot_t));
heap_block_head_t *prev_head = (heap_block_head_t *)
((void *) cur - prev_foot->size);
block_check(prev_head);
size_t reduced_size = cur->size - excess;
heap_block_head_t *next_head = ((void *) cur) + excess;
if ((!prev_head->free) &&
(excess >= STRUCT_OVERHEAD)) {
block_init(cur, excess, true, area);
} else {
block_init(prev_head, prev_head->size + excess,
prev_head->free, area);
}
block_init(next_head, reduced_size, true, area);
split_mark(next_head, real_size);
next_fit = next_head;
return aligned;
} else {
while (excess < STRUCT_OVERHEAD) {
aligned += falign;
excess += falign;
}
if (cur->size >= real_size + excess) {
size_t reduced_size = cur->size - excess;
cur = (heap_block_head_t *)
(AREA_FIRST_BLOCK_HEAD(area) + excess);
block_init((void *) AREA_FIRST_BLOCK_HEAD(area),
excess, true, area);
block_init(cur, reduced_size, true, area);
split_mark(cur, real_size);
next_fit = cur;
return aligned;
}
}
}
}
}
}
return NULL;
}
static void *heap_grow_and_alloc(size_t size, size_t align)
{
if (size == 0)
return NULL;
for (heap_area_t *area = first_heap_area; area != NULL;
area = area->next) {
if (area_grow(area, size + align)) {
heap_block_head_t *first =
(heap_block_head_t *) AREA_LAST_BLOCK_HEAD(area);
void *addr =
malloc_area(area, first, NULL, size, align);
malloc_assert(addr != NULL);
return addr;
}
}
if (area_create(AREA_OVERHEAD(size + align))) {
heap_block_head_t *first =
(heap_block_head_t *) AREA_FIRST_BLOCK_HEAD(last_heap_area);
void *addr =
malloc_area(last_heap_area, first, NULL, size, align);
malloc_assert(addr != NULL);
return addr;
}
return NULL;
}
static void *malloc_internal(size_t size, size_t align)
{
malloc_assert(first_heap_area != NULL);
if (size == 0)
size = 1;
if (align == 0)
align = BASE_ALIGN;
size_t falign = lcm(align, BASE_ALIGN);
if (falign < align)
return NULL;
size_t gross_size = GROSS_SIZE(ALIGN_UP(size, BASE_ALIGN));
heap_block_head_t *split = next_fit;
if (split != NULL) {
void *addr = malloc_area(split->area, split, NULL, gross_size,
falign);
if (addr != NULL)
return addr;
}
for (heap_area_t *area = first_heap_area; area != NULL;
area = area->next) {
heap_block_head_t *first = (heap_block_head_t *)
AREA_FIRST_BLOCK_HEAD(area);
void *addr = malloc_area(area, first, split, gross_size,
falign);
if (addr != NULL)
return addr;
}
return heap_grow_and_alloc(gross_size, falign);
}
void *malloc(const size_t size)
{
heap_lock();
void *block = malloc_internal(size, BASE_ALIGN);
heap_unlock();
return block;
}
void *memalign(const size_t align, const size_t size)
{
if (align == 0)
return NULL;
size_t palign =
1 << (fnzb(max(sizeof(void *), align) - 1) + 1);
heap_lock();
void *block = malloc_internal(size, palign);
heap_unlock();
return block;
}
void *realloc(void *const addr, size_t size)
{
if (size == 0) {
fprintf(stderr, "realloc() called with size 0\n");
size = 1;
}
if (addr == NULL)
return malloc(size);
heap_lock();
heap_block_head_t *head =
(heap_block_head_t *) (addr - sizeof(heap_block_head_t));
block_check(head);
malloc_assert(!head->free);
heap_area_t *area = head->area;
area_check(area);
malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
malloc_assert((void *) head < area->end);
void *ptr = NULL;
bool reloc = false;
size_t real_size = GROSS_SIZE(ALIGN_UP(size, BASE_ALIGN));
size_t orig_size = head->size;
if (orig_size > real_size) {
if (orig_size - real_size >= STRUCT_OVERHEAD) {
block_init((void *) head, real_size, false, area);
block_init((void *) head + real_size,
orig_size - real_size, true, area);
heap_shrink(area);
}
ptr = ((void *) head) + sizeof(heap_block_head_t);
} else {
heap_block_head_t *next_head =
(heap_block_head_t *) (((void *) head) + head->size);
bool have_next = ((void *) next_head < area->end);
if (((void *) head) + real_size > area->end) {
bool have_next_next;
if (have_next) {
have_next_next = (((void *) next_head) +
next_head->size < area->end);
}
if (!have_next || (next_head->free && !have_next_next)) {
(void) area_grow(area, real_size);
}
}
if (have_next && (head->size + next_head->size >= real_size) &&
next_head->free) {
block_check(next_head);
block_init(head, head->size + next_head->size, false,
area);
split_mark(head, real_size);
ptr = ((void *) head) + sizeof(heap_block_head_t);
next_fit = NULL;
} else {
reloc = true;
}
}
heap_unlock();
if (reloc) {
ptr = malloc(size);
if (ptr != NULL) {
memcpy(ptr, addr, NET_SIZE(orig_size));
free(addr);
}
}
return ptr;
}
void *reallocarray(void *ptr, size_t nelem, size_t elsize)
{
if (nelem > SIZE_MAX / elsize)
return NULL;
return realloc(ptr, nelem * elsize);
}
void free(void *const addr)
{
if (addr == NULL)
return;
heap_lock();
heap_block_head_t *head =
(heap_block_head_t *) (addr - sizeof(heap_block_head_t));
block_check(head);
malloc_assert(!head->free);
heap_area_t *area = head->area;
area_check(area);
malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
malloc_assert((void *) head < area->end);
head->free = true;
heap_block_head_t *next_head =
(heap_block_head_t *) (((void *) head) + head->size);
if ((void *) next_head < area->end) {
block_check(next_head);
if (next_head->free)
block_init(head, head->size + next_head->size, true, area);
}
if ((void *) head > (void *) AREA_FIRST_BLOCK_HEAD(area)) {
heap_block_foot_t *prev_foot =
(heap_block_foot_t *) (((void *) head) - sizeof(heap_block_foot_t));
heap_block_head_t *prev_head =
(heap_block_head_t *) (((void *) head) - prev_foot->size);
block_check(prev_head);
if (prev_head->free)
block_init(prev_head, prev_head->size + head->size, true,
area);
}
heap_shrink(area);
heap_unlock();
}
void *heap_check(void)
{
heap_lock();
if (first_heap_area == NULL) {
heap_unlock();
return (void *) -1;
}
for (heap_area_t *area = first_heap_area; area != NULL;
area = area->next) {
if ((area->magic != HEAP_AREA_MAGIC) ||
((void *) area != area->start) ||
(area->start >= area->end) ||
(((uintptr_t) area->start % PAGE_SIZE) != 0) ||
(((uintptr_t) area->end % PAGE_SIZE) != 0)) {
heap_unlock();
return (void *) area;
}
for (heap_block_head_t *head = (heap_block_head_t *)
AREA_FIRST_BLOCK_HEAD(area); (void *) head < area->end;
head = (heap_block_head_t *) (((void *) head) + head->size)) {
if (head->magic != HEAP_BLOCK_HEAD_MAGIC) {
heap_unlock();
return (void *) head;
}
heap_block_foot_t *foot = BLOCK_FOOT(head);
if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) ||
(head->size != foot->size)) {
heap_unlock();
return (void *) foot;
}
}
}
heap_unlock();
return NULL;
}
HelenOS homepage, sources at GitHub