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