Dynamic MSpace Configuration Sample

Description

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 the Control Panel).

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)
{
	BOOL IsLocalMem = FALSE;
	MSPACE_INFO MSpaceInfo;
	mspace ExtMSpace, IntMSpace;
	HANDLE hProcess = NULL;

	//
	//Step # 1
	// Call RtIsDefaultLocalMemory to determine whether the system process and RTSS processes default to use local 
       // memory.
	//
	IsLocalMem = RtIsDefaultLocalMemory();

	// If default memory configuration is using Windows memory, return.
	if (IsLocalMem == FALSE)
		return;

	//
	//Step # 2 
	// If local memory is the default memory setting, 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 #3 
	// 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 #4.  
	// 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;
}

Expand 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)
{
	BOOL IsLocalMem = FALSE;
	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