1 
2 #ifndef __XMALLOC_H__
3 #define __XMALLOC_H__
4 
5 #include <xen/types.h>
6 #include <xen/cache.h>
7 
8 /*
9  * Xen malloc/free-style interface.
10  */
11 
12 /* Allocate space for typed object. */
13 #define xmalloc(_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))
14 #define xzalloc(_type) ((_type *)_xzalloc(sizeof(_type), __alignof__(_type)))
15 
16 /* Allocate space for array of typed objects. */
17 #define xmalloc_array(_type, _num) \
18     ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))
19 #define xzalloc_array(_type, _num) \
20     ((_type *)_xzalloc_array(sizeof(_type), __alignof__(_type), _num))
21 
22 /* Allocate untyped storage. */
23 #define xmalloc_bytes(_bytes) _xmalloc(_bytes, SMP_CACHE_BYTES)
24 #define xzalloc_bytes(_bytes) _xzalloc(_bytes, SMP_CACHE_BYTES)
25 
26 /* Free any of the above. */
27 extern void xfree(void *);
28 
29 /* Underlying functions */
30 extern void *_xmalloc(unsigned long size, unsigned long align);
31 extern void *_xzalloc(unsigned long size, unsigned long align);
32 
_xmalloc_array(unsigned long size,unsigned long align,unsigned long num)33 static inline void *_xmalloc_array(
34     unsigned long size, unsigned long align, unsigned long num)
35 {
36     /* Check for overflow. */
37     if ( size && num > UINT_MAX / size )
38         return NULL;
39     return _xmalloc(size * num, align);
40 }
41 
_xzalloc_array(unsigned long size,unsigned long align,unsigned long num)42 static inline void *_xzalloc_array(
43     unsigned long size, unsigned long align, unsigned long num)
44 {
45     /* Check for overflow. */
46     if ( size && num > UINT_MAX / size )
47         return NULL;
48     return _xzalloc(size * num, align);
49 }
50 
51 /*
52  * Pooled allocator interface.
53  */
54 
55 struct xmem_pool;
56 
57 typedef void *(xmem_pool_get_memory)(unsigned long bytes);
58 typedef void (xmem_pool_put_memory)(void *ptr);
59 
60 /**
61  * xmem_pool_create - create dynamic memory pool
62  * @name: name of the pool
63  * @get_mem: callback function used to expand pool
64  * @put_mem: callback function used to shrink pool
65  * @init_size: inital pool size (in bytes)
66  * @max_size: maximum pool size (in bytes) - set this as 0 for no limit
67  * @grow_size: amount of memory (in bytes) added to pool whenever required
68  *
69  * All size values are rounded up to next page boundary.
70  */
71 struct xmem_pool *xmem_pool_create(
72     const char *name,
73     xmem_pool_get_memory get_mem,
74     xmem_pool_put_memory put_mem,
75     unsigned long init_size,
76     unsigned long max_size,
77     unsigned long grow_size);
78 
79 /**
80  * xmem_pool_destroy - cleanup given pool
81  * @mem_pool: Pool to be destroyed
82  *
83  * Data structures associated with pool are freed.
84  * All memory allocated from pool must be freed before
85  * destorying it.
86  */
87 void xmem_pool_destroy(struct xmem_pool *pool);
88 
89 /**
90  * xmem_pool_alloc - allocate memory from given pool
91  * @size: no. of bytes
92  * @mem_pool: pool to allocate from
93  */
94 void *xmem_pool_alloc(unsigned long size, struct xmem_pool *pool);
95 
96 /**
97  * xmem_pool_maxalloc - xmem_pool_alloc's greater than this size will fail
98  * @mem_pool: pool
99  */
100 int xmem_pool_maxalloc(struct xmem_pool *pool);
101 
102 /**
103  * xmem_pool_maxsize -
104  * @ptr: address of memory to be freed
105  * @mem_pool: pool to free from
106  */
107 void xmem_pool_free(void *ptr, struct xmem_pool *pool);
108 
109 /**
110  * xmem_pool_get_used_size - get memory currently used by given pool
111  *
112  * Used memory includes stored data + metadata + internal fragmentation
113  */
114 unsigned long xmem_pool_get_used_size(struct xmem_pool *pool);
115 
116 /**
117  * xmem_pool_get_total_size - get total memory currently allocated for pool
118  *
119  * This is the total memory currently allocated for this pool which includes
120  * used size + free size.
121  *
122  * (Total - Used) is good indicator of memory efficiency of allocator.
123  */
124 unsigned long xmem_pool_get_total_size(struct xmem_pool *pool);
125 
126 #endif /* __XMALLOC_H__ */
127