aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/io/mapped_file_input.cpp
blob: 3ec08c449743561c1e6c1eb9d00090eab10bc7fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "mapped_file_input.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>

namespace vespalib {

MappedFileInput::MappedFileInput(const vespalib::string &file_name)
    : _fd(open(file_name.c_str(), O_RDONLY)),
      _data((char *)MAP_FAILED),
      _size(0),
      _used(0)
{
    struct stat info;
    if ((_fd != -1) && fstat(_fd, &info) == 0) {
        _data = static_cast<char*>(mmap(0, info.st_size, PROT_READ, MAP_SHARED, _fd, 0));
        if (_data != MAP_FAILED) {
            _size = info.st_size;
            madvise(_data, _size, MADV_SEQUENTIAL);
#ifdef __linux__
            madvise(_data, _size, MADV_DONTDUMP);
#endif
        }
    }
}

MappedFileInput::~MappedFileInput()
{
    if (valid()) {
        munmap(_data, _size);
    }
    if (_fd != -1) {
        close(_fd);
    }
}

bool MappedFileInput::valid() const
{
    return (_data != MAP_FAILED);
}

Memory
MappedFileInput::obtain()
{
    return Memory((_data + _used), (_size - _used));
}

Input &
MappedFileInput::evict(size_t bytes)
{
    _used += bytes;
    return *this;
}

} // namespace vespalib