53 lines
1.2 KiB
C
53 lines
1.2 KiB
C
/* Bump allocator on the initial memory pool.
|
|
*
|
|
* Temporary — until the PM-actor malloc path is implemented.
|
|
* Thread-safe: no — __libc.heap_used is guarded by nothing yet.
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
#include "libc.h"
|
|
|
|
void *malloc(size_t size)
|
|
{
|
|
if (size == 0) size = 1;
|
|
|
|
unsigned long heap = __libc.heap_base;
|
|
unsigned long used = __libc.heap_used;
|
|
|
|
if (used + size > __libc.heap_size)
|
|
return 0;
|
|
|
|
void *ptr = (void *)(heap + used);
|
|
__libc.heap_used = used + size;
|
|
return ptr;
|
|
}
|
|
|
|
void free(void *ptr)
|
|
{
|
|
(void)ptr;
|
|
}
|
|
|
|
void *calloc(size_t nmemb, size_t size)
|
|
{
|
|
size_t total = nmemb * size;
|
|
void *p = malloc(total);
|
|
if (!p) return 0;
|
|
for (size_t i = 0; i < total; i++)
|
|
((unsigned char *)p)[i] = 0;
|
|
return p;
|
|
}
|
|
|
|
void *realloc(void *ptr, size_t size)
|
|
{
|
|
if (!ptr) return malloc(size);
|
|
if (size == 0) { free(ptr); return 0; }
|
|
/* Bump allocator cannot shrink/grow in place — always allocate + copy */
|
|
void *newp = malloc(size);
|
|
if (!newp) return 0;
|
|
unsigned long old = __libc.heap_used - size;
|
|
if (old > __libc.heap_used) old = __libc.heap_used;
|
|
for (size_t i = 0; i < old; i++)
|
|
((unsigned char *)newp)[i] = ((unsigned char *)ptr)[i];
|
|
return newp;
|
|
}
|