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_dma().
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 for DMA
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  * Allocates physically-aligned memory for DMA.
43  *
44  * @c align must be a power of two. @c size may not be zero.
45  */
46 static inline void * __malloc malloc_dma_offset ( size_t size,
47  size_t phys_align,
48  size_t offset ) {
49  void * ptr = alloc_memblock ( size, phys_align, offset );
50  if ( ptr && size )
51  VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
52  return ptr;
53 }
54 
55 /**
56  * Allocate memory for DMA
57  *
58  * @v size Requested size
59  * @v align Physical alignment
60  * @ret ptr Memory, or NULL
61  *
62  * Allocates physically-aligned memory for DMA.
63  *
64  * @c align must be a power of two. @c size may not be zero.
65  */
66 static inline void * __malloc malloc_dma ( size_t size, size_t phys_align ) {
67  return malloc_dma_offset ( size, phys_align, 0 );
68 }
69 
70 /**
71  * Free memory allocated with malloc_dma()
72  *
73  * @v ptr Memory allocated by malloc_dma(), or NULL
74  * @v size Size of memory, as passed to malloc_dma()
75  *
76  * Memory allocated with malloc_dma() can only be freed with
77  * free_dma(); it cannot be freed with the standard free().
78  *
79  * If @c ptr is NULL, no action is taken.
80  */
81 static inline void free_dma ( void *ptr, size_t size ) {
82  VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
83  free_memblock ( ptr, size );
84 }
85 
86 /** A cache discarder */
88  /**
89  * Discard some cached data
90  *
91  * @ret discarded Number of cached items discarded
92  */
93  unsigned int ( * discard ) ( void );
94 };
95 
96 /** Cache discarder table */
97 #define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
98 
99 /** Declare a cache discarder */
100 #define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
101 
102 /** @defgroup cache_cost Cache discarder costs
103  *
104  * @{
105  */
106 
107 #define CACHE_CHEAP 01 /**< Items with a low replacement cost */
108 #define CACHE_NORMAL 02 /**< Items with a normal replacement cost */
109 #define CACHE_EXPENSIVE 03 /**< Items with a high replacement cost */
110 
111 /** @} */
112 
113 #endif /* _IPXE_MALLOC_H */
void *__malloc alloc_memblock(size_t size, size_t align, size_t offset)
Allocate a memory block.
Definition: malloc.c:279
static void *__malloc malloc_dma_offset(size_t size, size_t phys_align, size_t offset)
Allocate memory for DMA.
Definition: malloc.h:46
void mdumpfree(void)
size_t maxusedmem
Maximum amount of used memory.
Definition: malloc.c:100
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:93
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:87
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
static void *__malloc malloc_dma(size_t size, size_t phys_align)
Allocate memory for DMA.
Definition: malloc.h:66
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.
#define VALGRIND_FREELIKE_BLOCK(addr, rzB)
Definition: valgrind.h:4421
static void free_dma(void *ptr, size_t size)
Free memory allocated with malloc_dma()
Definition: malloc.h:81
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)