summaryrefslogtreecommitdiff
path: root/src/lib/malloc.c
blob: 3cedc8f82fbbbc883164334ace9320d5d77a88fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdlib.h>
#include <console/console.h>

#if CONFIG_DEBUG_MALLOC
#define MALLOCDBG(x...) printk(BIOS_SPEW, x)
#else
#define MALLOCDBG(x...)
#endif

extern unsigned char _heap, _eheap;
static void *free_mem_ptr = &_heap;		/* Start of heap */
static void *free_mem_end_ptr = &_eheap;	/* End of heap */

void *malloc(size_t size)
{
	void *p;

	MALLOCDBG("%s Enter, size %ld, free_mem_ptr %p\n", __func__, size, free_mem_ptr);

	/* Checking arguments */
	if (size < 0)
		die("Error! malloc: size < 0");

	/* Overzealous linker check */
	if (free_mem_ptr <= 0)
		die("Error! malloc: Free_mem_ptr <= 0");

	free_mem_ptr = (void *)ALIGN((unsigned long)free_mem_ptr, 4);

	p = (void *) free_mem_ptr;
	free_mem_ptr += size;

	if (free_mem_ptr >= free_mem_end_ptr)
		die("Error! malloc: Out of memory (free_mem_ptr >= free_mem_end_ptr)");

	MALLOCDBG("malloc %p\n", p);

	return p;
}

void free(void *where)
{
	/* Don't care */
	MALLOCDBG("free %p\n", where);
}