Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <console/console.h> |
| 3 | |
Uwe Hermann | a953f37 | 2010-11-10 00:14:32 +0000 | [diff] [blame^] | 4 | #if CONFIG_DEBUG_MALLOC |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 5 | #define MALLOCDBG(x...) printk(BIOS_SPEW, x) |
Uwe Hermann | a953f37 | 2010-11-10 00:14:32 +0000 | [diff] [blame^] | 6 | #else |
| 7 | #define MALLOCDBG(x) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 8 | #endif |
Uwe Hermann | a953f37 | 2010-11-10 00:14:32 +0000 | [diff] [blame^] | 9 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 10 | extern unsigned char _heap, _eheap; |
Stefan Reinauer | 054c723 | 2009-03-06 17:22:35 +0000 | [diff] [blame] | 11 | static void *free_mem_ptr = &_heap; /* Start of heap */ |
| 12 | static void *free_mem_end_ptr = &_eheap; /* End of heap */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 13 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 14 | void *malloc(size_t size) |
| 15 | { |
| 16 | void *p; |
| 17 | |
Stefan Reinauer | 054c723 | 2009-03-06 17:22:35 +0000 | [diff] [blame] | 18 | MALLOCDBG("%s Enter, size %ld, free_mem_ptr %p\n", __func__, size, free_mem_ptr); |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 19 | |
| 20 | /* Checking arguments */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 21 | if (size < 0) |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 22 | die("Error! malloc: size < 0"); |
| 23 | |
| 24 | /* Overzealous linker check */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 25 | if (free_mem_ptr <= 0) |
| 26 | die("Error! malloc: Free_mem_ptr <= 0"); |
| 27 | |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 28 | free_mem_ptr = (void *)ALIGN((unsigned long)free_mem_ptr, 4); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 29 | |
| 30 | p = (void *) free_mem_ptr; |
| 31 | free_mem_ptr += size; |
| 32 | |
| 33 | if (free_mem_ptr >= free_mem_end_ptr) |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 34 | die("Error! malloc: Out of memory (free_mem_ptr >= free_mem_end_ptr)"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 35 | |
Stefan Reinauer | 054c723 | 2009-03-06 17:22:35 +0000 | [diff] [blame] | 36 | MALLOCDBG("malloc %p\n", p); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 37 | |
| 38 | return p; |
| 39 | } |
| 40 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 41 | void free(void *where) |
| 42 | { |
| 43 | /* Don't care */ |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 44 | MALLOCDBG("free %p\n", where); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 45 | } |