iPXE
malloc.h
Go to the documentation of this file.
1 #ifndef _IPXE_MALLOC_H
2 #define _IPXE_MALLOC_H
3 
4 #include <stdint.h>
5 
6 /** @file
7  *
8  * Dynamic memory allocation
9  *
10  */
11 
12 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
13 FILE_SECBOOT ( PERMITTED );
14 
15 /*
16  * Prototypes for the standard functions (malloc() et al) are in
17  * stdlib.h. Include <ipxe/malloc.h> only if you need the
18  * non-standard functions, such as malloc_phys().
19  *
20  */
21 #include <stdlib.h>
22 #include <ipxe/list.h>
23 #include <ipxe/tables.h>
24 #include <valgrind/memcheck.h>
25 
26 /**
27  * Address for zero-length memory blocks
28  *
29  * @c malloc(0) or @c realloc(ptr,0) will return the special value @c
30  * NOWHERE. Calling @c free(NOWHERE) will have no effect.
31  *
32  * This is consistent with the ANSI C standards, which state that
33  * "either NULL or a pointer suitable to be passed to free()" must be
34  * returned in these cases. Using a special non-NULL value means that
35  * the caller can take a NULL return value to indicate failure,
36  * without first having to check for a requested size of zero.
37  *
38  * Code outside of the memory allocators themselves does not ever need
39  * to refer to the actual value of @c NOWHERE; this is an internal
40  * definition.
41  */
42 #define NOWHERE ( ( void * ) ~( ( intptr_t ) 0 ) )
43 
44 /** A heap */
45 struct heap {
46  /** List of free memory blocks */
47  struct list_head blocks;
48 
49  /** Alignment for free memory blocks */
50  size_t align;
51  /** Alignment for size-tracked allocations */
52  size_t ptr_align;
53 
54  /** Total amount of free memory */
55  size_t freemem;
56  /** Total amount of used memory */
57  size_t usedmem;
58  /** Maximum amount of used memory */
59  size_t maxusedmem;
60 
61  /**
62  * Attempt to grow heap (optional)
63  *
64  * @v size Failed allocation size
65  * @ret grown Heap has grown: retry allocations
66  */
67  unsigned int ( * grow ) ( size_t size );
68  /**
69  * Allow heap to shrink (optional)
70  *
71  * @v ptr Start of free block
72  * @v size Size of free block
73  * @ret shrunk Heap has shrunk: discard block
74  *
75  * Note that the discarded block will be accessed once after
76  * this method returns, in order to clear the free block
77  * metadata.
78  */
79  unsigned int ( * shrink ) ( void *ptr, size_t size );
80 };
81 
82 extern void * heap_realloc ( struct heap *heap, void *old_ptr,
83  size_t new_size );
84 extern void heap_dump ( struct heap *heap );
85 extern void heap_populate ( struct heap *heap, void *start, size_t len );
86 
87 extern void * __malloc malloc_phys_offset ( size_t size, size_t phys_align,
88  size_t offset );
89 extern void * __malloc malloc_phys ( size_t size, size_t phys_align );
90 extern void free_phys ( void *ptr, size_t size );
91 
92 /** A cache discarder */
94  /**
95  * Discard some cached data
96  *
97  * @ret discarded Number of cached items discarded
98  */
99  unsigned int ( * discard ) ( void );
100 };
101 
102 /** Cache discarder table */
103 #define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
104 
105 /** Declare a cache discarder */
106 #define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
107 
108 /** @defgroup cache_cost Cache discarder costs
109  *
110  * @{
111  */
112 
113 #define CACHE_CHEAP 01 /**< Items with a low replacement cost */
114 #define CACHE_NORMAL 02 /**< Items with a normal replacement cost */
115 #define CACHE_EXPENSIVE 03 /**< Items with a high replacement cost */
116 
117 /** @} */
118 
119 #endif /* _IPXE_MALLOC_H */
void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
Definition: malloc.c:723
unsigned int(* shrink)(void *ptr, size_t size)
Allow heap to shrink (optional)
Definition: malloc.h:79
FILE_SECBOOT(PERMITTED)
uint16_t size
Buffer size.
Definition: dwmac.h:14
void * heap_realloc(struct heap *heap, void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:537
void *__malloc malloc_phys(size_t size, size_t phys_align)
Allocate memory with specified physical alignment.
Definition: malloc.c:707
A doubly-linked list entry (or list head)
Definition: list.h:19
uint32_t start
Starting offset.
Definition: netvsc.h:12
unsigned int(* grow)(size_t size)
Attempt to grow heap (optional)
Definition: malloc.h:67
void heap_dump(struct heap *heap)
Dump free block list (for debugging)
Definition: malloc.c:793
unsigned int(* discard)(void)
Discard some cached data.
Definition: malloc.h:99
ring len
Length.
Definition: dwmac.h:231
size_t ptr_align
Alignment for size-tracked allocations.
Definition: malloc.h:52
Linked lists.
size_t maxusedmem
Maximum amount of used memory.
Definition: malloc.h:59
A cache discarder.
Definition: malloc.h:93
size_t freemem
Total amount of free memory.
Definition: malloc.h:55
struct list_head blocks
List of free memory blocks.
Definition: malloc.h:47
size_t usedmem
Total amount of used memory.
Definition: malloc.h:57
#define __malloc
Declare a pointer returned by a function as a unique memory address as returned by malloc-type functi...
Definition: compiler.h:598
A heap.
Definition: malloc.h:45
void *__malloc 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
Linker tables.
void heap_populate(struct heap *heap, void *start, size_t len)
Add memory to allocation pool.
Definition: malloc.c:739
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
size_t align
Alignment for free memory blocks.
Definition: malloc.h:50
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)