From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41470) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bvauZ-00045j-BK for qemu-devel@nongnu.org; Sat, 15 Oct 2016 22:10:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bvauW-00082c-6r for qemu-devel@nongnu.org; Sat, 15 Oct 2016 22:10:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46258) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1bvauW-00081u-0p for qemu-devel@nongnu.org; Sat, 15 Oct 2016 22:10:48 -0400 Date: Sun, 16 Oct 2016 05:10:44 +0300 From: "Michael S. Tsirkin" Message-ID: <20161016050644-mutt-send-email-mst@kernel.org> References: <1467104499-27517-1-git-send-email-pl@kamp.de> <1467104499-27517-6-git-send-email-pl@kamp.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1467104499-27517-6-git-send-email-pl@kamp.de> Subject: Re: [Qemu-devel] [PATCH 05/15] util: add a helper to mmap private anonymous memory List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Lieven Cc: qemu-devel@nongnu.org, kwolf@redhat.com, mreitz@redhat.com, pbonzini@redhat.com, dgilbert@redhat.com, peter.maydell@linaro.org, kraxel@redhat.com On Tue, Jun 28, 2016 at 11:01:29AM +0200, Peter Lieven wrote: > Signed-off-by: Peter Lieven > --- > include/qemu/mmap-alloc.h | 6 ++++++ > util/mmap-alloc.c | 17 +++++++++++++++++ > 2 files changed, 23 insertions(+) > > diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h > index 0899b2f..a457721 100644 > --- a/include/qemu/mmap-alloc.h > +++ b/include/qemu/mmap-alloc.h > @@ -9,4 +9,10 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared); > > void qemu_ram_munmap(void *ptr, size_t size); > > +/* qemu_anon_ram_mmap maps private anonymous memory using mmap and > + * aborts if the allocation fails. its meant to act as an replacement > + * for g_malloc0 and friends. */ This needs better docs. When should one use g_malloc0 and when qemu_anon_ram_munmap? > +void *qemu_anon_ram_mmap(size_t size); > +void qemu_anon_ram_munmap(void *ptr, size_t size); > + The names are confusing - this isn't guest RAM, this is just internal QEMU memory, isn't it? Just rename it qemu_malloc0 then ... > #endif > diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c > index 629d97a..c099858 100644 > --- a/util/mmap-alloc.c > +++ b/util/mmap-alloc.c > @@ -107,3 +107,20 @@ void qemu_ram_munmap(void *ptr, size_t size) > munmap(ptr, size + getpagesize()); > } > } > + > +void *qemu_anon_ram_mmap(size_t size) > +{ > + void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, > + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); > + if (ptr == MAP_FAILED) { > + abort(); > + } > + return ptr; > +} > + > +void qemu_anon_ram_munmap(void *ptr, size_t size) > +{ > + if (ptr) { > + munmap(ptr, size); > + } > +} > -- > 1.9.1