57 sizeof ( ( (
struct refcnt * )
NULL )->count ) ];
67#define MIN_MEMBLOCK_ALIGN ( 4 * sizeof ( void * ) )
82#define HEAP_SIZE ( 4096 * 1024 )
85#define HEAP_ALIGN MIN_MEMBLOCK_ALIGN
126 sizeof ( *
block->list.next ) );
128 sizeof (
block->list.next->next->prev ) );
208 ( (
void * )
block ) );
214 assert ( ( (
void * )
block ) > ( (
void * ) prev ) );
216 ( ( (
void * ) prev ) + prev->size ) );
230 unsigned int discarded;
233 discarded = discarder->
discard();
245 unsigned int discarded;
249 }
while ( discarded );
269 size_t actual_offset;
281 assert ( ( align == 0 ) || ( ( align & ( align - 1 ) ) == 0 ) );
286 offset &= ( align ? ( align - 1 ) : 0 );
295 if ( ! actual_size ) {
307 align_mask = ( ( align - 1 ) | (
heap->
align - 1 ) );
309 DBGC2 (
heap,
"HEAP allocating %#zx (aligned %#zx+%#zx)\n",
314 pre_size = ( ( actual_offset - virt_to_phys (
block ) )
316 if ( (
block->size < pre_size ) ||
317 ( (
block->size - pre_size ) < actual_size ) )
319 post_size = (
block->size - pre_size - actual_size );
326 block = ( ( (
void * ) pre ) + pre_size );
327 post = ( ( (
void * )
block ) + actual_size );
328 DBGC2 (
heap,
"HEAP splitting [%p,%p) -> [%p,%p) "
330 ( ( (
void * ) pre ) + pre->size ), pre,
block,
331 post, ( ( (
void * ) pre ) + pre->size ) );
341 post->
size = post_size;
348 pre->size = pre_size;
367 ptr = ( ( (
void * )
block ) +
offset - actual_offset );
368 DBGC2 (
heap,
"HEAP allocated [%p,%p) within "
370 ( ( (
void * )
block ) + actual_size ) );
376 DBGC (
heap,
"HEAP attempting to grow for %#zx (aligned "
377 "%#zx+%zx), used %zdkB\n",
size, align,
offset,
385 DBGC (
heap,
"HEAP failed to allocate %#zx (aligned "
386 "%#zx)\n",
size, align );
429 sub_offset = ( virt_to_phys ( ptr ) & (
heap->
align - 1 ) );
430 freeing = ( ptr - sub_offset );
433 DBGC2 (
heap,
"HEAP freeing [%p,%p) within [%p,%p)\n",
434 ptr, ( ptr +
size ), freeing,
435 ( ( (
void * ) freeing ) + actual_size ) );
441 if ( ( ( (
void * )
block ) <
442 ( (
void * ) freeing + actual_size ) ) &&
443 ( (
void * ) freeing <
446 DBGC (
heap,
"HEAP double free of [%p,%p) "
447 "overlapping [%p,%p) detected from %p\n",
449 ( ( (
void * ) freeing ) +
size ),
block,
451 __builtin_return_address ( 0 ) );
457 freeing->
size = actual_size;
460 gap_before = ( ( (
void * ) freeing ) -
462 gap_after = ( ( (
void * )
block ) -
463 ( ( (
void * ) freeing ) + freeing->size ) );
465 if ( gap_before == 0 ) {
466 DBGC2 (
heap,
"HEAP merging [%p,%p) + [%p,%p) -> "
468 ( ( (
void * )
block ) +
block->size ), freeing,
469 ( ( (
void * ) freeing ) + freeing->size ),
471 ( ( (
void * ) freeing ) + freeing->size ) );
472 block->size += actual_size;
475 sizeof ( *freeing ) );
479 if ( gap_after >= 0 )
488 freeing, ( ( (
void * ) freeing ) + freeing->
size ) );
490 if ( gap_after == 0 ) {
491 DBGC2 (
heap,
"HEAP merging [%p,%p) + [%p,%p) -> [%p,%p)\n",
492 freeing, ( ( (
void * ) freeing ) + freeing->size ),
495 freeing->size +=
block->size;
540 size_t old_total_size;
541 size_t new_total_size;
550 new_total_size = ( new_size +
offset );
551 if ( new_total_size < new_size )
557 new_block->
size = new_total_size;
559 sizeof ( new_block->
size ) );
560 new_ptr = &new_block->
data;
571 if ( old_ptr && ( old_ptr !=
NOWHERE ) ) {
575 sizeof ( old_block->
size ) );
576 old_total_size = old_block->
size;
577 assert ( old_total_size != 0 );
578 old_size = ( old_total_size -
offset );
579 memcpy ( new_ptr, old_ptr,
580 ( ( old_size < new_size ) ? old_size : new_size ) );
586 DBGC (
heap,
"HEAP detected possible memory corruption "
587 "from %p\n", __builtin_return_address ( 0 ) );
596 .ptr_align =
sizeof (
void * ),
607void *
realloc (
void *old_ptr,
size_t new_size ) {
626 DBGC ( &
heap,
"HEAP detected possible memory corruption "
627 "from %p\n", __builtin_return_address ( 0 ) );
646 DBGC ( &
heap,
"HEAP detected possible memory corruption "
647 "from %p\n", __builtin_return_address ( 0 ) );
669 DBGC ( &
heap,
"HEAP detected possible memory corruption "
670 "from %p\n", __builtin_return_address ( 0 ) );
690 assert ( ( phys_align == 0 ) ||
691 ( ( ( virt_to_phys ( ptr ) ^
offset ) &
692 ( phys_align - 1 ) ) == 0 ) );
779 DBGC ( &
heap,
"HEAP maximum usage %zdkB\n",
#define NULL
NULL pointer (VOID *)
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
#define assert(condition)
Assert a condition at run-time.
struct bofm_section_header done
uint16_t offset
Offset to command line.
uint8_t data[48]
Additional event data.
#define __unused
Declare a variable or data structure as unused.
void dbg_printf(const char *fmt,...)
Print debug message.
#define INIT_EARLY
Early initialisation.
uint32_t start
Starting offset.
uint16_t size
Buffer size.
static unsigned int count
Number of entries.
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
#define STARTUP_EARLY
Early startup.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
#define __init_fn(init_order)
Declare an initialisation functon.
#define __startup_fn(startup_order)
Declare a startup/shutdown function.
#define LIST_HEAD_INIT(list)
Initialise a static list head.
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
#define list_del(list)
Delete an entry from a list.
#define list_check(list)
Check a list entry or list head is valid.
#define list_add(new, head)
Add a new entry to the head of a list.
void * heap_realloc(struct heap *heap, void *old_ptr, size_t new_size)
Reallocate memory.
static char heap_area[HEAP_SIZE]
The heap area.
#define HEAP_SIZE
Heap area size.
static unsigned int discard_cache(size_t size __unused)
Discard some cached data.
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
void * zalloc(size_t size)
Allocate cleared memory.
void * malloc_phys(size_t size, size_t phys_align)
Allocate memory with specified physical alignment.
static void init_heap(void)
Initialise the heap.
void heap_dump(struct heap *heap)
Dump free block list (for debugging)
static void shutdown_cache(int booting __unused)
Discard all cached data on shutdown.
static void valgrind_make_blocks_defined(struct heap *heap)
Mark all blocks in free list as defined.
void * malloc(size_t size)
Allocate memory.
static void discard_all_cache(void)
Discard all cached data.
void * malloc_phys_offset(size_t size, size_t phys_align, size_t offset)
Allocate memory with specified physical alignment and offset.
#define MIN_MEMBLOCK_ALIGN
Physical address alignment maintained for free blocks of memory.
#define HEAP_ALIGN
Heap area alignment.
void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
static void check_blocks(struct heap *heap)
Check integrity of the blocks in the free list.
static void valgrind_make_blocks_noaccess(struct heap *heap)
Mark all blocks in free list as inaccessible.
void heap_populate(struct heap *heap, void *start, size_t len)
Add memory to allocation pool.
static void * heap_alloc_block(struct heap *heap, size_t size, size_t align, size_t offset)
Allocate a memory block.
static void heap_free_block(struct heap *heap, void *ptr, size_t size)
Free a memory block.
Dynamic memory allocation.
#define CACHE_DISCARDERS
Cache discarder table.
#define NOWHERE
Address for zero-length memory blocks.
#define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr, _qzz_len)
#define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
#define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
uint8_t block[3][8]
DES-encrypted blocks.
static void(* free)(struct refcnt *refcnt))
#define offsetof(type, field)
Get offset of a field within a structure.
#define container_of(ptr, type, field)
Get containing structure.
A block of allocated memory complete with size information.
size_t size
Size of this block.
char data[0]
Remaining data.
unsigned int(* discard)(void)
Discard some cached data.
size_t usedmem
Total amount of used memory.
size_t freemem
Total amount of free memory.
size_t ptr_align
Alignment for size-tracked allocations.
unsigned int(* grow)(size_t size)
Attempt to grow heap (optional)
struct list_head blocks
List of free memory blocks.
size_t maxusedmem
Maximum amount of used memory.
size_t align
Alignment for free memory blocks.
unsigned int(* shrink)(void *ptr, size_t size)
Allow heap to shrink (optional)
An initialisation function.
A doubly-linked list entry (or list head)
struct list_head * next
Next list entry.
struct list_head * prev
Previous list entry.
size_t size
Size of this block.
struct list_head list
List of free blocks.
char pad[offsetof(struct refcnt, count)+sizeof(((struct refcnt *) NULL) ->count)]
Padding.
A startup/shutdown function.
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
#define RUNNING_ON_VALGRIND
#define VALGRIND_FREELIKE_BLOCK(addr, rzB)