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 "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 // Later we may want to allow the user to specify access. | 51 // Later we may want to allow the user to specify access. |
| 52 bool Initialize(const FilePath& file_name); | 52 bool Initialize(const FilePath& file_name); |
| 53 | 53 |
| 54 // As above, but works with an already-opened file. MemoryMappedFile takes | 54 // As above, but works with an already-opened file. MemoryMappedFile takes |
| 55 // ownership of |file| and closes it when done. | 55 // ownership of |file| and closes it when done. |
| 56 bool Initialize(File file); | 56 bool Initialize(File file); |
| 57 | 57 |
| 58 // As above, but works with a region of an already-opened file. | 58 // As above, but works with a region of an already-opened file. |
| 59 bool Initialize(File file, const Region& region); | 59 bool Initialize(File file, const Region& region); |
| 60 | 60 |
| 61 // As above, works with an already-opened file and enables write permission | |
| 62 // when |write| is true. | |
| 63 bool Initialize(File file, bool write); | |
| 64 | |
| 65 // As above, but works with a region of an already-opened file and enables | |
| 66 // write permission when |write| is true. | |
| 67 bool Initialize(File file, const Region& region, bool write); | |
|
wuchengli
2014/08/28 10:17:48
I think this can be added in the future when someo
| |
| 68 | |
| 61 #if defined(OS_WIN) | 69 #if defined(OS_WIN) |
| 62 // Opens an existing file and maps it as an image section. Please refer to | 70 // Opens an existing file and maps it as an image section. Please refer to |
| 63 // the Initialize function above for additional information. | 71 // the Initialize function above for additional information. |
| 64 bool InitializeAsImageSection(const FilePath& file_name); | 72 bool InitializeAsImageSection(const FilePath& file_name); |
| 65 #endif // OS_WIN | 73 #endif // OS_WIN |
| 66 | 74 |
| 67 const uint8* data() const { return data_; } | 75 const uint8* data() const { return data_; } |
| 68 size_t length() const { return length_; } | 76 size_t length() const { return length_; } |
| 69 | 77 |
| 70 // Is file_ a valid file handle that points to an open, memory mapped file? | 78 // Is file_ a valid file handle that points to an open, memory mapped file? |
| 71 bool IsValid() const; | 79 bool IsValid() const; |
| 72 | 80 |
| 73 private: | 81 private: |
| 74 // Given the arbitrarily aligned memory region [start, size], returns the | 82 // Given the arbitrarily aligned memory region [start, size], returns the |
| 75 // boundaries of the region aligned to the granularity specified by the OS, | 83 // boundaries of the region aligned to the granularity specified by the OS, |
| 76 // (a page on Linux, ~32k on Windows) as follows: | 84 // (a page on Linux, ~32k on Windows) as follows: |
| 77 // - |aligned_start| is page aligned and <= |start|. | 85 // - |aligned_start| is page aligned and <= |start|. |
| 78 // - |aligned_size| is a multiple of the VM granularity and >= |size|. | 86 // - |aligned_size| is a multiple of the VM granularity and >= |size|. |
| 79 // - |offset| is the displacement of |start| w.r.t |aligned_start|. | 87 // - |offset| is the displacement of |start| w.r.t |aligned_start|. |
| 80 static void CalculateVMAlignedBoundaries(int64 start, | 88 static void CalculateVMAlignedBoundaries(int64 start, |
| 81 int64 size, | 89 int64 size, |
| 82 int64* aligned_start, | 90 int64* aligned_start, |
| 83 int64* aligned_size, | 91 int64* aligned_size, |
| 84 int32* offset); | 92 int32* offset); |
| 85 | 93 |
| 86 // Map the file to memory, set data_ to that memory address. Return true on | 94 // Map the file to memory, set data_ to that memory address. Return true on |
| 87 // success, false on any kind of failure. This is a helper for Initialize(). | 95 // success, false on any kind of failure. This is a helper for Initialize(). |
| 88 bool MapFileRegionToMemory(const Region& region); | 96 // - |write| is used to enable write permission of mmap or not. |
|
wuchengli
2014/08/28 10:17:48
s/or not//
| |
| 97 bool MapFileRegionToMemory(const Region& region, bool write); | |
| 89 | 98 |
| 90 // Closes all open handles. | 99 // Closes all open handles. |
| 91 void CloseHandles(); | 100 void CloseHandles(); |
| 92 | 101 |
| 93 File file_; | 102 File file_; |
| 94 uint8* data_; | 103 uint8* data_; |
| 95 size_t length_; | 104 size_t length_; |
| 96 | 105 |
| 97 #if defined(OS_WIN) | 106 #if defined(OS_WIN) |
| 98 win::ScopedHandle file_mapping_; | 107 win::ScopedHandle file_mapping_; |
| 99 bool image_; // Map as an image. | 108 bool image_; // Map as an image. |
| 100 #endif | 109 #endif |
| 101 | 110 |
| 102 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); | 111 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); |
| 103 }; | 112 }; |
| 104 | 113 |
| 105 } // namespace base | 114 } // namespace base |
| 106 | 115 |
| 107 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ | 116 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ |
| OLD | NEW |