Chromium Code Reviews| 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 |
|
danakj
2016/04/25 21:26:17
|access| should refer to the variable name (no cap
bcwhite
2016/04/26 00:08:45
Done.
| |
| 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 |
|
danakj
2016/04/25 21:26:17
|access|
bcwhite
2016/04/26 00:08:45
Done.
| |
| 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|. | |
|
danakj
2016/04/25 21:26:17
if not, false is returned?
bcwhite
2016/04/26 00:08:45
Done.
| |
| 81 bool Initialize(File file, Access access); | |
| 82 bool Initialize(File file) { | |
| 83 return Initialize(std::move(file), READ_ONLY); | |
| 84 } | |
| 54 | 85 |
| 55 // As above, but works with a region of an already-opened file. | 86 // As above, but works with a region of an already-opened file. All forms of |
| 56 bool Initialize(File file, const Region& region); | 87 // |access| are allowed. If READ_WRITE_EXTEND is specified then |region| |
| 88 // provides the maximum size of the file. | |
| 89 bool Initialize(File file, const Region& region, Access access); | |
| 90 bool Initialize(File file, const Region& region) { | |
| 91 return Initialize(std::move(file), region, READ_ONLY); | |
| 92 } | |
| 57 | 93 |
| 58 #if defined(OS_WIN) | 94 #if defined(OS_WIN) |
| 59 // Opens an existing file and maps it as an image section. Please refer to | 95 // Opens an existing file and maps it as an image section. Please refer to |
| 60 // the Initialize function above for additional information. | 96 // the Initialize function above for additional information. |
| 61 bool InitializeAsImageSection(const FilePath& file_name); | 97 bool InitializeAsImageSection(const FilePath& file_name); |
| 62 #endif // OS_WIN | 98 #endif // OS_WIN |
| 63 | 99 |
| 64 const uint8_t* data() const { return data_; } | 100 const uint8_t* data() const { return data_; } |
| 101 uint8_t* data() { return data_; } | |
| 65 size_t length() const { return length_; } | 102 size_t length() const { return length_; } |
| 66 | 103 |
| 67 // Is file_ a valid file handle that points to an open, memory mapped file? | 104 // Is file_ a valid file handle that points to an open, memory mapped file? |
| 68 bool IsValid() const; | 105 bool IsValid() const; |
| 69 | 106 |
| 70 private: | 107 private: |
| 71 // Given the arbitrarily aligned memory region [start, size], returns the | 108 // Given the arbitrarily aligned memory region [start, size], returns the |
| 72 // boundaries of the region aligned to the granularity specified by the OS, | 109 // boundaries of the region aligned to the granularity specified by the OS, |
| 73 // (a page on Linux, ~32k on Windows) as follows: | 110 // (a page on Linux, ~32k on Windows) as follows: |
| 74 // - |aligned_start| is page aligned and <= |start|. | 111 // - |aligned_start| is page aligned and <= |start|. |
| 75 // - |aligned_size| is a multiple of the VM granularity and >= |size|. | 112 // - |aligned_size| is a multiple of the VM granularity and >= |size|. |
| 76 // - |offset| is the displacement of |start| w.r.t |aligned_start|. | 113 // - |offset| is the displacement of |start| w.r.t |aligned_start|. |
| 77 static void CalculateVMAlignedBoundaries(int64_t start, | 114 static void CalculateVMAlignedBoundaries(int64_t start, |
| 78 int64_t size, | 115 int64_t size, |
| 79 int64_t* aligned_start, | 116 int64_t* aligned_start, |
| 80 int64_t* aligned_size, | 117 int64_t* aligned_size, |
| 81 int32_t* offset); | 118 int32_t* offset); |
| 82 | 119 |
| 83 // Map the file to memory, set data_ to that memory address. Return true on | 120 // 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(). | 121 // success, false on any kind of failure. This is a helper for Initialize(). |
| 85 bool MapFileRegionToMemory(const Region& region); | 122 bool MapFileRegionToMemory(const Region& region, Access access); |
| 86 | 123 |
| 87 // Closes all open handles. | 124 // Closes all open handles. |
| 88 void CloseHandles(); | 125 void CloseHandles(); |
| 89 | 126 |
| 90 File file_; | 127 File file_; |
| 91 uint8_t* data_; | 128 uint8_t* data_; |
| 92 size_t length_; | 129 size_t length_; |
| 93 | 130 |
| 94 #if defined(OS_WIN) | 131 #if defined(OS_WIN) |
| 95 win::ScopedHandle file_mapping_; | 132 win::ScopedHandle file_mapping_; |
| 96 bool image_; // Map as an image. | 133 bool image_; // Map as an image. |
| 97 #endif | 134 #endif |
| 98 | 135 |
| 99 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); | 136 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); |
| 100 }; | 137 }; |
| 101 | 138 |
| 102 } // namespace base | 139 } // namespace base |
| 103 | 140 |
| 104 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ | 141 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ |
| OLD | NEW |