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(), and heap_realloc().

◆ 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 537 of file malloc.c.

537 {
538 struct autosized_block *old_block;
539 struct autosized_block *new_block;
540 size_t old_total_size;
541 size_t new_total_size;
542 size_t old_size;
543 size_t offset = offsetof ( struct autosized_block, data );
544 void *new_ptr = NOWHERE;
545
546 /* Allocate new memory if necessary. If allocation fails,
547 * return without touching the old block.
548 */
549 if ( new_size ) {
550 new_total_size = ( new_size + offset );
551 if ( new_total_size < new_size )
552 return NULL;
553 new_block = heap_alloc_block ( heap, new_total_size,
554 heap->ptr_align, -offset );
555 if ( ! new_block )
556 return NULL;
557 new_block->size = new_total_size;
558 VALGRIND_MAKE_MEM_NOACCESS ( &new_block->size,
559 sizeof ( new_block->size ) );
560 new_ptr = &new_block->data;
561 VALGRIND_MALLOCLIKE_BLOCK ( new_ptr, new_size, 0, 0 );
562 assert ( ( ( ( intptr_t ) new_ptr ) &
563 ( heap->ptr_align - 1 ) ) == 0 );
564 }
565
566 /* Copy across relevant part of the old data region (if any),
567 * then free it. Note that at this point either (a) new_ptr
568 * is valid, or (b) new_size is 0; either way, the memcpy() is
569 * valid.
570 */
571 if ( old_ptr && ( old_ptr != NOWHERE ) ) {
572 old_block = container_of ( old_ptr, struct autosized_block,
573 data );
574 VALGRIND_MAKE_MEM_DEFINED ( &old_block->size,
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 ) );
581 VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 );
582 heap_free_block ( heap, old_block, old_total_size );
583 }
584
585 if ( ASSERTED ) {
586 DBGC ( heap, "HEAP detected possible memory corruption "
587 "from %p\n", __builtin_return_address ( 0 ) );
588 }
589 return new_ptr;
590}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
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:50
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:505
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:266
static void heap_free_block(struct heap *heap, void *ptr, size_t size)
Free a memory block.
Definition malloc.c:407
#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:70
size_t size
Size of this block.
Definition malloc.c:72
char data[0]
Remaining data.
Definition malloc.c:74
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 793 of file malloc.c.

793 {
794 struct memory_block *block;
795
796 dbg_printf ( "HEAP free block list:\n" );
798 dbg_printf ( "...[%p,%p] (size %#zx)\n", block,
799 ( ( ( void * ) block ) + block->size ),
800 block->size );
801 }
802}
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:44
struct list_head list
List of free blocks.
Definition malloc.c:59

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 739 of file malloc.c.

739 {
740
741 /* Sanity checks */
742 assert ( ( virt_to_phys ( start ) & ( heap->align - 1 ) ) == 0 );
743 assert ( ( len & ( heap->align - 1 ) ) == 0 );
744
745 /* Add to allocation pool */
747
748 /* Fix up memory usage statistics */
749 heap->usedmem += len;
750}
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 685 of file malloc.c.

685 {
686 void * ptr;
687
688 ptr = heap_alloc_block ( &heap, size, phys_align, offset );
689 if ( ptr && size ) {
690 assert ( ( phys_align == 0 ) ||
691 ( ( ( virt_to_phys ( ptr ) ^ offset ) &
692 ( phys_align - 1 ) ) == 0 ) );
693 VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
694 }
695 return ptr;
696}
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 707 of file malloc.c.

707 {
708
709 return malloc_phys_offset ( size, phys_align, 0 );
710}
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:685

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(), tg3_alloc_consistent(), tg3_test_dma(), 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 723 of file malloc.c.

723 {
724
725 VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
726 heap_free_block ( &heap, ptr, size );
727}

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(), tg3_free_consistent(), tg3_rx_prodring_fini(), tg3_test_dma(), 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().