| 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/platform_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 // Opens an existing file and maps it into memory. Access is restricted to | 27 // 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 | 28 // 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, | 29 // 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. | 30 // 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. | 31 // Later we may want to allow the user to specify access. |
| 32 bool Initialize(const FilePath& file_name); | 32 bool Initialize(const FilePath& file_name); |
| 33 // As above, but works with an already-opened file. MemoryMappedFile will take | 33 |
| 34 // ownership of |file| and close it when done. | 34 // As above, but works with an already-opened file. MemoryMappedFile takes |
| 35 bool Initialize(PlatformFile file); | 35 // ownership of |file| and closes it when done. |
| 36 bool Initialize(File file); |
| 36 | 37 |
| 37 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
| 38 // Opens an existing file and maps it as an image section. Please refer to | 39 // Opens an existing file and maps it as an image section. Please refer to |
| 39 // the Initialize function above for additional information. | 40 // the Initialize function above for additional information. |
| 40 bool InitializeAsImageSection(const FilePath& file_name); | 41 bool InitializeAsImageSection(const FilePath& file_name); |
| 41 #endif // OS_WIN | 42 #endif // OS_WIN |
| 42 | 43 |
| 43 const uint8* data() const { return data_; } | 44 const uint8* data() const { return data_; } |
| 44 size_t length() const { return length_; } | 45 size_t length() const { return length_; } |
| 45 | 46 |
| 46 // Is file_ a valid file handle that points to an open, memory mapped file? | 47 // Is file_ a valid file handle that points to an open, memory mapped file? |
| 47 bool IsValid() const; | 48 bool IsValid() const; |
| 48 | 49 |
| 49 private: | 50 private: |
| 50 // Open the given file and pass it to MapFileToMemoryInternal(). | |
| 51 bool MapFileToMemory(const FilePath& file_name); | |
| 52 | |
| 53 // Map the file to memory, set data_ to that memory address. Return true on | 51 // Map the file to memory, set data_ to that memory address. Return true on |
| 54 // success, false on any kind of failure. This is a helper for Initialize(). | 52 // success, false on any kind of failure. This is a helper for Initialize(). |
| 55 bool MapFileToMemoryInternal(); | 53 bool MapFileToMemory(); |
| 56 | 54 |
| 57 // Closes all open handles. Later we may want to make this public. | 55 // Closes all open handles. |
| 58 void CloseHandles(); | 56 void CloseHandles(); |
| 59 | 57 |
| 60 #if defined(OS_WIN) | 58 File file_; |
| 61 // MapFileToMemoryInternal calls this function. It provides the ability to | |
| 62 // pass in flags which control the mapped section. | |
| 63 bool MapFileToMemoryInternalEx(int flags); | |
| 64 | |
| 65 HANDLE file_mapping_; | |
| 66 #endif | |
| 67 PlatformFile file_; | |
| 68 uint8* data_; | 59 uint8* data_; |
| 69 size_t length_; | 60 size_t length_; |
| 70 | 61 |
| 62 #if defined(OS_WIN) |
| 63 win::ScopedHandle file_mapping_; |
| 64 bool image_; // Map as an image. |
| 65 #endif |
| 66 |
| 71 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); | 67 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); |
| 72 }; | 68 }; |
| 73 | 69 |
| 74 } // namespace base | 70 } // namespace base |
| 75 | 71 |
| 76 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ | 72 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ |
| OLD | NEW |