rpi4-osdev/part12-wgt/lib/mem.c

47 lines
961 B
C
Raw Normal View History

2021-03-08 22:51:02 +00:00
// Define the heap
2021-03-11 22:33:27 +00:00
extern unsigned char _end[];
// unsigned char *HEAP_START = &_end[0]; // End of kernel
2021-03-11 22:33:27 +00:00
unsigned char *HEAP_START = (unsigned char *)0x400000; // Top of stack
2021-03-11 22:33:27 +00:00
unsigned int HEAP_SIZE = 0x30000000; // Max heap size is 768Mb
2021-03-08 22:51:02 +00:00
unsigned char *HEAP_END;
2021-03-11 22:33:27 +00:00
// Set up some static globals
2021-03-08 22:51:02 +00:00
2021-03-11 22:33:27 +00:00
static unsigned char *freeptr;
static unsigned int bytes_allocated = 0;
2021-03-08 22:51:02 +00:00
void mem_init()
{
// Align the start of heap to an 8-byte boundary
if ((long)&HEAP_START % 8 != 0) {
HEAP_START += 8 - ((long)&HEAP_START % 8);
}
HEAP_END = (unsigned char *)(HEAP_START + HEAP_SIZE);
freeptr = HEAP_START;
2021-03-08 22:51:02 +00:00
}
void *malloc(unsigned int size)
{
if (size > 0) {
void *allocated = freeptr;
if ((unsigned char *)(allocated + size) > HEAP_END) {
return 0;
} else {
freeptr += size;
2021-03-11 22:33:27 +00:00
bytes_allocated += size;
2021-03-08 22:51:02 +00:00
return allocated;
}
}
return 0;
}
void free(void *ptr) {
// TODO
}