From patchwork Tue Jun 28 09:01:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Lieven X-Patchwork-Id: 9202335 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7CDE36075F for ; Tue, 28 Jun 2016 09:15:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6B5482793D for ; Tue, 28 Jun 2016 09:15:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D5F0285D1; Tue, 28 Jun 2016 09:15:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 50DDE2793D for ; Tue, 28 Jun 2016 09:15:35 +0000 (UTC) Received: from localhost ([::1]:35229 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bHp7G-0005OT-4w for patchwork-qemu-devel@patchwork.kernel.org; Tue, 28 Jun 2016 05:15:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34868) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bHouT-00072x-HA for qemu-devel@nongnu.org; Tue, 28 Jun 2016 05:02:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bHouN-0003PJ-NH for qemu-devel@nongnu.org; Tue, 28 Jun 2016 05:02:21 -0400 Received: from mx-v6.kamp.de ([2a02:248:0:51::16]:45280 helo=mx01.kamp.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bHouN-0003P4-Dv for qemu-devel@nongnu.org; Tue, 28 Jun 2016 05:02:15 -0400 Received: (qmail 27274 invoked by uid 89); 28 Jun 2016 09:02:14 -0000 Received: from [195.62.97.28] by client-16-kamp (envelope-from , uid 89) with qmail-scanner-2010/03/19-MF (clamdscan: 0.99.2/21804. hbedv: 8.3.40.44/7.12.99.34. avast: 1.2.2/16062800. spamassassin: 3.4.1. Clear:RC:1(195.62.97.28):. Processed in 0.329562 secs); 28 Jun 2016 09:02:14 -0000 Received: from smtp.kamp.de (HELO submission.kamp.de) ([195.62.97.28]) by mx01.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted); 28 Jun 2016 09:02:11 -0000 X-GL_Whitelist: yes Received: (qmail 29660 invoked from network); 28 Jun 2016 09:01:47 -0000 Received: from lieven-pc.kamp-intra.net (HELO lieven-pc) (relay@kamp.de@::ffff:172.21.12.60) by submission.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted) ESMTPA; 28 Jun 2016 09:01:47 -0000 Received: by lieven-pc (Postfix, from userid 1000) id 4C4CD20858; Tue, 28 Jun 2016 11:01:46 +0200 (CEST) From: Peter Lieven To: qemu-devel@nongnu.org Date: Tue, 28 Jun 2016 11:01:29 +0200 Message-Id: <1467104499-27517-6-git-send-email-pl@kamp.de> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1467104499-27517-1-git-send-email-pl@kamp.de> References: <1467104499-27517-1-git-send-email-pl@kamp.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a02:248:0:51::16 Subject: [Qemu-devel] [PATCH 05/15] util: add a helper to mmap private anonymous memory X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, mst@redhat.com, Peter Lieven , dgilbert@redhat.com, mreitz@redhat.com, kraxel@redhat.com, pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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. */ +void *qemu_anon_ram_mmap(size_t size); +void qemu_anon_ram_munmap(void *ptr, size_t size); + #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); + } +}