ia64/xen-unstable

view xen/include/xen/xmalloc.h @ 18646:bb1a67a7db26

xmalloc: Add pooled allocator interface.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Oct 16 15:46:04 2008 +0100 (2008-10-16)
parents 3e2d3d737624
children f210a633571c
line source
2 #ifndef __XMALLOC_H__
3 #define __XMALLOC_H__
5 /*
6 * Xen malloc/free-style interface.
7 */
9 /* Allocate space for typed object. */
10 #define xmalloc(_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))
12 /* Allocate space for array of typed objects. */
13 #define xmalloc_array(_type, _num) \
14 ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))
16 /* Allocate untyped storage. */
17 #define xmalloc_bytes(_bytes) (_xmalloc(_bytes, SMP_CACHE_BYTES))
19 /* Free any of the above. */
20 extern void xfree(void *);
22 /* Underlying functions */
23 extern void *_xmalloc(unsigned long size, unsigned long align);
24 static inline void *_xmalloc_array(
25 unsigned long size, unsigned long align, unsigned long num)
26 {
27 /* Check for overflow. */
28 if (size && num > UINT_MAX / size)
29 return NULL;
30 return _xmalloc(size * num, align);
31 }
33 /*
34 * Pooled allocator interface.
35 */
37 struct xmem_pool;
39 typedef void *(xmem_pool_get_memory)(unsigned long bytes);
40 typedef void (xmem_pool_put_memory)(void *ptr);
42 /**
43 * xmem_pool_create - create dynamic memory pool
44 * @name: name of the pool
45 * @get_mem: callback function used to expand pool
46 * @put_mem: callback function used to shrink pool
47 * @init_size: inital pool size (in bytes)
48 * @max_size: maximum pool size (in bytes) - set this as 0 for no limit
49 * @grow_size: amount of memory (in bytes) added to pool whenever required
50 *
51 * All size values are rounded up to next page boundary.
52 */
53 struct xmem_pool *xmem_pool_create(
54 const char *name,
55 xmem_pool_get_memory get_mem,
56 xmem_pool_put_memory put_mem,
57 unsigned long init_size,
58 unsigned long max_size,
59 unsigned long grow_size);
61 /**
62 * xmem_pool_destroy - cleanup given pool
63 * @mem_pool: Pool to be destroyed
64 *
65 * Data structures associated with pool are freed.
66 * All memory allocated from pool must be freed before
67 * destorying it.
68 */
69 void xmem_pool_destroy(struct xmem_pool *pool);
71 /**
72 * xmem_pool_alloc - allocate memory from given pool
73 * @size: no. of bytes
74 * @mem_pool: pool to allocate from
75 */
76 void *xmem_pool_alloc(unsigned long size, struct xmem_pool *pool);
78 /**
79 * xmem_pool_free - free memory from given pool
80 * @ptr: address of memory to be freed
81 * @mem_pool: pool to free from
82 */
83 void xmem_pool_free(void *ptr, struct xmem_pool *pool);
85 /**
86 * xmem_pool_get_used_size - get memory currently used by given pool
87 *
88 * Used memory includes stored data + metadata + internal fragmentation
89 */
90 unsigned long xmem_pool_get_used_size(struct xmem_pool *pool);
92 /**
93 * xmem_pool_get_total_size - get total memory currently allocated for pool
94 *
95 * This is the total memory currently allocated for this pool which includes
96 * used size + free size.
97 *
98 * (Total - Used) is good indicator of memory efficiency of allocator.
99 */
100 unsigned long xmem_pool_get_total_size(struct xmem_pool *pool);
102 #endif /* __XMALLOC_H__ */