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