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  */
malloc_dma_offset(size_t size,size_t phys_align,size_t offset)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  */
malloc_dma(size_t size,size_t phys_align)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  */
free_dma(void * ptr,size_t size)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 */
87 struct 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 */
114