2008-03-20 00:56:58 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the libpayload project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
2010-03-25 23:15:19 +01:00
|
|
|
* Copyright (C) 2008-2010 coresystems GmbH
|
2008-03-20 00:56:58 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/*
|
2008-03-21 19:37:23 +01:00
|
|
|
* This is a classically weak malloc() implementation. We have a relatively
|
2008-03-20 20:54:59 +01:00
|
|
|
* small and static heap, so we take the easy route with an O(N) loop
|
|
|
|
* through the tree for every malloc() and free(). Obviously, this doesn't
|
|
|
|
* scale past a few hundred KB (if that).
|
|
|
|
*
|
2008-03-21 19:37:23 +01:00
|
|
|
* We're also susceptible to the usual buffer overrun poisoning, though the
|
2008-03-20 20:54:59 +01:00
|
|
|
* risk is within acceptable ranges for this implementation (don't overrun
|
|
|
|
* your buffers, kids!).
|
|
|
|
*/
|
|
|
|
|
2010-03-25 23:15:19 +01:00
|
|
|
#define IN_MALLOC_C
|
2008-03-20 00:56:58 +01:00
|
|
|
#include <libpayload.h>
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
extern char _heap, _eheap; /* Defined in the ldscript. */
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
static void *hstart = (void *)&_heap;
|
|
|
|
static void *hend = (void *)&_eheap;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
typedef unsigned int hdrtype_t;
|
|
|
|
|
|
|
|
#define MAGIC (0x2a << 26)
|
|
|
|
#define FLAG_FREE (1 << 25)
|
|
|
|
#define FLAG_USED (1 << 24)
|
|
|
|
|
|
|
|
#define SIZE(_h) ((_h) & 0xFFFFFF)
|
|
|
|
|
|
|
|
#define _HEADER(_s, _f) ((hdrtype_t) (MAGIC | (_f) | ((_s) & 0xFFFFFF)))
|
|
|
|
|
|
|
|
#define FREE_BLOCK(_s) _HEADER(_s, FLAG_FREE)
|
|
|
|
#define USED_BLOCK(_s) _HEADER(_s, FLAG_USED)
|
|
|
|
|
|
|
|
#define HDRSIZE (sizeof(hdrtype_t))
|
|
|
|
|
|
|
|
#define IS_FREE(_h) (((_h) & (MAGIC | FLAG_FREE)) == (MAGIC | FLAG_FREE))
|
|
|
|
#define HAS_MAGIC(_h) (((_h) & MAGIC) == MAGIC)
|
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
static int free_aligned(void* addr);
|
2008-03-20 00:56:58 +01:00
|
|
|
void print_malloc_map(void);
|
|
|
|
|
2010-03-25 23:15:19 +01:00
|
|
|
#ifdef CONFIG_DEBUG_MALLOC
|
|
|
|
static int heap_initialized = 0;
|
|
|
|
static int minimal_free = 0;
|
|
|
|
#endif
|
|
|
|
|
2012-03-02 00:12:11 +01:00
|
|
|
static void setup(hdrtype_t volatile *start, int size)
|
2008-03-20 00:56:58 +01:00
|
|
|
{
|
2012-03-02 00:12:11 +01:00
|
|
|
*start = FREE_BLOCK(size);
|
2010-03-25 23:15:19 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_MALLOC
|
|
|
|
heap_initialized = 1;
|
|
|
|
minimal_free = size;
|
|
|
|
#endif
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
static void *alloc(int len)
|
2008-03-20 00:56:58 +01:00
|
|
|
{
|
|
|
|
hdrtype_t header;
|
2012-03-02 00:12:11 +01:00
|
|
|
hdrtype_t volatile *ptr = (hdrtype_t volatile *) hstart;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
|
|
|
/* Align the size. */
|
2008-03-20 00:56:58 +01:00
|
|
|
len = (len + 3) & ~3;
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
if (!len || len > 0xffffff)
|
|
|
|
return (void *)NULL;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Make sure the region is setup correctly. */
|
2012-03-02 00:12:11 +01:00
|
|
|
if (!HAS_MAGIC(*ptr))
|
|
|
|
setup(ptr, len);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Find some free space. */
|
2008-03-20 00:56:58 +01:00
|
|
|
do {
|
2012-03-02 00:12:11 +01:00
|
|
|
header = *ptr;
|
2008-03-20 00:56:58 +01:00
|
|
|
int size = SIZE(header);
|
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
if (!HAS_MAGIC(header) || size == 0) {
|
2010-03-25 23:15:19 +01:00
|
|
|
printf("memory allocator panic. (%s%s)\n",
|
|
|
|
!HAS_MAGIC(header) ? " no magic " : "",
|
|
|
|
size == 0 ? " size=0 " : "");
|
2008-04-26 01:08:47 +02:00
|
|
|
halt();
|
2008-08-14 16:40:10 +02:00
|
|
|
}
|
2008-04-26 01:08:47 +02:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
if (header & FLAG_FREE) {
|
2009-04-29 21:11:18 +02:00
|
|
|
if (len <= size) {
|
2012-03-02 00:12:11 +01:00
|
|
|
hdrtype_t volatile *nptr = ptr + (HDRSIZE + len);
|
2008-08-14 16:40:10 +02:00
|
|
|
int nsize = size - (HDRSIZE + len);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
/* If there is still room in this block,
|
2009-05-21 12:02:52 +02:00
|
|
|
* then mark it as such otherwise account
|
|
|
|
* the whole space for that block.
|
2008-03-20 20:54:59 +01:00
|
|
|
*/
|
2008-04-26 01:08:47 +02:00
|
|
|
|
2009-05-21 12:02:52 +02:00
|
|
|
if (nsize > 0) {
|
|
|
|
/* Mark the block as used. */
|
2012-03-02 00:12:11 +01:00
|
|
|
*ptr = USED_BLOCK(len);
|
2009-05-21 12:02:52 +02:00
|
|
|
|
|
|
|
/* Create a new free block. */
|
2012-03-02 00:12:11 +01:00
|
|
|
*nptr = FREE_BLOCK(nsize);
|
2009-05-21 12:02:52 +02:00
|
|
|
} else {
|
|
|
|
/* Mark the block as used. */
|
2012-03-02 00:12:11 +01:00
|
|
|
*ptr = USED_BLOCK(size);
|
2009-05-21 12:02:52 +02:00
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
return (void *)(ptr + HDRSIZE);
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr += HDRSIZE + size;
|
|
|
|
|
2012-03-02 00:12:11 +01:00
|
|
|
} while (ptr < (hdrtype_t *) hend);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Nothing available. */
|
|
|
|
return (void *)NULL;
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _consolidate(void)
|
|
|
|
{
|
|
|
|
void *ptr = hstart;
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
while (ptr < hend) {
|
2008-03-20 00:56:58 +01:00
|
|
|
void *nptr;
|
|
|
|
hdrtype_t hdr = *((hdrtype_t *) ptr);
|
|
|
|
unsigned int size = 0;
|
|
|
|
|
|
|
|
if (!IS_FREE(hdr)) {
|
|
|
|
ptr += HDRSIZE + SIZE(hdr);
|
|
|
|
continue;
|
|
|
|
}
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
size = SIZE(hdr);
|
|
|
|
nptr = ptr + HDRSIZE + SIZE(hdr);
|
|
|
|
|
|
|
|
while (nptr < hend) {
|
2008-03-20 20:54:59 +01:00
|
|
|
hdrtype_t nhdr = *((hdrtype_t *) nptr);
|
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
if (!(IS_FREE(nhdr)))
|
|
|
|
break;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
size += SIZE(nhdr) + HDRSIZE;
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
*((hdrtype_t *) nptr) = 0;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
nptr += (HDRSIZE + SIZE(nhdr));
|
|
|
|
}
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
*((hdrtype_t *) ptr) = FREE_BLOCK(size);
|
|
|
|
ptr = nptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(void *ptr)
|
|
|
|
{
|
|
|
|
hdrtype_t hdr;
|
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
if (free_aligned(ptr)) return;
|
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
ptr -= HDRSIZE;
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Sanity check. */
|
2008-03-20 00:56:58 +01:00
|
|
|
if (ptr < hstart || ptr >= hend)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hdr = *((hdrtype_t *) ptr);
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Not our header (we're probably poisoned). */
|
2008-03-20 00:56:58 +01:00
|
|
|
if (!HAS_MAGIC(hdr))
|
|
|
|
return;
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* Double free. */
|
2008-03-20 00:56:58 +01:00
|
|
|
if (hdr & FLAG_FREE)
|
|
|
|
return;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
*((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
|
|
|
|
_consolidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void *malloc(size_t size)
|
|
|
|
{
|
2009-04-29 21:11:18 +02:00
|
|
|
return alloc(size);
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *calloc(size_t nmemb, size_t size)
|
|
|
|
{
|
2008-04-26 01:08:47 +02:00
|
|
|
size_t total = nmemb * size;
|
2009-04-29 21:11:18 +02:00
|
|
|
void *ptr = alloc(total);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
memset(ptr, 0, total);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *realloc(void *ptr, size_t size)
|
|
|
|
{
|
2008-03-20 20:54:59 +01:00
|
|
|
void *ret, *pptr;
|
2008-03-20 00:56:58 +01:00
|
|
|
unsigned int osize;
|
|
|
|
|
|
|
|
if (ptr == NULL)
|
2009-04-29 21:11:18 +02:00
|
|
|
return alloc(size);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
pptr = ptr - HDRSIZE;
|
|
|
|
|
|
|
|
if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
|
|
|
|
return NULL;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
|
|
|
/* Get the original size of the block. */
|
2008-03-20 00:56:58 +01:00
|
|
|
osize = SIZE(*((hdrtype_t *) pptr));
|
2008-03-20 20:54:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the memory to update the tables - this won't touch the actual
|
|
|
|
* memory, so we can still use it for the copy after we have
|
|
|
|
* reallocated the new space.
|
|
|
|
*/
|
|
|
|
free(ptr);
|
2009-04-29 21:11:18 +02:00
|
|
|
ret = alloc(size);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/*
|
|
|
|
* if ret == NULL, then doh - failure.
|
|
|
|
* if ret == ptr then woo-hoo! no copy needed.
|
|
|
|
*/
|
2008-03-20 00:56:58 +01:00
|
|
|
if (ret == NULL || ret == ptr)
|
|
|
|
return ret;
|
2008-03-20 20:54:59 +01:00
|
|
|
|
|
|
|
/* Copy the memory to the new location. */
|
2008-03-20 00:56:58 +01:00
|
|
|
memcpy(ret, ptr, osize > size ? size : osize);
|
2008-03-20 20:54:59 +01:00
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
struct align_region_t
|
|
|
|
{
|
|
|
|
int alignment;
|
|
|
|
/* start in memory, and size in bytes */
|
|
|
|
void* start;
|
|
|
|
int size;
|
|
|
|
/* layout within a region:
|
|
|
|
- num_elements bytes, 0: free, 1: used, 2: used, combines with next
|
|
|
|
- padding to alignment
|
|
|
|
- data section
|
|
|
|
- waste space
|
|
|
|
|
|
|
|
start_data points to the start of the data section
|
|
|
|
*/
|
|
|
|
void* start_data;
|
|
|
|
/* number of free blocks sized "alignment" */
|
|
|
|
int free;
|
|
|
|
struct align_region_t *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct align_region_t* align_regions = 0;
|
|
|
|
|
2010-03-25 23:15:19 +01:00
|
|
|
static struct align_region_t *allocate_region(int alignment, int num_elements)
|
2009-04-29 21:11:18 +02:00
|
|
|
{
|
2010-03-25 23:15:19 +01:00
|
|
|
struct align_region_t *new_region;
|
|
|
|
#ifdef CONFIG_DEBUG_MALLOC
|
2010-04-27 08:56:47 +02:00
|
|
|
printf("%s(old align_regions=%p, alignment=%u, num_elements=%u)\n",
|
2010-03-25 23:15:19 +01:00
|
|
|
__func__, align_regions, alignment, num_elements);
|
|
|
|
#endif
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2010-03-25 23:15:19 +01:00
|
|
|
new_region = malloc(sizeof(struct align_region_t));
|
|
|
|
|
2009-07-31 16:45:41 +02:00
|
|
|
if (!new_region)
|
|
|
|
return NULL;
|
2009-04-29 21:11:18 +02:00
|
|
|
new_region->alignment = alignment;
|
|
|
|
new_region->start = malloc((num_elements+1) * alignment + num_elements);
|
2009-07-31 16:45:41 +02:00
|
|
|
if (!new_region->start) {
|
|
|
|
free(new_region);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-29 21:11:18 +02:00
|
|
|
new_region->start_data = (void*)((u32)(new_region->start + num_elements + alignment - 1) & (~(alignment-1)));
|
|
|
|
new_region->size = num_elements * alignment;
|
|
|
|
new_region->free = num_elements;
|
2010-03-25 23:15:19 +01:00
|
|
|
new_region->next = align_regions;
|
2009-04-29 21:11:18 +02:00
|
|
|
memset(new_region->start, 0, num_elements);
|
2010-03-25 23:15:19 +01:00
|
|
|
align_regions = new_region;
|
2009-04-29 21:11:18 +02:00
|
|
|
return new_region;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int free_aligned(void* addr)
|
|
|
|
{
|
|
|
|
struct align_region_t *reg = align_regions;
|
|
|
|
while (reg != 0)
|
|
|
|
{
|
|
|
|
if ((addr >= reg->start_data) && (addr < reg->start_data + reg->size))
|
|
|
|
{
|
|
|
|
int i = (addr-reg->start_data)/reg->alignment;
|
|
|
|
while (((u8*)reg->start)[i]==2)
|
|
|
|
{
|
|
|
|
((u8*)reg->start)[i++]=0;
|
|
|
|
reg->free++;
|
|
|
|
}
|
|
|
|
((u8*)reg->start)[i]=0;
|
|
|
|
reg->free++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
reg = reg->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-02 17:49:32 +02:00
|
|
|
void *memalign(size_t align, size_t size)
|
|
|
|
{
|
2009-04-29 21:11:18 +02:00
|
|
|
if (size == 0) return 0;
|
|
|
|
if (align_regions == 0) {
|
|
|
|
align_regions = malloc(sizeof(struct align_region_t));
|
2009-07-31 13:39:55 +02:00
|
|
|
if (align_regions == NULL)
|
|
|
|
return NULL;
|
2009-04-29 21:11:18 +02:00
|
|
|
memset(align_regions, 0, sizeof(struct align_region_t));
|
|
|
|
}
|
|
|
|
struct align_region_t *reg = align_regions;
|
2010-04-27 08:56:47 +02:00
|
|
|
look_further:
|
2009-04-29 21:11:18 +02:00
|
|
|
while (reg != 0)
|
|
|
|
{
|
|
|
|
if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align))
|
|
|
|
{
|
2010-03-25 23:15:19 +01:00
|
|
|
#ifdef CONFIG_DEBUG_MALLOC
|
|
|
|
printf(" found memalign region. %x free, %x required\n", reg->free, (size + align - 1)/align);
|
|
|
|
#endif
|
2009-04-29 21:11:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
reg = reg->next;
|
|
|
|
}
|
|
|
|
if (reg == 0)
|
|
|
|
{
|
2010-03-25 23:15:19 +01:00
|
|
|
#ifdef CONFIG_DEBUG_MALLOC
|
|
|
|
printf(" need to allocate a new memalign region\n");
|
|
|
|
#endif
|
|
|
|
/* get align regions */
|
|
|
|
reg = allocate_region(align, (size<1024)?(1024/align):(((size-1)/align)+1));
|
|
|
|
#ifdef CONFIG_DEBUG_MALLOC
|
|
|
|
printf(" ... returned %p\n", align_regions);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (reg == 0) {
|
|
|
|
/* Nothing available. */
|
|
|
|
return (void *)NULL;
|
2009-04-29 21:11:18 +02:00
|
|
|
}
|
2010-03-25 23:15:19 +01:00
|
|
|
|
2009-04-29 21:11:18 +02:00
|
|
|
int i, count = 0, target = (size+align-1)/align;
|
|
|
|
for (i = 0; i < (reg->size/align); i++)
|
|
|
|
{
|
|
|
|
if (((u8*)reg->start)[i] == 0)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
if (count == target) {
|
|
|
|
count = i+1-count;
|
|
|
|
for (i=0; i<target-1; i++)
|
|
|
|
{
|
|
|
|
((u8*)reg->start)[count+i]=2;
|
|
|
|
}
|
|
|
|
((u8*)reg->start)[count+target-1]=1;
|
|
|
|
reg->free -= target;
|
|
|
|
return reg->start_data+(align*count);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto look_further; // end condition is once a new region is allocated - it always has enough space
|
2008-09-02 17:49:32 +02:00
|
|
|
}
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* This is for debugging purposes. */
|
2010-03-25 23:15:19 +01:00
|
|
|
#ifdef CONFIG_DEBUG_MALLOC
|
2008-03-20 00:56:58 +01:00
|
|
|
void print_malloc_map(void)
|
|
|
|
{
|
2009-04-29 21:11:18 +02:00
|
|
|
void *ptr = hstart;
|
2010-03-25 23:15:19 +01:00
|
|
|
int free_memory = 0;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
while (ptr < hend) {
|
2008-03-20 00:56:58 +01:00
|
|
|
hdrtype_t hdr = *((hdrtype_t *) ptr);
|
|
|
|
|
|
|
|
if (!HAS_MAGIC(hdr)) {
|
2010-03-25 23:15:19 +01:00
|
|
|
if (heap_initialized)
|
|
|
|
printf("Poisoned magic - we're toast\n");
|
|
|
|
else
|
|
|
|
printf("No magic yet - going to initialize\n");
|
2008-03-20 00:56:58 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
/* FIXME: Verify the size of the block. */
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
printf("%x: %s (%x bytes)\n",
|
2008-03-20 20:54:59 +01:00
|
|
|
(unsigned int)(ptr - hstart),
|
|
|
|
hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2010-03-25 23:15:19 +01:00
|
|
|
if (hdr & FLAG_FREE)
|
|
|
|
free_memory += SIZE(hdr);
|
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
ptr += HDRSIZE + SIZE(hdr);
|
|
|
|
}
|
2010-03-25 23:15:19 +01:00
|
|
|
|
|
|
|
if (free_memory && (minimal_free > free_memory))
|
|
|
|
minimal_free = free_memory;
|
|
|
|
printf("Maximum memory consumption: %d bytes",
|
|
|
|
(unsigned int)(&_eheap - &_heap) - HDRSIZE - minimal_free);
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
#endif
|