added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / util / file_mapper.hpp
1 /*************************************************************************
2  *
3  * Copyright 2016 Realm Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  **************************************************************************/
18
19 #ifndef REALM_UTIL_FILE_MAPPER_HPP
20 #define REALM_UTIL_FILE_MAPPER_HPP
21
22 #include <realm/util/file.hpp>
23 #include <realm/utilities.hpp>
24 #include <realm/util/thread.hpp>
25 #include <realm/util/encrypted_file_mapping.hpp>
26
27 namespace realm {
28 namespace util {
29
30 void* mmap(FileDesc fd, size_t size, File::AccessMode access, size_t offset, const char* encryption_key);
31 void munmap(void* addr, size_t size) noexcept;
32 void* mremap(FileDesc fd, size_t file_offset, void* old_addr, size_t old_size, File::AccessMode a, size_t new_size,
33              const char* encryption_key);
34 void msync(FileDesc fd, void* addr, size_t size);
35
36 // A function which may be given to encryption_read_barrier. If present, the read barrier is a
37 // a barrier for a full array. If absent, the read barrier is a barrier only for the address
38 // range give as argument. If the barrier is for a full array, it will read the array header
39 // and determine the address range from the header.
40 using HeaderToSize = size_t (*)(const char* addr);
41 class EncryptedFileMapping;
42
43 #if REALM_ENABLE_ENCRYPTION
44
45
46 // This variant allows the caller to obtain direct access to the encrypted file mapping
47 // for optimization purposes.
48 void* mmap(FileDesc fd, size_t size, File::AccessMode access, size_t offset, const char* encryption_key,
49            EncryptedFileMapping*& mapping);
50
51 void do_encryption_read_barrier(const void* addr, size_t size, HeaderToSize header_to_size,
52                                 EncryptedFileMapping* mapping);
53
54 void do_encryption_write_barrier(const void* addr, size_t size, EncryptedFileMapping* mapping);
55
56 void inline encryption_read_barrier(const void* addr, size_t size, EncryptedFileMapping* mapping,
57                                     HeaderToSize header_to_size = nullptr)
58 {
59     if (mapping)
60         do_encryption_read_barrier(addr, size, header_to_size, mapping);
61 }
62
63 void inline encryption_write_barrier(const void* addr, size_t size, EncryptedFileMapping* mapping)
64 {
65     if (mapping)
66         do_encryption_write_barrier(addr, size, mapping);
67 }
68
69
70 extern util::Mutex& mapping_mutex;
71
72 inline void do_encryption_read_barrier(const void* addr, size_t size, HeaderToSize header_to_size,
73                                        EncryptedFileMapping* mapping)
74 {
75     UniqueLock lock(mapping_mutex, defer_lock_tag());
76     mapping->read_barrier(addr, size, lock, header_to_size);
77 }
78
79 inline void do_encryption_write_barrier(const void* addr, size_t size, EncryptedFileMapping* mapping)
80 {
81     LockGuard lock(mapping_mutex);
82     mapping->write_barrier(addr, size);
83 }
84
85
86 #else
87 void inline encryption_read_barrier(const void*, size_t, EncryptedFileMapping*, HeaderToSize header_to_size = nullptr)
88 {
89     static_cast<void>(header_to_size);
90 }
91 void inline encryption_write_barrier(const void*, size_t)
92 {
93 }
94 void inline encryption_write_barrier(const void*, size_t, EncryptedFileMapping*)
95 {
96 }
97 #endif
98
99 // helpers for encrypted Maps
100 template <typename T>
101 void encryption_read_barrier(File::Map<T>& map, size_t index, size_t num_elements = 1)
102 {
103     T* addr = map.get_addr();
104     encryption_read_barrier(addr + index, sizeof(T) * num_elements, map.get_encrypted_mapping());
105 }
106
107 template <typename T>
108 void encryption_write_barrier(File::Map<T>& map, size_t index, size_t num_elements = 1)
109 {
110     T* addr = map.get_addr();
111     encryption_write_barrier(addr + index, sizeof(T) * num_elements, map.get_encrypted_mapping());
112 }
113
114 File::SizeType encrypted_size_to_data_size(File::SizeType size) noexcept;
115 File::SizeType data_size_to_encrypted_size(File::SizeType size) noexcept;
116
117 size_t round_up_to_page_size(size_t size) noexcept;
118 }
119 }
120 #endif