From: Andrew Jones Subject: [PATCH v6 06/17] Introduce alloc_ops Date: Fri, 11 Jul 2014 10:19:36 +0200 Message-ID: <1405066787-5793-7-git-send-email-drjones@redhat.com> References: <1405066787-5793-1-git-send-email-drjones@redhat.com> Cc: christoffer.dall@linaro.org, pbonzini@redhat.com To: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:64015 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751274AbaGKIUG (ORCPT ); Fri, 11 Jul 2014 04:20:06 -0400 In-Reply-To: <1405066787-5793-1-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: 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 --- 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