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" | |
10 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/macros.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. | 27 // Used to hold information about a region [offset + size] of a file. |
28 struct BASE_EXPORT Region { | 28 struct BASE_EXPORT Region { |
29 static const Region kWholeFile; | 29 static const Region kWholeFile; |
30 | 30 |
31 bool operator==(const Region& other) const; | 31 bool operator==(const Region& other) const; |
32 bool operator!=(const Region& other) const; | 32 bool operator!=(const Region& other) const; |
33 | 33 |
34 // Start of the region (measured in bytes from the beginning of the file). | 34 // Start of the region (measured in bytes from the beginning of the file). |
35 int64 offset; | 35 int64_t offset; |
36 | 36 |
37 // Length of the region in bytes. | 37 // Length of the region in bytes. |
38 int64 size; | 38 int64_t size; |
39 }; | 39 }; |
40 | 40 |
41 // Opens an existing file and maps it into memory. Access is restricted to | 41 // Opens an existing file and maps it into memory. Access is restricted to |
42 // read only. If this object already points to a valid memory mapped file | 42 // read only. If this object already points to a valid memory mapped file |
43 // then this method will fail and return false. If it cannot open the file, | 43 // then this method will fail and return false. If it cannot open the file, |
44 // the file does not exist, or the memory mapping fails, it will return false. | 44 // the file does not exist, or the memory mapping fails, it will return false. |
45 // Later we may want to allow the user to specify access. | 45 // Later we may want to allow the user to specify access. |
46 bool Initialize(const FilePath& file_name); | 46 bool Initialize(const FilePath& file_name); |
47 | 47 |
48 // As above, but works with an already-opened file. MemoryMappedFile takes | 48 // As above, but works with an already-opened file. MemoryMappedFile takes |
49 // ownership of |file| and closes it when done. | 49 // ownership of |file| and closes it when done. |
50 bool Initialize(File file); | 50 bool Initialize(File file); |
51 | 51 |
52 // As above, but works with a region of an already-opened file. | 52 // As above, but works with a region of an already-opened file. |
53 bool Initialize(File file, const Region& region); | 53 bool Initialize(File file, const Region& region); |
54 | 54 |
55 #if defined(OS_WIN) | 55 #if defined(OS_WIN) |
56 // Opens an existing file and maps it as an image section. Please refer to | 56 // Opens an existing file and maps it as an image section. Please refer to |
57 // the Initialize function above for additional information. | 57 // the Initialize function above for additional information. |
58 bool InitializeAsImageSection(const FilePath& file_name); | 58 bool InitializeAsImageSection(const FilePath& file_name); |
59 #endif // OS_WIN | 59 #endif // OS_WIN |
60 | 60 |
61 const uint8* data() const { return data_; } | 61 const uint8_t* data() const { return data_; } |
62 size_t length() const { return length_; } | 62 size_t length() const { return length_; } |
63 | 63 |
64 // Is file_ a valid file handle that points to an open, memory mapped file? | 64 // Is file_ a valid file handle that points to an open, memory mapped file? |
65 bool IsValid() const; | 65 bool IsValid() const; |
66 | 66 |
67 private: | 67 private: |
68 // Given the arbitrarily aligned memory region [start, size], returns the | 68 // Given the arbitrarily aligned memory region [start, size], returns the |
69 // boundaries of the region aligned to the granularity specified by the OS, | 69 // boundaries of the region aligned to the granularity specified by the OS, |
70 // (a page on Linux, ~32k on Windows) as follows: | 70 // (a page on Linux, ~32k on Windows) as follows: |
71 // - |aligned_start| is page aligned and <= |start|. | 71 // - |aligned_start| is page aligned and <= |start|. |
72 // - |aligned_size| is a multiple of the VM granularity and >= |size|. | 72 // - |aligned_size| is a multiple of the VM granularity and >= |size|. |
73 // - |offset| is the displacement of |start| w.r.t |aligned_start|. | 73 // - |offset| is the displacement of |start| w.r.t |aligned_start|. |
74 static void CalculateVMAlignedBoundaries(int64 start, | 74 static void CalculateVMAlignedBoundaries(int64_t start, |
75 int64 size, | 75 int64_t size, |
76 int64* aligned_start, | 76 int64_t* aligned_start, |
77 int64* aligned_size, | 77 int64_t* aligned_size, |
78 int32* offset); | 78 int32_t* offset); |
79 | 79 |
80 // Map the file to memory, set data_ to that memory address. Return true on | 80 // Map the file to memory, set data_ to that memory address. Return true on |
81 // success, false on any kind of failure. This is a helper for Initialize(). | 81 // success, false on any kind of failure. This is a helper for Initialize(). |
82 bool MapFileRegionToMemory(const Region& region); | 82 bool MapFileRegionToMemory(const Region& region); |
83 | 83 |
84 // Closes all open handles. | 84 // Closes all open handles. |
85 void CloseHandles(); | 85 void CloseHandles(); |
86 | 86 |
87 File file_; | 87 File file_; |
88 uint8* data_; | 88 uint8_t* data_; |
89 size_t length_; | 89 size_t length_; |
90 | 90 |
91 #if defined(OS_WIN) | 91 #if defined(OS_WIN) |
92 win::ScopedHandle file_mapping_; | 92 win::ScopedHandle file_mapping_; |
93 bool image_; // Map as an image. | 93 bool image_; // Map as an image. |
94 #endif | 94 #endif |
95 | 95 |
96 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); | 96 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); |
97 }; | 97 }; |
98 | 98 |
99 } // namespace base | 99 } // namespace base |
100 | 100 |
101 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ | 101 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ |
OLD | NEW |