blob: 6dc6aab138af26d6d7cf17cc4f5b55d353b4859c [file] [log] [blame]
Jordan Crousef6145c32008-03-19 23:56:58 +00001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
Uwe Hermann6a441bf2008-03-20 19:54:59 +000030/*
Uwe Hermann661e3802008-03-21 18:37:23 +000031 * This is a classically weak malloc() implementation. We have a relatively
Uwe Hermann6a441bf2008-03-20 19:54:59 +000032 * 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).
35 *
Uwe Hermann661e3802008-03-21 18:37:23 +000036 * We're also susceptible to the usual buffer overrun poisoning, though the
Uwe Hermann6a441bf2008-03-20 19:54:59 +000037 * risk is within acceptable ranges for this implementation (don't overrun
38 * your buffers, kids!).
39 */
Jordan Crousef6145c32008-03-19 23:56:58 +000040
Jordan Crousef6145c32008-03-19 23:56:58 +000041#include <libpayload.h>
42
Uwe Hermann6a441bf2008-03-20 19:54:59 +000043extern char _heap, _eheap; /* Defined in the ldscript. */
Jordan Crousef6145c32008-03-19 23:56:58 +000044
Uwe Hermann6a441bf2008-03-20 19:54:59 +000045static void *hstart = (void *)&_heap;
46static void *hend = (void *)&_eheap;
Jordan Crousef6145c32008-03-19 23:56:58 +000047
48typedef unsigned int hdrtype_t;
49
50#define MAGIC (0x2a << 26)
51#define FLAG_FREE (1 << 25)
52#define FLAG_USED (1 << 24)
53
54#define SIZE(_h) ((_h) & 0xFFFFFF)
55
56#define _HEADER(_s, _f) ((hdrtype_t) (MAGIC | (_f) | ((_s) & 0xFFFFFF)))
57
58#define FREE_BLOCK(_s) _HEADER(_s, FLAG_FREE)
59#define USED_BLOCK(_s) _HEADER(_s, FLAG_USED)
60
61#define HDRSIZE (sizeof(hdrtype_t))
62
63#define IS_FREE(_h) (((_h) & (MAGIC | FLAG_FREE)) == (MAGIC | FLAG_FREE))
64#define HAS_MAGIC(_h) (((_h) & MAGIC) == MAGIC)
65
66void print_malloc_map(void);
67
Uwe Hermann6a441bf2008-03-20 19:54:59 +000068static void setup(void)
Jordan Crousef6145c32008-03-19 23:56:58 +000069{
Jordan Crouse24a04042008-04-25 23:08:47 +000070 int size = (unsigned int)(&_eheap - &_heap) - HDRSIZE;
71
Jordan Crousef6145c32008-03-19 23:56:58 +000072 *((hdrtype_t *) hstart) = FREE_BLOCK(size);
73}
Uwe Hermann6a441bf2008-03-20 19:54:59 +000074
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +000075static void *alloc(int len, int align)
Jordan Crousef6145c32008-03-19 23:56:58 +000076{
77 hdrtype_t header;
78 void *ptr = hstart;
Uwe Hermann6a441bf2008-03-20 19:54:59 +000079
80 /* Align the size. */
Jordan Crousef6145c32008-03-19 23:56:58 +000081 len = (len + 3) & ~3;
82
Uwe Hermann6a441bf2008-03-20 19:54:59 +000083 if (!len || len > 0xffffff)
84 return (void *)NULL;
Jordan Crousef6145c32008-03-19 23:56:58 +000085
Uwe Hermann6a441bf2008-03-20 19:54:59 +000086 /* Make sure the region is setup correctly. */
Jordan Crousef6145c32008-03-19 23:56:58 +000087 if (!HAS_MAGIC(*((hdrtype_t *) ptr)))
88 setup();
89
Uwe Hermann6a441bf2008-03-20 19:54:59 +000090 /* Find some free space. */
Jordan Crousef6145c32008-03-19 23:56:58 +000091 do {
92 header = *((hdrtype_t *) ptr);
93 int size = SIZE(header);
94
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +000095 if (!HAS_MAGIC(header)) {
Stefan Reinaueree673192008-08-14 14:40:10 +000096 printf("memory allocator panic.\n");
Jordan Crouse24a04042008-04-25 23:08:47 +000097 halt();
Stefan Reinaueree673192008-08-14 14:40:10 +000098 }
Jordan Crouse24a04042008-04-25 23:08:47 +000099
Jordan Crousef6145c32008-03-19 23:56:58 +0000100 if (header & FLAG_FREE) {
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +0000101 int realaddr = (int)(ptr + HDRSIZE);
102 int overhead = ((realaddr+align-1) & ~(align-1)) - realaddr;
103 if (len + overhead <= size) {
104 if (overhead != 0) {
105 *((hdrtype_t *) ptr) = FREE_BLOCK(overhead - HDRSIZE);
106 ptr += overhead;
107 size -= overhead;
108 }
Jordan Crouse24a04042008-04-25 23:08:47 +0000109 void *nptr = ptr + (HDRSIZE + len);
Stefan Reinaueree673192008-08-14 14:40:10 +0000110 int nsize = size - (HDRSIZE + len);
Jordan Crousef6145c32008-03-19 23:56:58 +0000111
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000112 /* Mark the block as used. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000113 *((hdrtype_t *) ptr) = USED_BLOCK(len);
114
115 /* If there is still room in this block,
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000116 * then mark it as such.
117 */
Jordan Crouse24a04042008-04-25 23:08:47 +0000118
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000119 if (nsize > 0)
Jordan Crousef6145c32008-03-19 23:56:58 +0000120 *((hdrtype_t *) nptr) =
Stefan Reinaueree673192008-08-14 14:40:10 +0000121 FREE_BLOCK(nsize);
Jordan Crousef6145c32008-03-19 23:56:58 +0000122
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000123 return (void *)(ptr + HDRSIZE);
Jordan Crousef6145c32008-03-19 23:56:58 +0000124 }
125 }
126
127 ptr += HDRSIZE + size;
128
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000129 } while (ptr < hend);
Jordan Crousef6145c32008-03-19 23:56:58 +0000130
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000131 /* Nothing available. */
132 return (void *)NULL;
Jordan Crousef6145c32008-03-19 23:56:58 +0000133}
134
135static void _consolidate(void)
136{
137 void *ptr = hstart;
138
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000139 while (ptr < hend) {
Jordan Crousef6145c32008-03-19 23:56:58 +0000140 void *nptr;
141 hdrtype_t hdr = *((hdrtype_t *) ptr);
142 unsigned int size = 0;
143
144 if (!IS_FREE(hdr)) {
145 ptr += HDRSIZE + SIZE(hdr);
146 continue;
147 }
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000148
Jordan Crousef6145c32008-03-19 23:56:58 +0000149 size = SIZE(hdr);
150 nptr = ptr + HDRSIZE + SIZE(hdr);
151
152 while (nptr < hend) {
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000153 hdrtype_t nhdr = *((hdrtype_t *) nptr);
154
Jordan Crousef6145c32008-03-19 23:56:58 +0000155 if (!(IS_FREE(nhdr)))
156 break;
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000157
Jordan Crousef6145c32008-03-19 23:56:58 +0000158 size += SIZE(nhdr) + HDRSIZE;
159
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000160 *((hdrtype_t *) nptr) = 0;
Jordan Crousef6145c32008-03-19 23:56:58 +0000161
162 nptr += (HDRSIZE + SIZE(nhdr));
163 }
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000164
Jordan Crousef6145c32008-03-19 23:56:58 +0000165 *((hdrtype_t *) ptr) = FREE_BLOCK(size);
166 ptr = nptr;
167 }
168}
169
170void free(void *ptr)
171{
172 hdrtype_t hdr;
173
174 ptr -= HDRSIZE;
175
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000176 /* Sanity check. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000177 if (ptr < hstart || ptr >= hend)
178 return;
179
180 hdr = *((hdrtype_t *) ptr);
181
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000182 /* Not our header (we're probably poisoned). */
Jordan Crousef6145c32008-03-19 23:56:58 +0000183 if (!HAS_MAGIC(hdr))
184 return;
185
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000186 /* Double free. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000187 if (hdr & FLAG_FREE)
188 return;
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000189
Jordan Crousef6145c32008-03-19 23:56:58 +0000190 *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
191 _consolidate();
192}
193
194void *malloc(size_t size)
195{
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +0000196 return alloc(size, 1);
Jordan Crousef6145c32008-03-19 23:56:58 +0000197}
198
199void *calloc(size_t nmemb, size_t size)
200{
Jordan Crouse24a04042008-04-25 23:08:47 +0000201 size_t total = nmemb * size;
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +0000202 void *ptr = alloc(total, 1);
Jordan Crousef6145c32008-03-19 23:56:58 +0000203
204 if (ptr)
205 memset(ptr, 0, total);
206
207 return ptr;
208}
209
210void *realloc(void *ptr, size_t size)
211{
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000212 void *ret, *pptr;
Jordan Crousef6145c32008-03-19 23:56:58 +0000213 unsigned int osize;
214
215 if (ptr == NULL)
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +0000216 return alloc(size, 1);
Jordan Crousef6145c32008-03-19 23:56:58 +0000217
218 pptr = ptr - HDRSIZE;
219
220 if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
221 return NULL;
Jordan Crousef6145c32008-03-19 23:56:58 +0000222
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000223 /* Get the original size of the block. */
224 osize = SIZE(*((hdrtype_t *) pptr));
225
226 /*
227 * Free the memory to update the tables - this won't touch the actual
228 * memory, so we can still use it for the copy after we have
229 * reallocated the new space.
230 */
231 free(ptr);
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +0000232 ret = alloc(size, 1);
Jordan Crousef6145c32008-03-19 23:56:58 +0000233
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000234 /*
235 * if ret == NULL, then doh - failure.
236 * if ret == ptr then woo-hoo! no copy needed.
237 */
Jordan Crousef6145c32008-03-19 23:56:58 +0000238 if (ret == NULL || ret == ptr)
239 return ret;
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000240
241 /* Copy the memory to the new location. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000242 memcpy(ret, ptr, osize > size ? size : osize);
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000243
Jordan Crousef6145c32008-03-19 23:56:58 +0000244 return ret;
245}
246
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +0000247/**
248 * Allocate an aligned chunk of memory
249 *
250 * @param align alignment, must be power of two
251 * @param size size of chunk in bytes
252 * @return Return the address of such a memory region or NULL
253 */
254void *memalign(size_t align, size_t size)
255{
256 return alloc(size, align);
257}
258
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000259/* This is for debugging purposes. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000260#ifdef TEST
Jordan Crousef6145c32008-03-19 23:56:58 +0000261void print_malloc_map(void)
262{
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +0000263void *ptr = hstart;
Jordan Crousef6145c32008-03-19 23:56:58 +0000264
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000265 while (ptr < hend) {
Jordan Crousef6145c32008-03-19 23:56:58 +0000266 hdrtype_t hdr = *((hdrtype_t *) ptr);
267
268 if (!HAS_MAGIC(hdr)) {
269 printf("Poisoned magic - we're toast\n");
270 break;
271 }
272
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000273 /* FIXME: Verify the size of the block. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000274
275 printf("%x: %s (%x bytes)\n",
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000276 (unsigned int)(ptr - hstart),
277 hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
Jordan Crousef6145c32008-03-19 23:56:58 +0000278
279 ptr += HDRSIZE + SIZE(hdr);
280 }
281}
Jordan Crousef6145c32008-03-19 23:56:58 +0000282#endif