xref: /aosp_15_r20/external/icing/icing/legacy/index/icing-mmapper.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Copyright 2012 Google Inc. All Rights Reserved.
16 // Author: [email protected] (Scott Banachowski)
17 //
18 // This class is a helper for mmapping a file.
19 // Use as a scoped allocator, the memory is mapped
20 // on construction and released on destruction.
21 
22 #ifndef ICING_LEGACY_INDEX_ICING_MMAPPER_H_
23 #define ICING_LEGACY_INDEX_ICING_MMAPPER_H_
24 
25 #include <unistd.h>
26 
27 #include <cstddef>
28 #include <cstdint>
29 
30 namespace icing {
31 namespace lib {
32 
33 class IcingMMapper {
34  public:
35   // Provide a valid, open file description (with matching permissions
36   // for read or write).  The location into the file you wish to map,
37   // and the size. "flags" are passed in directly to mmap.
38   IcingMMapper(int fd, bool read_only, uint64_t location, size_t size,
39                int flags);
40 
41   // Set up Mmapper, but delay mapping until Remap is called.
42   IcingMMapper(bool read_only, int flags);
43 
44   // Will unmap the region on delete.  Does not close the file.
45   ~IcingMMapper();
46 
47   // Move the location of the mapping to a new location.  Returns
48   // true if valid.
49   bool Remap(int fd, uint64_t location, size_t size);
50 
51   // Close the mapping and become invalid.
52   void Unmap();
53 
54   // Sync the mapped region to the filesystem.
55   bool Sync();
56 
57   // Check to see if the file was successfully mapped.
is_valid()58   bool is_valid() const { return (address_ != nullptr); }
59 
60   // The address in memory of the mapped file, returns NULL if the
61   // mapping of the region was unsuccesful.
address()62   const uint8_t *address() const { return address_; }
63 
address()64   uint8_t *address() { return address_; }
65 
len()66   size_t len() const { return len_; }
67 
location()68   uint64_t location() const { return location_; }
69 
system_page_size()70   static size_t __attribute__((const)) system_page_size() {
71     static const size_t page_size = sysconf(_SC_PAGE_SIZE);
72     return page_size;
73   }
74 
75   // Rounds `size` up to a multiple of the system page size.
page_aligned_size(uint32_t size)76   static size_t page_aligned_size(uint32_t size) {
77     return ((size + system_page_size() - 1) / system_page_size()) *
78            system_page_size();
79   }
80 
81  private:
82   void DoMapping(int fd, uint64_t location, size_t size);
83 
84   uint8_t *address_;
85   size_t len_;         // the requested mapping length
86   const int flags_;    // flags passed in to mmap
87   uint64_t location_;  // the requested mapping file location
88   size_t mmap_len_;    // the actual mapping length
89   void *mmap_result_;
90   const bool read_only_;
91 };
92 
93 }  // namespace lib
94 }  // namespace icing
95 
96 #endif  // ICING_LEGACY_INDEX_ICING_MMAPPER_H_
97