Skip to content

shared_memory map

Alairion edited this page May 8, 2021 · 3 revisions

Functions

    template<typename T>
(1) unique_map_t<T> map(std::uint64_t offset, shared_memory_options options = /*see below*/) const;
    template<typename T, typename ValueType = typename std::remove_extent<T>::type>
(2) unique_map_t<T> map(std::uint64_t offset, std::size_t count, shared_memory_options options = /*see below*/) const;
    template<typename T>
(3) shared_map_t<T> shared_map(std::uint64_t offset, shared_memory_options options = /*see below*/) const;
    template<typename T, typename ValueType = typename std::remove_extent<T>::type>
(4) shared_map_t<T> shared_map(std::uint64_t offset, std::size_t count, shared_memory_options options = /*see below*/) const;
  1. Maps the shared memory in the process virtual memory. This overload only works if T is a trivial type. If T is constant, options value is nes::shared_memory_options::constant, otherwise it is nes::shared_memory_options::none.
  2. Maps the shared memory in the process virtual memory. This overload only works if T is an unbounded array type, i.e. ValueType[], and ValueType is a trivial type. If ValueType is constant, options value is nes::shared_memory_options::constant, otherwise it is nes::shared_memory_options::none. This overload is useful for compile-time unknown array size.
  3. Same as (1) except that the map is shareable within the calling process.
  4. Same as (2) except that the map is shareable within the calling process.

Parameters

Name Description
offset The offset of the map, in bytes, from the start of the shared memory object.
count The number of elements, of type ValueType, to map.
options Additional flags. If it is nes::shared_memory_options::constant then the map is a read-only map so any write operation is the map will lead to an error. You should always set it to nes::shared_memory_options::constant if you don't need to write inside the shared memory. If you give a constant type to the functions, nes::shared_memory_options::constant is the default value of options, otherwise it is nes::shared_memory_options::none.

Return value

  1. Returns a nes::unique_map_t<T> containing the map.
  2. Returns a nes::unique_map_t<T> containing the map. Because T is an unbounded array type, this nes::unique_map_t<T> defines the operator[] overload, and can be used as an array.
  3. Returns a nes::shared_map_t<T> that is constructed from the returns of (1).
  4. Returns a nes::shared_map_t<T> that is constructed from the returns of (2). Because T is an unbounded array type, this nes::shared_map_t<T> defines the operator[] overload, and can be used as an array.

Preconditions

*this must represents a valid shared memory object.

  1. offset + sizeof(T) must be lesser or equals to the size of the shared memory.
  2. offset + (sizeof(ValueType) * count) must be lesser or equals to the size of the shared memory
  3. Same as (1).
  4. Same as (2).

Complexity

Constant.

Exceptions

May throw a std::runtime_error if the mapping can not be done.

Implementation details

On Windows, the shared memory is mapped using MapViewOfFile
On Posix systems, the shared memory is mapped using mmap

Clone this wiki locally