VirtualMemory class
Owns one region allocated with VirtualAlloc.
- Use
import ThinkMeta.ConcurrentTasks.Windows.VirtualMemory;- Namespace
ThinkMeta::ConcurrentTasks::Windows
Syntax
class VirtualMemoryRemarks
The region is released (MEM_RELEASE) on destruction. Only the owner calls Reserve and Release, from one thread at a time; the static members act on any address and are as thread-safe as the Win32 calls they wrap.
See also
Constructors
VirtualMemory
Allocates a region (VirtualAlloc).
Syntax
VirtualMemory(std::size_t size, DWORD allocationType, DWORD protection) noexcept;Parameters
sizestd::size_t- Size in bytes; rounded up to whole pages.
allocationTypeDWORDMEM_RESERVE,MEM_COMMITor both, optionally withMEM_LARGE_PAGESorMEM_TOP_DOWN.protectionDWORD- Page protection, e.g.
PAGE_READWRITEorPAGE_NOACCESS.
Remarks
Calls VirtualAlloc(nullptr, size, allocationType, protection). Check IsValid; on failure GetLastError has the reason.
~VirtualMemory
Methods
IsValid
Returns whether the object owns a region.
Syntax
bool IsValid() const noexcept;Return value
Type: bool
true if a region is owned; otherwise false.
GetAllocationBase
Returns the start of the region, or nullptr.
Syntax
void* GetAllocationBase() noexcept;Return value
Type: void*
The region's base address; nullptr if the object owns no region.
AtOffset
Returns the address offset bytes into the region.
Syntax
void* AtOffset(std::size_t offset) noexcept;Parameters
offsetstd::size_t- Distance in bytes from the start of the region; must lie inside it.
Return value
Type: void*
The region's base address plus offset.
Remarks
No bounds check.
Reserve
Reserves address space.
Syntax
ErrorCode Reserve(std::size_t size) noexcept;Parameters
sizestd::size_t- Bytes to reserve (
MEM_RESERVE,PAGE_NOACCESS).
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
Releases the current region, if any, first. Pages must be committed (Commit) before they are touched.
See also
Release
Releases the whole region (MEM_RELEASE).
Syntax
ErrorCode Release() noexcept;Return value
Type: ErrorCode
Error::Success (0) on success, also if there was nothing to release; otherwise the Win32 error code.
Remarks
The object owns nothing afterwards, even if VirtualFree failed.
See also
Commit
Commits pages (MEM_COMMIT).
Syntax
static ErrorCode Commit(void* address, std::size_t size, DWORD protection) noexcept;Parameters
addressvoid*- Start of the range; rounded down to a page boundary.
sizestd::size_t- Length of the range in bytes; every page it touches is committed.
protectionDWORD- Page protection, e.g.
PAGE_READWRITE.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
The range must lie inside an existing reservation.
See also
Decommit
Decommits pages (MEM_DECOMMIT).
Syntax
static ErrorCode Decommit(void* address, std::size_t size) noexcept;Parameters
addressvoid*- Start of the range; rounded down to a page boundary.
sizestd::size_t- Length of the range in bytes; every page it touches is decommitted.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
The address range stays reserved.
See also
Lock
Locks pages into physical memory (VirtualLock).
Syntax
static ErrorCode Lock(void* address, std::size_t size) noexcept;Parameters
addressvoid*- Start of the range; the pages must be committed.
sizestd::size_t- Length of the range in bytes; every page it touches is locked.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
Limited by the working set size.
See also
Unlock
Unlocks pages locked with Lock (VirtualUnlock).
Syntax
static ErrorCode Unlock(void* address, std::size_t size) noexcept;Parameters
addressvoid*- Start of the range.
sizestd::size_t- Length of the range in bytes; every page it touches is unlocked.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Protect
Changes the protection of committed pages (VirtualProtect).
Syntax
static ErrorCode Protect(
void* address,
std::size_t size,
DWORD protection,
DWORD* oldProtection
) noexcept;Parameters
addressvoid*- Start of the range; the pages must be committed.
sizestd::size_t- Length of the range in bytes; every page it touches changes.
protectionDWORD- The new page protection, e.g.
PAGE_READONLY. oldProtectionDWORD*- Receives the old protection of the first page; required.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Changes the protection of committed pages (VirtualProtect).
Syntax
static ErrorCode Protect(
void* address,
std::size_t size,
DWORD protection
) noexcept;Parameters
addressvoid*- Start of the range; the pages must be committed.
sizestd::size_t- Length of the range in bytes; every page it touches changes.
protectionDWORD- The new page protection, e.g.
PAGE_READONLY.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
Discards the old protection.
Query
Describes the pages around an address (VirtualQuery).
Syntax
static ErrorCode Query(const void* address, MEMORY_BASIC_INFORMATION* mbi) noexcept;Parameters
addressconst void*- Any address; the pages around it are described.
mbiMEMORY_BASIC_INFORMATION*- Receives the description; required.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Generated from ThinkMeta.ConcurrentTasks.API @ 137ec1d (2026-10-17).