memory_mapped_file.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef BASE_FILES_MEMORY_MAPPED_FILE_H_
  5. #define BASE_FILES_MEMORY_MAPPED_FILE_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/base_export.h"
  9. #include "base/files/file.h"
  10. #include "base/macros.h"
  11. #include "build/build_config.h"
  12. #if defined(OS_WIN)
  13. #include <windows.h>
  14. #endif
  15. namespace base {
  16. class FilePath;
  17. class BASE_EXPORT MemoryMappedFile {
  18. public:
  19. enum Access {
  20. // Mapping a file into memory effectively allows for file I/O on any thread.
  21. // The accessing thread could be paused while data from the file is paged
  22. // into memory. Worse, a corrupted filesystem could cause a SEGV within the
  23. // program instead of just an I/O error.
  24. READ_ONLY,
  25. // This provides read/write access to a file and must be used with care of
  26. // the additional subtleties involved in doing so. Though the OS will do
  27. // the writing of data on its own time, too many dirty pages can cause
  28. // the OS to pause the thread while it writes them out. The pause can
  29. // be as much as 1s on some systems.
  30. READ_WRITE,
  31. // This provides read/write access but with the ability to write beyond
  32. // the end of the existing file up to a maximum size specified as the
  33. // "region". Depending on the OS, the file may or may not be immediately
  34. // extended to the maximum size though it won't be loaded in RAM until
  35. // needed. Note, however, that the maximum size will still be reserved
  36. // in the process address space.
  37. READ_WRITE_EXTEND,
  38. #if defined(OS_WIN)
  39. // This provides read access, but as executable code used for prefetching
  40. // DLLs into RAM to avoid inefficient hard fault patterns such as during
  41. // process startup. The accessing thread could be paused while data from
  42. // the file is read into memory (if needed).
  43. READ_CODE_IMAGE,
  44. #endif
  45. };
  46. // The default constructor sets all members to invalid/null values.
  47. MemoryMappedFile();
  48. ~MemoryMappedFile();
  49. // Used to hold information about a region [offset + size] of a file.
  50. struct BASE_EXPORT Region {
  51. static const Region kWholeFile;
  52. bool operator==(const Region& other) const;
  53. bool operator!=(const Region& other) const;
  54. // Start of the region (measured in bytes from the beginning of the file).
  55. int64_t offset;
  56. // Length of the region in bytes.
  57. size_t size;
  58. };
  59. // Opens an existing file and maps it into memory. |access| can be read-only
  60. // or read/write but not read/write+extend. If this object already points
  61. // to a valid memory mapped file then this method will fail and return
  62. // false. If it cannot open the file, the file does not exist, or the
  63. // memory mapping fails, it will return false.
  64. WARN_UNUSED_RESULT bool Initialize(const FilePath& file_name, Access access);
  65. WARN_UNUSED_RESULT bool Initialize(const FilePath& file_name) {
  66. return Initialize(file_name, READ_ONLY);
  67. }
  68. // As above, but works with an already-opened file. |access| can be read-only
  69. // or read/write but not read/write+extend. MemoryMappedFile takes ownership
  70. // of |file| and closes it when done. |file| must have been opened with
  71. // permissions suitable for |access|. If the memory mapping fails, it will
  72. // return false.
  73. WARN_UNUSED_RESULT bool Initialize(File file, Access access);
  74. WARN_UNUSED_RESULT bool Initialize(File file) {
  75. return Initialize(std::move(file), READ_ONLY);
  76. }
  77. // As above, but works with a region of an already-opened file. |access|
  78. // must not be READ_CODE_IMAGE. If READ_WRITE_EXTEND is specified then
  79. // |region| provides the maximum size of the file. If the memory mapping
  80. // fails, it return false.
  81. WARN_UNUSED_RESULT bool Initialize(File file,
  82. const Region& region,
  83. Access access);
  84. WARN_UNUSED_RESULT bool Initialize(File file, const Region& region) {
  85. return Initialize(std::move(file), region, READ_ONLY);
  86. }
  87. const uint8_t* data() const { return data_; }
  88. uint8_t* data() { return data_; }
  89. size_t length() const { return length_; }
  90. // Is file_ a valid file handle that points to an open, memory mapped file?
  91. bool IsValid() const;
  92. private:
  93. // Given the arbitrarily aligned memory region [start, size], returns the
  94. // boundaries of the region aligned to the granularity specified by the OS,
  95. // (a page on Linux, ~32k on Windows) as follows:
  96. // - |aligned_start| is page aligned and <= |start|.
  97. // - |aligned_size| is a multiple of the VM granularity and >= |size|.
  98. // - |offset| is the displacement of |start| w.r.t |aligned_start|.
  99. static void CalculateVMAlignedBoundaries(int64_t start,
  100. size_t size,
  101. int64_t* aligned_start,
  102. size_t* aligned_size,
  103. int32_t* offset);
  104. #if defined(OS_WIN)
  105. // Maps the executable file to memory, set |data_| to that memory address.
  106. // Return true on success.
  107. bool MapImageToMemory(Access access);
  108. #endif
  109. // Map the file to memory, set data_ to that memory address. Return true on
  110. // success, false on any kind of failure. This is a helper for Initialize().
  111. bool MapFileRegionToMemory(const Region& region, Access access);
  112. // Closes all open handles.
  113. void CloseHandles();
  114. File file_;
  115. uint8_t* data_;
  116. size_t length_;
  117. #if defined(OS_WIN)
  118. win::ScopedHandle file_mapping_;
  119. #endif
  120. DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile);
  121. };
  122. } // namespace base
  123. #endif // BASE_FILES_MEMORY_MAPPED_FILE_H_