blob: ddc7ea57398f1c80d6e5412ff3539b45647c7c0e [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <stdlib.h>
2#include <console/console.h>
Duncan Laurief5e9ac42012-06-23 13:33:32 -07003#include <cpu/x86/smm.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
58void free(void *ptr)
59{
60 if (ptr == NULL)
61 return;
62
63 if (ptr < (void *)&_heap || ptr >= free_mem_end_ptr) {
64 printk(BIOS_WARNING, "Warning - Pointer passed to %s is not "
65 "pointing to the heap\n", __func__);
66 return;
67 }
68
69 /*
70 * Rewind the heap pointer to the end of heap
71 * before the last successful malloc().
72 */
73 if (ptr == free_last_alloc_ptr) {
74 free_mem_ptr = free_last_alloc_ptr;
75 free_last_alloc_ptr = NULL;
76 }
77}