david/ipxe
Archived
1
0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/core/heap.c

138 lines
2.9 KiB
C
Raw Normal View History

2005-03-08 19:53:11 +01:00
#include "etherboot.h"
#include "init.h"
#include "memsizes.h"
#include "heap.h"
struct heap_block {
size_t size;
char data[0];
};
2005-03-08 19:53:11 +01:00
2005-05-13 13:16:14 +02:00
/* Linker symbols */
extern char _text[];
extern char _end[];
2005-03-08 19:53:11 +01:00
2005-05-13 13:16:14 +02:00
static unsigned long heap_start, heap_end, heap_ptr;
/*
* Find the largest contiguous area of memory that I can use for the
* heap.
*
*/
static void init_heap ( void ) {
unsigned int i;
unsigned long eb_start, eb_end;
unsigned long size;
2005-03-08 19:53:11 +01:00
size = 0;
2005-05-13 13:16:14 +02:00
/* Region occupied by Etherboot */
eb_start = virt_to_phys ( _text );
eb_end = virt_to_phys ( _end );
for ( i = 0 ; i < meminfo.map_count ; i++ ) {
unsigned long r_start, r_end, r_size;
unsigned long pre_eb, post_eb;
/* Get start and end addresses of the region */
if ( meminfo.map[i].type != E820_RAM )
2005-03-08 19:53:11 +01:00
continue;
2005-05-13 13:16:14 +02:00
if ( meminfo.map[i].addr > ULONG_MAX )
2005-03-08 19:53:11 +01:00
continue;
r_start = meminfo.map[i].addr;
2005-05-13 13:16:14 +02:00
if ( r_start + meminfo.map[i].size > ULONG_MAX ) {
2005-03-08 19:53:11 +01:00
r_end = ULONG_MAX;
2005-05-13 13:16:14 +02:00
} else {
r_end = r_start + meminfo.map[i].size;
2005-03-08 19:53:11 +01:00
}
2005-05-13 13:16:14 +02:00
/* Avoid overlap with Etherboot. When Etherboot is
* completely contained within the region, choose the
* larger of the two remaining portions.
*/
if ( ( eb_start < r_end ) && ( eb_end > r_start ) ) {
pre_eb = ( eb_start > r_start ) ?
( eb_start - r_start ) : 0;
post_eb = ( r_end > eb_end ) ?
( r_end - eb_end ) : 0;
if ( pre_eb > post_eb ) {
r_end = eb_start;
} else {
r_start = eb_end;
2005-03-08 19:53:11 +01:00
}
}
2005-05-13 13:16:14 +02:00
/* Use the biggest region. Where two regions are the
* same size, use the later region. (Provided that
* the memory map is laid out in a sensible order,
* this should give us the higher region.)
*/
r_size = r_end - r_start;
if ( r_size >= size ) {
heap_start = r_start;
heap_end = r_end;
size = r_size;
2005-03-08 19:53:11 +01:00
}
}
2005-05-13 13:16:14 +02:00
ASSERT ( size != 0 );
heap_ptr = heap_end;
2005-03-08 19:53:11 +01:00
}
/*
* Allocate a block from the heap.
*
*/
void * emalloc ( size_t size, unsigned int align ) {
physaddr_t addr;
struct heap_block *block;
2005-05-13 12:18:21 +02:00
ASSERT ( ( align & ( align - 1 ) ) == 0 );
addr = ( ( ( heap_ptr - size ) & ~( align - 1 ) )
- sizeof ( struct heap_block ) );
2005-05-13 13:16:14 +02:00
if ( addr < heap_start ) {
return NULL;
2005-03-08 19:53:11 +01:00
}
block = phys_to_virt ( addr );
block->size = ( heap_ptr - addr );
heap_ptr = addr;
return block->data;
2005-03-08 19:53:11 +01:00
}
/*
* Allocate all remaining space on the heap
*
*/
void * emalloc_all ( size_t *size ) {
2005-05-13 13:16:14 +02:00
*size = heap_ptr - heap_start - sizeof ( struct heap_block );
return emalloc ( *size, sizeof ( void * ) );
}
2005-03-08 19:53:11 +01:00
/*
* Free a heap block
*
*/
void efree ( void *ptr ) {
struct heap_block *block;
2005-03-08 19:53:11 +01:00
2005-05-13 12:18:21 +02:00
ASSERT ( ptr == phys_to_virt ( heap_ptr + sizeof ( size_t ) ) );
2005-03-08 19:53:11 +01:00
block = ( struct heap_block * )
( ptr - offsetof ( struct heap_block, data ) );
heap_ptr += block->size;
2005-03-08 19:53:11 +01:00
2005-05-13 13:16:14 +02:00
ASSERT ( heap_ptr <= heap_end );
}
2005-03-08 19:53:11 +01:00
/*
* Free all allocated heap blocks
*
*/
void efree_all ( void ) {
2005-05-13 13:16:14 +02:00
heap_ptr = heap_end;
2005-03-08 19:53:11 +01:00
}
INIT_FN ( INIT_HEAP, init_heap, efree_all, NULL );