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() implmentation. 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 susecptable to the usual buffer overun 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)(_heap - _eheap) - HDRSIZE;
71 *((hdrtype_t *) hstart) = FREE_BLOCK(size);
74 static void *alloc(int len)
82 if (!len || len > 0xffffff)
85 /* Make sure the region is setup correctly. */
86 if (!HAS_MAGIC(*((hdrtype_t *) ptr)))
89 /* Find some free space. */
91 header = *((hdrtype_t *) ptr);
92 int size = SIZE(header);
94 if (header & FLAG_FREE) {
96 void *nptr = ptr + HDRSIZE + len;
97 int nsize = size - (len + 8);
99 /* Mark the block as used. */
100 *((hdrtype_t *) ptr) = USED_BLOCK(len);
102 /* If there is still room in this block,
103 * then mark it as such.
106 *((hdrtype_t *) nptr) =
107 FREE_BLOCK(nsize - 4);
109 return (void *)(ptr + HDRSIZE);
113 ptr += HDRSIZE + size;
115 } while (ptr < hend);
117 /* Nothing available. */
121 static void _consolidate(void)
127 hdrtype_t hdr = *((hdrtype_t *) ptr);
128 unsigned int size = 0;
131 ptr += HDRSIZE + SIZE(hdr);
136 nptr = ptr + HDRSIZE + SIZE(hdr);
138 while (nptr < hend) {
139 hdrtype_t nhdr = *((hdrtype_t *) nptr);
141 if (!(IS_FREE(nhdr)))
144 size += SIZE(nhdr) + HDRSIZE;
146 *((hdrtype_t *) nptr) = 0;
148 nptr += (HDRSIZE + SIZE(nhdr));
151 *((hdrtype_t *) ptr) = FREE_BLOCK(size);
163 if (ptr < hstart || ptr >= hend)
166 hdr = *((hdrtype_t *) ptr);
168 /* Not our header (we're probably poisoned). */
176 *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
180 void *malloc(size_t size)
185 void *calloc(size_t nmemb, size_t size)
187 unsigned int total = (nmemb * size);
188 void *ptr = alloc(size);
191 memset(ptr, 0, total);
196 void *realloc(void *ptr, size_t size)
204 pptr = ptr - HDRSIZE;
206 if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
209 /* Get the original size of the block. */
210 osize = SIZE(*((hdrtype_t *) pptr));
213 * Free the memory to update the tables - this won't touch the actual
214 * memory, so we can still use it for the copy after we have
215 * reallocated the new space.
221 * if ret == NULL, then doh - failure.
222 * if ret == ptr then woo-hoo! no copy needed.
224 if (ret == NULL || ret == ptr)
227 /* Copy the memory to the new location. */
228 memcpy(ret, ptr, osize > size ? size : osize);
233 /* This is for debugging purposes. */
235 void print_malloc_map(void)
240 hdrtype_t hdr = *((hdrtype_t *) ptr);
242 if (!HAS_MAGIC(hdr)) {
243 printf("Poisoned magic - we're toast\n");
247 /* FIXME: Verify the size of the block. */
249 printf("%x: %s (%x bytes)\n",
250 (unsigned int)(ptr - hstart),
251 hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
253 ptr += HDRSIZE + SIZE(hdr);