From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 129152 invoked by alias); 24 Jul 2018 02:18:00 -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 129122 invoked by uid 89); 24 Jul 2018 02:18:00 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-3.3 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=layer X-HELO: gateway24.websitewelcome.com Received: from gateway24.websitewelcome.com (HELO gateway24.websitewelcome.com) (192.185.51.61) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 24 Jul 2018 02:17:58 +0000 Received: from cm15.websitewelcome.com (cm15.websitewelcome.com [100.42.49.9]) by gateway24.websitewelcome.com (Postfix) with ESMTP id 91CB613D8 for ; Mon, 23 Jul 2018 21:17:56 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id hmtTf3595bXuJhmtaf9sTg; Mon, 23 Jul 2018 21:17:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date: References:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=/WcrSuGz+MmEJ5UPF4cPBOMvoGgI0dxZL1hMA7FJZkc=; b=v74HxQulAG7ONb5sUBmzZIX4ms jiIL9iRxXSTekkjuxueCSntK4K5vXApq+0lKFWCLafaBsQl3quu0tt3PrKGJXV+jdUf5ynBBwHhfY mhLp2v8GXhCVqVf7J7s+4r5Xh; Received: from 75-166-85-72.hlrn.qwest.net ([75.166.85.72]:47486 helo=bapiya) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1fhmtS-003hvZ-Ox; Mon, 23 Jul 2018 21:17:42 -0500 From: Tom Tromey To: Simon Marchi Cc: Subject: Re: [PATCH 2/3] Introduce scoped_mmapped_file References: <1531173351-6351-1-git-send-email-simon.marchi@ericsson.com> <1531173351-6351-3-git-send-email-simon.marchi@ericsson.com> Date: Tue, 24 Jul 2018 02:18:00 -0000 In-Reply-To: <1531173351-6351-3-git-send-email-simon.marchi@ericsson.com> (Simon Marchi's message of "Mon, 9 Jul 2018 17:55:50 -0400") Message-ID: <874lgpfkca.fsf@tromey.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-SW-Source: 2018-07/txt/msg00675.txt.bz2 >>>>> "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