Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 30 | /* |
Uwe Hermann | 661e380 | 2008-03-21 18:37:23 +0000 | [diff] [blame] | 31 | * This is a classically weak malloc() implementation. We have a relatively |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 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). |
| 35 | * |
Uwe Hermann | 661e380 | 2008-03-21 18:37:23 +0000 | [diff] [blame] | 36 | * We're also susceptible to the usual buffer overrun poisoning, though the |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 37 | * risk is within acceptable ranges for this implementation (don't overrun |
| 38 | * your buffers, kids!). |
| 39 | */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 40 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 41 | #include <libpayload.h> |
| 42 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 43 | extern char _heap, _eheap; /* Defined in the ldscript. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 44 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 45 | static void *hstart = (void *)&_heap; |
| 46 | static void *hend = (void *)&_eheap; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 47 | |
| 48 | typedef 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 | |
| 66 | void print_malloc_map(void); |
| 67 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 68 | static void setup(void) |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 69 | { |
Jordan Crouse | 24a0404 | 2008-04-25 23:08:47 +0000 | [diff] [blame] | 70 | int size = (unsigned int)(&_eheap - &_heap) - HDRSIZE; |
| 71 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 72 | *((hdrtype_t *) hstart) = FREE_BLOCK(size); |
| 73 | } |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 74 | |
Patrick Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 75 | static void *alloc(int len, int align) |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 76 | { |
| 77 | hdrtype_t header; |
| 78 | void *ptr = hstart; |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 79 | |
| 80 | /* Align the size. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 81 | len = (len + 3) & ~3; |
| 82 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 83 | if (!len || len > 0xffffff) |
| 84 | return (void *)NULL; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 85 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 86 | /* Make sure the region is setup correctly. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 87 | if (!HAS_MAGIC(*((hdrtype_t *) ptr))) |
| 88 | setup(); |
| 89 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 90 | /* Find some free space. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 91 | do { |
| 92 | header = *((hdrtype_t *) ptr); |
| 93 | int size = SIZE(header); |
| 94 | |
Patrick Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 95 | if (!HAS_MAGIC(header)) { |
Stefan Reinauer | ee67319 | 2008-08-14 14:40:10 +0000 | [diff] [blame] | 96 | printf("memory allocator panic.\n"); |
Jordan Crouse | 24a0404 | 2008-04-25 23:08:47 +0000 | [diff] [blame] | 97 | halt(); |
Stefan Reinauer | ee67319 | 2008-08-14 14:40:10 +0000 | [diff] [blame] | 98 | } |
Jordan Crouse | 24a0404 | 2008-04-25 23:08:47 +0000 | [diff] [blame] | 99 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 100 | if (header & FLAG_FREE) { |
Patrick Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 101 | 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 Crouse | 24a0404 | 2008-04-25 23:08:47 +0000 | [diff] [blame] | 109 | void *nptr = ptr + (HDRSIZE + len); |
Stefan Reinauer | ee67319 | 2008-08-14 14:40:10 +0000 | [diff] [blame] | 110 | int nsize = size - (HDRSIZE + len); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 111 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 112 | /* Mark the block as used. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 113 | *((hdrtype_t *) ptr) = USED_BLOCK(len); |
| 114 | |
| 115 | /* If there is still room in this block, |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 116 | * then mark it as such. |
| 117 | */ |
Jordan Crouse | 24a0404 | 2008-04-25 23:08:47 +0000 | [diff] [blame] | 118 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 119 | if (nsize > 0) |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 120 | *((hdrtype_t *) nptr) = |
Stefan Reinauer | ee67319 | 2008-08-14 14:40:10 +0000 | [diff] [blame] | 121 | FREE_BLOCK(nsize); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 122 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 123 | return (void *)(ptr + HDRSIZE); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | ptr += HDRSIZE + size; |
| 128 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 129 | } while (ptr < hend); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 130 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 131 | /* Nothing available. */ |
| 132 | return (void *)NULL; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void _consolidate(void) |
| 136 | { |
| 137 | void *ptr = hstart; |
| 138 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 139 | while (ptr < hend) { |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 140 | 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 Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 148 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 149 | size = SIZE(hdr); |
| 150 | nptr = ptr + HDRSIZE + SIZE(hdr); |
| 151 | |
| 152 | while (nptr < hend) { |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 153 | hdrtype_t nhdr = *((hdrtype_t *) nptr); |
| 154 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 155 | if (!(IS_FREE(nhdr))) |
| 156 | break; |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 157 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 158 | size += SIZE(nhdr) + HDRSIZE; |
| 159 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 160 | *((hdrtype_t *) nptr) = 0; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 161 | |
| 162 | nptr += (HDRSIZE + SIZE(nhdr)); |
| 163 | } |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 164 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 165 | *((hdrtype_t *) ptr) = FREE_BLOCK(size); |
| 166 | ptr = nptr; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void free(void *ptr) |
| 171 | { |
| 172 | hdrtype_t hdr; |
| 173 | |
| 174 | ptr -= HDRSIZE; |
| 175 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 176 | /* Sanity check. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 177 | if (ptr < hstart || ptr >= hend) |
| 178 | return; |
| 179 | |
| 180 | hdr = *((hdrtype_t *) ptr); |
| 181 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 182 | /* Not our header (we're probably poisoned). */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 183 | if (!HAS_MAGIC(hdr)) |
| 184 | return; |
| 185 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 186 | /* Double free. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 187 | if (hdr & FLAG_FREE) |
| 188 | return; |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 189 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 190 | *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr)); |
| 191 | _consolidate(); |
| 192 | } |
| 193 | |
| 194 | void *malloc(size_t size) |
| 195 | { |
Patrick Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 196 | return alloc(size, 1); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void *calloc(size_t nmemb, size_t size) |
| 200 | { |
Jordan Crouse | 24a0404 | 2008-04-25 23:08:47 +0000 | [diff] [blame] | 201 | size_t total = nmemb * size; |
Patrick Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 202 | void *ptr = alloc(total, 1); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 203 | |
| 204 | if (ptr) |
| 205 | memset(ptr, 0, total); |
| 206 | |
| 207 | return ptr; |
| 208 | } |
| 209 | |
| 210 | void *realloc(void *ptr, size_t size) |
| 211 | { |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 212 | void *ret, *pptr; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 213 | unsigned int osize; |
| 214 | |
| 215 | if (ptr == NULL) |
Patrick Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 216 | return alloc(size, 1); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 217 | |
| 218 | pptr = ptr - HDRSIZE; |
| 219 | |
| 220 | if (!HAS_MAGIC(*((hdrtype_t *) pptr))) |
| 221 | return NULL; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 222 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 223 | /* 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 Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 232 | ret = alloc(size, 1); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 233 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 234 | /* |
| 235 | * if ret == NULL, then doh - failure. |
| 236 | * if ret == ptr then woo-hoo! no copy needed. |
| 237 | */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 238 | if (ret == NULL || ret == ptr) |
| 239 | return ret; |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 240 | |
| 241 | /* Copy the memory to the new location. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 242 | memcpy(ret, ptr, osize > size ? size : osize); |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 243 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 244 | return ret; |
| 245 | } |
| 246 | |
Patrick Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 247 | /** |
| 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 | */ |
| 254 | void *memalign(size_t align, size_t size) |
| 255 | { |
| 256 | return alloc(size, align); |
| 257 | } |
| 258 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 259 | /* This is for debugging purposes. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 260 | #ifdef TEST |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 261 | void print_malloc_map(void) |
| 262 | { |
Patrick Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 263 | void *ptr = hstart; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 264 | |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 265 | while (ptr < hend) { |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 266 | 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 Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 273 | /* FIXME: Verify the size of the block. */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 274 | |
| 275 | printf("%x: %s (%x bytes)\n", |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 276 | (unsigned int)(ptr - hstart), |
| 277 | hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr)); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 278 | |
| 279 | ptr += HDRSIZE + SIZE(hdr); |
| 280 | } |
| 281 | } |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 282 | #endif |