From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paolo Bonzini Subject: Re: [PATCH v6 06/17] Introduce alloc_ops Date: Fri, 11 Jul 2014 10:40:42 +0200 Message-ID: <53BFA30A.6040602@redhat.com> References: <1405066787-5793-1-git-send-email-drjones@redhat.com> <1405066787-5793-7-git-send-email-drjones@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit Cc: christoffer.dall@linaro.org To: Andrew Jones , kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:7756 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751469AbaGKIks (ORCPT ); Fri, 11 Jul 2014 04:40:48 -0400 In-Reply-To: <1405066787-5793-7-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Il 11/07/2014 10:19, Andrew Jones ha scritto: > 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 > Why do you need the wrappers? Could you just have lib/malloc.c define the three functions, with interface in lib/stdlib.h and lib/memregion.h? Also, please call them with the "right" C names: void *malloc(size_t size); void free(void *ptr); void *calloc(size_t nmemb, size_t size); void *memalign(size_t alignment, size_t size); Only calloc should do the memset. Later, x86 could also add a memregion_init call and be able to use malloc/free. Paolo