文件映射基础介绍参考微软官网:
使用文件映射 - Win32 apps | Microsoft Learn
在文件中创建视图 - Win32 apps | Microsoft Learn
创建命名的共享内存 - Win32 apps | Microsoft Learn
使用大页面创建文件映射 - Win32 apps | Microsoft Learn
从文件句柄获取文件名 - Win32 apps | Microsoft Learn
Chromium中在windows平台是对GetMappedFileName的封装实现:
本文主要介绍三种文件映射使用方法示例:
1、路径初始化mapfile
2、 文件句柄初始化mapfile
3、文件+偏移 初始化mapfile 【base::MemoryMappedFile::Region】
一、看下base::MemoryMappedFile定义:
base\files\memory_mapped_file.h
base\files\memory_mapped_file.cc
namespace base {class FilePath;class BASE_EXPORT MemoryMappedFile {public:enum Access {// Mapping a file into memory effectively allows for file I/O on any thread.// The accessing thread could be paused while data from the file is paged// into memory. Worse, a corrupted filesystem could cause a SEGV within the// program instead of just an I/O error.READ_ONLY,// This provides read/write access to a file and must be used with care of// the additional subtleties involved in doing so. Though the OS will do// the writing of data on its own time, too many dirty pages can cause// the OS to pause the thread while it writes them out. The pause can// be as much as 1s on some systems.READ_WRITE,// This provides read/write access to the mapped file contents as above, but// applies a copy-on-write policy such that no writes are carried through to// the underlying file.READ_WRITE_COPY,// This provides read/write access but with the ability to write beyond// the end of the existing file up to a maximum size specified as the// "region". Depending on the OS, the file may or may not be immediately// extended to the maximum size though it won't be loaded in RAM until// needed. Note, however, that the maximum size will still be reserved// in the process address space.READ_WRITE_EXTEND,#if BUILDFLAG(IS_WIN)// This provides read access, but as executable code used for prefetching// DLLs into RAM to avoid inefficient hard fault patterns such as during// process startup. The accessing thread could be paused while data from// the file is read into memory (if needed).READ_CODE_IMAGE,
#endif};// The default constructor sets all members to invalid/null values.MemoryMappedFile();MemoryMappedFile(const MemoryMappedFile&) = delete;MemoryMappedFile& operator=(const MemoryMappedFile&) = delete;~MemoryMappedFile();// Used to hold information about a region [offset + size] of a file.struct BASE_EXPORT Region {static const Region kWholeFile;friend bool operator==(const Region&, const Region&) = default;// Start of the region (measured in bytes from the beginning of the file).int64_t offset;// Length of the region in bytes.size_t size;};// Opens an existing file and maps it into memory. |access| can be read-only// or read/write but not read/write+extend. If this object already points// to a valid memory mapped file then this method will fail and return// false. If it cannot open the file, the file does not exist, or the// memory mapping fails, it will return false.[[nodiscard]] bool Initialize(const FilePath& file_name, Access access);[[nodiscard]] bool Initialize(const FilePath& file_name) {return Initialize(file_name, READ_ONLY);}// As above, but works with an already-opened file. |access| can be read-only// or read/write but not read/write+extend. MemoryMappedFile takes ownership// of |file| and closes it when done. |file| must have been opened with// permissions suitable for |access|. If the memory mapping fails, it will// return false.[[nodiscard]] bool Initialize(File file, Access access);[[nodiscard]] bool Initialize(File file) {return Initialize(std::move(file), READ_ONLY);}// As above, but works with a region of an already-opened file. |access|// must not be READ_CODE_IMAGE. If READ_WRITE_EXTEND is specified then// |region| provides the maximum size of the file. If the memory mapping// fails, it return false.[[nodiscard]] bool Initialize(File file, const Region& region, Access access);[[nodiscard]] bool Initialize(File file, const Region& region) {return Initialize(std::move(file), region, READ_ONLY);}const uint8_t* data() const { return data_; }uint8_t* data() { return data_; }size_t length() const { return length_; }span<const uint8_t> bytes() const { return make_span(data_, length_); }span<uint8_t> mutable_bytes() const { return make_span(data_, length_); }// Is file_ a valid file handle that points to an open, memory mapped file?bool IsValid() const;private:// Given the arbitrarily aligned memory region [start, size], returns the// boundaries of the region aligned to the granularity specified by the OS,// (a page on Linux, ~32k on Windows) as follows:// - |aligned_start| is page aligned and <= |start|.// - |aligned_size| is a multiple of the VM granularity and >= |size|.// - |offset| is the displacement of |start| w.r.t |aligned_start|.static void CalculateVMAlignedBoundaries(int64_t start,size_t size,int64_t* aligned_start,size_t* aligned_size,int32_t* offset);#if BUILDFLAG(IS_WIN)// Maps the executable file to memory, set |data_| to that memory address.// Return true on success.bool MapImageToMemory(Access access);
#endif// Map the file to memory, set data_ to that memory address. Return true on// success, false on any kind of failure. This is a helper for Initialize().bool MapFileRegionToMemory(const Region& region, Access access);// Closes all open handles.void CloseHandles();File file_;// `data_` is never allocated by PartitionAlloc, so there is no benefit to// using a raw_ptr.RAW_PTR_EXCLUSION uint8_t* data_ = nullptr;size_t length_ = 0;#if BUILDFLAG(IS_WIN)win::ScopedHandle file_mapping_;
#endif
};} // namespace base#endif // BASE_FILES_MEMORY_MAPPED_FILE_H_
二、例子演示:
1、demo\BUILD.gn
executable("04-mapfile") {sources = ["04-mapfile/client.cc",]if (is_win) {ldflags = [ "/LARGEADDRESSAWARE" ]}deps = ["//base","//build/win:default_exe_manifest",]
}
2、demo\04-mapfile\client.cc
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <windows.h>
#include <iostream>#include "base/command_line.h"
#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/types/optional.h"namespace {
base::FilePath GetTestFilePath() {return base::FilePath(FILE_PATH_LITERAL("d:/test.txt"));
}void initTest_1() {LOG(ERROR) << " initTest_1";// 以路径初始化mapfilebase::MemoryMappedFile map;bool is_ok = map.Initialize(GetTestFilePath());if (!is_ok) {LOG(ERROR) << "Initialize erro";return;}is_ok = map.IsValid();size_t lg = map.length();// base::span<uint8_t> bytes = map.mutable_bytes();std::string out(reinterpret_cast<const char*>(map.data()), lg);LOG(ERROR) << out;
}void initTest_2() {// 以文件句柄初始化mapfileLOG(ERROR) << " initTest_2";base::MemoryMappedFile map;base::File file(GetTestFilePath(),base::File::FLAG_OPEN | base::File::FLAG_READ);bool is_ok = map.Initialize(std::move(file));if (!is_ok) {LOG(ERROR) << "Initialize erro";return;}is_ok = map.IsValid();size_t lg = map.length();// base::span<uint8_t> bytes = map.mutable_bytes();std::string out(reinterpret_cast<const char*>(map.data()), lg);LOG(ERROR) << out;
}void initTest_3() {// 文件+偏移 初始化mapfileLOG(ERROR) << " initTest_3";base::MemoryMappedFile map;base::File file(GetTestFilePath(),base::File::FLAG_OPEN | base::File::FLAG_READ);int64_t nLength = file.GetLength();LOG(ERROR) << "File Length: " << nLength;// 0到40个字节区域base::MemoryMappedFile::Region region = {0, 40};bool is_ok = map.Initialize(std::move(file), region);if (!is_ok) {LOG(ERROR) << "Initialize erro";return;}is_ok = map.IsValid();size_t lg = map.length();LOG(ERROR) << "map.length(): " << lg; //40// base::span<uint8_t> bytes = map.mutable_bytes();std::string out(reinterpret_cast<const char*>(map.data()), lg);LOG(ERROR) << out;
}} // namespaceint main(int argc, const char* argv[]) {base::CommandLine::Init(argc, argv);initTest_1();initTest_2();initTest_3();return 0;
}
3、gn gen out/debug
4、ninja -C out/debug demo:04-mapfile
5、编译运行之后效果:
6、test.txt文件内容如下:
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <windows.h>
#include <iostream>