2003-04-22 21:02:15 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <console/console.h>
|
2012-06-23 22:33:32 +02:00
|
|
|
#include <cpu/x86/smm.h>
|
2003-04-22 21:02:15 +02:00
|
|
|
|
2019-03-06 01:53:33 +01:00
|
|
|
#if CONFIG(DEBUG_MALLOC)
|
2010-03-22 12:42:32 +01:00
|
|
|
#define MALLOCDBG(x...) printk(BIOS_SPEW, x)
|
2010-11-10 01:14:32 +01:00
|
|
|
#else
|
2010-11-10 03:00:32 +01:00
|
|
|
#define MALLOCDBG(x...)
|
2003-04-22 21:02:15 +02:00
|
|
|
#endif
|
2010-11-10 01:14:32 +01:00
|
|
|
|
2003-04-22 21:02:15 +02:00
|
|
|
extern unsigned char _heap, _eheap;
|
2009-03-06 18:22:35 +01:00
|
|
|
static void *free_mem_ptr = &_heap; /* Start of heap */
|
|
|
|
static void *free_mem_end_ptr = &_eheap; /* End of heap */
|
2019-12-24 01:44:06 +01:00
|
|
|
static void *free_last_alloc_ptr = &_heap; /* End of heap before
|
|
|
|
last allocation */
|
2003-04-22 21:02:15 +02:00
|
|
|
|
2012-04-11 19:30:15 +02:00
|
|
|
/* We don't restrict the boundary. This is firmware,
|
|
|
|
* you are supposed to know what you are doing.
|
|
|
|
*/
|
|
|
|
void *memalign(size_t boundary, size_t size)
|
2003-04-22 21:02:15 +02:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
2012-06-01 01:02:26 +02:00
|
|
|
MALLOCDBG("%s Enter, boundary %zu, size %zu, free_mem_ptr %p\n",
|
2012-04-11 19:30:15 +02:00
|
|
|
__func__, boundary, size, free_mem_ptr);
|
2009-09-25 23:59:57 +02:00
|
|
|
|
2012-04-11 19:30:15 +02:00
|
|
|
free_mem_ptr = (void *)ALIGN((unsigned long)free_mem_ptr, boundary);
|
2003-04-22 21:02:15 +02:00
|
|
|
|
2012-04-11 19:30:15 +02:00
|
|
|
p = free_mem_ptr;
|
2003-04-22 21:02:15 +02:00
|
|
|
free_mem_ptr += size;
|
2019-12-24 01:44:06 +01:00
|
|
|
/*
|
|
|
|
* Store last allocation pointer after ALIGN, as malloc() will
|
|
|
|
* return it. This may cause n bytes of gap between allocations
|
|
|
|
* where n < boundary.
|
|
|
|
*/
|
|
|
|
free_last_alloc_ptr = p;
|
2003-04-22 21:02:15 +02:00
|
|
|
|
2012-06-01 01:02:26 +02:00
|
|
|
if (free_mem_ptr >= free_mem_end_ptr) {
|
|
|
|
printk(BIOS_ERR, "memalign(boundary=%zu, size=%zu): failed: ",
|
|
|
|
boundary, size);
|
|
|
|
printk(BIOS_ERR, "Tried to round up free_mem_ptr %p to %p\n",
|
|
|
|
p, free_mem_ptr);
|
|
|
|
printk(BIOS_ERR, "but free_mem_end_ptr is %p\n",
|
|
|
|
free_mem_end_ptr);
|
2012-04-11 19:30:15 +02:00
|
|
|
die("Error! memalign: Out of memory (free_mem_ptr >= free_mem_end_ptr)");
|
2012-06-01 01:02:26 +02:00
|
|
|
}
|
2003-04-22 21:02:15 +02:00
|
|
|
|
2012-04-11 19:30:15 +02:00
|
|
|
MALLOCDBG("memalign %p\n", p);
|
2003-04-22 21:02:15 +02:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-04-11 19:30:15 +02:00
|
|
|
void *malloc(size_t size)
|
|
|
|
{
|
|
|
|
return memalign(sizeof(u64), size);
|
|
|
|
}
|
2019-12-24 01:44:06 +01:00
|
|
|
|
|
|
|
void free(void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ptr < (void *)&_heap || ptr >= free_mem_end_ptr) {
|
|
|
|
printk(BIOS_WARNING, "Warning - Pointer passed to %s is not "
|
|
|
|
"pointing to the heap\n", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rewind the heap pointer to the end of heap
|
|
|
|
* before the last successful malloc().
|
|
|
|
*/
|
|
|
|
if (ptr == free_last_alloc_ptr) {
|
|
|
|
free_mem_ptr = free_last_alloc_ptr;
|
|
|
|
free_last_alloc_ptr = NULL;
|
|
|
|
}
|
|
|
|
}
|