enum classaccess_mode { read, write }; /** * This is the basis for all read-only mmap objects and should be preferred over * directly using `basic_mmap`. */ template<typename ByteT> using basic_mmap_source = basic_mmap<access_mode::read, ByteT>;
/** * This is the basis for all read-write mmap objects and should be preferred over * directly using `basic_mmap`. */ template<typename ByteT> using basic_mmap_sink = basic_mmap<access_mode::write, ByteT>;
/** * These aliases cover the most common use cases, both representing a raw byte stream * (either with a char or an unsigned char/uint8_t). */ using mmap_source = basic_mmap_source<char>; using ummap_source = basic_mmap_source<unsignedchar>;
using mmap_sink = basic_mmap_sink<char>; using ummap_sink = basic_mmap_sink<unsignedchar>;
/** * Establishes a memory mapping with AccessMode. If the mapping is unsuccesful, the * reason is reported via `error` and the object remains in a state as if this * function hadn't been called. * * `path`, which must be a path to an existing file, is used to retrieve a file * handle (which is closed when the object destructs or `unmap` is called), which is * then used to memory map the requested region. Upon failure, `error` is set to * indicate the reason and the object remains in an unmapped state. * * `offset` is the number of bytes, relative to the start of the file, where the * mapping should begin. When specifying it, there is no need to worry about * providing a value that is aligned with the operating system's page allocation * granularity. This is adjusted by the implementation such that the first requested * byte (as returned by `data` or `begin`), so long as `offset` is valid, will be at * `offset` from the start of the file. * * `length` is the number of bytes to map. It may be `map_entire_file`, in which * case a mapping of the entire file is created. */ template<typename String> voidmap(const String& path, const size_type offset, const size_type length, std::error_code& error);
/** * Establishes a memory mapping with AccessMode. If the mapping is unsuccesful, the * reason is reported via `error` and the object remains in a state as if this * function hadn't been called. * * `path`, which must be a path to an existing file, is used to retrieve a file * handle (which is closed when the object destructs or `unmap` is called), which is * then used to memory map the requested region. Upon failure, `error` is set to * indicate the reason and the object remains in an unmapped state. * * The entire file is mapped. */ template<typename String> voidmap(const String& path, std::error_code& error) { map(path, 0, map_entire_file, error); }
/** * Establishes a memory mapping with AccessMode. If the mapping is * unsuccesful, the reason is reported via `error` and the object remains in * a state as if this function hadn't been called. * * `handle`, which must be a valid file handle, which is used to memory map the * requested region. Upon failure, `error` is set to indicate the reason and the * object remains in an unmapped state. * * `offset` is the number of bytes, relative to the start of the file, where the * mapping should begin. When specifying it, there is no need to worry about * providing a value that is aligned with the operating system's page allocation * granularity. This is adjusted by the implementation such that the first requested * byte (as returned by `data` or `begin`), so long as `offset` is valid, will be at * `offset` from the start of the file. * * `length` is the number of bytes to map. It may be `map_entire_file`, in which * case a mapping of the entire file is created. */ voidmap(const handle_type handle, const size_type offset, const size_type length, std::error_code& error);
/** * Establishes a memory mapping with AccessMode. If the mapping is * unsuccesful, the reason is reported via `error` and the object remains in * a state as if this function hadn't been called. * * `handle`, which must be a valid file handle, which is used to memory map the * requested region. Upon failure, `error` is set to indicate the reason and the * object remains in an unmapped state. * * The entire file is mapped. */ voidmap(const handle_type handle, std::error_code& error) { map(handle, 0, map_entire_file, error); }
basic_mmap<AccessMode, ByteT>的map方法实现
一个是针对 file handle type 为 文件路径的。类型为 String&
一个是针对 file handle type 为 通用句柄的。如果是 WIN32 则 file handle type 为 HANDLE,其他为 int 。
constauto ctx = detail::memory_map(handle, offset, length == map_entire_file ? (file_size - offset) : length, AccessMode, error); if(!error) { // We must unmap the previous mapping that may have existed prior to this call. // Note that this must only be invoked after a new mapping has been created in // order to provide the strong guarantee that, should the new mapping fail, the // `map` function leaves this instance in a state as though the function had // never been invoked. unmap(); file_handle_ = handle; is_handle_internal_ = false; data_ = reinterpret_cast<pointer>(ctx.data); length_ = ctx.length; mapped_length_ = ctx.mapped_length; #ifdef _WIN32 file_mapping_handle_ = ctx.file_mapping_handle; #endif } }
/** * Returns the last platform specific system error (errno on POSIX and * GetLastError on Win) as a `std::error_code`. */ inline std::error_code last_error()noexcept { std::error_code error; #ifdef _WIN32 error.assign(GetLastError(), std::system_category()); #else error.assign(errno, std::system_category()); #endif return error; }
Linux 134 错误码
在 Linux 系统中,错误码 134 对应的是 EOWNERDEAD(Owner died)。以下是详细解释:
/* Return value of `mmap' in case of an error. */ #define MAP_FAILED ((void *) -1)
__BEGIN_DECLS /* Map addresses starting near ADDR and extending for LEN bytes. from OFFSET into the file FD describes according to PROT and FLAGS. If ADDR is nonzero, it is the desired mapping address. If the MAP_FIXED bit is set in FLAGS, the mapping will be at ADDR exactly (which must be page-aligned); otherwise the system chooses a convenient nearby address. The return value is the actual mapping address chosen or MAP_FAILED for errors (in which case `errno' is set). A successful `mmap' call deallocates any previous mapping for the affected region. */
#ifndef __USE_FILE_OFFSET64 externvoid *mmap(void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset) __THROW; #else # ifdef __REDIRECT_NTH externvoid * __REDIRECT_NTH (mmap, (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off64_t __offset), mmap64); # else # define mmap mmap64 # endif #endif #ifdef __USE_LARGEFILE64 externvoid *mmap64(void *__addr, size_t __len, int __prot, int __flags, int __fd, __off64_t __offset) __THROW; #endif
详解内存映射系统调用 mmap
1 2 3 4 5 6 7
#include<sys/mman.h> void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset);