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 /* If there is still room in this block,
107 * then mark it as such otherwise account
108 * the whole space for that block.
112 /* Mark the block as used. */
113 *((hdrtype_t *) ptr) = USED_BLOCK(len);
115 /* Create a new free block. */
116 *((hdrtype_t *) nptr) =
119 /* Mark the block as used. */
120 *((hdrtype_t *) ptr) = USED_BLOCK(size);
123 return (void *)(ptr + HDRSIZE);
127 ptr += HDRSIZE + size;
129 } while (ptr < hend);
131 /* Nothing available. */
135 static void _consolidate(void)
141 hdrtype_t hdr = *((hdrtype_t *) ptr);
142 unsigned int size = 0;
145 ptr += HDRSIZE + SIZE(hdr);
150 nptr = ptr + HDRSIZE + SIZE(hdr);
152 while (nptr < hend) {
153 hdrtype_t nhdr = *((hdrtype_t *) nptr);
155 if (!(IS_FREE(nhdr)))
158 size += SIZE(nhdr) + HDRSIZE;
160 *((hdrtype_t *) nptr) = 0;
162 nptr += (HDRSIZE + SIZE(nhdr));
165 *((hdrtype_t *) ptr) = FREE_BLOCK(size);
174 if (free_aligned(ptr)) return;
179 if (ptr < hstart || ptr >= hend)
182 hdr = *((hdrtype_t *) ptr);
184 /* Not our header (we're probably poisoned). */
192 *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
196 void *malloc(size_t size)
201 void *calloc(size_t nmemb, size_t size)
203 size_t total = nmemb * size;
204 void *ptr = alloc(total);
207 memset(ptr, 0, total);
212 void *realloc(void *ptr, size_t size)
220 pptr = ptr - HDRSIZE;
222 if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
225 /* Get the original size of the block. */
226 osize = SIZE(*((hdrtype_t *) pptr));
229 * Free the memory to update the tables - this won't touch the actual
230 * memory, so we can still use it for the copy after we have
231 * reallocated the new space.
237 * if ret == NULL, then doh - failure.
238 * if ret == ptr then woo-hoo! no copy needed.
240 if (ret == NULL || ret == ptr)
243 /* Copy the memory to the new location. */
244 memcpy(ret, ptr, osize > size ? size : osize);
249 struct align_region_t
252 /* start in memory, and size in bytes */
255 /* layout within a region:
256 - num_elements bytes, 0: free, 1: used, 2: used, combines with next
257 - padding to alignment
261 start_data points to the start of the data section
264 /* number of free blocks sized "alignment" */
266 struct align_region_t *next;
269 static struct align_region_t* align_regions = 0;
271 static struct align_region_t *allocate_region(struct align_region_t *old_first, int alignment, int num_elements)
273 struct align_region_t *new_region = malloc(sizeof(struct align_region_t));
274 new_region->alignment = alignment;
275 new_region->start = malloc((num_elements+1) * alignment + num_elements);
276 new_region->start_data = (void*)((u32)(new_region->start + num_elements + alignment - 1) & (~(alignment-1)));
277 new_region->size = num_elements * alignment;
278 new_region->free = num_elements;
279 new_region->next = old_first;
280 memset(new_region->start, 0, num_elements);
285 static int free_aligned(void* addr)
287 struct align_region_t *reg = align_regions;
290 if ((addr >= reg->start_data) && (addr < reg->start_data + reg->size))
292 int i = (addr-reg->start_data)/reg->alignment;
293 while (((u8*)reg->start)[i]==2)
295 ((u8*)reg->start)[i++]=0;
298 ((u8*)reg->start)[i]=0;
307 void *memalign(size_t align, size_t size)
309 if (size == 0) return 0;
310 if (align_regions == 0) {
311 align_regions = malloc(sizeof(struct align_region_t));
312 if (align_regions == NULL)
314 memset(align_regions, 0, sizeof(struct align_region_t));
316 struct align_region_t *reg = align_regions;
320 if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align))
328 align_regions = allocate_region(align_regions, align, (size/align<99)?100:((size/align)+1));
331 int i, count = 0, target = (size+align-1)/align;
332 for (i = 0; i < (reg->size/align); i++)
334 if (((u8*)reg->start)[i] == 0)
337 if (count == target) {
339 for (i=0; i<target-1; i++)
341 ((u8*)reg->start)[count+i]=2;
343 ((u8*)reg->start)[count+target-1]=1;
345 return reg->start_data+(align*count);
351 goto look_further; // end condition is once a new region is allocated - it always has enough space
354 /* This is for debugging purposes. */
356 void print_malloc_map(void)
361 hdrtype_t hdr = *((hdrtype_t *) ptr);
363 if (!HAS_MAGIC(hdr)) {
364 printf("Poisoned magic - we're toast\n");
368 /* FIXME: Verify the size of the block. */
370 printf("%x: %s (%x bytes)\n",
371 (unsigned int)(ptr - hstart),
372 hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
374 ptr += HDRSIZE + SIZE(hdr);