NUMA++ 0.11.0
Loading...
Searching...
No Matches
allocHugeExample.cpp
1#include <numapp/memory.hpp>
2
3void Example() {
4 // Allocate 16MiB from NUMA node 1 in 2MiB pages.
5 auto const size = 16 * 1024 * 1024;
6 auto const page_size = numapp::HugePagePreset::Huge2M;
7
8 // 1) AllocateHuge will allocate size, rounding up to nearest page_size
9 // multiple
10 void* ptr = numapp::AllocateHuge(size,
11 page_size,
13 numapp::MemPolicyFlag::Strict);
14
15 // 2) It is particularly important to pre-fault huge-pages as the likelihood
16 // for failure is higher. If page-fault would fail when memory is accessed
17 // the result is a segmentation fault.
18 if (auto ec = numapp::MemLock(ptr, size, numapp::LockFlag::PreFault); ec) {
19 // Page-fault failed; this would have caused a segmentation fault if not
20 // for `numapp::MemLock()`.
21 throw std::system_error(ec, "Page fault failed");
22 }
23
24 // 3) FreeHuge will free memory, rounding up to nearest page_size multiple
25 // as required by mmap.
26 numapp::FreeHuge(ptr, size, page_size);
27}
static MemPolicy MakeBindNode(int node)
Creates a strict policy (using MPOL_BIND) to allocate all memory to the specified node.
void FreeHuge(void *ptr, std::size_t size, HugePageSize page_size, std::error_code &ec) noexcept
Free huge pages previously allocated with AllocateHuge.
Definition memory.cpp:205
void * AllocateHuge(std::size_t size, HugePageSize page_size, MemPolicy const &policy, MemPolicyFlag flags, std::error_code &ec) noexcept
Non-throwing version of AllocateHuge()
Definition memory.cpp:165
@ Huge2M
2 MiB page size.
Definition memory.hpp:505
std::error_code MemLock(void const *addr, std::size_t len, LockFlag flag) noexcept
Lock memory pages in the specified address range.
Definition memory.cpp:58
@ PreFault
Locks pages whether they are resident or not.
Definition memory.hpp:126
Contains memory function declarations.