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 |
Peter Stuge | 5015f79 | 2010-11-10 02:00:32 +0000 | [diff] [blame] | 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 | |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 20 | /* Overzealous linker check */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 21 | if (free_mem_ptr <= 0) |
| 22 | die("Error! malloc: Free_mem_ptr <= 0"); |
| 23 | |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 24 | free_mem_ptr = (void *)ALIGN((unsigned long)free_mem_ptr, 4); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 25 | |
| 26 | p = (void *) free_mem_ptr; |
| 27 | free_mem_ptr += size; |
| 28 | |
| 29 | if (free_mem_ptr >= free_mem_end_ptr) |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 30 | 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] | 31 | |
Stefan Reinauer | 054c723 | 2009-03-06 17:22:35 +0000 | [diff] [blame] | 32 | MALLOCDBG("malloc %p\n", p); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 33 | |
| 34 | return p; |
| 35 | } |
| 36 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 37 | void free(void *where) |
| 38 | { |
| 39 | /* Don't care */ |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 40 | MALLOCDBG("free %p\n", where); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 41 | } |