iPXE
malloc.h File Reference

Dynamic memory allocation. More...

#include <stdint.h>
#include <stdlib.h>
#include <ipxe/list.h>
#include <ipxe/tables.h>
#include <valgrind/memcheck.h>

Go to the source code of this file.

Data Structures

struct  heap
 A heap. More...
struct  cache_discarder
 A cache discarder. More...

Macros

#define NOWHERE   ( ( void * ) ~( ( intptr_t ) 0 ) )
 Address for zero-length memory blocks.
#define CACHE_DISCARDERS   __table ( struct cache_discarder, "cache_discarders" )
 Cache discarder table.
#define __cache_discarder(cost)
 Declare a cache discarder.
#define CACHE_CHEAP   01
 Items with a low replacement cost.
#define CACHE_NORMAL   02
 Items with a normal replacement cost.
#define CACHE_EXPENSIVE   03
 Items with a high replacement cost.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
void * heap_realloc (struct heap *heap, void *old_ptr, size_t new_size)
 Reallocate memory.
void heap_dump (struct heap *heap)
 Dump free block list (for debugging).
void heap_populate (struct heap *heap, void *start, size_t len)
 Add memory to allocation pool.
void *__malloc malloc_phys_offset (size_t size, size_t phys_align, size_t offset)
 Allocate memory with specified physical alignment and offset.
void *__malloc malloc_phys (size_t size, size_t phys_align)
 Allocate memory with specified physical alignment.
void free_phys (void *ptr, size_t size)
 Free memory allocated with malloc_phys().

Detailed Description

Dynamic memory allocation.

Definition in file malloc.h.

Macro Definition Documentation

◆ NOWHERE

#define NOWHERE   ( ( void * ) ~( ( intptr_t ) 0 ) )

Address for zero-length memory blocks.

malloc(0) or realloc(ptr,0) will return the special value NOWHERE. Calling free(NOWHERE) will have no effect.

This is consistent with the ANSI C standards, which state that "either NULL or a pointer suitable to be passed to free()" must be returned in these cases. Using a special non-NULL value means that the caller can take a NULL return value to indicate failure, without first having to check for a requested size of zero.

Code outside of the memory allocators themselves does not ever need to refer to the actual value of NOWHERE; this is an internal definition.

Definition at line 42 of file malloc.h.

Referenced by efi_urealloc(), heap_realloc(), and zfree().

◆ CACHE_DISCARDERS

#define CACHE_DISCARDERS   __table ( struct cache_discarder, "cache_discarders" )

Cache discarder table.

Definition at line 103 of file malloc.h.

Referenced by discard_cache().

◆ __cache_discarder

#define __cache_discarder ( cost)
Value:
#define CACHE_DISCARDERS
Cache discarder table.
Definition malloc.h:103
uint32_t cost
Root path cost.
Definition stp.h:17
#define __table_entry(table, idx)
Declare a linker table entry.
Definition tables.h:239

Declare a cache discarder.

Definition at line 106 of file malloc.h.

Referenced by __cache_discarder(), and __cache_discarder().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ heap_realloc()

void * heap_realloc ( struct heap * heap,
void * old_ptr,
size_t new_size )
extern

Reallocate memory.

Parameters
heapHeap
old_ptrMemory previously allocated by heap_realloc(), or NULL
new_sizeRequested size
Return values
new_ptrAllocated memory, or NULL

Allocates memory with no particular alignment requirement. new_ptr will be aligned to at least a multiple of sizeof(void*). If old_ptr is non-NULL, then the contents of the newly allocated memory will be the same as the contents of the previously allocated memory, up to the minimum of the old and new sizes. The old memory will be freed.

If allocation fails the previously allocated block is left untouched and NULL is returned.

Calling heap_realloc() with a new size of zero is a valid way to free a memory block.

Definition at line 593 of file malloc.c.

