RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
MMapReader.cpp
Go to the documentation of this file.
1
/*
2
RawSpeed - RAW file decoder.
3
4
Copyright (C) 2025 Roman Lebedev
5
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
10
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Lesser General Public License for more details.
15
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
#if !defined(_WIN32)
22
23
#include "
io/MMapReader.h
"
24
#include "
adt/Array1DRef.h
"
25
#include "
adt/Casts.h
"
26
#include "
io/Buffer.h
"
27
#include "
io/FileIOException.h
"
28
#include <cstdint>
29
#include <fcntl.h>
30
#include <limits>
31
#include <string>
32
#include <sys/mman.h>
33
#include <sys/stat.h>
34
#include <unistd.h>
35
36
namespace
rawspeed
{
37
38
MMapReader::MMapReader
(
const
std::string& fname)
39
:
fd
(open(fname.c_str(), O_RDONLY)) {
40
41
if
(
fd
== -1)
42
ThrowFIE
(
"Could not open file \"%s\"."
, fname.c_str());
43
44
struct
stat sb;
45
if (fstat(
fd
, &sb) == -1)
46
ThrowFIE
(
"Could not obtain the file size"
);
47
48
length
= sb.st_size;
49
50
addr
= mmap(
nullptr
,
length
, PROT_READ, MAP_PRIVATE,
fd
, 0);
51
if
(
addr
== MAP_FAILED)
52
ThrowFIE
(
"Could not mmap the file"
);
53
}
54
55
Buffer
MMapReader::getAsBuffer
()
const
{
56
if
(
static_cast<
int64_t
>
(
length
) >
57
std::numeric_limits<Buffer::size_type>::max())
58
ThrowFIE
(
"File is too big (%zu bytes)."
,
length
);
59
return
Array1DRef
(
static_cast<
const
uint8_t*
>
(
addr
),
60
implicit_cast<Buffer::size_type>
(
length
));
61
}
62
63
MMapReader::~MMapReader
() {
64
munmap(
addr
,
length
);
65
close(
fd
);
66
}
67
68
}
// namespace rawspeed
69
70
#endif
// !defined(_WIN32)
Array1DRef.h
Buffer.h
Casts.h
FileIOException.h
ThrowFIE
#define ThrowFIE(...)
Definition
FileIOException.h:38
MMapReader.h
rawspeed::Buffer
Definition
Buffer.h:47
rawspeed::MMapReader::getAsBuffer
Buffer getAsBuffer() const
Definition
MMapReader.cpp:55
rawspeed::MMapReader::addr
void * addr
Definition
MMapReader.h:33
rawspeed::MMapReader::fd
int fd
Definition
MMapReader.h:32
rawspeed::MMapReader::~MMapReader
~MMapReader()
Definition
MMapReader.cpp:63
rawspeed::MMapReader::length
size_t length
Definition
MMapReader.h:34
rawspeed::MMapReader::MMapReader
MMapReader(const std::string &fname)
Definition
MMapReader.cpp:38
rawspeed
Definition
CoalescingOutputIteratorBenchmark.cpp:35
rawspeed::implicit_cast
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition
Casts.h:32
rawspeed::Array1DRef
Array1DRef(T *data_, int numElts_) -> Array1DRef< T >
librawspeed
io
MMapReader.cpp
Generated by
1.15.0