From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 82638 invoked by alias); 24 Jul 2018 12:31:42 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 82625 invoked by uid 89); 24 Jul 2018 12:31:41 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy=UD:ca X-HELO: simark.ca Received: from simark.ca (HELO simark.ca) (158.69.221.121) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 24 Jul 2018 12:31:35 +0000 Received: from [10.0.0.11] (unknown [192.222.164.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 511F01E4AE; Tue, 24 Jul 2018 08:31:33 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=simark.ca; s=mail; t=1532435493; bh=O9cQzdtrkiKCw3/XZ2w9uF38b1p39Dq9HkQ1amWX69E=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=iMdhoHnOIROit4y5cYNU/jLB7KznuZb8hGV7gjBdZ8lAE7BU7HuYyQDKGfgFUC/P4 gQSu0CpculF4YHWtfQI6YldU12hZbcD17k7lP7wN8QdjyvS4DE4IgWXXovZG+LhXfU E53F+eSI0J6831uBu0g79JMqSQxPvMXcwdwCCK0Q= Subject: Re: [PATCH 2/3] Introduce scoped_mmapped_file To: Tom Tromey , Simon Marchi Cc: gdb-patches@sourceware.org References: <1531173351-6351-1-git-send-email-simon.marchi@ericsson.com> <1531173351-6351-3-git-send-email-simon.marchi@ericsson.com> <874lgpfkca.fsf@tromey.com> From: Simon Marchi Message-ID: <026bc55b-ee93-2146-17a2-9d03f8b96ac5@simark.ca> Date: Tue, 24 Jul 2018 12:31:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <874lgpfkca.fsf@tromey.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-SW-Source: 2018-07/txt/msg00679.txt.bz2 On 2018-07-23 10:17 PM, Tom Tromey wrote: >>>>>> "Simon" == Simon Marchi writes: > > Simon> We already have scoped_mmap, which can is a thin RAII layer over mmap. > Simon> If one simply wants to mmap an entire file for reading, it takes a bit > Simon> of boilerplate. This patch introduces the scoped_mmapped_file class to > Simon> make this easier. > > Simon> + /* Map FILENAME in memory. Throw an error if anything goes wrong. */ > Simon> + scoped_mmapped_file (const char *filename) > Simon> + { > Simon> + m_fd.reset (open (filename, O_RDONLY)); > Simon> + if (m_fd.get () < 0) > Simon> + perror_with_name ("open"); > Simon> + > Simon> + off_t size = lseek (m_fd.get (), 0, SEEK_END); > Simon> + if (size < 0) > Simon> + perror_with_name ("lseek"); > Simon> + > Simon> + /* We can't map an empty file. */ > Simon> + if (size == 0) > Simon> + error (_("file to mmap is empty")); > Simon> + > Simon> + m_mmap.reset (nullptr, size, PROT_READ, MAP_PRIVATE, m_fd.get (), 0); > Simon> + if (m_mmap.get () == MAP_FAILED) > Simon> + perror_with_name ("mmap"); > > It seems to me that there's no need to keep the fd open after the mmap, > so this could be done by just having the scoped_fd as a local variable, > and then I suppose the scoped_fd changes wouldn't be needed either. > > Tom > Ah, I didn't know the fd didn't need to be kept open, but you're right: https://stackoverflow.com/questions/17490033/do-i-need-to-keep-a-file-open-after-calling-mmap-on-it I'll try that. Simon