iPXE
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.

Functions

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

Variables

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

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.

Referenced by uheap_find(), and uheap_resize().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ uheap_resize()

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}
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
#define DBGC(...)
Definition compiler.h:505
static void memmap_use(struct used_region *used, physaddr_t start, size_t size)
Update an in-use memory region.
Definition memmap.h:154
static void memmap_dump_all(int hide)
Dump system memory map (for debugging)
Definition memmap.h:216
physaddr_t uheap_end
End of external heap.
Definition uheap.c:60
physaddr_t uheap_start
Start of external heap.
Definition uheap.c:57
physaddr_t uheap_limit
Minimum possible start of external heap.
Definition uheap.c:54
#define UHEAP_ALIGN
Alignment for external heap allocations.
Definition uheap.c:49
static struct heap uheap
The external heap.
Definition uheap.c:51

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

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 */
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 */
125 uheap_end = end;
126 uheap_resize ( 0 );
127}
unsigned long physaddr_t
Definition stdint.h:20
uint32_t start
Starting offset.
Definition netvsc.h:1
uint16_t size
Buffer size.
Definition dwmac.h:3
size_t memmap_largest(physaddr_t *start)
Find largest usable memory region.
Definition memmap.c:120
uint32_t end
Ending offset.
Definition netvsc.h:7
int32_t after
Final microcode version.
Definition ucode.h:7
int32_t before
Initial microcode version.
Definition ucode.h:5
static void uheap_resize(ssize_t delta)
Adjust size of external heap in-use memory region.
Definition uheap.c:72

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

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}
void heap_populate(struct heap *heap, void *start, size_t len)
Add memory to allocation pool.
Definition malloc.c:739
static void uheap_find(void)
Find an external heap region.
Definition uheap.c:92

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

◆ uheap_shrink()

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}

References size, uheap_resize(), and uheap_start.

◆ uheap_realloc()

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}
void * heap_realloc(struct heap *heap, void *old_ptr, size_t new_size)
Reallocate memory.
Definition malloc.c:537

References heap_realloc(), and uheap.

Referenced by PROVIDE_UMALLOC().

◆ PROVIDE_UMALLOC()

PROVIDE_UMALLOC ( uheap ,
urealloc ,
uheap_realloc  )

References uheap, uheap_realloc(), and urealloc().

Variable Documentation

◆ uheap

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,
}
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition list.h:31
static unsigned int uheap_grow(size_t size)
Attempt to grow external heap.
Definition uheap.c:135
static unsigned int uheap_shrink(void *ptr, size_t size)
Allow external heap to shrink.
Definition uheap.c:161

The external heap.

Definition at line 51 of file uheap.c.

Referenced by PROVIDE_UMALLOC(), 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.

63 {
64 .name = "uheap",
65};