OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_FILES_MEMORY_MAPPED_FILE_H_ | 5 #ifndef BASE_FILES_MEMORY_MAPPED_FILE_H_ |
6 #define BASE_FILES_MEMORY_MAPPED_FILE_H_ | 6 #define BASE_FILES_MEMORY_MAPPED_FILE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 | 15 |
16 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
17 #include <windows.h> | 17 #include <windows.h> |
18 #endif | 18 #endif |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 class FilePath; | 22 class FilePath; |
23 | 23 |
24 class BASE_EXPORT MemoryMappedFile { | 24 class BASE_EXPORT MemoryMappedFile { |
25 public: | 25 public: |
| 26 enum Access { |
| 27 // Mapping a file into memory effectively allows for file I/O on any thread. |
| 28 // The accessing thread could be paused while data from the file is paged |
| 29 // into memory. Worse, a corrupted filesystem could cause a SEGV within the |
| 30 // program instead of just an I/O error. |
| 31 READ_ONLY, |
| 32 |
| 33 // This provides read/write access to a file and must be used with care of |
| 34 // the additional subtleties involved in doing so. Though the OS will do |
| 35 // the writing of data on its own time, too many dirty pages can cause |
| 36 // the OS to pause the thread while it writes them out. The pause can |
| 37 // be as much as 1s on some systems. |
| 38 READ_WRITE, |
| 39 |
| 40 // This provides read/write access but with the ability to write beyond |
| 41 // the end of the existing file up to a maximum size specified as the |
| 42 // "region". Depending on the OS, the file may or may not be immediately |
| 43 // extended to the maximum size though it won't be loaded in RAM until |
| 44 // needed. Note, however, that the maximum size will still be reserved |
| 45 // in the process address space. |
| 46 READ_WRITE_EXTEND, |
| 47 }; |
| 48 |
26 // The default constructor sets all members to invalid/null values. | 49 // The default constructor sets all members to invalid/null values. |
27 MemoryMappedFile(); | 50 MemoryMappedFile(); |
28 ~MemoryMappedFile(); | 51 ~MemoryMappedFile(); |
29 | 52 |
30 // Used to hold information about a region [offset + size] of a file. | 53 // Used to hold information about a region [offset + size] of a file. |
31 struct BASE_EXPORT Region { | 54 struct BASE_EXPORT Region { |
32 static const Region kWholeFile; | 55 static const Region kWholeFile; |
33 | 56 |
34 bool operator==(const Region& other) const; | 57 bool operator==(const Region& other) const; |
35 bool operator!=(const Region& other) const; | 58 bool operator!=(const Region& other) const; |
36 | 59 |
37 // Start of the region (measured in bytes from the beginning of the file). | 60 // Start of the region (measured in bytes from the beginning of the file). |
38 int64_t offset; | 61 int64_t offset; |
39 | 62 |
40 // Length of the region in bytes. | 63 // Length of the region in bytes. |
41 int64_t size; | 64 int64_t size; |
42 }; | 65 }; |
43 | 66 |
44 // Opens an existing file and maps it into memory. Access is restricted to | 67 // Opens an existing file and maps it into memory. |access| can be read-only |
45 // read only. If this object already points to a valid memory mapped file | 68 // or read/write but not read/write+extend. If this object already points |
46 // then this method will fail and return false. If it cannot open the file, | 69 // to a valid memory mapped file then this method will fail and return |
47 // the file does not exist, or the memory mapping fails, it will return false. | 70 // false. If it cannot open the file, the file does not exist, or the |
48 // Later we may want to allow the user to specify access. | 71 // memory mapping fails, it will return false. |
49 bool Initialize(const FilePath& file_name); | 72 bool Initialize(const FilePath& file_name, Access access); |
| 73 bool Initialize(const FilePath& file_name) { |
| 74 return Initialize(file_name, READ_ONLY); |
| 75 } |
50 | 76 |
51 // As above, but works with an already-opened file. MemoryMappedFile takes | 77 // As above, but works with an already-opened file. |access| can be read-only |
52 // ownership of |file| and closes it when done. | 78 // or read/write but not read/write+extend. MemoryMappedFile takes ownership |
53 bool Initialize(File file); | 79 // of |file| and closes it when done. |file| must have been opened with |
| 80 // permissions suitable for |access|. If the memory mapping fails, it will |
| 81 // return false. |
| 82 bool Initialize(File file, Access access); |
| 83 bool Initialize(File file) { |
| 84 return Initialize(std::move(file), READ_ONLY); |
| 85 } |
54 | 86 |
55 // As above, but works with a region of an already-opened file. | 87 // As above, but works with a region of an already-opened file. All forms of |
56 bool Initialize(File file, const Region& region); | 88 // |access| are allowed. If READ_WRITE_EXTEND is specified then |region| |
57 | 89 // provides the maximum size of the file. If the memory mapping fails, it |
58 #if defined(OS_WIN) | 90 // return false. |
59 // Opens an existing file and maps it as an image section. Please refer to | 91 bool Initialize(File file, const Region& region, Access access); |
60 // the Initialize function above for additional information. | 92 bool Initialize(File file, const Region& region) { |
61 bool InitializeAsImageSection(const FilePath& file_name); | 93 return Initialize(std::move(file), region, READ_ONLY); |
62 #endif // OS_WIN | 94 } |
63 | 95 |
64 const uint8_t* data() const { return data_; } | 96 const uint8_t* data() const { return data_; } |
| 97 uint8_t* data() { return data_; } |
65 size_t length() const { return length_; } | 98 size_t length() const { return length_; } |
66 | 99 |
67 // Is file_ a valid file handle that points to an open, memory mapped file? | 100 // Is file_ a valid file handle that points to an open, memory mapped file? |
68 bool IsValid() const; | 101 bool IsValid() const; |
69 | 102 |
70 private: | 103 private: |
71 // Given the arbitrarily aligned memory region [start, size], returns the | 104 // Given the arbitrarily aligned memory region [start, size], returns the |
72 // boundaries of the region aligned to the granularity specified by the OS, | 105 // boundaries of the region aligned to the granularity specified by the OS, |
73 // (a page on Linux, ~32k on Windows) as follows: | 106 // (a page on Linux, ~32k on Windows) as follows: |
74 // - |aligned_start| is page aligned and <= |start|. | 107 // - |aligned_start| is page aligned and <= |start|. |
75 // - |aligned_size| is a multiple of the VM granularity and >= |size|. | 108 // - |aligned_size| is a multiple of the VM granularity and >= |size|. |
76 // - |offset| is the displacement of |start| w.r.t |aligned_start|. | 109 // - |offset| is the displacement of |start| w.r.t |aligned_start|. |
77 static void CalculateVMAlignedBoundaries(int64_t start, | 110 static void CalculateVMAlignedBoundaries(int64_t start, |
78 int64_t size, | 111 int64_t size, |
79 int64_t* aligned_start, | 112 int64_t* aligned_start, |
80 int64_t* aligned_size, | 113 int64_t* aligned_size, |
81 int32_t* offset); | 114 int32_t* offset); |
82 | 115 |
83 // Map the file to memory, set data_ to that memory address. Return true on | 116 // Map the file to memory, set data_ to that memory address. Return true on |
84 // success, false on any kind of failure. This is a helper for Initialize(). | 117 // success, false on any kind of failure. This is a helper for Initialize(). |
85 bool MapFileRegionToMemory(const Region& region); | 118 bool MapFileRegionToMemory(const Region& region, Access access); |
86 | 119 |
87 // Closes all open handles. | 120 // Closes all open handles. |
88 void CloseHandles(); | 121 void CloseHandles(); |
89 | 122 |
90 File file_; | 123 File file_; |
91 uint8_t* data_; | 124 uint8_t* data_; |
92 size_t length_; | 125 size_t length_; |
93 | 126 |
94 #if defined(OS_WIN) | 127 #if defined(OS_WIN) |
95 win::ScopedHandle file_mapping_; | 128 win::ScopedHandle file_mapping_; |
96 bool image_; // Map as an image. | |
97 #endif | 129 #endif |
98 | 130 |
99 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); | 131 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); |
100 }; | 132 }; |
101 | 133 |
102 } // namespace base | 134 } // namespace base |
103 | 135 |
104 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ | 136 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ |
OLD | NEW |