66 #define MIN_MEMBLOCK_ALIGN ( 4 * sizeof ( void * ) ) 81 #define HEAP_SIZE ( 512 * 1024 ) 84 #define HEAP_ALIGN MIN_MEMBLOCK_ALIGN 125 sizeof ( *
block->list.next ) );
127 sizeof (
block->list.next->next->prev ) );
207 ( (
void * )
block ) );
213 assert ( ( (
void * )
block ) > ( (
void * ) prev ) );
215 ( ( (
void * ) prev ) + prev->size ) );
229 unsigned int discarded;
232 discarded = discarder->
discard();
244 unsigned int discarded;
248 }
while ( discarded );
268 size_t actual_offset;
280 assert ( ( align == 0 ) || ( ( align & ( align - 1 ) ) == 0 ) );
285 offset &= ( align ? ( align - 1 ) : 0 );
294 if ( ! actual_size ) {
306 align_mask = ( ( align - 1 ) | (
heap->
align - 1 ) );
308 DBGC2 (
heap,
"HEAP allocating %#zx (aligned %#zx+%#zx)\n",
313 pre_size = ( ( actual_offset - virt_to_phys (
block ) )
315 if ( (
block->size < pre_size ) ||
316 ( (
block->size - pre_size ) < actual_size ) )
318 post_size = (
block->size - pre_size - actual_size );
325 block = ( ( (
void * ) pre ) + pre_size );
326 post = ( ( (
void * )
block ) + actual_size );
327 DBGC2 (
heap,
"HEAP splitting [%p,%p) -> [%p,%p) " 329 ( ( (
void * ) pre ) + pre->size ), pre,
block,
330 post, ( ( (
void * ) pre ) + pre->size ) );
340 post->
size = post_size;
347 pre->size = pre_size;
366 ptr = ( ( (
void * )
block ) +
offset - actual_offset );
367 DBGC2 (
heap,
"HEAP allocated [%p,%p) within " 369 ( ( (
void * )
block ) + actual_size ) );
375 DBGC (
heap,
"HEAP attempting to grow for %#zx (aligned " 376 "%#zx+%zx), used %zdkB\n",
size, align,
offset,
384 DBGC (
heap,
"HEAP failed to allocate %#zx (aligned " 385 "%#zx)\n",
size, align );
428 sub_offset = ( virt_to_phys ( ptr ) & (
heap->
align - 1 ) );
429 freeing = ( ptr - sub_offset );
432 DBGC2 (
heap,
"HEAP freeing [%p,%p) within [%p,%p)\n",
433 ptr, ( ptr +
size ), freeing,
434 ( ( (
void * ) freeing ) + actual_size ) );
440 if ( ( ( (
void * )
block ) <
441 ( (
void * ) freeing + actual_size ) ) &&
442 ( (
void * ) freeing <
445 DBGC (
heap,
"HEAP double free of [%p,%p) " 446 "overlapping [%p,%p) detected from %p\n",
448 ( ( (
void * ) freeing ) +
size ),
block,
450 __builtin_return_address ( 0 ) );
456 freeing->size = actual_size;
459 gap_before = ( ( (
void * ) freeing ) -
461 gap_after = ( ( (
void * )
block ) -
462 ( ( (
void * ) freeing ) + freeing->size ) );
464 if ( gap_before == 0 ) {
465 DBGC2 (
heap,
"HEAP merging [%p,%p) + [%p,%p) -> " 467 ( ( (
void * )
block ) +
block->size ), freeing,
468 ( ( (
void * ) freeing ) + freeing->size ),
470 ( ( (
void * ) freeing ) + freeing->size ) );
471 block->size += actual_size;
474 sizeof ( *freeing ) );
478 if ( gap_after >= 0 )
487 freeing, ( ( (
void * ) freeing ) + freeing->size ) );
489 if ( gap_after == 0 ) {
490 DBGC2 (
heap,
"HEAP merging [%p,%p) + [%p,%p) -> [%p,%p)\n",
491 freeing, ( ( (
void * ) freeing ) + freeing->size ),
494 freeing->size +=
block->size;
539 size_t old_total_size;
540 size_t new_total_size;
549 new_total_size = ( new_size +
offset );
550 if ( new_total_size < new_size )
556 new_block->
size = new_total_size;
558 sizeof ( new_block->
size ) );
559 new_ptr = &new_block->
data;
570 if ( old_ptr && ( old_ptr !=
NOWHERE ) ) {
574 sizeof ( old_block->
size ) );
575 old_total_size = old_block->
size;
576 assert ( old_total_size != 0 );
577 old_size = ( old_total_size -
offset );
578 memcpy ( new_ptr, old_ptr,
579 ( ( old_size < new_size ) ? old_size : new_size ) );
585 DBGC (
heap,
"HEAP detected possible memory corruption " 586 "from %p\n", __builtin_return_address ( 0 ) );
595 .ptr_align =
sizeof (
void * ),
606 void *
realloc (
void *old_ptr,
size_t new_size ) {
625 DBGC ( &
heap,
"HEAP detected possible memory corruption " 626 "from %p\n", __builtin_return_address ( 0 ) );
645 DBGC ( &
heap,
"HEAP detected possible memory corruption " 646 "from %p\n", __builtin_return_address ( 0 ) );
668 DBGC ( &
heap,
"HEAP detected possible memory corruption " 669 "from %p\n", __builtin_return_address ( 0 ) );
689 assert ( ( phys_align == 0 ) ||
690 ( ( ( virt_to_phys ( ptr ) ^
offset ) &
691 ( phys_align - 1 ) ) == 0 ) );
778 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