Chromium Code Reviews| Index: base/files/memory_mapped_file.h |
| diff --git a/base/files/memory_mapped_file.h b/base/files/memory_mapped_file.h |
| index 6362e765cb4ba8c808f3be7f214772844b2d218a..6f1490910583d9c41bb07f469d1128f8074b6d15 100644 |
| --- a/base/files/memory_mapped_file.h |
| +++ b/base/files/memory_mapped_file.h |
| @@ -23,6 +23,29 @@ class FilePath; |
| class BASE_EXPORT MemoryMappedFile { |
| public: |
| + enum Access { |
| + // Mapping a file into memory effectively allows for file I/O on any thread. |
| + // The accessing thread could be paused while data from the file is paged |
| + // into memory. Worse, a corrupted filesystem could cause a SEGV within the |
| + // program instead of just an I/O error. |
| + READ_ONLY, |
| + |
| + // This provides read/write access to a file and must be used with care of |
| + // the additional subtleties involved in doing so. Though the OS will do |
| + // the writing of data on its own time, too many dirty pages can cause |
| + // the OS to pause the thread while it writes them out. The pause can |
| + // be as much as 1s on some systems. |
| + READ_WRITE, |
| + |
| + // This provides read/write access but with the ability to write beyond |
| + // the end of the existing file up to a maximum size specified as the |
| + // "region". Depending on the OS, the file may or may not be immediately |
| + // extended to the maximum size though it won't be loaded in RAM until |
| + // needed. Note, however, that the maximum size will still be reserved |
| + // in the process address space. |
| + READ_WRITE_EXTEND, |
| + }; |
| + |
| // The default constructor sets all members to invalid/null values. |
| MemoryMappedFile(); |
| ~MemoryMappedFile(); |
| @@ -41,19 +64,32 @@ class BASE_EXPORT MemoryMappedFile { |
| int64_t size; |
| }; |
| - // Opens an existing file and maps it into memory. Access is restricted to |
| - // read only. If this object already points to a valid memory mapped file |
| - // then this method will fail and return false. If it cannot open the file, |
| - // the file does not exist, or the memory mapping fails, it will return false. |
| - // Later we may want to allow the user to specify access. |
| - bool Initialize(const FilePath& file_name); |
| - |
| - // As above, but works with an already-opened file. MemoryMappedFile takes |
| - // ownership of |file| and closes it when done. |
| - bool Initialize(File file); |
| - |
| - // As above, but works with a region of an already-opened file. |
| - bool Initialize(File file, const Region& region); |
| + // 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.
|
| + // or read/write but not read/write+extend. If this object already points |
| + // to a valid memory mapped file then this method will fail and return |
| + // false. If it cannot open the file, the file does not exist, or the |
| + // memory mapping fails, it will return false. |
| + bool Initialize(const FilePath& file_name, Access access); |
| + bool Initialize(const FilePath& file_name) { |
| + return Initialize(file_name, READ_ONLY); |
| + } |
| + |
| + // 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.
|
| + // or read/write but not read/write+extend. MemoryMappedFile takes ownership |
| + // of |file| and closes it when done. |file| must have been opened with |
| + // permissions suitable for |access|. |
|
danakj
2016/04/25 21:26:17
if not, false is returned?
bcwhite
2016/04/26 00:08:45
Done.
|
| + bool Initialize(File file, Access access); |
| + bool Initialize(File file) { |
| + return Initialize(std::move(file), READ_ONLY); |
| + } |
| + |
| + // As above, but works with a region of an already-opened file. All forms of |
| + // |access| are allowed. If READ_WRITE_EXTEND is specified then |region| |
| + // provides the maximum size of the file. |
| + bool Initialize(File file, const Region& region, Access access); |
| + bool Initialize(File file, const Region& region) { |
| + return Initialize(std::move(file), region, READ_ONLY); |
| + } |
| #if defined(OS_WIN) |
| // Opens an existing file and maps it as an image section. Please refer to |
| @@ -62,6 +98,7 @@ class BASE_EXPORT MemoryMappedFile { |
| #endif // OS_WIN |
| const uint8_t* data() const { return data_; } |
| + uint8_t* data() { return data_; } |
| size_t length() const { return length_; } |
| // Is file_ a valid file handle that points to an open, memory mapped file? |
| @@ -82,7 +119,7 @@ class BASE_EXPORT MemoryMappedFile { |
| // Map the file to memory, set data_ to that memory address. Return true on |
| // success, false on any kind of failure. This is a helper for Initialize(). |
| - bool MapFileRegionToMemory(const Region& region); |
| + bool MapFileRegionToMemory(const Region& region, Access access); |
| // Closes all open handles. |
| void CloseHandles(); |