aboutsummaryrefslogtreecommitdiffstats
path: root/xen/include/xen/xmalloc.h
diff options
context:
space:
mode:
authorKeir Fraser <keir.fraser@citrix.com>2008-10-16 15:46:04 +0100
committerKeir Fraser <keir.fraser@citrix.com>2008-10-16 15:46:04 +0100
commit7c54283cf2973b7285d5ebf70ecc0ecc5864edf7 (patch)
tree0f238d538c8c5a531a37326bb8edcec89f659100 /xen/include/xen/xmalloc.h
parent9736b76d829b2dba07e9941978bdf132fa118e18 (diff)
downloadxen-7c54283cf2973b7285d5ebf70ecc0ecc5864edf7.tar.gz
xen-7c54283cf2973b7285d5ebf70ecc0ecc5864edf7.tar.bz2
xen-7c54283cf2973b7285d5ebf70ecc0ecc5864edf7.zip
xmalloc: Add pooled allocator interface.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
Diffstat (limited to 'xen/include/xen/xmalloc.h')
-rw-r--r--xen/include/xen/xmalloc.h81
1 files changed, 78 insertions, 3 deletions
diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h
index dbfdce4fa4..e41cc36e74 100644
--- a/xen/include/xen/xmalloc.h
+++ b/xen/include/xen/xmalloc.h
@@ -2,11 +2,16 @@
#ifndef __XMALLOC_H__
#define __XMALLOC_H__
+/*
+ * Xen malloc/free-style interface.
+ */
+
/* Allocate space for typed object. */
#define xmalloc(_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))
/* Allocate space for array of typed objects. */
-#define xmalloc_array(_type, _num) ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))
+#define xmalloc_array(_type, _num) \
+ ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))
/* Allocate untyped storage. */
#define xmalloc_bytes(_bytes) (_xmalloc(_bytes, SMP_CACHE_BYTES))
@@ -15,8 +20,9 @@
extern void xfree(void *);
/* Underlying functions */
-extern void *_xmalloc(size_t size, size_t align);
-static inline void *_xmalloc_array(size_t size, size_t align, size_t num)
+extern void *_xmalloc(unsigned long size, unsigned long align);
+static inline void *_xmalloc_array(
+ unsigned long size, unsigned long align, unsigned long num)
{
/* Check for overflow. */
if (size && num > UINT_MAX / size)
@@ -24,4 +30,73 @@ static inline void *_xmalloc_array(size_t size, size_t align, size_t num)
return _xmalloc(size * num, align);
}
+/*
+ * Pooled allocator interface.
+ */
+
+struct xmem_pool;
+
+typedef void *(xmem_pool_get_memory)(unsigned long bytes);
+typedef void (xmem_pool_put_memory)(void *ptr);
+
+/**
+ * xmem_pool_create - create dynamic memory pool
+ * @name: name of the pool
+ * @get_mem: callback function used to expand pool
+ * @put_mem: callback function used to shrink pool
+ * @init_size: inital pool size (in bytes)
+ * @max_size: maximum pool size (in bytes) - set this as 0 for no limit
+ * @grow_size: amount of memory (in bytes) added to pool whenever required
+ *
+ * All size values are rounded up to next page boundary.
+ */
+struct xmem_pool *xmem_pool_create(
+ const char *name,
+ xmem_pool_get_memory get_mem,
+ xmem_pool_put_memory put_mem,
+ unsigned long init_size,
+ unsigned long max_size,
+ unsigned long grow_size);
+
+/**
+ * xmem_pool_destroy - cleanup given pool
+ * @mem_pool: Pool to be destroyed
+ *
+ * Data structures associated with pool are freed.
+ * All memory allocated from pool must be freed before
+ * destorying it.
+ */
+void xmem_pool_destroy(struct xmem_pool *pool);
+
+/**
+ * xmem_pool_alloc - allocate memory from given pool
+ * @size: no. of bytes
+ * @mem_pool: pool to allocate from
+ */
+void *xmem_pool_alloc(unsigned long size, struct xmem_pool *pool);
+
+/**
+ * xmem_pool_free - free memory from given pool
+ * @ptr: address of memory to be freed
+ * @mem_pool: pool to free from
+ */
+void xmem_pool_free(void *ptr, struct xmem_pool *pool);
+
+/**
+ * xmem_pool_get_used_size - get memory currently used by given pool
+ *
+ * Used memory includes stored data + metadata + internal fragmentation
+ */
+unsigned long xmem_pool_get_used_size(struct xmem_pool *pool);
+
+/**
+ * xmem_pool_get_total_size - get total memory currently allocated for pool
+ *
+ * This is the total memory currently allocated for this pool which includes
+ * used size + free size.
+ *
+ * (Total - Used) is good indicator of memory efficiency of allocator.
+ */
+unsigned long xmem_pool_get_total_size(struct xmem_pool *pool);
+
#endif /* __XMALLOC_H__ */