gint/src/kmalloc/arena_osheap.c
Lephe 162b11cc73
kmalloc: create the kmalloc interface
This change introduces a centralized memory allocator in the kernel.
This interface can call into multiple arenas, including the default OS
heap and planned arenas managed by a gint algorithm.

The main advantage of this method is that it allows the heap to be
extended over previously-unused areas of RAM such as the end of the
static RAM region (apart from where the stack resides). Not using the OS
heap is also sometimes a matter of correctness since on some OS versions
the heap is known to fragment badly and degrade over time.

I hope the deep control this interfaces gives over meomry allocation
will allow very particular applications like object-specific allocators
in fragmented SPU memory.

This change does not introduce any new algorithm or arena so programs
should behave exactly as before.
2021-03-12 17:24:49 +01:00

41 lines
1,012 B
C

//---
// gint:kmalloc:arena_osheap - An arena that uses the OS heap as input
//---
#include <gint/kmalloc.h>
#include <gint/defs/attributes.h>
/* Syscalls relating to the OS heap */
extern void *__malloc(size_t size);
extern void *__realloc(void *ptr, size_t newsize);
extern void __free(void *ptr);
static void *osheap_malloc(size_t size, GUNUSED void *data)
{
return __malloc(size);
}
static void *osheap_realloc(void *ptr, size_t newsize, GUNUSED void *data)
{
return __realloc(ptr, newsize);
}
static void osheap_free(void *ptr, GUNUSED void *data)
{
return __free(ptr);
}
/* This is a global variable, it's pulled by kmalloc.c. This arena is the only
one allowed to not specify start/end as the values are hard to determine. */
kmalloc_arena_t kmalloc_arena_osheap = {
.malloc = osheap_malloc,
.realloc = osheap_realloc,
.free = osheap_free,
.name = "_os",
.start = NULL,
.end = NULL,
.data = NULL,
.is_default = 1,
.stats = { 0 },
};