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" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 | 12 |
| 13 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
| 14 #include <windows.h> | 14 #include <windows.h> |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 | 18 |
| 19 class FilePath; | 19 class FilePath; |
| 20 | 20 |
| 21 class BASE_EXPORT MemoryMappedFile { | 21 class BASE_EXPORT MemoryMappedFile { |
| 22 public: | 22 public: |
| 23 // The default constructor sets all members to invalid/null values. | 23 // The default constructor sets all members to invalid/null values. |
| 24 MemoryMappedFile(); | 24 MemoryMappedFile(); |
| 25 ~MemoryMappedFile(); | 25 ~MemoryMappedFile(); |
| 26 | 26 |
| 27 // Used to hold information about a region [offset + size] of a file. | |
| 28 struct BASE_EXPORT Region { | |
| 29 static const Region kWholeFile; | |
|
sky
2014/07/28 22:07:43
Doesn't this result in a static constructor, which
Primiano Tucci (use gerrit)
2014/07/29 09:05:49
We had an offline discussion with Will on this poi
| |
| 30 | |
| 31 Region(int64 offset, int64 size); | |
| 32 | |
| 33 bool operator==(const Region& other) const; | |
| 34 | |
| 35 // Start of the region (measured in bytes from the beginning of the file). | |
| 36 int64 offset; | |
| 37 | |
| 38 // Length of the region in bytes. | |
| 39 int64 size; | |
| 40 | |
| 41 private: | |
| 42 // This ctor is used only by kWholeFile, to construct a zero-sized Region | |
| 43 // (which is forbidden by the public ctor) and uniquely identify kWholeFile. | |
| 44 Region(); | |
| 45 }; | |
| 46 | |
| 27 // Opens an existing file and maps it into memory. Access is restricted to | 47 // Opens an existing file and maps it into memory. Access is restricted to |
| 28 // read only. If this object already points to a valid memory mapped file | 48 // read only. If this object already points to a valid memory mapped file |
| 29 // then this method will fail and return false. If it cannot open the file, | 49 // then this method will fail and return false. If it cannot open the file, |
| 30 // the file does not exist, or the memory mapping fails, it will return false. | 50 // the file does not exist, or the memory mapping fails, it will return false. |
| 31 // Later we may want to allow the user to specify access. | 51 // Later we may want to allow the user to specify access. |
| 32 bool Initialize(const FilePath& file_name); | 52 bool Initialize(const FilePath& file_name); |
| 33 | 53 |
| 34 // As above, but works with an already-opened file. MemoryMappedFile takes | 54 // As above, but works with an already-opened file. MemoryMappedFile takes |
| 35 // ownership of |file| and closes it when done. | 55 // ownership of |file| and closes it when done. |
| 36 bool Initialize(File file); | 56 bool Initialize(File file); |
| 37 | 57 |
| 58 // As above, but works with a region of an already-opened file. | |
| 59 bool Initialize(File file, const Region& region); | |
| 60 | |
| 38 #if defined(OS_WIN) | 61 #if defined(OS_WIN) |
| 39 // Opens an existing file and maps it as an image section. Please refer to | 62 // Opens an existing file and maps it as an image section. Please refer to |
| 40 // the Initialize function above for additional information. | 63 // the Initialize function above for additional information. |
| 41 bool InitializeAsImageSection(const FilePath& file_name); | 64 bool InitializeAsImageSection(const FilePath& file_name); |
| 42 #endif // OS_WIN | 65 #endif // OS_WIN |
| 43 | 66 |
| 44 const uint8* data() const { return data_; } | 67 const uint8* data() const { return data_; } |
| 45 size_t length() const { return length_; } | 68 size_t length() const { return length_; } |
| 46 | 69 |
| 47 // Is file_ a valid file handle that points to an open, memory mapped file? | 70 // Is file_ a valid file handle that points to an open, memory mapped file? |
| 48 bool IsValid() const; | 71 bool IsValid() const; |
| 49 | 72 |
| 50 private: | 73 private: |
| 74 // Given the arbitrarily aligned memory region [start, size], returns the | |
| 75 // boundaries of the region aligned to the granularity specified by the OS, | |
| 76 // (a page on Linux, ~32k on Windows) as follows: | |
| 77 // - |aligned_start| is page aligned and <= |start|. | |
| 78 // - |aligned_size| is a multiple of the VM granularity and >= |size|. | |
| 79 // - |offset| is the displacement of |start| w.r.t |aligned_start|. | |
| 80 static void CalculateVMAlignedBoundaries(int64 start, | |
| 81 int64 size, | |
| 82 int64* aligned_start, | |
| 83 int64* aligned_size, | |
| 84 int32* offset); | |
| 85 | |
| 51 // Map the file to memory, set data_ to that memory address. Return true on | 86 // Map the file to memory, set data_ to that memory address. Return true on |
| 52 // success, false on any kind of failure. This is a helper for Initialize(). | 87 // success, false on any kind of failure. This is a helper for Initialize(). |
| 53 bool MapFileToMemory(); | 88 bool MapFileRegionToMemory(const Region& region); |
| 54 | 89 |
| 55 // Closes all open handles. | 90 // Closes all open handles. |
| 56 void CloseHandles(); | 91 void CloseHandles(); |
| 57 | 92 |
| 58 File file_; | 93 File file_; |
| 59 uint8* data_; | 94 uint8* data_; |
| 60 size_t length_; | 95 size_t length_; |
| 61 | 96 |
| 62 #if defined(OS_WIN) | 97 #if defined(OS_WIN) |
| 63 win::ScopedHandle file_mapping_; | 98 win::ScopedHandle file_mapping_; |
| 64 bool image_; // Map as an image. | 99 bool image_; // Map as an image. |
| 65 #endif | 100 #endif |
| 66 | 101 |
| 67 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); | 102 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); |
| 68 }; | 103 }; |
| 69 | 104 |
| 70 } // namespace base | 105 } // namespace base |
| 71 | 106 |
| 72 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ | 107 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ |
| OLD | NEW |