soc/intel/alderlake: Add ADL-P 4+4 with 28W TDP
[coreboot.git] / src / lib / malloc.c
blob052a53e5684b9d190731c65d61633f8ff89c9bff
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #if CONFIG(DEBUG_MALLOC)
8 #define MALLOCDBG(x...) printk(BIOS_SPEW, x)
9 #else
10 #define MALLOCDBG(x...)
11 #endif
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 */
19 /* We don't restrict the boundary. This is firmware,
20 * you are supposed to know what you are doing.
22 void *memalign(size_t boundary, size_t size)
24 void *p;
26 MALLOCDBG("%s Enter, boundary %zu, size %zu, free_mem_ptr %p\n",
27 __func__, boundary, size, free_mem_ptr);
29 free_mem_ptr = (void *)ALIGN_UP((unsigned long)free_mem_ptr, boundary);
31 p = free_mem_ptr;
32 free_mem_ptr += size;
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.
38 free_last_alloc_ptr = p;
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 die("Error! %s: Out of memory (free_mem_ptr >= free_mem_end_ptr)", __func__);
50 MALLOCDBG("%s %p\n", __func__, p);
52 return p;
55 void *malloc(size_t size)
57 return memalign(sizeof(u64), size);
60 void *calloc(size_t nitems, size_t size)
62 void *p = malloc(nitems * size);
63 if (p)
64 memset(p, 0, nitems * size);
66 return p;
69 void free(void *ptr)
71 if (ptr == NULL)
72 return;
74 if (ptr < (void *)&_heap || ptr >= free_mem_end_ptr) {
75 printk(BIOS_WARNING, "Pointer passed to %s is not "
76 "pointing to the heap\n", __func__);
77 return;
81 * Rewind the heap pointer to the end of heap
82 * before the last successful malloc().
84 if (ptr == free_last_alloc_ptr) {
85 free_mem_ptr = free_last_alloc_ptr;
86 free_last_alloc_ptr = NULL;