Martin Roth | fb8876d | 2022-08-07 15:12:12 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 3 | #include <console/console.h> |
Tim Wawrzynczak | c556dff | 2021-03-30 11:49:14 -0600 | [diff] [blame] | 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 6 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 7 | #if CONFIG(DEBUG_MALLOC) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 8 | #define MALLOCDBG(x...) printk(BIOS_SPEW, x) |
Uwe Hermann | a953f37 | 2010-11-10 00:14:32 +0000 | [diff] [blame] | 9 | #else |
Peter Stuge | 5015f79 | 2010-11-10 02:00:32 +0000 | [diff] [blame] | 10 | #define MALLOCDBG(x...) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 11 | #endif |
Uwe Hermann | a953f37 | 2010-11-10 00:14:32 +0000 | [diff] [blame] | 12 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 13 | extern unsigned char _heap, _eheap; |
Stefan Reinauer | 054c723 | 2009-03-06 17:22:35 +0000 | [diff] [blame] | 14 | static void *free_mem_ptr = &_heap; /* Start of heap */ |
| 15 | static void *free_mem_end_ptr = &_eheap; /* End of heap */ |
Bora Guvendik | a347ea3 | 2019-12-23 16:44:06 -0800 | [diff] [blame] | 16 | static void *free_last_alloc_ptr = &_heap; /* End of heap before |
| 17 | last allocation */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 18 | |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 19 | /* We don't restrict the boundary. This is firmware, |
| 20 | * you are supposed to know what you are doing. |
| 21 | */ |
| 22 | void *memalign(size_t boundary, size_t size) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 23 | { |
| 24 | void *p; |
| 25 | |
Ronald G. Minnich | 79431f5 | 2012-05-31 16:02:26 -0700 | [diff] [blame] | 26 | MALLOCDBG("%s Enter, boundary %zu, size %zu, free_mem_ptr %p\n", |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 27 | __func__, boundary, size, free_mem_ptr); |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 28 | |
Elyes Haouas | d6b6b22 | 2022-10-10 12:34:21 +0200 | [diff] [blame] | 29 | free_mem_ptr = (void *)ALIGN_UP((unsigned long)free_mem_ptr, boundary); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 30 | |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 31 | p = free_mem_ptr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 32 | free_mem_ptr += size; |
Bora Guvendik | a347ea3 | 2019-12-23 16:44:06 -0800 | [diff] [blame] | 33 | /* |
| 34 | * Store last allocation pointer after ALIGN, as malloc() will |
| 35 | * return it. This may cause n bytes of gap between allocations |
| 36 | * where n < boundary. |
| 37 | */ |
| 38 | free_last_alloc_ptr = p; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 39 | |
Ronald G. Minnich | 79431f5 | 2012-05-31 16:02:26 -0700 | [diff] [blame] | 40 | if (free_mem_ptr >= free_mem_end_ptr) { |
| 41 | printk(BIOS_ERR, "memalign(boundary=%zu, size=%zu): failed: ", |
| 42 | boundary, size); |
| 43 | printk(BIOS_ERR, "Tried to round up free_mem_ptr %p to %p\n", |
| 44 | p, free_mem_ptr); |
| 45 | printk(BIOS_ERR, "but free_mem_end_ptr is %p\n", |
| 46 | free_mem_end_ptr); |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 47 | die("Error! memalign: Out of memory (free_mem_ptr >= free_mem_end_ptr)"); |
Ronald G. Minnich | 79431f5 | 2012-05-31 16:02:26 -0700 | [diff] [blame] | 48 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 49 | |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 50 | MALLOCDBG("memalign %p\n", p); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 51 | |
| 52 | return p; |
| 53 | } |
| 54 | |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 55 | void *malloc(size_t size) |
| 56 | { |
| 57 | return memalign(sizeof(u64), size); |
| 58 | } |
Bora Guvendik | a347ea3 | 2019-12-23 16:44:06 -0800 | [diff] [blame] | 59 | |
Tim Wawrzynczak | c556dff | 2021-03-30 11:49:14 -0600 | [diff] [blame] | 60 | void *calloc(size_t nitems, size_t size) |
| 61 | { |
| 62 | void *p = malloc(nitems * size); |
| 63 | if (p) |
| 64 | memset(p, 0, nitems * size); |
| 65 | |
| 66 | return p; |
| 67 | } |
| 68 | |
Bora Guvendik | a347ea3 | 2019-12-23 16:44:06 -0800 | [diff] [blame] | 69 | void free(void *ptr) |
| 70 | { |
| 71 | if (ptr == NULL) |
| 72 | return; |
| 73 | |
| 74 | if (ptr < (void *)&_heap || ptr >= free_mem_end_ptr) { |
| 75 | printk(BIOS_WARNING, "Warning - Pointer passed to %s is not " |
| 76 | "pointing to the heap\n", __func__); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Rewind the heap pointer to the end of heap |
| 82 | * before the last successful malloc(). |
| 83 | */ |
| 84 | if (ptr == free_last_alloc_ptr) { |
| 85 | free_mem_ptr = free_last_alloc_ptr; |
| 86 | free_last_alloc_ptr = NULL; |
| 87 | } |
| 88 | } |