593 {
594 struct autosized_block *old_block;
595 struct autosized_block *new_block;
596 size_t old_total_size;
597 size_t new_total_size;
598 size_t old_size;
599 size_t offset = offsetof ( struct autosized_block, data );
600 void *new_ptr = NOWHERE;
601
602 /* Allocate new memory if necessary. If allocation fails,
603 * return without touching the old block.
604 */
605 if ( new_size ) {
606 new_total_size = ( new_size + offset );
607 if ( new_total_size < new_size )
608 return NULL;
609 new_block = heap_alloc_block ( heap, new_total_size,
610 heap->ptr_align, -offset );
611 if ( ! new_block )
612 return NULL;
613 new_block->size = new_total_size;
614 VALGRIND_MAKE_MEM_NOACCESS ( &new_block->size,
615 sizeof ( new_block->size ) );
616 new_ptr = &new_block->data;
617 VALGRIND_MALLOCLIKE_BLOCK ( new_ptr, new_size, 0, 0 );
618 assert ( ( ( ( intptr_t ) new_ptr ) &
619 ( heap->ptr_align - 1 ) ) == 0 );
620 }
621
622 /* Copy across relevant part of the old data region (if any),
623 * then free it. Note that at this point either (a) new_ptr
624 * is valid, or (b) new_size is 0; either way, the memcpy() is
625 * valid.
626 */
627 if ( old_ptr && ( old_ptr != NOWHERE ) ) {
628 old_block = container_of ( old_ptr, struct autosized_block,
629 data );
630 VALGRIND_MAKE_MEM_DEFINED ( &old_block->size,
631 sizeof ( old_block->size ) );
632 old_total_size = old_block->size;
633 assert ( old_total_size != 0 );
634 old_size = ( old_total_size - offset );
635 memcpy ( new_ptr, old_ptr,
636 ( ( old_size < new_size ) ? old_size : new_size ) );
637 VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 );
638 heap_free_block ( heap, old_block, old_total_size );
639 }
640
641 if ( ASSERTED ) {
642 DBGC ( heap, "HEAP detected possible memory corruption "
643 "from %p\n", __builtin_return_address ( 0 ) );
644 }
645 return new_ptr;
646}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
unsigned long intptr_t
Definition stdint.h:21
#define ASSERTED
Definition assert.h:26
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
uint16_t offset
Offset to command line.
Definition bzimage.h:3
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define DBGC(...)
Definition compiler.h:530
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static void * heap_alloc_block(struct heap *heap, size_t size, size_t align, size_t offset)
Allocate a memory block.
Definition malloc.c:317
static void heap_free_block(struct heap *heap, void *ptr, size_t size)
Free a memory block.
Definition malloc.c:463
#define NOWHERE
Address for zero-length memory blocks.
Definition malloc.h:42
#define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr, _qzz_len)
Definition memcheck.h:112
#define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
Definition memcheck.h:132
#define offsetof(type, field)
Get offset of a field within a structure.
Definition stddef.h:25
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
A block of allocated memory complete with size information.
Definition malloc.c:121
size_t size
Size of this block.
Definition malloc.c:123
char data[0]
Remaining data.
Definition malloc.c:125
A heap.
Definition malloc.h:45
size_t ptr_align
Alignment for size-tracked allocations.
Definition malloc.h:52
#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
Definition valgrind.h:4412
#define VALGRIND_FREELIKE_BLOCK(addr, rzB)
Definition valgrind.h:4422

References assert, ASSERTED, container_of, autosized_block::data, data, DBGC, heap_alloc_block(), heap_free_block(), memcpy(), NOWHERE, NULL, offset, offsetof, heap::ptr_align, autosized_block::size, VALGRIND_FREELIKE_BLOCK, VALGRIND_MAKE_MEM_DEFINED, VALGRIND_MAKE_MEM_NOACCESS, and VALGRIND_MALLOCLIKE_BLOCK.

Referenced by realloc(), and uheap_realloc().

◆ heap_dump()

void heap_dump ( struct heap * heap)
extern

Dump free block list (for debugging).

Definition at line 875 of file malloc.c.

875 {
876 struct memory_block *block;
877
878 dbg_printf ( "HEAP free block list:\n" );
880 dbg_printf ( "...[%p,%p] (size %#zx)\n", block,
881 ( ( ( void * ) block ) + block->size ),
882 block->size );
883 }
884}
void dbg_printf(const char *fmt,...)
Print debug message.
Definition debug.c:39
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
uint8_t block[3][8]
DES-encrypted blocks.
Definition mschapv2.h:1
struct list_head blocks
List of free memory blocks.
Definition malloc.h:47
A free block of memory.
Definition malloc.c:95
struct list_head list
List of free blocks.
Definition malloc.c:110

References block, heap::blocks, dbg_printf(), memory_block::list, and list_for_each_entry.

