iPXE
Macros | Functions | Variables
uheap.c File Reference

External ("user") heap. More...

#include <ipxe/io.h>
#include <ipxe/memmap.h>
#include <ipxe/malloc.h>
#include <ipxe/umalloc.h>

Go to the source code of this file.

Macros

#define UHEAP_ALIGN   PAGE_SIZE
 Alignment for external heap allocations. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void uheap_resize (ssize_t delta)
 Adjust size of external heap in-use memory region. More...
 
static void uheap_find (void)
 Find an external heap region. More...
 
static unsigned int uheap_grow (size_t size)
 Attempt to grow external heap. More...
 
static unsigned int uheap_shrink (void *ptr, size_t size)
 Allow external heap to shrink. More...
 
static void * uheap_realloc (void *old_ptr, size_t new_size)
 Reallocate external memory. More...
 
 PROVIDE_UMALLOC (uheap, urealloc, uheap_realloc)
 

Variables

static struct heap uheap
 The external heap. More...
 
physaddr_t uheap_limit
 Minimum possible start of external heap. More...
 
physaddr_t uheap_start
 Start of external heap. More...
 
physaddr_t uheap_end
 End of external heap. More...
 
struct used_region uheap_used __used_region
 In-use memory region. More...
 

Detailed Description

External ("user") heap.

This file implements an external heap (for umalloc()) that grows downwards from the top of the largest contiguous accessible block in the system memory map.

Definition in file uheap.c.

Macro Definition Documentation

◆ UHEAP_ALIGN

#define UHEAP_ALIGN   PAGE_SIZE

Alignment for external heap allocations.

Historically, umalloc() has produced page-aligned allocations, and the hidden region in the system memory map has been aligned to a page boundary. Preserve this behaviour, to avoid needing to inspect and update large amounts of driver code, and also because it keeps the resulting memory maps easy to read.

Definition at line 49 of file uheap.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ uheap_resize()

static void uheap_resize ( ssize_t  delta)
static

Adjust size of external heap in-use memory region.

Parameters
deltaSize change

Definition at line 72 of file uheap.c.

