Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / include / ipxe / malloc.h
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 );
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
26 extern void * __malloc alloc_memblock ( size_t size, size_t align,
27                                         size_t offset );
28 extern void free_memblock ( void *ptr, size_t size );
29 extern void mpopulate ( void *start, size_t len );
30 extern void mdumpfree ( void );
31
32 /**
33  * Allocate memory for DMA
34  *
35  * @v size              Requested size
36  * @v align             Physical alignment
37  * @v offset            Offset from physical alignment
38  * @ret ptr             Memory, or NULL
39  *
40  * Allocates physically-aligned memory for DMA.
41  *
42  * @c align must be a power of two.  @c size may not be zero.
43  */
44 static inline void * __malloc malloc_dma_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 for DMA
55  *
56  * @v size              Requested size
57  * @v align             Physical alignment
58  * @ret ptr             Memory, or NULL
59  *
60  * Allocates physically-aligned memory for DMA.
61  *
62  * @c align must be a power of two.  @c size may not be zero.
63  */
64 static inline void * __malloc malloc_dma ( size_t size, size_t phys_align ) {
65         return malloc_dma_offset ( size, phys_align, 0 );
66 }
67
68 /**
69  * Free memory allocated with malloc_dma()
70  *
71  * @v ptr               Memory allocated by malloc_dma(), or NULL
72  * @v size              Size of memory, as passed to malloc_dma()
73  *
74  * Memory allocated with malloc_dma() can only be freed with
75  * free_dma(); it cannot be freed with the standard free().
76  *
77  * If @c ptr is NULL, no action is taken.
78  */
79 static inline void free_dma ( void *ptr, size_t size ) {
80         free_memblock ( ptr, size );
81         VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
82 }
83
84 /** A cache discarder */
85 struct cache_discarder {
86         /**
87          * Discard some cached data
88          *
89          * @ret discarded       Number of cached items discarded
90          */
91         unsigned int ( * discard ) ( void );
92 };
93
94 /** Cache discarder table */
95 #define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
96
97 /** Declare a cache discarder */
98 #define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
99
100 /** @defgroup cache_cost Cache discarder costs
101  *
102  * @{
103  */
104
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 */
108
109 /** @} */
110
111 #endif /* _IPXE_MALLOC_H */