blob: 052a53e5684b9d190731c65d61633f8ff89c9bff [file] [log] [blame]
Martin Rothfb8876d2022-08-07 15:12:12 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2
Eric Biederman8ca8d762003-04-22 19:02:15 +00003#include <console/console.h>
Tim Wawrzynczakc556dff2021-03-30 11:49:14 -06004#include <stdlib.h>
5#include <string.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00006
Julius Wernercd49cce2019-03-05 16:53:33 -08007#if CONFIG(DEBUG_MALLOC)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00008#define MALLOCDBG(x...) printk(BIOS_SPEW, x)
Uwe Hermanna953f372010-11-10 00:14:32 +00009#else
Peter Stuge5015f792010-11-10 02:00:32 +000010#define MALLOCDBG(x...)
Eric Biederman8ca8d762003-04-22 19:02:15 +000011#endif
Uwe Hermanna953f372010-11-10 00:14:32 +000012
Eric Biederman8ca8d762003-04-22 19:02:15 +000013extern unsigned char _heap, _eheap;
Stefan Reinauer054c7232009-03-06 17:22:35 +000014static void *free_mem_ptr = &_heap; /* Start of heap */
15static void *free_mem_end_ptr = &_eheap; /* End of heap */
Bora Guvendika347ea32019-12-23 16:44:06 -080016static void *free_last_alloc_ptr = &_heap; /* End of heap before
17 last allocation */
Eric Biederman8ca8d762003-04-22 19:02:15 +000018
Ron Minnicheb596362012-04-11 10:30:15 -070019/* We don't restrict the boundary. This is firmware,
20 * you are supposed to know what you are doing.
21 */
22void *memalign(size_t boundary, size_t size)
Eric Biederman8ca8d762003-04-22 19:02:15 +000023{
24 void *p;
25
Ronald G. Minnich79431f52012-05-31 16:02:26 -070026 MALLOCDBG("%s Enter, boundary %zu, size %zu, free_mem_ptr %p\n",
Ron Minnicheb596362012-04-11 10:30:15 -070027 __func__, boundary, size, free_mem_ptr);
Stefan Reinauer6bd571e2009-09-25 21:59:57 +000028
Elyes Haouasd6b6b222022-10-10 12:34:21 +020029 free_mem_ptr = (void *)ALIGN_UP((unsigned long)free_mem_ptr, boundary);
Eric Biederman8ca8d762003-04-22 19:02:15 +000030
Ron Minnicheb596362012-04-11 10:30:15 -070031 p = free_mem_ptr;
Eric Biederman8ca8d762003-04-22 19:02:15 +000032 free_mem_ptr += size;
Bora Guvendika347ea32019-12-23 16:44:06 -080033 /*
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 Biederman8ca8d762003-04-22 19:02:15 +000039
Ronald G. Minnich79431f52012-05-31 16:02:26 -070040 if (free_mem_ptr >= free_mem_end_ptr) {
Elyes Haouas39efcd12023-01-22 11:53:56 +010041 printk(BIOS_ERR, "%s(boundary=%zu, size=%zu): failed: ",
42 __func__, boundary, size);
Ronald G. Minnich79431f52012-05-31 16:02:26 -070043 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);
Elyes Haouas39efcd12023-01-22 11:53:56 +010047 die("Error! %s: Out of memory (free_mem_ptr >= free_mem_end_ptr)", __func__);
Ronald G. Minnich79431f52012-05-31 16:02:26 -070048 }
Eric Biederman8ca8d762003-04-22 19:02:15 +000049
Elyes Haouas39efcd12023-01-22 11:53:56 +010050 MALLOCDBG("%s %p\n", __func__, p);
Eric Biederman8ca8d762003-04-22 19:02:15 +000051
52 return p;
53}
54
Ron Minnicheb596362012-04-11 10:30:15 -070055void *malloc(size_t size)
56{
57 return memalign(sizeof(u64), size);
58}
Bora Guvendika347ea32019-12-23 16:44:06 -080059
Tim Wawrzynczakc556dff2021-03-30 11:49:14 -060060void *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 Guvendika347ea32019-12-23 16:44:06 -080069void free(void *ptr)
70{
71 if (ptr == NULL)
72 return;
73
74 if (ptr < (void *)&_heap || ptr >= free_mem_end_ptr) {
Elyes Haouasb1bcd5b2022-11-15 09:51:34 +010075 printk(BIOS_WARNING, "Pointer passed to %s is not "
Bora Guvendika347ea32019-12-23 16:44:06 -080076 "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}