alloc_ops provide interfaces for alloc(), free() and friends, allowing unit tests and common code to use dynamic memory allocation. arch-specific code must provide the implementations. Signed-off-by: Andrew Jones <drjones@xxxxxxxxxx> --- lib/alloc.c | 2 ++ lib/alloc.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 lib/alloc.c create mode 100644 lib/alloc.h diff --git a/lib/alloc.c b/lib/alloc.c new file mode 100644 index 0000000000000..868664b4dcaa3 --- /dev/null +++ b/lib/alloc.c @@ -0,0 +1,2 @@ +#include "alloc.h" +struct alloc_ops alloc_ops; diff --git a/lib/alloc.h b/lib/alloc.h new file mode 100644 index 0000000000000..c8cd61b387a9a --- /dev/null +++ b/lib/alloc.h @@ -0,0 +1,31 @@ +#ifndef _ALLOC_H_ +#define _ALLOC_H_ +#include "libcflat.h" + +struct alloc_ops { + void *(*alloc)(size_t size); + void *(*alloc_aligned)(size_t size, size_t align); + void (*free)(const void *addr); +}; + +extern struct alloc_ops alloc_ops; + +static inline void *alloc(size_t size) +{ + assert(alloc_ops.alloc); + return alloc_ops.alloc(size); +} + +static inline void *alloc_aligned(size_t size, size_t align) +{ + assert(alloc_ops.alloc_aligned); + return alloc_ops.alloc_aligned(size, align); +} + +static inline void free(const void *addr) +{ + assert(alloc_ops.free); + alloc_ops.free(addr); +} + +#endif -- 1.9.3 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html