◆ heap_populate()

void heap_populate ( struct heap * heap,
void * start,
size_t len )
extern

Add memory to allocation pool.

Parameters
heapHeap
startStart address
lenLength of memory

Adds a block of memory to the allocation pool. The memory must be aligned to the heap's required free memory block alignment.

Definition at line 821 of file malloc.c.

821 {
822
823 /* Sanity checks */
824 assert ( ( virt_to_phys ( start ) & ( heap->align - 1 ) ) == 0 );
825 assert ( ( len & ( heap->align - 1 ) ) == 0 );
826
827 /* Add to allocation pool */
829
830 /* Fix up memory usage statistics */
831 heap->usedmem += len;
832}
ring len
Length.
Definition dwmac.h:226
uint32_t start
Starting offset.
Definition netvsc.h:1
size_t usedmem
Total amount of used memory.
Definition malloc.h:57
size_t align
Alignment for free memory blocks.
Definition malloc.h:50

References heap::align, assert, heap_free_block(), len, start, and heap::usedmem.

Referenced by init_heap(), and uheap_grow().

◆ malloc_phys_offset()

void *__malloc malloc_phys_offset ( size_t size,
size_t phys_align,
size_t offset )
extern

Allocate memory with specified physical alignment and offset.

Parameters
sizeRequested size
alignPhysical alignment
offsetOffset from physical alignment
Return values
ptrMemory, or NULL

align must be a power of two. size may not be zero.

Definition at line 767 of file malloc.c.

767 {
768 void * ptr;
769
770 assert ( phys_align != 0 );
771 ptr = heap_alloc_block ( &heap, size, phys_align, offset );
772 if ( ptr && size ) {
773 assert ( ( ( virt_to_phys ( ptr ) ^ offset ) &
774 ( phys_align - 1 ) ) == 0 );
775 VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
776 }
777 return ptr;
778}
uint16_t size
Buffer size.
Definition dwmac.h:3

References assert, heap_alloc_block(), offset, size, and VALGRIND_MALLOCLIKE_BLOCK.

Referenced by alloc_iob_raw(), and malloc_phys().

◆ malloc_phys()

void *__malloc malloc_phys ( size_t size,
size_t phys_align )
extern

Allocate memory with specified physical alignment.

Parameters
sizeRequested size
alignPhysical alignment
Return values
ptrMemory, or NULL

align must be a power of two. size may not be zero.

Definition at line 789 of file malloc.c.

789 {
790
791 return malloc_phys_offset ( size, phys_align, 0 );
792}
void * malloc_phys_offset(size_t size, size_t phys_align, size_t offset)
Allocate memory with specified physical alignment and offset.
Definition malloc.c:767

References malloc_phys_offset(), and size.

Referenced by __vxge_hw_fifo_create(), __vxge_hw_ring_create(), a3c90x_setup_rx_ring(), a3c90x_setup_tx_ring(), arbel_alloc(), arbel_alloc_icm(), arbel_create_cq(), arbel_create_eq(), arbel_create_recv_wq(), arbel_create_send_wq(), ath5k_desc_alloc(), ath_descdma_setup(), atl1e_setup_ring_resources(), b44_init_rx_ring(), b44_init_tx_ring(), efx_hunt_alloc_special_buffer(), ehci_bus_open(), ehci_ring_alloc(), ena_create_admin(), ena_create_async(), ena_create_cq(), ena_create_sq(), ena_probe(), exanic_probe(), falcon_alloc_special_buffer(), golan_cmd_init(), golan_create_cq(), golan_create_eq(), golan_create_qp_aux(), hermon_alloc(), hermon_create_cq(), hermon_create_eq(), hermon_create_qp(), hv_alloc_message(), hv_alloc_pages(), hvm_map_hypercall(), icplus_create_ring(), ifec_net_open(), ifec_tx_setup(), igbvf_setup_rx_resources(), igbvf_setup_tx_resources(), jme_alloc_rx_resources(), jme_alloc_tx_resources(), legacy_probe(), linda_create_recv_wq(), linda_init_send(), mlx_memory_alloc_dma_priv(), myri10ge_net_open(), myson_create_ring(), natsemi_create_ring(), netfront_create_ring(), nv_init_rings(), pcnet32_setup_rx_resources(), pcnet32_setup_tx_resources(), phantom_create_rx_ctx(), phantom_create_tx_ctx(), phantom_open(), qib7322_create_recv_wq(), qib7322_init_send(), rhine_create_ring(), rtl818x_init_rx_ring(), rtl818x_init_tx_ring(), sis190_open(), skge_up(), sky2_probe(), sky2_up(), uhci_bus_open(), uhci_enqueue(), uhci_ring_alloc(), velocity_alloc_rings(), vmbus_open(), and vmxnet3_open().

