aboutsummaryrefslogtreecommitdiff
path: root/src/core/utils/misc/MMappedFile.cpp
blob: 6d0b0bed6a02e9b005d56000f1ea1e0d833f4d32 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 2019 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#if !defined(BARE_METAL)

#include "arm_compute/core/utils/misc/MMappedFile.h"

#include <cstdio>
#include <cstring>
#include <tuple>

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

namespace arm_compute
{
namespace utils
{
namespace mmap_io
{
namespace
{
/** File size accessor
 *
 * @param[in] filename File to extract its size
 *
 * @return A pair of size and status.
 */
std::pair<size_t, bool> get_file_size(const std::string &filename)
{
    struct stat st; // NOLINT
    memset(&st, 0, sizeof(struct stat));
    if(stat(filename.c_str(), &st) == 0)
    {
        return std::make_pair(st.st_size, true);
    }
    else
    {
        return std::make_pair(0, false);
    }
}

/** Get OS page size
 *
 * @return Page size
 */
size_t get_page_size()
{
    return sysconf(_SC_PAGESIZE);
}
} // namespace

MMappedFile::MMappedFile()
    : _filename(), _file_size(0), _map_size(0), _map_offset(0), _fp(nullptr), _data(nullptr)
{
}

MMappedFile::MMappedFile(std::string filename, size_t size, size_t offset)
    : _filename(std::move(filename)), _file_size(0), _map_size(size), _map_offset(offset), _fp(nullptr), _data(nullptr)
{
    map(_filename, _map_size, _map_offset);
}

MMappedFile::~MMappedFile()
{
    release();
}

bool MMappedFile::map(const std::string &filename, size_t size, size_t offset)
{
    // Check if file is mapped
    if(is_mapped())
    {
        return false;
    }

    // Open file
    _fp = fopen(filename.c_str(), "a+be");
    if(_fp == nullptr)
    {
        return false;
    }

    // Extract file descriptor
    int  fd     = fileno(_fp);
    bool status = fd >= 0;
    if(status)
    {
        // Get file size
        std::tie(_file_size, status) = get_file_size(_filename);

        if(status)
        {
            // Map all file from offset if map size is 0
            _map_size   = (size == 0) ? _file_size : size;
            _map_offset = offset;

            // Check offset mapping
            if((_map_offset > _file_size) || (_map_offset % get_page_size() != 0))
            {
                status = false;
            }
            else
            {
                // Truncate to file size
                if(_map_offset + _map_size > _file_size)
                {
                    _map_size = _file_size - _map_offset;
                }

                // Perform mapping
                _data = ::mmap(nullptr, _map_size, PROT_WRITE, MAP_SHARED, fd, _map_offset);
            }
        }
    }

    if(!status)
    {
        fclose(_fp);
    }

    return status;
}

void MMappedFile::release()
{
    // Unmap file
    if(_data != nullptr)
    {
        ::munmap(_data, _file_size);
        _data = nullptr;
    }

    // Close file
    if(_fp != nullptr)
    {
        fclose(_fp);
        _fp = nullptr;
    }

    // Clear variables
    _file_size  = 0;
    _map_size   = 0;
    _map_offset = 0;
}

unsigned char *MMappedFile::data()
{
    return static_cast<unsigned char *>(_data);
}

size_t MMappedFile::file_size() const
{
    return _file_size;
}

size_t MMappedFile::map_size() const
{
    return _map_size;
}

bool MMappedFile::is_mapped() const
{
    return _data != nullptr;
}
} // namespace mmap_io
} // namespace utils
} // namespace arm_compute
#endif // !defined(BARE_METAL)