13#ifndef NUMAPP_MEMORY_HPP_
14#define NUMAPP_MEMORY_HPP_
20#include <memory_resource>
22#include <system_error>
75[[nodiscard]] std::optional<
int>
GetNodeDistance(
int node1,
int node2) noexcept;
89[[nodiscard]] std::optional<
int>
GetNodeOfCpu(
int cpu) noexcept;
157#if defined(MCL_ONFAULT) || defined(DOXYGEN)
194[[nodiscard]] std::error_code
MemLock(
void const* addr, std::size_t len,
LockFlag flag)
noexcept;
207[[nodiscard]] std::error_code
MemUnlock(
void const* addr, std::size_t len)
noexcept;
307[[nodiscard]]
void*
Allocate(std::
size_t size,
308 MemPolicy const& policy,
309 std::error_code& ec) noexcept;
314[[nodiscard]]
void*
Allocate(std::
size_t size,
315 MemPolicy const& policy,
317 std::error_code& ec) noexcept;
322[[nodiscard]]
void*
Allocate(std::
size_t size,
323 MemPolicy const& policy);
328[[nodiscard]]
void*
Allocate(std::
size_t size,
329 MemPolicy const& policy,
330 MemPolicyFlag flags);
336void Free(
void* ptr, std::
size_t size, std::error_code& ec) noexcept;
342void Free(
void* ptr, std::
size_t size);
387 auto
GetFlags() const noexcept -> MemPolicyFlag;
390 auto do_allocate(std::
size_t bytes, std::
size_t alignment) ->
void* override;
391 void do_deallocate(
void* p, std::
size_t bytes, std::
size_t alignment) override;
392 auto do_is_equal(std::pmr::memory_resource const& other) const noexcept ->
bool override;
395 MemPolicyFlag m_flags;
422 LockResource(std::pmr::memory_resource* upstream)
noexcept;
455 auto
GetUpstream() const noexcept -> std::pmr::memory_resource*;
458 auto do_allocate(std::
size_t bytes, std::
size_t alignment) ->
void* override;
459 void do_deallocate(
void* p, std::
size_t bytes, std::
size_t alignment) override;
460 auto do_is_equal(std::pmr::memory_resource const& other) const noexcept ->
bool override;
462 std::pmr::memory_resource* m_upstream;
560 constexpr auto Size()
const noexcept -> std::size_t;
626 std::error_code& ec)
noexcept;
639 MemPolicyFlag flags);
654void FreeHuge(
void* ptr, std::size_t size,
HugePageSize page_size, std::error_code& ec)
noexcept;
691 MemPolicyFlag flags = MemPolicyFlag::None);
711 auto
GetFlags() const noexcept -> MemPolicyFlag;
719 auto do_allocate(std::
size_t bytes, std::
size_t alignment) ->
void* override;
720 void do_deallocate(
void* p, std::
size_t bytes, std::
size_t alignment) override;
721 auto do_is_equal(std::pmr::memory_resource const& other) const noexcept ->
bool override;
725 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.