wRTOS Contiguous Memory Allocation Calls Programming Example
This sample demonstrates how to allocate, retrieve, and free contiguous physical memory using wRTOS APIs:
- RtAllocateContiguousMemory allocates memory below a specified physical address
- RtGetPhysicalAddress retrieves the physical address of the allocation
- RtFreeContiguousMemory frees the memory afterward.
You can use this sample to verify memory allocation limits or test driver compatibility with physical memory mappings.
Building the Sample
Build using any of the four supported configurations: Debug, Release, RTSSDebug, or RTSSRelease.
Running the Sample
Run the program as an EXE or RTSS binary.
Expected Results
When successful, the sample program allocates contiguous memory, retrieves its physical address, and frees it without errors.
Unexpected Results
The program generates failure messages, which are typically caused by insufficient memory or invalid allocation parameters.
Example Code
#include <windows.h> #include <stdio.h> #include <rtapi.h>
static PVOID vAddress; // virtual memory address returned by // RtAllocateContiguousMemory
static LARGE_INTEGER pAddress; // physical memory address returned by // RtGetPhysicalAddress
void main(void)
{
LARGE_INTEGER maxPhyAddr; //highest physical memory address
ULONG size=0; //bytes to allocate
maxPhyAddr.QuadPart = 0xFFFFFF;
size= 0x1000; // Size in KB.// Allocate memory vAddress = RtAllocateContiguousMemory( size, maxPhyAddr); if (!vAddress)
{
printf("\nFailure on RtAllocateContiguousMemory\t");
printf("Error=%d\n", GetLastError());
break;
}
else {
printf("\nSuccess on RtAllocateContiguousMemory\n");
printf("Virtual memory address = 0x%08X\n", vAddress);
// Get the physical address
pAddress = RtGetPhysicalAddress(vAddress);
if (!pAddress.QuadPart) {
printf("\nFailure on RtGetPhysicalAddress(0x%08X).\t, vAddress");
printf("Error=%d\n", GetLastError());} else
{
printf("Success on RtGetPhysicalAddress(0x%08X).\n", vAddress);
}
// Free memory
if (!RtFreeContiguousMemory(vAddress)) {
printf("\nFailure on
RtFreeContiguousMemory(0x%08X).\t", vAddress);
printf("Error Code = %d\n", GetLastError());
}
else {
printf("Success on RtFreeContiguousMemory(0x%X).\n", vAddress);
}
}ExitProcess(0); }
Remarks
- The sample allocates 0x1000 bytes (4 KB) of contiguous physical memory, constrained to be below the physical address 0xFFFFFF.
- All memory operations are validated with GetLastError() reporting in case of failure.
- If any operation fails, the program outputs a clear error message and exits.
APIs Referenced
|
RTAPI |
|---|
See Also