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 mpopulate ( void *start, size_t len );
32 extern void mdumpfree ( void );
33 
34 /**
35  * Allocate memory with specified physical alignment and offset
36  *
37  * @v size Requested size
38  * @v align Physical alignment
39  * @v offset Offset from physical alignment
40  * @ret ptr Memory, or NULL
41  *
42  * @c align must be a power of two. @c size may not be zero.
43  */
44 static inline void * __malloc malloc_phys_offset ( size_t size,
45  size_t phys_align,
46  size_t offset ) {
47  void * ptr = alloc_memblock ( size, phys_align, offset );
48  if ( ptr && size )
49  VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
50  return ptr;
51 }
52 
53 /**
54  * Allocate memory with specified physical alignment
55  *
56  * @v size Requested size
57  * @v align Physical alignment
58  * @ret ptr Memory, or NULL
59  *
60  * @c align must be a power of two. @c size may not be zero.
61  */
62 static inline void * __malloc malloc_phys ( size_t size, size_t phys_align ) {
63  return malloc_phys_offset ( size, phys_align, 0 );
64 }
65 
66 /**
67  * Free memory allocated with malloc_phys()
68  *
69  * @v ptr Memory allocated by malloc_phys(), or NULL
70  * @v size Size of memory, as passed to malloc_phys()
71  *
72  * Memory allocated with malloc_phys() can only be freed with
73  * free_phys(); it cannot be freed with the standard free().
74  *
75  * If @c ptr is NULL, no action is taken.
76  */
77 static inline void free_phys ( void *ptr, size_t size ) {
78  VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
79  free_memblock ( ptr, size );
80 }
81 
82 /** A cache discarder */
84  /**
85  * Discard some cached data
86  *
87  * @ret discarded Number of cached items discarded
88  */
89  unsigned int ( * discard ) ( void );
90 };
91 
92 /** Cache discarder table */
93 #define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
94 
95 /** Declare a cache discarder */
96 #define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
97 
98 /** @defgroup cache_cost Cache discarder costs
99  *
100  * @{
101  */
102 
103 #define CACHE_CHEAP 01 /**< Items with a low replacement cost */
104 #define CACHE_NORMAL 02 /**< Items with a normal replacement cost */
105 #define CACHE_EXPENSIVE 03 /**< Items with a high replacement cost */
106 
107 /** @} */
108 
109 #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:279
void mdumpfree(void)
size_t maxusedmem
Maximum amount of used memory.
Definition: malloc.c:100
static void *__malloc malloc_phys(size_t size, size_t phys_align)
Allocate memory with specified physical alignment.
Definition: malloc.h:62
void free_memblock(void *ptr, size_t size)
Free a memory block.
Definition: malloc.c:404
uint32_t start
Starting offset.
Definition: netvsc.h:12
size_t usedmem
Total amount of used memory.
Definition: malloc.c:97
unsigned int(* discard)(void)
Discard some cached data.
Definition: malloc.h:89
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
void mpopulate(void *start, size_t len)
Add memory to allocation pool.
Definition: malloc.c:648
#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed)
Definition: valgrind.h:4411
A cache discarder.
Definition: malloc.h:83
uint32_t len
Length.
Definition: ena.h:14
#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:94
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
Linker tables.
static void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
Definition: malloc.h:77
#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:44
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)