Click here to Skip to main content
13,596,176 members
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 5 Nov 2002
Licenced CPOL

The Palm Memory Manager API

, 5 Nov 2002
Rate this:
Please Sign up or sign in to vote.
How to use Palm's Memory Manager API for dynamic memory allocation in palm handheld applications.


Memory management on Palm handhelds is quite different from standard C/C++ and windows memory management that you may be used to. This article will try to shed some light on the crazy world of Palm memory.

Memory is Limited

Although newer Palms are getting more and more RAM most still have under 16MB and none of them have hard drives. All applications and databases are stored in the RAM so Palms have to do things a little differently than PCs. We wouldn't want our applications overwriting permanent storage now would we.

The RAM is divided into two sections: the storage area and the dynamic area (or dynamic heap). As you can guess, the storage area is where permanent data resides, like applications and databases, and is handled by the Database Manager. The dynamic heap is used for Palm OS globals, Palm OS dynamic allocations, your application's global variables, your application's stack space, and any dynamic allocations your application makes. The dynamic heap is handled by the Memory Manager and its size depends on the amount of RAM your Palm has, the OS version and the applications you have installed.

Because of the limited amount of dynamic memory, the Palm OS needs to be able to move chunks of memory around to keep its free space contiguous so there is enough room for new allocation. Thus there are two types of memory chunks, pointers and handles. Pointers are nonmovable chunks of memory, whereas handles are movable by the Palm OS. It is recommended that you use handles as much as possible.

The Memory Manager API

In order to manipulate memory on the Palm, you must use the Memory Manager API.

Using Handles

Lets first look at how we can allocate a new memory handle using the API.

  • The MemHandle MemHandleNew (UInt32 size) function returns a newly allocated MemHandle of the size in bytes specified in the size parameter. FYI: You cannot allocate a single memory chunk larger than 64KB.

Since the OS can move this handle around at will, you need to "lock" it in order to read or write to it.

  • Use MemPtr MemHandleLock (MemHandle h) to lock a handle and obtain a MemPtr (pointer) to the handle's chunk of memory.

After using a locked handle, you must "unlock" it quickly so the OS can move it around again.

  • To unlock a previously locked handle, use Err MemHandleUnlock (MemHandle h).

When you are finished with a dynamically created handle, you need to call Err MemHandleFree (MemHandle h) to dispose of it.

Here is an example of using the Memory Manager functions for handles:

/* example of handle allocation and use */
MemHandle myHandle = MemHandleNew(13);          // create a 13-byte handle
Char* myStr = (Char*)MemHandleLock(myHandle);   // lock the handle before use
StrCopy(myStr, "Hello World!");                 // use it
MemHandleUnlock(myHandle);                      // unlock the handle
MemHandleFree(myHandle);                        // free the handle

When a handle is locked, the OS increments a lock count for that handle. You can lock a handle up to 14 times before you receive a "chunk overlocked" error. Each lock you perform on a handle, you must also unlock. If you unlock a handle more times than the lock count, you will receive a "chunk underlocked" error.

An alternative to the MemHandleUnlock function is Err MemPtrUnlock (MemPtr p). This is handy if you just want to pass your locked pointer to another function and your handle is not available. It decreases the lock count on the locked handle just like MemHandleUnlock.

Using Pointers

To allocate a nonmovable chunk of memory (a pointer), use MemPtr MemPtrNew (UInt32 size). FYI: Remember, you cannot allocate a single memory chunk larger than 64KB.

When you are finished with the pointer, call Err MemPtrFree (MemPtr p) to free the chunk.

Here is an example of using the pointer functions:

/* example of handle allocation and use */
Char* myStr = (Char*) MemPtrNew(13);  // allocate a 13-byte pointer
StrCopy(myStr, "Hello World!");       // use it
MemPtrFree(myStr);                    // free the pointer

You should use pointers instead of handles if you need the memory throughout your entire application, or if your allocation will be short lived. Keep in mind that frequent locking and unlocking of handles will also incur a performance cost over pointer allocation.

Other Memory Functions

