mirror of
https://github.com/xcat2/xNBA.git
synced 2025-04-15 09:39:26 +00:00
Tear out old heap code, replace with code that simply allocates memory
for use by malloc(). This breaks the image-loading code (which previously used the heap to allocate the buffer for downloading the image), but that's not a major concern since I'm going to tear out all the image formats within the next couple of days anyway. Byebye, NBI! :)
This commit is contained in:
parent
689218618f
commit
0afa9db2de
217
src/core/heap.c
217
src/core/heap.c
@ -1,208 +1,27 @@
|
||||
#include "etherboot.h"
|
||||
#include <gpxe/init.h>
|
||||
#include "memsizes.h"
|
||||
#include <assert.h>
|
||||
#include "heap.h"
|
||||
#include <malloc.h>
|
||||
#include <gpxe/heap.h>
|
||||
|
||||
struct heap_block {
|
||||
size_t size;
|
||||
unsigned int align;
|
||||
char data[0];
|
||||
};
|
||||
|
||||
/* Linker symbols */
|
||||
extern char _text[];
|
||||
extern char _end[];
|
||||
|
||||
static physaddr_t heap_start;
|
||||
|
||||
/*
|
||||
* Find the largest contiguous area of memory that I can use for the
|
||||
* heap.
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Heap
|
||||
*
|
||||
*/
|
||||
static void init_heap ( void ) {
|
||||
unsigned int i;
|
||||
physaddr_t eb_start, eb_end;
|
||||
physaddr_t size;
|
||||
|
||||
size = 0;
|
||||
|
||||
/* Region occupied by Etherboot */
|
||||
eb_start = virt_to_phys ( _text );
|
||||
eb_end = virt_to_phys ( _end );
|
||||
/**
|
||||
* Heap size
|
||||
*
|
||||
* Currently fixed at 48kB.
|
||||
*/
|
||||
#define HEAP_SIZE ( 48 * 1024 )
|
||||
|
||||
for ( i = 0 ; i < meminfo.map_count ; i++ ) {
|
||||
physaddr_t r_start, r_end, r_size;
|
||||
physaddr_t pre_eb, post_eb;
|
||||
/** The heap itself */
|
||||
char heap[HEAP_SIZE] __attribute__ (( aligned ( __alignof__(void *) )));
|
||||
|
||||
/* Get start and end addresses of the region */
|
||||
if ( meminfo.map[i].type != E820_RAM )
|
||||
continue;
|
||||
if ( meminfo.map[i].addr > ULONG_MAX )
|
||||
continue;
|
||||
r_start = meminfo.map[i].addr;
|
||||
if ( r_start + meminfo.map[i].size > ULONG_MAX ) {
|
||||
r_end = ULONG_MAX;
|
||||
} else {
|
||||
r_end = r_start + meminfo.map[i].size;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
||||
assert ( size != 0 );
|
||||
DBG ( "HEAP using region [%x,%x)\n", heap_start, heap_end );
|
||||
heap_ptr = heap_end;
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocate a block from the heap.
|
||||
/**
|
||||
* Initialise the heap
|
||||
*
|
||||
*/
|
||||
static inline physaddr_t block_alloc_addr ( physaddr_t heap_ptr,
|
||||
size_t size, unsigned int align ) {
|
||||
return ( ( ( heap_ptr - size ) & ~( align - 1 ) )
|
||||
- sizeof ( struct heap_block ) );
|
||||
void init_heap ( void ) {
|
||||
mpopulate ( heap, sizeof ( heap ) );
|
||||
}
|
||||
|
||||
void * emalloc ( size_t size, unsigned int align ) {
|
||||
struct heap_block *block;
|
||||
physaddr_t addr;
|
||||
|
||||
assert ( ( align & ( align - 1 ) ) == 0 );
|
||||
|
||||
addr = block_alloc_addr ( heap_ptr, size, align );
|
||||
if ( addr < heap_start ) {
|
||||
DBG ( "HEAP no space for %x bytes (alignment %d) in [%x,%x)\n",
|
||||
size, align, heap_start, heap_ptr );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
block = phys_to_virt ( addr );
|
||||
block->size = ( heap_ptr - addr );
|
||||
block->align = align;
|
||||
DBG ( "HEAP allocated %x bytes (alignment %d) at %x [%x,%x)\n",
|
||||
size, align, virt_to_phys ( block->data ), addr, heap_ptr );
|
||||
heap_ptr = addr;
|
||||
return block->data;
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocate all remaining space on the heap
|
||||
*
|
||||
*/
|
||||
void * emalloc_all ( size_t *size ) {
|
||||
*size = heap_ptr - heap_start - sizeof ( struct heap_block );
|
||||
return emalloc ( *size, sizeof ( void * ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* Free a heap block
|
||||
*
|
||||
*/
|
||||
static inline physaddr_t block_free_addr ( size_t size ) {
|
||||
return heap_ptr + size;
|
||||
}
|
||||
|
||||
void efree ( void *ptr ) {
|
||||
struct heap_block *block;
|
||||
|
||||
assert ( ptr == phys_to_virt ( heap_ptr + sizeof ( size_t ) ) );
|
||||
|
||||
block = ( struct heap_block * )
|
||||
( ptr - offsetof ( struct heap_block, data ) );
|
||||
heap_ptr = block_free_addr ( block->size );
|
||||
|
||||
DBG ( "HEAP freed %x [%x,%x)\n", virt_to_phys ( ptr ),
|
||||
virt_to_phys ( block ), heap_ptr );
|
||||
|
||||
assert ( heap_ptr <= heap_end );
|
||||
}
|
||||
|
||||
/*
|
||||
* Free all allocated heap blocks
|
||||
*
|
||||
*/
|
||||
void efree_all ( void ) {
|
||||
DBG ( "HEAP discarding allocated blocks in [%x,%x)\n",
|
||||
heap_ptr, heap_end );
|
||||
|
||||
heap_ptr = heap_end;
|
||||
}
|
||||
|
||||
/*
|
||||
* Resize a heap block
|
||||
*
|
||||
*/
|
||||
void * erealloc ( void *ptr, size_t size ) {
|
||||
struct heap_block *old_block;
|
||||
size_t old_size;
|
||||
unsigned int old_align;
|
||||
physaddr_t new_addr;
|
||||
size_t move_size;
|
||||
|
||||
/* Get descriptor of the old block */
|
||||
old_block = ( struct heap_block * )
|
||||
( ptr - offsetof ( struct heap_block, data ) );
|
||||
old_size = old_block->size;
|
||||
old_align = old_block->align;
|
||||
|
||||
/* Check that allocation is going to succeed */
|
||||
new_addr = block_alloc_addr ( block_free_addr ( old_size ),
|
||||
size, old_align );
|
||||
if ( new_addr < heap_start ) {
|
||||
DBG ( "HEAP no space for %x bytes (alignment %d) in [%x,%x)\n",
|
||||
size, align, heap_start, block_free_addr ( old_size ) );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Free the old block */
|
||||
efree ( ptr );
|
||||
|
||||
/* Move the data. Do this *before* allocating the new block,
|
||||
* because the new block's descriptor may overwrite the old
|
||||
* block's data, if the new block is smaller than the old
|
||||
* block.
|
||||
*/
|
||||
move_size = size + sizeof ( struct heap_block );
|
||||
if ( old_size < move_size )
|
||||
move_size = old_size;
|
||||
memmove ( phys_to_virt ( new_addr ), old_block, move_size );
|
||||
|
||||
/* Allocate the new block. This must succeed, because we
|
||||
* already checked that there was sufficient space.
|
||||
*/
|
||||
ptr = emalloc ( size, old_align );
|
||||
assert ( ptr != NULL );
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
INIT_FN ( INIT_HEAP, init_heap, efree_all, NULL );
|
||||
|
@ -18,6 +18,8 @@ void print_images ( void ) {
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
/*
|
||||
* Identify the image format
|
||||
*
|
||||
@ -80,3 +82,5 @@ int autoload ( struct dev *dev, struct image **image, void **context ) {
|
||||
out:
|
||||
return rc;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -30,7 +30,6 @@ Modifications: Ken Yap (for Etherboot/16)
|
||||
*/
|
||||
|
||||
#include "io.h"
|
||||
#include "heap.h"
|
||||
#include "memsizes.h"
|
||||
|
||||
/* Linker symbols */
|
||||
@ -57,18 +56,6 @@ int prep_segment ( physaddr_t start, physaddr_t mid, physaddr_t end ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check for overlap with used portion of heap. Since the
|
||||
* heap code just finds the biggest available memory area, we
|
||||
* check only against the used heap area, rather than the
|
||||
* whole heap area.
|
||||
*/
|
||||
if ( ( end > heap_ptr ) && ( start < heap_end ) ) {
|
||||
DBG ( "OSLOADER got segment [%lX, %lX) "
|
||||
"overlapping used heap [%lX, %lX)\n",
|
||||
start, end, heap_ptr, heap_end );
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check that block fits entirely inside a single memory region */
|
||||
fit = 0;
|
||||
for ( i = 0 ; i < meminfo.map_count ; i++ ) {
|
||||
|
15
src/include/gpxe/heap.h
Normal file
15
src/include/gpxe/heap.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef _GPXE_HEAP_H
|
||||
#define _GPXE_HEAP_H
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Heap
|
||||
*
|
||||
*/
|
||||
|
||||
extern char heap[];
|
||||
|
||||
extern void init_heap ( void );
|
||||
|
||||
#endif /* _GPXE_HEAP_H */
|
@ -1,92 +0,0 @@
|
||||
#ifndef HEAP_H
|
||||
#define HEAP_H
|
||||
|
||||
/*
|
||||
* Allocate a block with specified (physical) alignment
|
||||
*
|
||||
* "align" must be a power of 2.
|
||||
*
|
||||
* Note that "align" affects the alignment of the physical address,
|
||||
* not the virtual address. This is almost certainly what you want.
|
||||
*
|
||||
*/
|
||||
extern void * emalloc ( size_t size, unsigned int align );
|
||||
|
||||
/*
|
||||
* Allocate all remaining space on the heap
|
||||
*
|
||||
*/
|
||||
extern void * emalloc_all ( size_t *size );
|
||||
|
||||
/*
|
||||
* Free a block.
|
||||
*
|
||||
* The caller must ensure that the block being freed is the last (most
|
||||
* recent) block allocated on the heap, otherwise heap corruption will
|
||||
* occur.
|
||||
*
|
||||
*/
|
||||
extern void efree ( void *ptr );
|
||||
|
||||
/*
|
||||
* Free all allocated blocks on the heap
|
||||
*
|
||||
*/
|
||||
extern void efree_all ( void );
|
||||
|
||||
/*
|
||||
* Resize a block.
|
||||
*
|
||||
* The caller must ensure that the block being resized is the last
|
||||
* (most recent) block allocated on the heap, otherwise heap
|
||||
* corruption will occur.
|
||||
*
|
||||
*/
|
||||
extern void * erealloc ( void *ptr, size_t size );
|
||||
|
||||
/*
|
||||
* Allocate, free, and resize blocks without caring about alignment
|
||||
*
|
||||
*/
|
||||
static inline void * malloc ( size_t size ) {
|
||||
return emalloc ( size, sizeof ( void * ) );
|
||||
}
|
||||
|
||||
static inline void free ( void *ptr ) {
|
||||
efree ( ptr );
|
||||
}
|
||||
|
||||
static inline void * realloc ( void *ptr, size_t size ) {
|
||||
return erealloc ( ptr, size );
|
||||
}
|
||||
|
||||
/*
|
||||
* Legacy API calls
|
||||
*
|
||||
*/
|
||||
static inline void * allot ( size_t size ) {
|
||||
return emalloc ( size, sizeof ( void * ) );
|
||||
}
|
||||
|
||||
static inline void forget ( void *ptr ) {
|
||||
efree ( ptr );
|
||||
}
|
||||
|
||||
static inline void * allot2 ( size_t size, uint32_t mask ) {
|
||||
return emalloc ( size, mask + 1 );
|
||||
}
|
||||
|
||||
static inline void forget2 ( void *ptr ) {
|
||||
efree ( ptr );
|
||||
}
|
||||
|
||||
/*
|
||||
* Heap markers. osloader.c and other code may wish to know the heap
|
||||
* location, without necessarily wanting to drag in heap.o. We
|
||||
* therefore declare these as shared (i.e. common) symbols.
|
||||
*
|
||||
*/
|
||||
physaddr_t heap_ptr __asm__ ( "_shared_heap_ptr" );
|
||||
physaddr_t heap_end __asm__ ( "_shared_heap_end" );
|
||||
|
||||
#endif /* HEAP_H */
|
Loading…
x
Reference in New Issue
Block a user