MorePalmOS
A development library for developing Palm OS applications

MoreMemoryMgr.c File Reference

#include "MorePrefix.h"
#include "MoreMemoryMgr.h"
#include "MorePrivate.h"

Include dependency graph for MoreMemoryMgr.c:

Include dependency graph

Go to the source code of this file.


Functions

MemPtr MMemPtrRealloc (MemPtr p, UInt32 newSize)
 realloc-compatible pointer resize. Resizes a pointer in place if possible, otherwise move it.
Boolean MMemPtrResizeRef (MemPtr *p, UInt32 newSize)
 Resizes a pointer in place if possible, otherwise move it.
void MMemFreeHandleFromLockedPtr (MemPtr p)
 Dispose of a handle given a pointer from MemHandleLock( ).

Function Documentation

void MMemFreeHandleFromLockedPtr MemPtr  p  ) 
 

Dispose of a handle given a pointer from MemHandleLock( ).

Given a pointer which has a corresponding handle, unlock and release that handle.

Definition at line 69 of file MoreMemoryMgr.c.

00070 {
00071         MemHandle h = MemPtrRecoverHandle( p );
00072         _require( h, fail );
00073         MemHandleUnlock( h );
00074         MemHandleFree( h );
00075 fail:
00076         return;
00077 }

MemPtr MMemPtrRealloc MemPtr  p,
UInt32  newSize
 

realloc-compatible pointer resize. Resizes a pointer in place if possible, otherwise move it.

Resize a pointer in place if possible, otherwise move it.

Note that this routine follows the realloc( ) standard for failure conditions, which means that if the pointer can't be resized NULL is returned and the original pointer is untouched. This will cause a memory leak if you do something like:

p = MMemPtrRealloc( p, MemPtrSize( p )+1024 );

Big deal? Well, if MemPtrResize fails, it's probably because you're dangerously low on memory and can't afford to leak any. See MMemPtrResizeRef for a solution.

MMemPtrResizeRef( p, MemPtrSize( p )+1024, &p );

Definition at line 23 of file MoreMemoryMgr.c.

00024 {
00025         if ( MMemPtrResizeRef( &p, newSize ) )
00026         {
00027                 return p;
00028         }
00029         else
00030         {
00031                 /* Couldn't resize? return NULL but don't free the original pointer.
00032                    Yes, this is the right behaviour for realloc... */
00033                 return NULL;
00034         }
00035 }

Boolean MMemPtrResizeRef MemPtr *  p,
UInt32  newSize
 

Resizes a pointer in place if possible, otherwise move it.

Resize a pointer in place if possible, otherwise move it.

This routine does not return the new pointer as a function result. Rather, you pass the pointer by reference. If it can't be resized, it is not touched. The result indicates if it was succesful.

if ( !MMemPtrResizeRef( &s.p, MemPtrSIze( s.p )+1024 ) ) fail;

Definition at line 39 of file MoreMemoryMgr.c.

00040 {
00041         void* oldP = *p;
00042         Err err = MemPtrResize( oldP, newSize );
00043         if ( err == memErrChunkLocked )
00044         {
00045                 // couldn't resize because there's no space after the chunk 
00046                 void* newPtr = MMemPtrNew( newSize );
00047                 if ( newPtr )
00048                 {
00049                         /* manages to allocate a new pointer; copy the contents of the
00050                            old block and free it. */
00051                         MemMove( newPtr, oldP, MemPtrSize( oldP ) );
00052                         MemPtrFree( oldP );
00053                         *p = newPtr;
00054                         return true;
00055                 }
00056                 else
00057                 {
00058                         return false;
00059                 }
00060         }
00061         else
00062         {
00063                 return true;
00064         }
00065 }

SourceForge.net Logo