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 static int free_aligned(void* addr);
67 void print_malloc_map(void);
69 static void setup(void)
71 int size = (unsigned int)(&_eheap - &_heap) - HDRSIZE;
73 *((hdrtype_t *) hstart) = FREE_BLOCK(size);
76 static void *alloc(int len)
84 if (!len || len > 0xffffff)
87 /* Make sure the region is setup correctly. */
88 if (!HAS_MAGIC(*((hdrtype_t *) ptr)))
91 /* Find some free space. */
93 header = *((hdrtype_t *) ptr);
94 int size = SIZE(header);
96 if (!HAS_MAGIC(header) || size == 0) {
97 printf("memory allocator panic.\n");
101 if (header & FLAG_FREE) {
103 void *nptr = ptr + (HDRSIZE + len);
104 int nsize = size - (HDRSIZE + len);
106 /* Mark the block as used. */
107 *((hdrtype_t *) ptr) = USED_BLOCK(len);
109 /* If there is still room in this block,
110 * then mark it as such.
114 *((hdrtype_t *) nptr) =
117 return (void *)(ptr + HDRSIZE);
121 ptr += HDRSIZE + size;
123 } while (ptr < hend);
125 /* Nothing available. */
129 static void _consolidate(void)
135 hdrtype_t hdr = *((hdrtype_t *) ptr);
136 unsigned int size = 0;
139 ptr += HDRSIZE + SIZE(hdr);
144 nptr = ptr + HDRSIZE + SIZE(hdr);
146 while (nptr < hend) {
147 hdrtype_t nhdr = *((hdrtype_t *) nptr);
149 if (!(IS_FREE(nhdr)))
152 size += SIZE(nhdr) + HDRSIZE;
154 *((hdrtype_t *) nptr) = 0;
156 nptr += (HDRSIZE + SIZE(nhdr));
159 *((hdrtype_t *) ptr) = FREE_BLOCK(size);
168 if (free_aligned(ptr)) return;
173 if (ptr < hstart || ptr >= hend)
176 hdr = *((hdrtype_t *) ptr);
178 /* Not our header (we're probably poisoned). */
186 *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
190 void *malloc(size_t size)
195 void *calloc(size_t nmemb, size_t size)
197 size_t total = nmemb * size;
198 void *ptr = alloc(total);
201 memset(ptr, 0, total);
206 void *realloc(void *ptr, size_t size)
214 pptr = ptr - HDRSIZE;
216 if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
219 /* Get the original size of the block. */
220 osize = SIZE(*((hdrtype_t *) pptr));
223 * Free the memory to update the tables - this won't touch the actual
224 * memory, so we can still use it for the copy after we have
225 * reallocated the new space.
231 * if ret == NULL, then doh - failure.
232 * if ret == ptr then woo-hoo! no copy needed.
234 if (ret == NULL || ret == ptr)
237 /* Copy the memory to the new location. */
238 memcpy(ret, ptr, osize > size ? size : osize);
243 struct align_region_t
246 /* start in memory, and size in bytes */
249 /* layout within a region:
250 - num_elements bytes, 0: free, 1: used, 2: used, combines with next
251 - padding to alignment
255 start_data points to the start of the data section
258 /* number of free blocks sized "alignment" */
260 struct align_region_t *next;
263 static struct align_region_t* align_regions = 0;
265 static struct align_region_t *allocate_region(struct align_region_t *old_first, int alignment, int num_elements)
267 struct align_region_t *new_region = malloc(sizeof(struct align_region_t));
268 new_region->alignment = alignment;
269 new_region->start = malloc((num_elements+1) * alignment + num_elements);
270 new_region->start_data = (void*)((u32)(new_region->start + num_elements + alignment - 1) & (~(alignment-1)));
271 new_region->size = num_elements * alignment;
272 new_region->free = num_elements;
273 new_region->next = old_first;
274 memset(new_region->start, 0, num_elements);
279 static int free_aligned(void* addr)
281 struct align_region_t *reg = align_regions;
284 if ((addr >= reg->start_data) && (addr < reg->start_data + reg->size))
286 int i = (addr-reg->start_data)/reg->alignment;
287 while (((u8*)reg->start)[i]==2)
289 ((u8*)reg->start)[i++]=0;
292 ((u8*)reg->start)[i]=0;
301 void *memalign(size_t align, size_t size)
303 if (size == 0) return 0;
304 if (align_regions == 0) {
305 align_regions = malloc(sizeof(struct align_region_t));
306 memset(align_regions, 0, sizeof(struct align_region_t));
308 struct align_region_t *reg = align_regions;
312 if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align))
320 align_regions = allocate_region(align_regions, align, (size/align<99)?100:((size/align)+1));
323 int i, count = 0, target = (size+align-1)/align;
324 for (i = 0; i < (reg->size/align); i++)
326 if (((u8*)reg->start)[i] == 0)
329 if (count == target) {
331 for (i=0; i<target-1; i++)
333 ((u8*)reg->start)[count+i]=2;
335 ((u8*)reg->start)[count+target-1]=1;
337 return reg->start_data+(align*count);
343 goto look_further; // end condition is once a new region is allocated - it always has enough space
346 /* This is for debugging purposes. */
348 void print_malloc_map(void)
353 hdrtype_t hdr = *((hdrtype_t *) ptr);
355 if (!HAS_MAGIC(hdr)) {
356 printf("Poisoned magic - we're toast\n");
360 /* FIXME: Verify the size of the block. */
362 printf("%x: %s (%x bytes)\n",
363 (unsigned int)(ptr - hstart),
364 hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
366 ptr += HDRSIZE + SIZE(hdr);