[comp.sys.apple2] Handles

toddpw@nntp-server.caltech.edu (Todd P. Whitesel) (12/22/90)

zane@ddsw1.MCS.COM (Sameer Parekh) writes:

>	Why DO handles exist?  Why can't one just use the pointers?
>(I am learning programming now. . .)

Handles are a concept that Apple came up with to deal with memory management
on the Mac and the GS. The basic idea is that you allocate memory through the
memory manager, and the memory manager returns a handle, which is actually
a pointer to the memory manager's struct/record of info about the allocated
block. The first element of the structure is a pointer to the current location
of the memory block -- if the memory manager can't satisfy a request (or
someone explicitly requests it) the memory manager will move or purge blocks
in order to free up memory. When it moves a block, the memory manager changes
the pointer that your handle points to, so you can still get at the block.
If you need a pointer to the block, then you need to tell the memory manager
to not move the block and dereference the handle to get a pointer to the block.
(If the block gets purged, the pointer to the block is set to 0 -- this is
called an 'empty handle'. Handles themselves cannot be 0 and represent an
error condition.)

Todd Whitesel
toddpw @ tybalt.caltech.edu

toddpw@nntp-server.caltech.edu (Todd P. Whitesel) (12/22/90)

>zane@ddsw1.MCS.COM (Sameer Parekh) writes:

>>	Why DO handles exist?  Why can't one just use the pointers?
>>(I am learning programming now. . .)

Heh.. the half screen lecture I just posted buries the real answer to your
question -- it's so the memory manager can move blocks around without worrying
about what you're doing. Here's how to politely use memory:

If the block is going to be used a lot, allocate it as Fixed and Locked.
Dereference the handle and use the pointer for whatever evil purposes.

If the block is not going to be used a lot, allocate it without restrictions
and either always reference it through the handle or call HLock(handle) before
making and using a pointer to it and HUnlock(handle) afterwards. This way the
block is always fixed &/or locked (I haven't bothered with purgable yet) while
a pointer to it exists. If all you need is a handle, unlock it so the memory
manager can move it around if the situation warrants. This is what Apple asks
you to do and it helps the memory manager keep down the number of little
pockets of wasted memory (i.e. it helps avoid "memory fragmentation").

Todd Whitesel
toddpw @ tybalt.caltech.edu

bchurch@oucsace.cs.OHIOU.EDU (Bob Church) (12/22/90)

In article <1990Dec22.083627.5994@nntp-server.caltech.edu> toddpw@nntp-server.caltech.edu (Todd P. Whitesel) writes:
>zane@ddsw1.MCS.COM (Sameer Parekh) writes:
>
>>	Why DO handles exist?  Why can't one just use the pointers?
>>(I am learning programming now. . .)
>
>Handles are a concept that Apple came up with to deal with memory management
>on the Mac and the GS.

Handles (under the name of vectors, but same thing) have been used in
the Apple // all along. 

Bob Church
bchurch@oucsace.cs.ohiou.edu