"Andy Yeong" <[EMAIL PROTECTED]> wrote in message news:74842@palm-dev-forum...
> Bob Whiteman <[EMAIL PROTECTED]> wrote in message news:74827@palm-dev-forum...
> > It is my understanding that if you use MemHandleFree() with
MemHandleNew()
> > things go smoothly. The same is true if you use MemPtrFree() with
> > MemPtrNew(). But if you use MemPtrFree() with MemHandleNew() the OS has
to
> > call MemPtrRecoverHandle() and then call MemHandleFree() for you. It
still
> > works but it takes more time.
>
> Wow ! That's great. But I never managed to retrieve the handle using
MemPtrRecoverHandle
> when the memory was created using "MemPtrNew()". Is there any other
command whereby
> I could retrieve ?
Actually, there is no underlying handle.
I'm sorry, I have to take back my earlier comments about MemPtrFree. I just
looked at the memory manager source code and I've been mistaken.
When you allocate a handle using MemHandleNew() and then free it using
MemHandeFree() the OS uses the following functions:
MemSemaphoreReserve()
PrvHandleCheck()
PrvPtrFree()
MemSemaphoreRelease()
If you allocate a handle using MemHandleNew() and then free the pointer it
using MemPtrFree the OS uses these functions:
PrvPtrCheck()
MemSemaphoreReserve()
PrvPtrFree()
MemSemaphoreRelease()
So the only difference is that one calls PrvHandleCheck() and the other
calls PrvPtrCheck. They perform very similar checks, so the two have similar
runtimes.
> The problem that I encounter using "MemPtrNew()" is that it doesn't allow
> me to resize (increase) the memory size using "MemPtrResize()". Thus, I
have
> to use "MemHandleNew()" instead. Is there work around ?
There's three resize functions: MemPtrResize(), MemHandleResize() and
DmResizeHandle(). When reducing the amount of memory allocated they always
succeed. They generally follow these steps:
1. If the amount of memory is decreasing, the resize occurs immediately and
succeeds.
2. If the memory space just beyond the current allocation is free (or can be
made free?), the resize occurs immediately and succeeds.
3. If the memory is movable it moves the memory to a larger block of space
and performs the resize.
The problem is step 3. Memory allocated using MemPtrNew() is never movable,
and handles are only movable if they are unlocked.
There's a couple ways that you can make a MemPtrResize wrapper function
which always succeeds:
The first technique would be to allocate the maximum amount of memory you
will ever need so that the allocation never increases.
The second technique would be a function like the following:
Err MemPtrResizeWell( void **pp, UInt16 cbOldSize, UInt16 cbNewSize ) {
void *pNew;
pNew = MemPtrNew( cbNewSize );
if ( !pNew ) {
// Out of memory
return memErrNotEnoughSpace;
}
MemMove( pNew, *pp, cbOldSize );
*pp = pNew;
return 0;
}
There are three drawbacks to this function:
First, the pointer will change. If the pointer is being used by a field (set
by FldSetTextPtr()) then that field may cause a crash the next time it
redraws or the text changes.
Second, it has a high peak memory usage. If cbOldSize+cbNewSize is larger
than the amount of free dynamic heap this function will fail, even if
cbNewSize is less than the amount of free dynamic heap.
Third, you must keep track of how large the pointer was before the resize.
> Thanks for all comments ! It's very enriching.
I'm glad to help.
Bob Whiteman
Senior Software Engineer
Pico Communications
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/