• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_MISC_MMAPPED_FILE_H
25 #define ARM_COMPUTE_MISC_MMAPPED_FILE_H
26 
27 #if !defined(_WIN64) && !defined(BARE_METAL)
28 
29 #include <string>
30 #include <utility>
31 
32 namespace arm_compute
33 {
34 namespace utils
35 {
36 namespace mmap_io
37 {
38 /** Memory mapped file class */
39 class MMappedFile
40 {
41 public:
42     /** Constructor */
43     MMappedFile();
44     /** Constructor
45      *
46      * @note file will be created if it doesn't exist.
47      *
48      * @param[in] filename File to be mapped, if doesn't exist will be created.
49      * @param[in] size     Size of file to map
50      * @param[in] offset   Offset to mapping point, should be multiple of page size
51      */
52     MMappedFile(std::string filename, size_t size, size_t offset);
53     /** Prevent instances of this class from being copied (As this class contains pointers) */
54     MMappedFile(const MMappedFile &) = delete;
55     /** Default move constructor */
56     MMappedFile(MMappedFile &&) = default;
57     /** Prevent instances of this class from being copied (As this class contains pointers) */
58     MMappedFile &operator=(const MMappedFile &) = delete;
59     /** Default move assignment operator */
60     MMappedFile &operator=(MMappedFile &&) = default;
61     /** Destructor */
62     ~MMappedFile();
63     /** Opens and maps a file
64      *
65      * @note file will be created if it doesn't exist.
66      *
67      * @param[in] filename File to be mapped, if doesn't exist will be created.
68      * @param[in] size     Size of file to map. If 0 all the file will be mapped.
69      * @param[in] offset   Offset to mapping point, should be multiple of page size.
70      *
71      * @return True if operation was successful else false
72      */
73     bool map(const std::string &filename, size_t size, size_t offset);
74     /** Unmaps and closes file */
75     void release();
76     /** Mapped data accessor
77      *
78      * @return Pointer to the mapped data, nullptr if not mapped
79      */
80     unsigned char *data();
81     /** File size accessor
82      *
83      * @return Size of file
84      */
85     size_t file_size() const;
86     /** Map size accessor
87      *
88      * @return Mapping size
89      */
90     size_t map_size() const;
91     /** Checks if file mapped
92      *
93      * @return True if file is mapped else false
94      */
95     bool is_mapped() const;
96 
97 private:
98     std::string _filename;
99     size_t      _file_size;
100     size_t      _map_size;
101     size_t      _map_offset;
102     FILE       *_fp;
103     void       *_data;
104 };
105 } // namespace mmap_io
106 } // namespace utils
107 } // namespace arm_compute
108 #endif // !defined(_WIN64) &&!defined(BARE_METAL)
109 
110 #endif /* ARM_COMPUTE_MISC_MMAPPED_FILE_H */
111