1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #if CONFIG(DEBUG_MALLOC)
8 #define MALLOCDBG(x...) printk(BIOS_SPEW, x)
9 #else
10 #define MALLOCDBG(x...)
11 #endif
12
13 extern unsigned char _heap, _eheap;
14 static void *free_mem_ptr = &_heap; /* Start of heap */
15 static void *free_mem_end_ptr = &_eheap; /* End of heap */
16 static void *free_last_alloc_ptr = &_heap; /* End of heap before
17 last allocation */
18
19 /* We don't restrict the boundary. This is firmware,
20 * you are supposed to know what you are doing.
21 */
memalign(size_t boundary,size_t size)22 void *memalign(size_t boundary, size_t size)
23 {
24 void *p;
25
26 MALLOCDBG("%s Enter, boundary %zu, size %zu, free_mem_ptr %p\n",
27 __func__, boundary, size, free_mem_ptr);
28
29 free_mem_ptr = (void *)ALIGN_UP((unsigned long)free_mem_ptr, boundary);
30
31 p = free_mem_ptr;
32 free_mem_ptr += size;
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;
39
40 if (free_mem_ptr >= free_mem_end_ptr) {
41 printk(BIOS_ERR, "%s(boundary=%zu, size=%zu): failed: ",
42 __func__, 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);
47 printk(BIOS_ERR, "Error! %s: Out of memory "
48 "(free_mem_ptr >= free_mem_end_ptr)",
49 __func__);
50 return NULL;
51 }
52
53 MALLOCDBG("%s %p\n", __func__, p);
54
55 return p;
56 }
57
malloc(size_t size)58 void *malloc(size_t size)
59 {
60 return memalign(sizeof(u64), size);
61 }
62
calloc(size_t nitems,size_t size)63 void *calloc(size_t nitems, size_t size)
64 {
65 void *p = malloc(nitems * size);
66 if (p)
67 memset(p, 0, nitems * size);
68
69 return p;
70 }
71
free(void * ptr)72 void free(void *ptr)
73 {
74 if (ptr == NULL)
75 return;
76
77 if (ptr < (void *)&_heap || ptr >= free_mem_end_ptr) {
78 printk(BIOS_WARNING, "Pointer passed to %s is not "
79 "pointing to the heap\n", __func__);
80 return;
81 }
82
83 /*
84 * Rewind the heap pointer to the end of heap
85 * before the last successful malloc().
86 */
87 if (ptr == free_last_alloc_ptr) {
88 free_mem_ptr = free_last_alloc_ptr;
89 free_last_alloc_ptr = NULL;
90 }
91 }
92