15#ifndef NUMAPP_MEMORY_HPP_
16#define NUMAPP_MEMORY_HPP_
22#include <memory_resource>
24#include <system_error>
77[[nodiscard]] std::optional<
int>
GetNodeDistance(
int node1,
int node2) noexcept;
91[[nodiscard]] std::optional<
int>
GetNodeOfCpu(
int cpu) noexcept;
159#if defined(MCL_ONFAULT) || defined(DOXYGEN)
196[[nodiscard]] std::error_code
MemLock(
void const* addr, std::size_t len,
LockFlag flag)
noexcept;
209[[nodiscard]] std::error_code
MemUnlock(
void const* addr, std::size_t len)
noexcept;
309[[nodiscard]]
void*
Allocate(std::
size_t size,
310 MemPolicy const& policy,
311 std::error_code& ec) noexcept;
316[[nodiscard]]
void*
Allocate(std::
size_t size,
317 MemPolicy const& policy,
319 std::error_code& ec) noexcept;
324[[nodiscard]]
void*
Allocate(std::
size_t size,
325 MemPolicy const& policy);
330[[nodiscard]]
void*
Allocate(std::
size_t size,
331 MemPolicy const& policy,
332 MemPolicyFlag flags);
338void Free(
void* ptr, std::
size_t size, std::error_code& ec) noexcept;
344void Free(
void* ptr, std::
size_t size);
389 auto
GetFlags() const noexcept -> MemPolicyFlag;
392 auto do_allocate(std::
size_t bytes, std::
size_t alignment) ->
void* override;
393 void do_deallocate(
void* p, std::
size_t bytes, std::
size_t alignment) override;
394 auto do_is_equal(std::pmr::memory_resource const& other) const noexcept ->
bool override;
397 MemPolicyFlag m_flags;
424 LockResource(std::pmr::memory_resource* upstream)
noexcept;
457 auto
GetUpstream() const noexcept -> std::pmr::memory_resource*;
460 auto do_allocate(std::
size_t bytes, std::
size_t alignment) ->
void* override;
461 void do_deallocate(
void* p, std::
size_t bytes, std::
size_t alignment) override;
462 auto do_is_equal(std::pmr::memory_resource const& other) const noexcept ->
bool override;
464 std::pmr::memory_resource* m_upstream;
562 constexpr auto Size()
const noexcept -> std::size_t;
628 std::error_code& ec)
noexcept;
641 MemPolicyFlag flags);
656void FreeHuge(
void* ptr, std::size_t size,
HugePageSize page_size, std::error_code& ec)
noexcept;
693 MemPolicyFlag flags = MemPolicyFlag::None);
713 auto
GetFlags() const noexcept -> MemPolicyFlag;
721 auto do_allocate(std::
size_t bytes, std::
size_t alignment) ->
void* override;
722 void do_deallocate(
void* p, std::
size_t bytes, std::
size_t alignment) override;
723 auto do_is_equal(std::pmr::memory_resource const& other) const noexcept ->
bool override;
727 MemPolicyFlag m_flags;
Describes a huge page size and maintains the invariant that page size is an integral power of 2,...
constexpr auto Size() const noexcept -> std::size_t
Query size in bytes.
HugePageSize(HugePagePreset preset) noexcept
Construct from preset page size value.
auto operator<=>(HugePageSize const &) const noexcept -> std::strong_ordering=default
Three-way comparison operator.
LockResource & operator=(LockResource const &)=delete
LockResource is not copy assignable.
virtual ~LockResource()=default
Destroy LockResource.
LockResource(LockFlag flag) noexcept
Initialize with specified flag and upstream memory resource initialized from std::pmr::get_default_re...
auto GetUpstream() const noexcept -> std::pmr::memory_resource *
Get upstream memory resource.
auto GetFlags() const noexcept -> LockFlag
Get lock flag in use.
Class representing a memory policy that can be modified and used to apply to the current thread or a ...
Contains definitions for bitwise operators for enums.
#define NUMAPP_ENABLE_BITFLAG(enum)
Enables numapp bitwise operators in overload set for the specified enumeration enum.
void FreeHuge(void *ptr, std::size_t size, HugePageSize page_size, std::error_code &ec) noexcept
Free huge pages previously allocated with AllocateHuge.
auto EncodeMmapFlags(HugePageSize page_size) noexcept -> int
Encodes page size into bit representation expected by mmap().
HugePagePreset
Preset huge page sizes.
void * AllocateHuge(std::size_t size, HugePageSize page_size, MemPolicy const &policy, MemPolicyFlag flags, std::error_code &ec) noexcept
Non-throwing version of AllocateHuge()
@ Huge64k
64 kiB page size.
@ Huge16k
16 KiB page size.
@ Huge16M
16 MiB page size.
@ Huge256k
256 kiB page size.
@ Huge256M
256 MiB page size.
int GetNumNodes() noexcept
Query number of configured NUMA nodes.
std::error_code MemLock(void const *addr, std::size_t len, LockFlag flag) noexcept
Lock memory pages in the specified address range.
std::size_t GetPageSize() noexcept
Fast query of system page size.
LockAllFlag
Flags that are combined to modify behaviour of MemLockAll().
std::error_code MemUnlock(void const *addr, std::size_t len) noexcept
Unlock memory pages in the specified address range.
std::optional< int > GetNodeDistance(int node1, int node2) noexcept
Get NUMA distance between two nodes.
void Free(void *ptr, std::size_t size, std::error_code &ec) noexcept
See group for details.
LockFlag
Mutually exclusive flags that modifies behaviour of MemLock().
void * Allocate(std::size_t size, MemPolicy const &policy, std::error_code &ec) noexcept
See group for details.
std::error_code MemUnlockAll() noexcept
Unlock all locked memory in this process.
std::error_code MemLockAll(LockAllFlag flags) noexcept
Lock all memory pages as specified by provided flags.
std::optional< int > GetNodeOfCpu(int cpu) noexcept
Get NUMA node of the given CPU.
@ Current
Lock all pages which are currently mapped into the address space of the process.
@ Future
Lock all future pages that are mapped into the address space of the process.
@ PreFault
Locks pages whether they are resident or not.
@ OnFault
Locks currently resident pages as well as future resident pages when they are first faulted in.
Contains declarations for numapp::MemPolicy.
Contains declarations for Nodemask.