63 #define MEMBLOCK_ALIGN \ 64 ( ( size_t ) ( 1 << ( fls ( sizeof ( struct memory_block ) - 1 ) ) ) ) 89 #define NOWHERE ( ( void * ) ~( ( intptr_t ) 0 ) ) 108 #define HEAP_SIZE ( 512 * 1024 ) 134 sizeof ( free_blocks.prev->next ) );
136 sizeof ( *free_blocks.next ) );
138 sizeof ( free_blocks.next->next->prev ) );
148 sizeof ( *
block->list.next ) );
150 sizeof (
block->list.next->next->prev ) );
186 sizeof ( free_blocks.next->prev ) );
196 sizeof ( free_blocks.next->prev ) );
228 ( (
void * )
block ) );
234 assert ( ( (
void * )
block ) > ( (
void * ) prev ) );
236 ( ( (
void * ) prev ) + prev->size ) );
249 unsigned int discarded;
252 discarded = discarder->
discard();
264 unsigned int discarded;
268 }
while ( discarded );
286 size_t actual_offset;
293 unsigned int discarded;
309 if ( ! actual_size ) {
323 DBGC2 ( &
heap,
"HEAP allocating %#zx (aligned %#zx+%zx)\n",
330 if ( (
block->size < pre_size ) ||
331 ( (
block->size - pre_size ) < actual_size ) )
333 post_size = (
block->size - pre_size - actual_size );
340 block = ( ( (
void * ) pre ) + pre_size );
341 post = ( ( (
void * )
block ) + actual_size );
342 DBGC2 ( &
heap,
"HEAP splitting [%p,%p) -> [%p,%p) " 344 ( ( (
void * ) pre ) + pre->size ), pre,
block,
345 post, ( ( (
void * ) pre ) + pre->size ) );
353 post->
size = post_size;
360 pre->size = pre_size;
377 ptr = ( ( (
void * )
block ) +
offset - actual_offset );
378 DBGC2 ( &
heap,
"HEAP allocated [%p,%p) within " 380 ( ( (
void * )
block ) + actual_size ) );
386 DBGC ( &
heap,
"HEAP attempting discard for %#zx (aligned " 395 DBGC ( &
heap,
"HEAP failed to allocate %#zx (aligned " 439 freeing = ( ptr - sub_offset );
442 DBGC2 ( &
heap,
"HEAP freeing [%p,%p) within [%p,%p)\n",
443 ptr, ( ptr +
size ), freeing,
444 ( ( (
void * ) freeing ) + actual_size ) );
450 if ( ( ( (
void * )
block ) <
451 ( (
void * ) freeing + actual_size ) ) &&
452 ( (
void * ) freeing <
455 DBGC ( &
heap,
"HEAP double free of [%p,%p) " 456 "overlapping [%p,%p) detected from %p\n",
458 ( ( (
void * ) freeing ) +
size ),
block,
460 __builtin_return_address ( 0 ) );
466 freeing->size = actual_size;
469 gap_before = ( ( (
void * ) freeing ) -
471 gap_after = ( ( (
void * )
block ) -
472 ( ( (
void * ) freeing ) + freeing->size ) );
474 if ( gap_before == 0 ) {
475 DBGC2 ( &
heap,
"HEAP merging [%p,%p) + [%p,%p) -> " 477 ( ( (
void * )
block ) +
block->size ), freeing,
478 ( ( (
void * ) freeing ) + freeing->size ),
480 ( ( (
void * ) freeing ) + freeing->size ) );
481 block->size += actual_size;
484 sizeof ( *freeing ) );
488 if ( gap_after >= 0 )
497 freeing, ( ( (
void * ) freeing ) + freeing->size ) );
499 if ( gap_after == 0 ) {
500 DBGC2 ( &
heap,
"HEAP merging [%p,%p) + [%p,%p) -> [%p,%p)\n",
501 freeing, ( ( (
void * ) freeing ) + freeing->size ),
504 freeing->size +=
block->size;
537 void *
realloc (
void *old_ptr,
size_t new_size ) {
540 size_t old_total_size;
541 size_t new_total_size;
549 new_total_size = ( new_size +
551 if ( new_total_size < new_size )
556 new_block->
size = new_total_size;
558 sizeof ( new_block->
size ) );
559 new_ptr = &new_block->
data;
568 if ( old_ptr && ( old_ptr !=
NOWHERE ) ) {
572 sizeof ( old_block->
size ) );
573 old_total_size = old_block->
size;
574 assert ( old_total_size != 0 );
575 old_size = ( old_total_size -
577 memcpy ( new_ptr, old_ptr,
578 ( ( old_size < new_size ) ? old_size : new_size ) );
584 DBGC ( &
heap,
"HEAP detected possible memory corruption " 585 "from %p\n", __builtin_return_address ( 0 ) );
604 DBGC ( &
heap,
"HEAP detected possible memory corruption " 605 "from %p\n", __builtin_return_address ( 0 ) );
624 DBGC ( &
heap,
"HEAP detected possible memory corruption " 625 "from %p\n", __builtin_return_address ( 0 ) );
647 DBGC ( &
heap,
"HEAP detected possible memory corruption " 648 "from %p\n", __builtin_return_address ( 0 ) );
static __always_inline void struct dma_mapping size_t size_t align
void free_memblock(void *ptr, size_t size)
Free a memory block.
#define list_add(new, head)
Add a new entry to the head of a list.
#define INIT_EARLY
Early initialisation.
#define STARTUP_EARLY
Early startup.
#define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
uint8_t size
Entry size (in 32-bit words)
char data[0]
Remaining data.
#define CACHE_DISCARDERS
Cache discarder table.
#define RUNNING_ON_VALGRIND
#define offsetof(type, field)
Get offset of a field within a structure.
static void mpopulate(void *start, size_t len)
Add memory to allocation pool.
static void valgrind_make_blocks_noaccess(void)
Mark all blocks in free list as inaccessible.
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
A doubly-linked list entry (or list head)
Dynamic memory allocation.
static char heap[HEAP_SIZE]
The heap itself.
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.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
An initialisation function.
size_t freemem
Total amount of free memory.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
static void check_blocks(void)
Check integrity of the blocks in the free list.
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.
uint16_t count
Number of entries.
#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 MEMBLOCK_ALIGN
Physical address alignment maintained for free blocks of memory.
#define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr, _qzz_len)
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
static LIST_HEAD(free_blocks)
List of free memory blocks.
void * alloc_memblock(size_t size, size_t align, size_t offset)
Allocate a memory block.
#define NOWHERE
Address for zero-length memory blocks.
void * malloc(size_t size)
Allocate memory.
#define HEAP_SIZE
Heap size.
#define list_check(list)
Check a list entry or list head is valid.
static unsigned int discard_cache(void)
Discard some cached data.
void free(void *ptr)
Free memory.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
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 mdumpfree(void)
Dump free block list (for debugging)
A block of allocated memory complete with size information.
static void init_heap(void)
Initialise the heap.
uint8_t data[48]
Additional event data.
static void valgrind_make_blocks_defined(void)
Mark all blocks in free list as defined.
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
uint16_t offset
Offset to command line.
struct startup_fn heap_startup_fn __startup_fn(STARTUP_EARLY)
Memory allocator shutdown function.
#define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
#define VALGRIND_FREELIKE_BLOCK(addr, rzB)
#define NULL
NULL pointer (VOID *)
size_t maxusedmem
Maximum amount of used 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.
size_t usedmem
Total amount of used memory.
void * memset(void *dest, int character, size_t len) __nonnull