HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
locked_memory_allocator_win32_impl.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
8
9#include "locked_memory_allocator_intf.hpp"
10#include "../telemetry/telemetry.hpp"
11#include "../utility/utility.hpp"
12#include "../macros.hpp"
13
14hi_export_module(hikogui.memory.locked_memory_allocator : impl);
15
16hi_export namespace hi::inline v1 {
17
18[[nodiscard]] inline std::byte *locked_memory_allocator_allocate(std::size_t n) noexcept
19{
21 if (p == NULL) {
22 hi_log_fatal("Could not allocate locked memory. '{}'", get_last_error_message());
23 }
24
28
30 hi_log_warning("Could not get process working set size. '{}'", get_last_error_message());
31
32 } else {
36 hi_log_warning(
37 "Could not set process working set size to {}:{}. '{}'",
41
42 } else {
43 if (!VirtualLock(p, n)) {
44 hi_log_warning("Could not lock memory. '{}'", get_last_error_message());
45 }
46 }
47 }
48 return static_cast<std::byte *>(p);
49}
50
51inline void locked_memory_allocator_deallocate(std::byte *p, std::size_t n) noexcept
52{
53 if (not VirtualUnlock(p, n)) {
54 hi_log_warning("Could not unlock memory. '{}'", get_last_error_message());
55
56 } else {
60
62 hi_log_warning("Could not get process working set size. '{}'", get_last_error_message());
63
64 } else {
68 hi_log_warning(
69 "Could not set process working set size to {}:{}. '{}'",
73 }
74 }
75 }
76
77 if (not VirtualFree(p, 0, MEM_RELEASE)) {
78 hi_log_fatal("Could not deallocate locked memory. '{}'", get_last_error_message());
79 }
80}
81
82} // namespace hi::inline v1
Rules for working with win32 headers.
DOXYGEN BUG.
Definition algorithm.hpp:16
std::string get_last_error_message()
Get the OS error message from the last error received on this thread.
Definition exception_win32_impl.hpp:31
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
T ceil(T... args)