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