iPXE
Data Structures | Macros | Functions | Variables
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. More...
 
#define CACHE_DISCARDERS   __table ( struct cache_discarder, "cache_discarders" )
 Cache discarder table. More...
 
#define __cache_discarder(cost)   __table_entry ( CACHE_DISCARDERS, cost )
 Declare a cache discarder. More...
 
#define CACHE_CHEAP   01
 Items with a low replacement cost. More...
 
#define CACHE_NORMAL   02
 Items with a normal replacement cost. More...
 
#define CACHE_EXPENSIVE   03
 Items with a high replacement cost. More...
 

Functions

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

Variables

struct heap __attribute__
 

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 41 of file malloc.h.

◆ CACHE_DISCARDERS

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

Cache discarder table.

Definition at line 102 of file malloc.h.

◆ __cache_discarder

#define __cache_discarder (   cost)    __table_entry ( CACHE_DISCARDERS, cost )

Declare a cache discarder.

Definition at line 105 of file malloc.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ heap_realloc()

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

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

536  {
537  struct autosized_block *old_block;
538  struct autosized_block *new_block;
539  size_t old_total_size;
540  size_t new_total_size;
541  size_t old_size;
542  size_t offset = offsetof ( struct autosized_block, data );
543  void *new_ptr = NOWHERE;
544 
545  /* Allocate new memory if necessary. If allocation fails,
546  * return without touching the old block.
547  */
548  if ( new_size ) {
549  new_total_size = ( new_size + offset );
550  if ( new_total_size < new_size )
551  return NULL;
552  new_block = heap_alloc_block ( heap, new_total_size,
553  heap->ptr_align, -offset );
554  if ( ! new_block )
555  return NULL;
556  new_block->size = new_total_size;
557  VALGRIND_MAKE_MEM_NOACCESS ( &new_block->size,
558  sizeof ( new_block->size ) );
559  new_ptr = &new_block->data;
560  VALGRIND_MALLOCLIKE_BLOCK ( new_ptr, new_size, 0, 0 );
561  assert ( ( ( ( intptr_t ) new_ptr ) &
562  ( heap->ptr_align - 1 ) ) == 0 );
563  }
564 
565  /* Copy across relevant part of the old data region (if any),
566  * then free it. Note that at this point either (a) new_ptr
567  * is valid, or (b) new_size is 0; either way, the memcpy() is
568  * valid.
569  */
570  if ( old_ptr && ( old_ptr != NOWHERE ) ) {
571  old_block = container_of ( old_ptr, struct autosized_block,
572  data );
573  VALGRIND_MAKE_MEM_DEFINED ( &old_block->size,
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 ) );
580  VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 );
581  heap_free_block ( heap, old_block, old_total_size );
582  }
583 
584  if ( ASSERTED ) {
585  DBGC ( heap, "HEAP detected possible memory corruption "
586  "from %p\n", __builtin_return_address ( 0 ) );
587  }
588  return new_ptr;
589 }
#define NOWHERE
Address for zero-length memory blocks.
Definition: malloc.h:41
#define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
Definition: memcheck.h:131
char data[0]
Remaining data.
Definition: malloc.c:73
#define DBGC(...)
Definition: compiler.h:505
static void * heap_alloc_block(struct heap *heap, size_t size, size_t align, size_t offset)
Allocate a memory block.
Definition: malloc.c:265
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
unsigned long intptr_t
Definition: stdint.h:21
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
size_t ptr_align
Alignment for size-tracked allocations.
Definition: malloc.h:51
size_t size
Size of this block.
Definition: malloc.c:71
#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
Definition: valgrind.h:4411
#define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr, _qzz_len)
Definition: memcheck.h:111
#define ASSERTED
Definition: assert.h:25
A heap.
Definition: malloc.h:44
A block of allocated memory complete with size information.
Definition: malloc.c:69
static void heap_free_block(struct heap *heap, void *ptr, size_t size)
Free a memory block.
Definition: malloc.c:406
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define VALGRIND_FREELIKE_BLOCK(addr, rzB)
Definition: valgrind.h:4421
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), ASSERTED, container_of, data, autosized_block::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)

Dump free block list (for debugging)

Definition at line 792 of file malloc.c.

792  {
793  struct memory_block *block;
794 
795  dbg_printf ( "HEAP free block list:\n" );
797  dbg_printf ( "...[%p,%p] (size %#zx)\n", block,
798  ( ( ( void * ) block ) + block->size ),
799  block->size );
800  }
801 }
struct list_head list
List of free blocks.
Definition: malloc.c:58
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
A free block of memory.
Definition: malloc.c:43
struct list_head blocks
List of free memory blocks.
Definition: malloc.h:46
uint8_t block[3][8]
DES-encrypted blocks.
Definition: mschapv2.h:12
A heap.
Definition: malloc.h:44
void dbg_printf(const char *fmt,...)
Print debug message.
Definition: debug.c:38

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 
)

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

738  {
739 
740  /* Sanity checks */
741  assert ( ( virt_to_phys ( start ) & ( heap->align - 1 ) ) == 0 );
742  assert ( ( len & ( heap->align - 1 ) ) == 0 );
743 
744  /* Add to allocation pool */
746 
747  /* Fix up memory usage statistics */
748  heap->usedmem += len;
749 }
uint32_t start
Starting offset.
Definition: netvsc.h:12
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
size_t usedmem
Total amount of used memory.
Definition: malloc.h:56
A heap.
Definition: malloc.h:44
static void heap_free_block(struct heap *heap, void *ptr, size_t size)
Free a memory block.
Definition: malloc.c:406
size_t align
Alignment for free memory blocks.
Definition: malloc.h:49

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 
)

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

684  {
685  void * ptr;
686 
687  ptr = heap_alloc_block ( &heap, size, phys_align, offset );
688  if ( ptr && size ) {
689  assert ( ( phys_align == 0 ) ||
690  ( ( ( virt_to_phys ( ptr ) ^ offset ) &
691  ( phys_align - 1 ) ) == 0 ) );
692  VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
693  }
694  return ptr;
695 }
uint16_t size
Buffer size.
Definition: dwmac.h:14
static void * heap_alloc_block(struct heap *heap, size_t size, size_t align, size_t offset)
Allocate a memory block.
Definition: malloc.c:265
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
Definition: valgrind.h:4411
A heap.
Definition: malloc.h:44
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

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 
)

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

706  {
707 
708  return malloc_phys_offset ( size, phys_align, 0 );
709 }
uint16_t size
Buffer size.
Definition: dwmac.h:14
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:684

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 
)

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

722  {
723 
724  VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
725  heap_free_block ( &heap, ptr, size );
726 }
uint16_t size
Buffer size.
Definition: dwmac.h:14
A heap.
Definition: malloc.h:44
static void heap_free_block(struct heap *heap, void *ptr, size_t size)
Free a memory block.
Definition: malloc.c:406
#define VALGRIND_FREELIKE_BLOCK(addr, rzB)
Definition: valgrind.h:4421

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().

Variable Documentation

◆ __attribute__