1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#ifndef _IPXE_MALLOC_H
#define _IPXE_MALLOC_H
#include <stdint.h>
/** @file
*
* Dynamic memory allocation
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/*
* Prototypes for the standard functions (malloc() et al) are in
* stdlib.h. Include <ipxe/malloc.h> only if you need the
* non-standard functions, such as malloc_phys().
*
*/
#include <stdlib.h>
#include <ipxe/tables.h>
#include <valgrind/memcheck.h>
extern size_t freemem;
extern size_t usedmem;
extern size_t maxusedmem;
extern void * __malloc alloc_memblock ( size_t size, size_t align,
size_t offset );
extern void free_memblock ( void *ptr, size_t size );
extern void mdumpfree ( void );
/**
* Allocate memory with specified physical alignment and offset
*
* @v size Requested size
* @v align Physical alignment
* @v offset Offset from physical alignment
* @ret ptr Memory, or NULL
*
* @c align must be a power of two. @c size may not be zero.
*/
static inline void * __malloc malloc_phys_offset ( size_t size,
size_t phys_align,
size_t offset ) {
void * ptr = alloc_memblock ( size, phys_align, offset );
if ( ptr && size )
VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
return ptr;
}
/**
* Allocate memory with specified physical alignment
*
* @v size Requested size
* @v align Physical alignment
* @ret ptr Memory, or NULL
*
* @c align must be a power of two. @c size may not be zero.
*/
static inline void * __malloc malloc_phys ( size_t size, size_t phys_align ) {
return malloc_phys_offset ( size, phys_align, 0 );
}
/**
* Free memory allocated with malloc_phys()
*
* @v ptr Memory allocated by malloc_phys(), or NULL
* @v size Size of memory, as passed to malloc_phys()
*
* Memory allocated with malloc_phys() can only be freed with
* free_phys(); it cannot be freed with the standard free().
*
* If @c ptr is NULL, no action is taken.
*/
static inline void free_phys ( void *ptr, size_t size ) {
VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
free_memblock ( ptr, size );
}
/** A cache discarder */
struct cache_discarder {
/**
* Discard some cached data
*
* @ret discarded Number of cached items discarded
*/
unsigned int ( * discard ) ( void );
};
/** Cache discarder table */
#define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
/** Declare a cache discarder */
#define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
/** @defgroup cache_cost Cache discarder costs
*
* @{
*/
#define CACHE_CHEAP 01 /**< Items with a low replacement cost */
#define CACHE_NORMAL 02 /**< Items with a normal replacement cost */
#define CACHE_EXPENSIVE 03 /**< Items with a high replacement cost */
/** @} */
#endif /* _IPXE_MALLOC_H */
|