From: Simon Marchi <simark@simark.ca>
To: Tom Tromey <tom@tromey.com>, Simon Marchi <simon.marchi@ericsson.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 2/3] Introduce scoped_mmapped_file
Date: Tue, 24 Jul 2018 12:31:00 -0000 [thread overview]
Message-ID: <026bc55b-ee93-2146-17a2-9d03f8b96ac5@simark.ca> (raw)
In-Reply-To: <874lgpfkca.fsf@tromey.com>
On 2018-07-23 10:17 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> 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
next prev parent reply other threads:[~2018-07-24 12:31 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-09 21:56 [PATCH 0/3] Add a DWARF index cache Simon Marchi
2018-07-09 21:56 ` [PATCH 1/3] Make index reading functions more modular Simon Marchi
2018-07-25 16:51 ` Tom Tromey
2018-07-09 21:56 ` [PATCH 2/3] Introduce scoped_mmapped_file Simon Marchi
2018-07-24 2:18 ` Tom Tromey
2018-07-24 12:31 ` Simon Marchi [this message]
2018-07-09 21:57 ` [PATCH 3/3] Add DWARF index cache Simon Marchi
2018-07-17 19:48 ` Tom Tromey
2018-07-17 20:48 ` Simon Marchi
2018-07-18 18:20 ` Tom Tromey
2018-07-25 18:48 ` Tom Tromey
2018-07-25 20:20 ` Simon Marchi
2018-07-26 12:38 ` Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=026bc55b-ee93-2146-17a2-9d03f8b96ac5@simark.ca \
--to=simark@simark.ca \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@ericsson.com \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox