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/tables.h>
22 #include <valgrind/memcheck.h>
23 
24 extern size_t freemem;
25 extern size_t usedmem;
26 extern size_t maxusedmem;
27 
28 extern void * __malloc alloc_memblock ( size_t size, size_t align,
29  size_t offset );
30 extern void free_memblock ( void *ptr, size_t size );
31 extern void mdumpfree ( void );
32 
33 /**
34  * Allocate memory with specified physical alignment and offset
35  *
36  * @v size Requested size
37  * @v align Physical alignment
38  * @v offset Offset from physical alignment
39  * @ret ptr Memory, or NULL
40  *
41  * @c align must be a power of two. @c size may not be zero.
42  */
43 static inline void * __malloc malloc_phys_offset ( size_t size,
44  size_t phys_align,
45  size_t offset ) {
46  void * ptr = alloc_memblock ( size, phys_align, offset );
47  if ( ptr && size )
48  VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
49  return ptr;
50 }
51 
52 /**
53  * Allocate memory with specified physical alignment
54  *
55  * @v size Requested size
56  * @v align Physical alignment
57  * @ret ptr Memory, or NULL
58  *
59  * @c align must be a power of two. @c size may not be zero.
60  */
61 static inline void * __malloc malloc_phys ( size_t size, size_t phys_align ) {
62  return malloc_phys_offset ( size, phys_align, 0 );
63 }
64 
65 /**
66  * Free memory allocated with malloc_phys()
67  *
68  * @v ptr Memory allocated by malloc_phys(), or NULL
69  * @v size Size of memory, as passed to malloc_phys()
70  *
71  * Memory allocated with malloc_phys() can only be freed with
72  * free_phys(); it cannot be freed with the standard free().
73  *
74  * If @c ptr is NULL, no action is taken.
75  */
76 static inline void free_phys ( void *ptr, size_t size ) {
77  VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
78  free_memblock ( ptr, size );
79 }
80 
81 /** A cache discarder */
83  /**
84  * Discard some cached data
85  *
86  * @ret discarded Number of cached items discarded
87  */
88  unsigned int ( * discard ) ( void );
89 };
90 
91 /** Cache discarder table */
92 #define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
93 
94 /** Declare a cache discarder */
95 #define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
96 
97 /** @defgroup cache_cost Cache discarder costs
98  *
99  * @{
100  */
101 
102 #define CACHE_CHEAP 01 /**< Items with a low replacement cost */
103 #define CACHE_NORMAL 02 /**< Items with a normal replacement cost */
104 #define CACHE_EXPENSIVE 03 /**< Items with a high replacement cost */
105 
106 /** @} */
107 
108 #endif /* _IPXE_MALLOC_H */
static __always_inline void struct dma_mapping size_t size_t align
Definition: dma.h:222
void *__malloc alloc_memblock(size_t size, size_t align, size_t offset)
Allocate a memory block.
Definition: malloc.c:284
void mdumpfree(void)
Dump free block list (for debugging)
Definition: malloc.c:718
size_t maxusedmem
Maximum amount of used memory.
Definition: malloc.c:101
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
static void *__malloc malloc_phys(size_t size, size_t phys_align)
Allocate memory with specified physical alignment.
Definition: malloc.h:61
void free_memblock(void *ptr, size_t size)
Free a memory block.
Definition: malloc.c:416
size_t usedmem
Total amount of used memory.
Definition: malloc.c:98
unsigned int(* discard)(void)
Discard some cached data.
Definition: malloc.h:88
#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
Definition: valgrind.h:4411
A cache discarder.
Definition: malloc.h:82
#define __malloc
Declare a pointer returned by a function as a unique memory address as returned by malloc-type functi...
Definition: compiler.h:598
size_t freemem
Total amount of free memory.
Definition: malloc.c:95
Linker tables.
static void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
Definition: malloc.h:76
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define VALGRIND_FREELIKE_BLOCK(addr, rzB)
Definition: valgrind.h:4421
static 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.h:43
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)