[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

 

LGrowZone



Overview:

LGrowZone is a PowerPlant class to use for managing any low memory situations that occur during your program's execution. For general information about the Mac OS GrowZone concepts, refer to Inside Macintosh: Memory, published by Addison-Wesley.

LGrowZone's implementation provides two levels of functionality. First, it provides the classic behavior of allocating a user-specified emergency reserve buffer of memory upon creation of the GrowZone. The OS will then notify your application when to draw from this emergency reserve in low-memory situations. Second, LGrowZone can work with other PowerPlant objects to ask them to free memory when it becomes scarce.

Methods :

The methods in the LGrowZone class are:

 

LGrowZone()  
~LGrowZone()  
AskListenersToFree()  
DoGrowZone()  
GetGrowZone()  
GiveWarning()  
GrowZoneCallBack()  
MemoryIsLow()  
SpendTime()  
UseLocalReserve()  

Data Members:

The data members in the LGrowZone class are:

 

sGrowZone  
sGrowZoneUPP  
mLocalReserve  
mReserveSize  
mGiveWarning  
 

Operation:

Near the start of your application code you should create a single instance of LGrowZone, as shown here:


main() {
	// do init stuff here
	// create a memory reserve of 32K bytes
	new LGrowZone( 32 * 1024 );

	// the rest of your code goes here...
}

LGrowZone inherits from LBroadcaster. Objects which are able to free memory when it is becoming depleted should be Listeners, and attach themselves to the LGrowZone object so they will be notified when memory is low. The following code shows how to add your objects as listeners for this low-memory notification. This code attaches myObj as a Listener to the LGrowZone messages.


LGrowZone::GetGrowZone()->AddListener(myObj);

When memory is needed, you'll want myObj to be able to respond to a ListenToMessage() call. The call to ListenToMessage() will occur with a msg_GrowZone message, and a pointer to the number of bytes needed. You should add code similar to the following to your object's class definition to handle the ListenToMessage() method, that you inherit from LBroadcaster.


MyClass::ListenToMessage(					MessageT inMessage, 
													void *ioParam)
{
		if (inMessage == msg_GrowZone) {
				// Memory is low, free our cache
				SInt32 freedBytes = 					
				::GetHandleSize(myCache);
				::DisposeHandle(myCache);
				myCache = nil;

				// Pass back bytes freed
				*(SInt32*)ioParam = freedBytes;
		}
}

For example, suppose MyClass stores the myCache Handle with data generated from a lengthy calculation. If memory gets low, it can dispose of the cached data, meaning that it will have to recalculate the data if it needs it later. This is a common trade-off: memory versus speed.

This class also inherits from LPeriodical. LGrowZone's constructor installs the object in the Periodical Repeater queue so that its SpendTime() function gets called each time through the main event loop. This is required so that the LGrowZone object can monitor memory usage and can respond appropriately, perhaps by displaying a warning to the user.

Source files:

(Support Classes)

LGrowZone.h

LGrowZone.cp

See also:

LBroadcaster

LPeriodical

LGrowZone()

Purpose:

The constructor allocates a memory reserve of the passed-in size, and a GrowZone function for the current process is then registered with the operating system.

The constructor also installs this LGrowZone object in the Periodical Repeater queue defined by LPeriodical, so that the SpendTime() method will get called each time through the application's main event loop.

Access:

Public

Prototype:

LGrowZone(Size inReserveSize);
Parameters:

This method has the following parameter:

 

Size  
inReserveSize  
Indicates the size of the memory block that you wish to reserve in your heap area. The unit of this value is in bytes.  

Return:

None

~LGrowZone()

Purpose:

The destructor destroys the LGrowZone object. This includes deallocating the previously-allocated memory reserve and deinstalling the GrowZone function.

Access:

Public

Prototype:

virtual ~LGrowZone();
Parameters:

None

Return:

None

AskListenersToFree()

Purpose:

This method broadcasts a message to listeners that they should free up some memory if possible. In order for an object to be a listener, it must have registered with the LGrowZone object using AddListener().

Access:

Virtual, Public

Prototype:

virtual SInt32 AskListenersToFree( 
Size inBytesNeeded );
Parameters:

This method has the following parameter:

 

Size  
inBytesNeeded  
The number of bytes needed.  

Return:

SInt32 containing the number of bytes that were freed as a result of calling this method. If no memory could be freed, then it returns 0.

Remarks:

DoGrowZone()

Purpose:

This method is called by the GrowZone function installed by the LGrowZone() constructor when the operating system needs more memory.

Access:

Virtual, Protected

Prototype:

virtual DoGrowZone( Size inBytesNeeded );
Parameters:

This method has the following parameter:

 

Size  
inBytesNeeded  
The number of bytes needed.  

Return:

SInt32 containing the number of bytes that were freed as a result of calling this method. If no memory could be freed, then it returns 0.

Remarks:

This method is not normally called directly in your normal application code.

GetGrowZone()

Purpose:

This method is an accessor, designed to return a pointer to the LGrowZone object.

Access:

Inline, Public, Static

Prototype:

static LGrowZone* GetGrowZone();
Parameters:

None

Return:

A pointer to the LGrowZone object in your application.

Remarks:

You might need this method if you are going to call AddListener(). This would be done to register an object for future notification of low-memory conditions.

GiveWarning()

Purpose:

This method sets the mGiveWarning data member to true.

Access:

Public, Inline

Prototype:

void GiveWarning();
Parameters:

None

Return:

None

Remarks:

You might want to call this method if you wish to have the PowerPlant framework display a generic alert to the user when memory is depleted.

GrowZoneCallBack()

Purpose:

This method is called when the System requires more memory.

Access:

Protected, Static

Prototype:

static pascal SInt32 GrowZoneCallBack( 
Size inBytesNeeded );
Parameters:

This method has the following parameter:

 

Size  
inBytesNeeded  
The number of bytes needed.  

Return:

SInt32 containing the number of bytes that were freed by your program.

Remarks:

This is the GrowZone function registered with the System. It sets up the Motorola 68K A5 world so we can access globals, then calls a virtual function for the LGrowZone class.

MemoryIsLow()

Purpose:

This method provides a way of determining whether the memory reserve allocated by the LGrowZone() constructor has been utilized.

Access:

Virtual, Public

Prototype:

virtual Boolean MemoryIsLow();
Parameters:

None

Return:

Return true if the LGrowZone() memory reserve has been used, false otherwise.

Remarks:

Objects in your program can call this method if they wish to behave differently under low memory situations. For example, a program could disable the "New" and "Open" commands to prevent new Documents from being created when memory is low.

SpendTime()

Purpose:

This method performs periodic maintenance for the LGrowZone object, including

Access:

Virtual, Public

Prototype:

virtual void SpendTime(
const EventRecord& inMacEvent );
Parameters:

This method has the following parameter:

 

const
EventRecord&
 
inMacEvent  
a struct containing information about the Mac OS event that has just occurred  

An EventRecord reference is the parameter to this method, but it is not utilized by this override.

Return:

None

Remarks:This method is an override of the SpendTime() base class method from LPeriodical(). This method attempts to reallocate the LGrowZone() memory reserve if necessary, and (optionally) warn the user if memory is getting low. This method is called each time through the main event loop. In order for the framework to provide a warning, the GiveWarning() method must be called first.

UseLocalReserve()

Purpose:

This method empties the reserve that was allocated during creation of the LGrowZone object.

Access:

Virtual, Protected

Prototype:

virtual SInt32 UseLocalReserve(Size inBytesNeeded 
);
Parameters:

This method has the following parameter:

 

Size  
inBytesNeeded  
The number of bytes needed.  

Return:

SInt32 containing the number of bytes that were freed.

Remarks:Calling this method is a last chance desperate attempt to free enough memory to proceed. If the memory reserve for LGrowZone exists, it will be freed. Also, mGiveWarning is set to true. The user will receive warning alerts from the framework if memory becomes low again.

sGrowZone

Purpose:

This is a pointer to the LGrowZone object. It is accessible using the GetGrowZone() accessor method.

Access:

Static, Protected

Prototype:

static LGrowZone* sGrowZone;
Remarks:

You do not normally need to directly access this data member, and should use the GetGrowZone() accessor method instead.

sGrowZoneUPP

Purpose:

This is a Universal Procedure Pointer (UPP) used for pointing to the GrowZone procedure.

Access:

Static, Protected

Prototype:

static GrowZoneUPP sGrowZoneUPP;

mLocalReserve

Purpose:

This member is a Handle to the memory reserve allocated by the LGrowZone() constructor.

Access:

Protected

Prototype:

Handle mLocalReserve;

mReserveSize

Purpose:

This member is a count of the size (in bytes) of the memory reserve area allocated by the LGrowZone() constructor.

Access:

Protected

Prototype:

Size mReserveSize;

mGiveWarning

Purpose:

This data member is set to true if you want the PowerPlant framework to provide a default warning alert to the user under low-memory conditions.

Access:

Protected

Prototype:

Boolean GiveWarning;
Remarks:

The default for this data member is false when the LGrowZone() constructor is invoked.

 


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: July 21, 2000