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  */
malloc_phys_offset(size_t size,size_t phys_align,size_t offset)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  */
malloc_phys(size_t size,size_t phys_align)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  */
free_phys(void * ptr,size_t size)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 */
83 struct 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 */
110