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 17 matching lines...) Expand all Loading... | |
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 | 33 |
34 // As above, but works with an already-opened file. MemoryMappedFile takes | 34 // As above, but works with an already-opened file. MemoryMappedFile takes |
35 // ownership of |file| and closes it when done. | 35 // ownership of |file| and closes it when done. |
36 bool Initialize(File file); | 36 bool Initialize(File file); |
37 | 37 |
38 // As above, but works with a region of an already-opened file. | |
39 bool Initialize(File file, const base::File::Region& region); | |
40 | |
38 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
39 // Opens an existing file and maps it as an image section. Please refer to | 42 // Opens an existing file and maps it as an image section. Please refer to |
40 // the Initialize function above for additional information. | 43 // the Initialize function above for additional information. |
41 bool InitializeAsImageSection(const FilePath& file_name); | 44 bool InitializeAsImageSection(const FilePath& file_name); |
42 #endif // OS_WIN | 45 #endif // OS_WIN |
43 | 46 |
44 const uint8* data() const { return data_; } | 47 const uint8* data() const { return data_; } |
45 size_t length() const { return length_; } | 48 size_t length() const { return length_; } |
46 | 49 |
47 // Is file_ a valid file handle that points to an open, memory mapped file? | 50 // Is file_ a valid file handle that points to an open, memory mapped file? |
48 bool IsValid() const; | 51 bool IsValid() const; |
49 | 52 |
50 private: | 53 private: |
54 // Given the arbitrarily aligned memory region [start, size], returns the | |
55 // boundaries of the region aligned to the granularity specified by the OS, | |
56 // (a page on Linux, ~32k on Windows) as follows: | |
57 // - |aligned_start| is page aligned and <= |start|. | |
58 // - |aligned_size| is a multiple of the VM granularity and >= |size|. | |
59 // - |offset| is the displacement of |start| w.r.t |aligned_start|. | |
60 static void CalculateVMAlignedBoundaries(int64 start, | |
61 int64 size, | |
62 int64* aligned_start, | |
63 int64* aligned_size, | |
64 int32* offset); | |
65 | |
51 // Map the file to memory, set data_ to that memory address. Return true on | 66 // 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(). | 67 // success, false on any kind of failure. This is a helper for Initialize(). |
53 bool MapFileToMemory(); | 68 bool MapFileToMemory(const base::File::Region& region); |
willchan no longer on Chromium
2014/07/21 23:55:40
Perhaps rename this to MapFileRegionToMemory()
Primiano Tucci (use gerrit)
2014/07/22 10:32:01
Done.
| |
54 | 69 |
55 // Closes all open handles. | 70 // Closes all open handles. |
56 void CloseHandles(); | 71 void CloseHandles(); |
57 | 72 |
58 File file_; | 73 File file_; |
59 uint8* data_; | 74 uint8* data_; |
60 size_t length_; | 75 size_t length_; |
61 | 76 |
62 #if defined(OS_WIN) | 77 #if defined(OS_WIN) |
63 win::ScopedHandle file_mapping_; | 78 win::ScopedHandle file_mapping_; |
64 bool image_; // Map as an image. | 79 bool image_; // Map as an image. |
65 #endif | 80 #endif |
66 | 81 |
67 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); | 82 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); |
68 }; | 83 }; |
69 | 84 |
70 } // namespace base | 85 } // namespace base |
71 | 86 |
72 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ | 87 #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_ |
OLD | NEW |