Allocating and freeing memory
Information about API functions is provided in the table below.
Using the API
Virtual memory pages may be free, reserved, or committed. Free pages that are used to allocate a virtual memory region become reserved pages. Reserved pages may or may not be mapped to physical memory. Reserved pages that are mapped to physical memory are committed pages.
Pages of a virtual memory region that are allocated by the KnVmAllocate()
function call can be committed in three different ways:
- Fully committed when the region is allocated.
- Fully or partially committed after allocation of the region (by calling the
KnVmCommit()
function). - Committed whenever virtual addresses are queried (so-called "lazy" mode).
When the first option is used, the entire required volume of physical memory is allocated immediately after the virtual memory region is reserved. When the second or third option is used, the virtual memory region is reserved without allocating physical memory. This lets you conserve physical memory if a reserved virtual memory region will not actually be required or will only be partially used. In addition, a virtual memory region can be allocated faster if its allocation does not include commitment.
When in "lazy" mode, physical memory is allocated only when the virtual memory region is actually queried. In this case, the page containing the queried address and several pages before and after this address are committed.
If you call the KnVmAllocate()
function with the VMM_FLAG_COMMIT
and VMM_FLAG_LOCKED
flags, the virtual memory region will be reserved and fully committed. If you call the KnVmAllocate()
function with the VMM_FLAG_COMMIT
flag but without the VMM_FLAG_LOCKED
flag, the virtual memory region will be reserved and committed only in "lazy" mode. If you call the KnVmAllocate()
function with the VMM_FLAG_LOCKED
flag but without the VMM_FLAG_COMMIT
flag, the virtual memory region will be reserved without commitment and a subsequent call of the KnVmCommit()
function will fully commit this region. If you call the KnVmAllocate()
function without the VMM_FLAG_COMMIT
and VMM_FLAG_LOCKED
flags, the virtual memory region will be reserved without commitment and a subsequent call of the KnVmCommit()
function will commit this region in "lazy" mode.
A virtual memory region is allocated when the MDL buffer, DMA buffer or MMIO memory region is mapped to process memory. This region is allocated by the mapping function.
A protected page may reside at the beginning and/or end of the virtual memory region. This page is never committed. Any query of this page will result in an exception signaling that the region boundaries have been exceeded.
To change the access rights to the virtual memory region, call the KnVmProtect()
function. You can fully close and then re-open access to a region while retaining is contents.
To free physical memory while retaining the reservation of virtual addresses, call the KnVmDecommit()
or KnVmReset()
function. In this case, the contents of the virtual memory region will be lost. After freeing physical memory by calling the KnVmDecommit()
function, you must call the KnVmCommit()
function to subsequently use the virtual memory region. After freeing physical memory by calling the KnVmReset()
function, the virtual memory region can be used without any additional actions required. This virtual memory region will correspond to memory allocated by calling the KnVmAllocate()
function with the VMM_FLAG_COMMIT
flag but without the VMM_FLAG_LOCKED
flag.
The KnVmProtect()
, KnVmDecommit()
, and KnVmReset()
functions cannot be used if the MDL buffer, DMA buffer or MMIO memory region is mapped to the virtual memory region.
To free the virtual memory region, call the KnVmUnmap()
function. When this function is called, the reserved pages will become free regardless of whether or not they were committed, and the physical memory mapped to committed pages is also freed.
The KnVmUnmap()
function frees the virtual addresses of a region mapped to the MDL buffer, but does not delete the MDL buffer. In addition, this function cannot be used if the DMA buffer or MMIO memory region is mapped to the virtual memory region.
The KnVmCommit()
, KnVmProtect()
, KnVmDecommit()
, KnVmReset()
, and KnVmUnmap()
functions can be applied for the entire allocated virtual memory region or for a portion of it.
The functions presented in this section provide the basis for implementing memory allocation/deallocation functions of the libkos library, and functions of POSIX interfaces such as malloc()
, calloc()
, realloc()
, free()
, mmap()
, and munmap()
.
Information about API functions
vmm_api.h functions
Function |
Information about the function |
---|---|
|
Purpose Allocates (reserves and optionally commits) a virtual memory region. Parameters
Returned values If successful, the function returns the base address of the allocated virtual memory region, otherwise it returns Additional information In the
Permissible combinations of flags defining the access rights to the virtual memory region:
|
|
Purpose Commits a virtual memory region. Parameters
Returned values If successful, the function returns |
|
Purpose Decommits a virtual memory region. Parameters
Returned values If successful, the function returns |
|
Purpose Modifies the access rights to the virtual memory region. Parameters
Returned values If successful, the function returns Additional information In the
|
|
Purpose Frees up the virtual memory region. Parameters
Returned values If successful, the function returns |
|
Purpose Gets information about a virtual memory page. Parameters
Returned values If successful, the function returns |
|
Purpose Decommits a virtual memory region. Parameters
Returned values If successful, the function returns |