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