8 * Dynamic memory allocation
12 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
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().
21 #include <ipxe/tables.h>
22 #include <valgrind/memcheck.h>
24 extern size_t freemem;
26 extern void * __malloc alloc_memblock ( size_t size, size_t align,
28 extern void free_memblock ( void *ptr, size_t size );
29 extern void mpopulate ( void *start, size_t len );
30 extern void mdumpfree ( void );
33 * Allocate memory for DMA
35 * @v size Requested size
36 * @v align Physical alignment
37 * @v offset Offset from physical alignment
38 * @ret ptr Memory, or NULL
40 * Allocates physically-aligned memory for DMA.
42 * @c align must be a power of two. @c size may not be zero.
44 static inline void * __malloc malloc_dma_offset ( size_t size,
47 void * ptr = alloc_memblock ( size, phys_align, offset );
49 VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
54 * Allocate memory for DMA
56 * @v size Requested size
57 * @v align Physical alignment
58 * @ret ptr Memory, or NULL
60 * Allocates physically-aligned memory for DMA.
62 * @c align must be a power of two. @c size may not be zero.
64 static inline void * __malloc malloc_dma ( size_t size, size_t phys_align ) {
65 return malloc_dma_offset ( size, phys_align, 0 );
69 * Free memory allocated with malloc_dma()
71 * @v ptr Memory allocated by malloc_dma(), or NULL
72 * @v size Size of memory, as passed to malloc_dma()
74 * Memory allocated with malloc_dma() can only be freed with
75 * free_dma(); it cannot be freed with the standard free().
77 * If @c ptr is NULL, no action is taken.
79 static inline void free_dma ( void *ptr, size_t size ) {
80 VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
81 free_memblock ( ptr, size );
84 /** A cache discarder */
85 struct cache_discarder {
87 * Discard some cached data
89 * @ret discarded Number of cached items discarded
91 unsigned int ( * discard ) ( void );
94 /** Cache discarder table */
95 #define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
97 /** Declare a cache discarder */
98 #define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
100 /** @defgroup cache_cost Cache discarder costs
105 #define CACHE_CHEAP 01 /**< Items with a low replacement cost */
106 #define CACHE_NORMAL 02 /**< Items with a normal replacement cost */
107 #define CACHE_EXPENSIVE 03 /**< Items with a high replacement cost */
111 #endif /* _IPXE_MALLOC_H */