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 * ),
607 void *
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 NOWHERE
Address for zero-length memory blocks.
unsigned int(* shrink)(void *ptr, size_t size)
Allow heap to shrink (optional)
static unsigned int discard_cache(size_t size __unused)
Discard some cached data.
#define list_add(new, head)
Add a new entry to the head of a list.
struct list_head * next
Next list entry.
#define INIT_EARLY
Early initialisation.
#define STARTUP_EARLY
Early startup.
#define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
uint16_t size
Buffer size.
char data[0]
Remaining data.
#define CACHE_DISCARDERS
Cache discarder table.
static void * heap_alloc_block(struct heap *heap, size_t size, size_t align, size_t offset)
Allocate a memory block.
static char heap_area[HEAP_SIZE]
The heap area.
#define HEAP_ALIGN
Heap area alignment.
#define RUNNING_ON_VALGRIND
#define offsetof(type, field)
Get offset of a field within a structure.
A doubly-linked list entry (or list head)
Dynamic memory allocation.
uint32_t start
Starting offset.
struct list_head list
List of free blocks.
A startup/shutdown function.
#define list_del(list)
Delete an entry from a list.
unsigned int(* grow)(size_t size)
Attempt to grow heap (optional)
void * memcpy(void *dest, const void *src, size_t len) __nonnull
An initialisation function.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
static void valgrind_make_blocks_noaccess(struct heap *heap)
Mark all blocks in free list as inaccessible.
unsigned int(* discard)(void)
Discard some cached data.
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
#define __unused
Declare a variable or data structure as unused.
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
void heap_populate(struct heap *heap, void *start, size_t len)
Add memory to allocation pool.
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
void * malloc_phys_offset(size_t size, size_t phys_align, size_t offset)
Allocate memory with specified physical alignment and offset.
static unsigned int count
Number of entries.
static void valgrind_make_blocks_defined(struct heap *heap)
Mark all blocks in free list as defined.
size_t ptr_align
Alignment for size-tracked allocations.
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
size_t size
Size of this block.
struct init_fn heap_init_fn __init_fn(INIT_EARLY)
Memory allocator initialisation function.
void * zalloc(size_t size)
Allocate cleared memory.
#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
static void discard_all_cache(void)
Discard all cached data.
#define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr, _qzz_len)
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
#define MIN_MEMBLOCK_ALIGN
Physical address alignment maintained for free blocks of memory.
size_t maxusedmem
Maximum amount of used memory.
size_t freemem
Total amount of free memory.
void heap_dump(struct heap *heap)
Dump free block list (for debugging)
struct list_head blocks
List of free memory blocks.
size_t usedmem
Total amount of used memory.
void * malloc(size_t size)
Allocate memory.
#define HEAP_SIZE
Heap area size.
#define list_check(list)
Check a list entry or list head is valid.
void free(void *ptr)
Free memory.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static void check_blocks(struct heap *heap)
Check integrity of the blocks in the free list.
struct list_head * prev
Previous list entry.
uint8_t block[3][8]
DES-encrypted blocks.
char pad[offsetof(struct refcnt, count)+sizeof(((struct refcnt *) NULL) ->count)]
Padding.
size_t size
Size of this block.
void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
A block of allocated memory complete with size information.
static void init_heap(void)
Initialise the heap.
static void heap_free_block(struct heap *heap, void *ptr, size_t size)
Free a memory block.
uint8_t data[48]
Additional event data.
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
struct startup_fn heap_startup_fn __startup_fn(STARTUP_EARLY)
Memory allocator shutdown function.
uint16_t offset
Offset to command line.
#define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
#define LIST_HEAD_INIT(list)
Initialise a static list head.
#define VALGRIND_FREELIKE_BLOCK(addr, rzB)
size_t align
Alignment for free memory blocks.
#define NULL
NULL pointer (VOID *)
void * heap_realloc(struct heap *heap, void *old_ptr, size_t new_size)
Reallocate memory.
struct bofm_section_header done
void dbg_printf(const char *fmt,...)
Print debug message.
static void shutdown_cache(int booting __unused)
Discard all cached data on shutdown.
void * malloc_phys(size_t size, size_t phys_align)
Allocate memory with specified physical alignment.
void * memset(void *dest, int character, size_t len) __nonnull