blob: 7d787d631e13579b4d42ee5b6c117e4152bc5155 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <console/console.h>
Tim Wawrzynczakc556dff2021-03-30 11:49:14 -06002#include <stdlib.h>
3#include <string.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00004
Julius Wernercd49cce2019-03-05 16:53:33 -08005#if CONFIG(DEBUG_MALLOC)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00006#define MALLOCDBG(x...) printk(BIOS_SPEW, x)
Uwe Hermanna953f372010-11-10 00:14:32 +00007#else
Peter Stuge5015f792010-11-10 02:00:32 +00008#define MALLOCDBG(x...)
Eric Biederman8ca8d762003-04-22 19:02:15 +00009#endif
Uwe Hermanna953f372010-11-10 00:14:32 +000010
Eric Biederman8ca8d762003-04-22 19:02:15 +000011extern unsigned char _heap, _eheap;
Stefan Reinauer054c7232009-03-06 17:22:35 +000012static void *free_mem_ptr = &_heap; /* Start of heap */
13static void *free_mem_end_ptr = &_eheap; /* End of heap */
Bora Guvendika347ea32019-12-23 16:44:06 -080014static void *free_last_alloc_ptr = &_heap; /* End of heap before
15 last allocation */
Eric Biederman8ca8d762003-04-22 19:02:15 +000016
Ron Minnicheb596362012-04-11 10:30:15 -070017/* We don't restrict the boundary. This is firmware,
18 * you are supposed to know what you are doing.
19 */
20void *memalign(size_t boundary, size_t size)
Eric Biederman8ca8d762003-04-22 19:02:15 +000021{
22 void *p;
23
Ronald G. Minnich79431f52012-05-31 16:02:26 -070024 MALLOCDBG("%s Enter, boundary %zu, size %zu, free_mem_ptr %p\n",
Ron Minnicheb596362012-04-11 10:30:15 -070025 __func__, boundary, size, free_mem_ptr);
Stefan Reinauer6bd571e2009-09-25 21:59:57 +000026
Ron Minnicheb596362012-04-11 10:30:15 -070027 free_mem_ptr = (void *)ALIGN((unsigned long)free_mem_ptr, boundary);
Eric Biederman8ca8d762003-04-22 19:02:15 +000028
Ron Minnicheb596362012-04-11 10:30:15 -070029 p = free_mem_ptr;
Eric Biederman8ca8d762003-04-22 19:02:15 +000030 free_mem_ptr += size;
Bora Guvendika347ea32019-12-23 16:44:06 -080031 /*
32 * Store last allocation pointer after ALIGN, as malloc() will
33 * return it. This may cause n bytes of gap between allocations
34 * where n < boundary.
35 */
36 free_last_alloc_ptr = p;
Eric Biederman8ca8d762003-04-22 19:02:15 +000037
Ronald G. Minnich79431f52012-05-31 16:02:26 -070038 if (free_mem_ptr >= free_mem_end_ptr) {
39 printk(BIOS_ERR, "memalign(boundary=%zu, size=%zu): failed: ",
40 boundary, size);
41 printk(BIOS_ERR, "Tried to round up free_mem_ptr %p to %p\n",
42 p, free_mem_ptr);
43 printk(BIOS_ERR, "but free_mem_end_ptr is %p\n",
44 free_mem_end_ptr);
Ron Minnicheb596362012-04-11 10:30:15 -070045 die("Error! memalign: Out of memory (free_mem_ptr >= free_mem_end_ptr)");
Ronald G. Minnich79431f52012-05-31 16:02:26 -070046 }
Eric Biederman8ca8d762003-04-22 19:02:15 +000047
Ron Minnicheb596362012-04-11 10:30:15 -070048 MALLOCDBG("memalign %p\n", p);
Eric Biederman8ca8d762003-04-22 19:02:15 +000049
50 return p;
51}
52
Ron Minnicheb596362012-04-11 10:30:15 -070053void *malloc(size_t size)
54{
55 return memalign(sizeof(u64), size);
56}
Bora Guvendika347ea32019-12-23 16:44:06 -080057
Tim Wawrzynczakc556dff2021-03-30 11:49:14 -060058void *calloc(size_t nitems, size_t size)
59{
60 void *p = malloc(nitems * size);
61 if (p)
62 memset(p, 0, nitems * size);
63
64 return p;
65}
66
Bora Guvendika347ea32019-12-23 16:44:06 -080067void free(void *ptr)
68{
69 if (ptr == NULL)
70 return;
71
72 if (ptr < (void *)&_heap || ptr >= free_mem_end_ptr) {
73 printk(BIOS_WARNING, "Warning - Pointer passed to %s is not "
74 "pointing to the heap\n", __func__);
75 return;
76 }
77
78 /*
79 * Rewind the heap pointer to the end of heap
80 * before the last successful malloc().
81 */
82 if (ptr == free_last_alloc_ptr) {
83 free_mem_ptr = free_last_alloc_ptr;
84 free_last_alloc_ptr = NULL;
85 }
86}