Dynamic MSpace Configuration Sample
You can use the provided sample code, building as RTSS processes and Windows processes, respectively, to avoid memory request errors when memory allocation spaces (MSpaces) are configured to not expand automatically when exhausted (Auto expand MSpace is turned off in wRTOS Settings).
Building the Sample
Build using any of the four supported configurations: RTSSDebug, RTSSRelease (to build Mspace.rtss) or Debug, Release (to build Mspace.exe)
Running the Sample
Deploy and run the RTSS process and Windows process binaries on the wRTOS system.
Expanding MSpaces from an RTSS process:
Call DynamicExpandMSpaceFromRtss with parameters specifying:
- Whether the target is the system or user process (IsSystem)
- Whether to check the internal or external MSpace (IsInternal)
- Requested allocation size in bytes (RequestSizeInbytes)
The DynamicExpandMSpaceFromRtss function performs these steps:
- Calls RtGetProcessMSpace to obtain process MSpace descriptors.
- Calls RtQueryProcessMSpace to query MSpace usage.
- If auto expansion is disabled, and contiguous free memory is insufficient, calls RtExpandMSpace to increase the MSpace size.
Expanding MSpaces from a Windows process:
Call DynamicExpandMSpaceFromWindows with the requested size, in bytes.
Expected Results
- If the MSpace is expanded, no direct output occurs (the function returns after expansion).
- This output appears if the MSpace already has sufficient free memory, or auto expansion is enabled:
Auto expand enabled or requested MSpace size ([requested size]) bytes is already available.
Sample Code
Sample code is provided for:
Expanding MSpaces from an RTSS Process
//
// Expand MSpaces from RTSS Processes
// This routine dynamically expands MSpace if requested memory size in bytes is not available
//
void DynamicExpandMSpaceFromRtss(BOOL IsSystem, BOOL IsInternal, ULONG RequestSizeInbytes)
{
MSPACE_INFO MSpaceInfo;
mspace ExtMSpace, IntMSpace;
HANDLE hProcess = NULL;
//
//Step #1
// Call RtGetProcessMSpace:
//
// a. to retrieve the descriptors of the MSpaces for the calling process, pass GetCurrentProcess() for parameter
// hProcess.
// b. to retrieve the descriptors of the MSpaces for the system process, pass NULL for parameter hProcess.
//
if (IsSystem == FALSE)
hProcess = GetCurrentProcess();
else
hProcess = NULL;
if (!RtGetProcessMSpace(
hProcess,
&ExtMSpace,
&IntMSpace))
return;
//
//Step #2
// Call RtQueryProcessMSpace and pass NormalPerformanceLevel for parameter PerformanceLevel to query MSpace
// usage for the current process and system process.
//
if (IsInternal == TRUE)
{
// Query allocation information for the internal MSpace of the process.
if (!RtQueryProcessMSpace(
IntMSpace,
&MSpaceInfo,
NormalPerformanceLevel))
return;
}
else
{
// Query allocation information for the external MSpace of the process.
if (!RtQueryProcessMSpace(
ExtMSpace,
&MSpaceInfo,
NormalPerformanceLevel))
return;
}
//
//Step #3
// If the returning MaxContigFreeMem value is less than the memory size you plan to allocate, and AutoExpandEnabled
// is FALSE,
// call RtExpandMSpace to expand the MSpace with the size you plan to allocate.
//
if ((MSpaceInfo.AutoExpandEnabled == FALSE) && (MSpaceInfo.MaxContigFreeMem < RequestSizeInbytes))
{
//
//Step # 5
// Allocate the memory with the size you plan to use.
//
if (IsInternal == TRUE)
{
if (!RtExpandMSpace(IntMSpace, RequestSizeInbytes))
return;
}
else
if (!RtExpandMSpace(ExtMSpace, RequestSizeInbytes))
return;
}
else
{
RtPrintf("Auto expand enabled or requested MSpace size (%d) bytes is already available.. \n",
RequestSizeInbytes);
}
return;
}
Expanding MSpaces from a Windows Process
//
// Expand MSpaces from Windows Processes
// This routine dynamically expands system process internal MSpace if requested memory size in bytes is not available
//
void DynamicExpandMSpaceFromWindows(ULONG RequestSizeInbytes)
{
MSPACE_INFO MSpaceInfo;
mspace IntMSpace, ExtMSpace;
HANDLE hProcess = NULL;
//
//Step # 1
// Call RtGetProcessMSpace to retrieve the descriptors of the MSpaces for the system process by passing NULL for
// parameter hProcess.
//
if (!RtGetProcessMSpace(
NULL,
&ExtMSpace,
&IntMSpace))
return;
//
//Step #2
// Call RtQueryProcessMSpace by passing the descriptor of the system process internal MSpace for MSpace parameter and
// passing NormalPerformanceLevel for parameter PerformanceLevel.
//
if (!RtQueryProcessMSpace(
IntMSpace,
&MSpaceInfo,
NormalPerformanceLevel))
return;
//
//Step #3.
// If the returning MaxContigFreeMem value is less than the image size of the RTSS process you plan to start, and
// AutoExpandEnabled is FALSE,
// call RtExpandMSpace to expand the system process internal MSpace with that image size.
//
if ((MSpaceInfo.AutoExpandEnabled == FALSE) && (MSpaceInfo.MaxContigFreeMem < RequestSizeInbytes))
{
if (!RtExpandMSpace(IntMSpace, RequestSizeInbytes))
return;
}
else
{
RtPrintf("Auto expand enabled or requested MSpace size (%d) bytes is already available.. \n", RequestSizeInbytes);
}
return;
}
APIs Referenced
|
RTAPI |
|---|