blob: 30298064d9eb92e6c072499eb15ca7e30844eb1a [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);
Patrick Georgic59426f2024-01-26 19:47:29 +010047 printk(BIOS_ERR, "Error! %s: Out of memory "
48 "(free_mem_ptr >= free_mem_end_ptr)",
49 __func__);
50 return NULL;
Ronald G. Minnich79431f52012-05-31 16:02:26 -070051 }
Eric Biederman8ca8d762003-04-22 19:02:15 +000052
Elyes Haouas39efcd12023-01-22 11:53:56 +010053 MALLOCDBG("%s %p\n", __func__, p);
Eric Biederman8ca8d762003-04-22 19:02:15 +000054
55 return p;
56}
57
Ron Minnicheb596362012-04-11 10:30:15 -070058void *malloc(size_t size)
59{
60 return memalign(sizeof(u64), size);
61}
Bora Guvendika347ea32019-12-23 16:44:06 -080062
Tim Wawrzynczakc556dff2021-03-30 11:49:14 -060063void *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
Bora Guvendika347ea32019-12-23 16:44:06 -080072void free(void *ptr)
73{
74 if (ptr == NULL)
75 return;
76
77 if (ptr < (void *)&_heap || ptr >= free_mem_end_ptr) {
Elyes Haouasb1bcd5b2022-11-15 09:51:34 +010078 printk(BIOS_WARNING, "Pointer passed to %s is not "
Bora Guvendika347ea32019-12-23 16:44:06 -080079 "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}