Err MemSet (void* dstP, Int32 numBytes, UInt8 value) will set numBytes of the dstP pointer to value (just like C++'s memset).

Err MemMove (void* dstP, const void* sP, Int32 numBytes) will move numBytes of the sP source pointer to the dstP destination pointer, handling overlapping ranges automatically.

UInt32 MemHandleSize (MemHandle h) reports the size in bytes of a handle.

UInt32 MemPtrSize (MemPtr p) reports the size in bytes of a pointer.

To resize an unlocked handle, call Err MemHandleResize (MemHandle h, UInt32 newSize). If you are making the handle larger and there is not enough free space after the handle, the OS will move the chunk to a new location.

If you need to resize a pointer, call Err MemPtrResize (MemPtr p, UInt32 newSize). A pointer will only be resized if you are making it smaller or if there is enough free space directly after the pointer. This function may also be used on a locked handle given its pointer.

The Demo

I modified the "Hello World!" application from my first palm article, An Introduction to Palm Handheld Development, to show how you would use the various Memory Manager functions. As before, the demo was compiled with Metrowerk's CodeWarrior IDE (version 6).

A static/global MemHandle, called myHandle, was added which will hold the text to display on the screen. This handle is created in the MainFormInit function then locked in the SayHello or SayGoodbye functions. The pointer returned from the lock is then passed to the DrawText function for display to the screen. After returning from the drawing function, the handle or pointer is unlocked. The handle is freed in the MainFormClose function.

As you will notice from the code, myHandle is being resized using MemHandleResize and there are also calls to MemSet and MemMove in there just so you can see an example of how they are used. MemPtrResize was not used in the demo since it may not always work. It is essentially the same as resizing a handle.

Here are the main code highlights:

// need a global handle for demo purposes
static MemHandle myHandle = NULL;
static short nCount = 0;

// This routine draws text on the center of the form
static void DrawText(Char* pText)
	short nCharWidth = 0;
	short width = 0, height = 0;
	// get the width of the string		
	nCharWidth = FntCharsWidth(pText, StrLen(pText));  
	// get the width and height of the string
	WinGetWindowExtent(&width, &height);
	// draw the text in the center
	WinDrawChars(pText, StrLen(pText), (width/2) - (nCharWidth/2), height/2);

// This routine draws "Hello World!" on the form
static void SayHello()
	// resize the handle only if you need to
	if (MemHandleSize(myHandle) == 13 || 
		MemHandleResize(myHandle, 13) == 0)
		// lock the handle before use 
		Char* pText = (Char*) MemHandleLock(myHandle);  
		// make sure none of the previous string is left in
		MemSet(pText, 13, 0);	
		// copy Hello World! into the char ptr
		StrCopy(pText, "Hello World!");

		// pass the pointer and draw it on the screen
		// unlock the handle

// This routine draws "Goodbye World!" on the form
static void SayGoodbye()
	// resize the handle only if you need to
	if (MemHandleSize(myHandle) == 15 || 
		MemHandleResize(myHandle, 15) == 0)
		// lock the handle before use  
		Char* pText = (Char*) MemHandleLock(myHandle);  
		// make sure none of the previous string is left in	
		MemSet(pText, 15, 0);	

		// copy Goodbye World! into the char ptr
		StrCopy(pText, "Goodbye World!");
		// Lets use MemMove
		if (nCount%2 == 0)
			Char* pTemp = (Char*) MemPtrNew(15);
			StrCopy(pTemp, "Bye bye World!");
			MemMove(pText, pTemp, 7);
			pTemp = NULL;
		// pass the pointer and draw it on the screen
		// unlock the pointer in this function

// This routine initializes the MainForm form.
static void MainFormInit(FormType* frmP)
	// we're going to lock this handle in our SayWhat functions
	// then we'll pass the pointer to the DrawText function
	myHandle = MemHandleNew(20);  // allocate 20 bytes

// This routine closes the MainForm form.
static void MainFormClose()
	// free the handle
	if (myHandle != NULL) 
		myHandle = NULL;


From my own experience, Palm memory handling can be quite harrowing. Writing this article has helped me understand the whole system a little better and I hope it will help you as well

Look for more palm articles in the near future from myself and Christian Graus. We plan to cover palm databases, custom drawn lists, tables, palm conduits and other relevant topics specific to the PalmOS.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jason Henderson
United States United States
I have been a professional developer since 1996. I live in the middle of no where in Illinois, USA. I am married and have four children.

You may also be interested in...


Comments and Discussions

QuestionHow can i read the information about battery of smartPhone in C# Pin
02122533-Jan-07 22:37
member02122533-Jan-07 22:37 
AnswerRe: How can i read the information about battery of smartPhone in C# Pin
Jason Henderson4-Jan-07 2:30
memberJason Henderson4-Jan-07 2:30 
Generalhelp in database Pin
ankitapatel30-Jan-06 20:14
memberankitapatel30-Jan-06 20:14 
Generalhey jason! why don't check the site, again, problem with my pda, and a hard drive Pin
lanmaster268330-Jan-06 3:06
memberlanmaster268330-Jan-06 3:06 
Generalhi, problems with my clie pda Pin
lanmaster268328-Jan-06 8:47
memberlanmaster268328-Jan-06 8:47 
GeneralHGLOBAL memory problem? please help Pin
freddy1ca8-Aug-04 23:19
memberfreddy1ca8-Aug-04 23:19 
QuestionWhere is MemHandle declared? Pin
Amit Dey6-Apr-03 5:16
memberAmit Dey6-Apr-03 5:16 
AnswerRe: Where is MemHandle declared? Pin
Jason Henderson7-Apr-03 3:25
memberJason Henderson7-Apr-03 3:25 
GeneralRemembers me Windows 3.1 Pin
Daniel Turini7-Nov-02 9:11
memberDaniel Turini7-Nov-02 9:11 
GeneralRe: Remembers me Windows 3.1 Pin
Christian Graus7-Nov-02 12:35
memberChristian Graus7-Nov-02 12:35 
GeneralRe: Remembers me Windows 3.1 Pin
Jason Henderson7-Nov-02 17:00
memberJason Henderson7-Nov-02 17:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 6 Nov 2002
Article Copyright 2002 by Jason Henderson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid