2 * This file is part of the libpayload project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This is a classically weak malloc() implementation. We have a relatively
32 * small and static heap, so we take the easy route with an O(N) loop
33 * through the tree for every malloc() and free(). Obviously, this doesn't
34 * scale past a few hundred KB (if that).
36 * We're also susceptible to the usual buffer overrun poisoning, though the
37 * risk is within acceptable ranges for this implementation (don't overrun
38 * your buffers, kids!).
41 #include <libpayload.h>
43 extern char _heap, _eheap; /* Defined in the ldscript. */
45 static void *hstart = (void *)&_heap;
46 static void *hend = (void *)&_eheap;
48 typedef unsigned int hdrtype_t;
50 #define MAGIC (0x2a << 26)
51 #define FLAG_FREE (1 << 25)
52 #define FLAG_USED (1 << 24)
54 #define SIZE(_h) ((_h) & 0xFFFFFF)
56 #define _HEADER(_s, _f) ((hdrtype_t) (MAGIC | (_f) | ((_s) & 0xFFFFFF)))
58 #define FREE_BLOCK(_s) _HEADER(_s, FLAG_FREE)
59 #define USED_BLOCK(_s) _HEADER(_s, FLAG_USED)
61 #define HDRSIZE (sizeof(hdrtype_t))
63 #define IS_FREE(_h) (((_h) & (MAGIC | FLAG_FREE)) == (MAGIC | FLAG_FREE))
64 #define HAS_MAGIC(_h) (((_h) & MAGIC) == MAGIC)
66 void print_malloc_map(void);
68 static void setup(void)
70 int size = (unsigned int)(&_eheap - &_heap) - HDRSIZE;
72 *((hdrtype_t *) hstart) = FREE_BLOCK(size);
75 static void *alloc(int len)
83 if (!len || len > 0xffffff)
86 /* Make sure the region is setup correctly. */
87 if (!HAS_MAGIC(*((hdrtype_t *) ptr)))
90 /* Find some free space. */
92 header = *((hdrtype_t *) ptr);
93 int size = SIZE(header);
95 if (!HAS_MAGIC(header) || size == 0) {
96 printf("memory allocator panic.\n");
100 if (header & FLAG_FREE) {
102 void *nptr = ptr + (HDRSIZE + len);
103 int nsize = size - (HDRSIZE + len);
105 /* Mark the block as used. */
106 *((hdrtype_t *) ptr) = USED_BLOCK(len);
108 /* If there is still room in this block,
109 * then mark it as such.
113 *((hdrtype_t *) nptr) =
116 return (void *)(ptr + HDRSIZE);
120 ptr += HDRSIZE + size;
122 } while (ptr < hend);
124 /* Nothing available. */
128 static void _consolidate(void)
134 hdrtype_t hdr = *((hdrtype_t *) ptr);
135 unsigned int size = 0;
138 ptr += HDRSIZE + SIZE(hdr);
143 nptr = ptr + HDRSIZE + SIZE(hdr);
145 while (nptr < hend) {
146 hdrtype_t nhdr = *((hdrtype_t *) nptr);
148 if (!(IS_FREE(nhdr)))
151 size += SIZE(nhdr) + HDRSIZE;
153 *((hdrtype_t *) nptr) = 0;
155 nptr += (HDRSIZE + SIZE(nhdr));
158 *((hdrtype_t *) ptr) = FREE_BLOCK(size);
170 if (ptr < hstart || ptr >= hend)
173 hdr = *((hdrtype_t *) ptr);
175 /* Not our header (we're probably poisoned). */
183 *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
187 void *malloc(size_t size)
192 void *calloc(size_t nmemb, size_t size)
194 size_t total = nmemb * size;
195 void *ptr = alloc(total);
198 memset(ptr, 0, total);
203 void *realloc(void *ptr, size_t size)
211 pptr = ptr - HDRSIZE;
213 if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
216 /* Get the original size of the block. */
217 osize = SIZE(*((hdrtype_t *) pptr));
220 * Free the memory to update the tables - this won't touch the actual
221 * memory, so we can still use it for the copy after we have
222 * reallocated the new space.
228 * if ret == NULL, then doh - failure.
229 * if ret == ptr then woo-hoo! no copy needed.
231 if (ret == NULL || ret == ptr)
234 /* Copy the memory to the new location. */
235 memcpy(ret, ptr, osize > size ? size : osize);
240 /* This is for debugging purposes. */
242 void print_malloc_map(void)
247 hdrtype_t hdr = *((hdrtype_t *) ptr);
249 if (!HAS_MAGIC(hdr)) {
250 printf("Poisoned magic - we're toast\n");
254 /* FIXME: Verify the size of the block. */
256 printf("%x: %s (%x bytes)\n",
257 (unsigned int)(ptr - hstart),
258 hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
260 ptr += HDRSIZE + SIZE(hdr);