MorePalmOS
A development library for developing Palm OS applications

MoreMemoryMgr.c

Go to the documentation of this file.
00001 /******************************************************************************
00002  * MorePalmOS
00003  * Copyright (c) 2004 Steven Fisher
00004  *
00005  * Distributed under the Boost Software License, Version 1.0. See accompanying
00006  * license file License.txt or <http://www.boost.org/LICENSE_1_0.txt>.
00007  *
00008  * http://morepalmos.sourceforge.net
00009  *
00010  *   MorePalmOS is the humble begining of an attempt to provide a library for
00011  *   Palm OS development which works around system bugs, illustrate how to
00012  *   use system calls, and provide "glue code" for programming Palm OS
00013  *   applications in a more straightforward way while minimally impacting
00014  *   code size.
00015  *****************************************************************************/
00016 
00017 #include "MorePrefix.h"
00018 #include "MoreMemoryMgr.h"
00019 #include "MorePrivate.h"
00020 
00021 //-----------------------------------------------------------------------------
00022 
00023 MemPtr MMemPtrRealloc( MemPtr p, UInt32 newSize )
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 }
00036 
00037 //-----------------------------------------------------------------------------
00038 
00039 Boolean MMemPtrResizeRef( MemPtr *p, UInt32 newSize )
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 }
00066 
00067 //-----------------------------------------------------------------------------
00068 
00069 void MMemFreeHandleFromLockedPtr( MemPtr p )
00070 {
00071         MemHandle h = MemPtrRecoverHandle( p );
00072         _require( h, fail );
00073         MemHandleUnlock( h );
00074         MemHandleFree( h );
00075 fail:
00076         return;
00077 }

SourceForge.net Logo