72  {
73 
74  /* Update in-use memory region */
75  assert ( ( delta & ( UHEAP_ALIGN - 1 ) ) == 0 );
76  uheap_start -= delta;
79  assert ( ( uheap_limit & ( UHEAP_ALIGN - 1 ) ) == 0 );
80  assert ( ( uheap_start & ( UHEAP_ALIGN - 1 ) ) == 0 );
81  assert ( ( uheap_end & ( UHEAP_ALIGN - 1 ) ) == 0 );
82  memmap_use ( &uheap_used, uheap_start, ( uheap_end - uheap_start ) );
83  DBGC ( &uheap, "UHEAP now at (%#08lx)...[%#08lx,%#08lx)\n",
85  memmap_dump_all ( 1 );
86 }
static void memmap_use(struct used_region *used, physaddr_t start, size_t size)
Update an in-use memory region.
Definition: memmap.h:153
#define DBGC(...)
Definition: compiler.h:505
physaddr_t uheap_start
Start of external heap.
Definition: uheap.c:57
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static struct heap uheap
The external heap.
Definition: uheap.c:51
#define UHEAP_ALIGN
Alignment for external heap allocations.
Definition: uheap.c:49
static void memmap_dump_all(int hide)
Dump system memory map (for debugging)
Definition: memmap.h:215
physaddr_t uheap_limit
Minimum possible start of external heap.
Definition: uheap.c:54
physaddr_t uheap_end
End of external heap.
Definition: uheap.c:60

References assert(), DBGC, memmap_dump_all(), memmap_use(), uheap, UHEAP_ALIGN, uheap_end, uheap_limit, and uheap_start.

Referenced by uheap_find(), uheap_grow(), and uheap_shrink().

◆ uheap_find()

static void uheap_find ( void  )
static

Find an external heap region.

Definition at line 92 of file uheap.c.

92  {
95  size_t before;
96  size_t after;
97  size_t strip;
98  size_t size;
99 
100  /* Sanity checks */
103  assert ( uheap_used.size == 0 );
104 
105  /* Find the largest region within the system memory map */
106  size = memmap_largest ( &start );
107  end = ( start + size );
108  DBGC ( &uheap, "UHEAP largest region is [%#08lx,%#08lx)\n",
109  start, end );
110 
111  /* Align start and end addresses, and prevent overflow to zero */
112  after = ( end ? ( end & ( UHEAP_ALIGN - 1 ) ) : UHEAP_ALIGN );
113  before = ( ( -start ) & ( UHEAP_ALIGN - 1 ) );
114  strip = ( before + after );
115  if ( strip > size )
116  return;
117  start += before;
118  end -= after;
119  size -= strip;
120  assert ( ( end - start ) == size );
121 
122  /* Record region */
123  uheap_limit = start;
124  uheap_start = end;
125  uheap_end = end;
126  uheap_resize ( 0 );
127 }
size_t memmap_largest(physaddr_t *start)
Find largest usable memory region.
Definition: memmap.c:120
uint16_t size
Buffer size.
Definition: dwmac.h:14
#define DBGC(...)
Definition: compiler.h:505
static void uheap_resize(ssize_t delta)
Adjust size of external heap in-use memory region.
Definition: uheap.c:72
physaddr_t uheap_start
Start of external heap.
Definition: uheap.c:57
int32_t before
Initial microcode version.
Definition: ucode.h:16
uint32_t start
Starting offset.
Definition: netvsc.h:12
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static struct heap uheap
The external heap.
Definition: uheap.c:51
#define UHEAP_ALIGN
Alignment for external heap allocations.
Definition: uheap.c:49
unsigned long physaddr_t
Definition: stdint.h:20
int32_t after
Final microcode version.
Definition: ucode.h:18
physaddr_t uheap_limit
Minimum possible start of external heap.
Definition: uheap.c:54
uint32_t end
Ending offset.
Definition: netvsc.h:18
physaddr_t uheap_end
End of external heap.
Definition: uheap.c:60

References after, assert(), before, DBGC, end, memmap_largest(), size, start, uheap, UHEAP_ALIGN, uheap_end, uheap_limit, uheap_resize(), and uheap_start.

Referenced by uheap_grow().

◆ uheap_grow()

static unsigned int uheap_grow ( size_t  size)
static

Attempt to grow external heap.

Parameters
sizeFailed allocation size
Return values
grownHeap has grown: retry allocations

Definition at line 135 of file uheap.c.

135  {
136  void *new;
137 
138  /* Initialise heap, if it does not yet exist */
139  if ( uheap_limit == uheap_end )
140  uheap_find();
141 
142  /* Fail if insufficient space remains */
143  if ( size > ( uheap_start - uheap_limit ) )
144  return 0;
145 
146  /* Grow heap */
147  new = ( phys_to_virt ( uheap_start ) - size );
148  heap_populate ( &uheap, new, size );
149  uheap_resize ( size );
150 
151  return 1;
152 }
uint16_t size
Buffer size.
Definition: dwmac.h:14
static void uheap_resize(ssize_t delta)
Adjust size of external heap in-use memory region.
Definition: uheap.c:72
physaddr_t uheap_start
Start of external heap.
Definition: uheap.c:57
static void uheap_find(void)
Find an external heap region.
Definition: uheap.c:92
void heap_populate(struct heap *heap, void *start, size_t len)
Add memory to allocation pool.
Definition: malloc.c:738
static struct heap uheap
The external heap.
Definition: uheap.c:51
physaddr_t uheap_limit
Minimum possible start of external heap.
Definition: uheap.c:54
physaddr_t uheap_end
End of external heap.
Definition: uheap.c:60

References heap_populate(), size, uheap, uheap_end, uheap_find(), uheap_limit, uheap_resize(), and uheap_start.

◆ uheap_shrink()

static unsigned int uheap_shrink ( void *  ptr,
size_t  size 
)
static

Allow external heap to shrink.

Parameters
ptrStart of free block
sizeSize of free block
Return values
shrunkHeap has shrunk: discard block

Definition at line 161 of file uheap.c.

161  {
162 
163  /* Do nothing unless this is the lowest block in the heap */
164  if ( virt_to_phys ( ptr ) != uheap_start )
165  return 0;
166 
167  /* Shrink heap */
168  uheap_resize ( -size );
169 
170  return 1;
171 }
uint16_t size
Buffer size.
Definition: dwmac.h:14
static void uheap_resize(ssize_t delta)
Adjust size of external heap in-use memory region.
Definition: uheap.c:72
physaddr_t uheap_start
Start of external heap.
Definition: uheap.c:57

References size, uheap_resize(), and uheap_start.

◆ uheap_realloc()

static void* uheap_realloc ( void *  old_ptr,
size_t  new_size 
)
static

Reallocate external memory.

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

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

Definition at line 192 of file uheap.c.

192  {
193 
194  return heap_realloc ( &uheap, old_ptr, new_size );
195 }
static struct heap uheap
The external heap.
Definition: uheap.c:51
void * heap_realloc(struct heap *heap, void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:536

References heap_realloc(), and uheap.

◆ PROVIDE_UMALLOC()

PROVIDE_UMALLOC ( uheap  ,
urealloc  ,
uheap_realloc   
)

Variable Documentation

◆ uheap

static struct heap uheap
static
Initial value:
= {
.blocks = LIST_HEAD_INIT ( uheap.blocks ),
.align = UHEAP_ALIGN,
.ptr_align = UHEAP_ALIGN,
.grow = uheap_grow,
.shrink = uheap_shrink,
}
static struct heap uheap
The external heap.
Definition: uheap.c:51
#define UHEAP_ALIGN
Alignment for external heap allocations.
Definition: uheap.c:49
static unsigned int uheap_grow(size_t size)
Attempt to grow external heap.
Definition: uheap.c:135
struct list_head blocks
List of free memory blocks.
Definition: malloc.h:46
static unsigned int uheap_shrink(void *ptr, size_t size)
Allow external heap to shrink.
Definition: uheap.c:161
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:30

The external heap.

Definition at line 51 of file uheap.c.

Referenced by uheap_find(), uheap_grow(), uheap_realloc(), and uheap_resize().

◆ uheap_limit

physaddr_t uheap_limit

Minimum possible start of external heap.

Definition at line 54 of file uheap.c.

Referenced by initrd_region(), initrd_reshuffle(), uheap_find(), uheap_grow(), and uheap_resize().

◆ uheap_start

physaddr_t uheap_start

Start of external heap.

Definition at line 57 of file uheap.c.

Referenced by initrd_startup(), int15_sync(), uheap_find(), uheap_grow(), uheap_resize(), and uheap_shrink().

◆ uheap_end

physaddr_t uheap_end

End of external heap.

Definition at line 60 of file uheap.c.

Referenced by initrd_region(), initrd_reshuffle(), int15_sync(), uheap_find(), uheap_grow(), and uheap_resize().

◆ __used_region

struct used_region uheap_used __used_region
Initial value:
= {
.name = "uheap",
}

In-use memory region.

Definition at line 63 of file uheap.c.