◆ free_phys()

void free_phys ( void * ptr,
size_t size )
extern

Free memory allocated with malloc_phys().

Parameters
ptrMemory allocated by malloc_phys(), or NULL
sizeSize of memory, as passed to malloc_phys()

Memory allocated with malloc_phys() can only be freed with free_phys(); it cannot be freed with the standard free().

If ptr is NULL, no action is taken.

Definition at line 805 of file malloc.c.

805 {
806
807 VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
808 heap_free_block ( &heap, ptr, size );
809}

References heap_free_block(), size, and VALGRIND_FREELIKE_BLOCK.

Referenced by __vxge_hw_fifo_delete(), __vxge_hw_ring_delete(), a3c90x_free_rx_ring(), a3c90x_free_tx_ring(), alloc_iob_raw(), arbel_alloc(), arbel_alloc_icm(), arbel_create_cq(), arbel_create_eq(), arbel_create_qp(), arbel_create_recv_wq(), arbel_destroy_cq(), arbel_destroy_eq(), arbel_destroy_qp(), arbel_free(), arbel_free_icm(), ath5k_desc_alloc(), ath5k_desc_free(), ath_descdma_cleanup(), ath_descdma_setup(), atl1e_free_ring_resources(), b44_free_rx_ring(), b44_free_tx_ring(), b44_init_rx_ring(), b44_init_tx_ring(), efx_hunt_free_special_buffer(), ehci_bus_close(), ehci_bus_open(), ehci_ring_alloc(), ehci_ring_free(), ena_create_admin(), ena_create_async(), ena_create_cq(), ena_create_sq(), ena_destroy_admin(), ena_destroy_async(), ena_destroy_cq(), ena_destroy_sq(), ena_probe(), ena_remove(), exanic_probe(), exanic_remove(), falcon_free_special_buffer(), free_iob(), golan_cmd_init(), golan_cmd_uninit(), golan_create_cq(), golan_create_eq(), golan_create_qp_aux(), golan_destory_eq(), golan_destroy_cq(), golan_destroy_qp(), hermon_alloc(), hermon_create_cq(), hermon_create_eq(), hermon_create_qp(), hermon_destroy_cq(), hermon_destroy_eq(), hermon_destroy_qp(), hermon_free(), hv_alloc_pages(), hv_free_message(), hv_free_pages(), hvm_unmap_hypercall(), icplus_create_ring(), icplus_destroy_ring(), ifec_free(), ifec_net_open(), igbvf_free_rx_resources(), igbvf_free_tx_resources(), jme_free_rx_resources(), jme_free_tx_resources(), legacy_probe(), legacy_remove(), linda_create_recv_wq(), linda_destroy_recv_wq(), linda_fini_send(), linda_init_send(), mlx_memory_free_dma_priv(), myri10ge_net_close(), myri10ge_net_open(), myson_create_ring(), myson_destroy_ring(), natsemi_create_ring(), natsemi_destroy_ring(), netfront_create_ring(), netfront_destroy_ring(), nv_free_rxtx_resources(), pcnet32_free_rx_resources(), pcnet32_free_tx_resources(), phantom_close(), phantom_create_rx_ctx(), phantom_create_tx_ctx(), phantom_open(), qib7322_create_recv_wq(), qib7322_destroy_recv_wq(), qib7322_fini_send(), qib7322_init_send(), rhine_destroy_ring(), rtl818x_free_rx_ring(), rtl818x_free_tx_ring(), sis190_free(), skge_free(), sky2_free_rings(), sky2_probe(), sky2_remove(), uhci_bus_close(), uhci_bus_open(), uhci_dequeue(), uhci_enqueue(), uhci_ring_alloc(), uhci_ring_free(), velocity_alloc_rings(), velocity_close(), vmbus_close(), vmbus_open(), vmxnet3_close(), and vmxnet3_open().