[comp.sys.mac.programmer] NewHandle

gregh@inmet.inmet.com (10/18/90)

   Is it permissable to call NewHandle (or NewPtr) with 0 as an argument?
   For instance:

	{
            Handle myHandle;

   	    myHandle = NewHandle(0L);
	}


   And then make myHandle bigger with calls to SetHandleSize() when needed.

   I have not found anything in any Apple literature to warn against this
   technique. But I am concerned that the call to NewHandle() may lead to 
   problems.

   Greg Herlihy

   gregh@inmet.inmet.com

dorner@pequod.cso.uiuc.edu (Steve Dorner) (10/19/90)

In article <32600007@inmet> gregh@inmet.inmet.com writes:
>   Is it permissable to call NewHandle (or NewPtr) with 0 as an argument?

I do it all the time, and have never had a problem.  How's that for
anecdotal evidence?
--
Steve Dorner, U of Illinois Computing Services Office
Internet: s-dorner@uiuc.edu  UUCP: uunet!uiucuxc!uiuc.edu!s-dorner

scott@scotty (Scott Howard) (10/19/90)

Why call a handle up before you need to? If you 
pre-allocate it with 0L, you lose the advantage of being
able to tuck it into an unobtrusive corner of your application's
heap. At any rate, It ought to work as long as you don't try
disposing it.

woody@tybalt.caltech.edu (William Edward Woody) (10/19/90)

In article <1990Oct19.151254.8197@ux1.cso.uiuc.edu> scott@scotty (Scott Howard) writes:
>Why call a handle up before you need to? If you 
>pre-allocate it with 0L, you lose the advantage of being
>able to tuck it into an unobtrusive corner of your application's
>heap. At any rate, It ought to work as long as you don't try
>disposing it.


No cigar; spin again.

If you allocate a handle with size zero, and leave it unlocked, the OS
shouldn't have any problems moving the object around to an unobtrusive
area in your heap if need be; this is the reason why you use handles instead
of pointers.

If you allocate a handle with zero size, you will eventually need to
dispose it again in order to release the master pointer (if for no
other reason).

Manipulating handles with size 0 is like manipulating handles with any other
size; size 0 handles are just smaller, that's all.

BTW, handling 0 size pointers in the Macintosh memory manager model is just
like pointers of larger size, except that the size 0 pointers are smaller.

--
	William Edward Woody		   | Disclamer:
USNAIL	P.O.Box 50986; Pasadena, CA 91115  |
EMAIL	woody@tybalt.caltech.edu	   | The useful stuff in this message
ICBM	34 08' 44''N x 118 08' 41''W	   | was only line noise. 

zellers@prisoner.Eng.Sun.COM (Steve Zellers) (10/20/90)

Well, I've always done it, but now I've been informed by someone that
someone at apple 'just says no', as it were.  Can anyone point
out a technote for this, or indeed support this persons claim?

Seems to me, that if NewHandle will sometimes work with '0', and sometimes
NOT, then it is a bug, and needs to be fixed.

Comments?

--
------------------------------------------------------------------------
Steve Zellers 					zellers@prisoner.sun.com
"All you slimy  fruitcakes in San Fransisco are going to find out how to 
get to 180 and the letter 'G'. I repeat, there is No Other Possibility."

russotto@eng.umd.edu (Matthew T. Russotto) (10/20/90)

In article <1990Oct19.151254.8197@ux1.cso.uiuc.edu> scott@scotty (Scott Howard) writes:
>
>Why call a handle up before you need to? If you 
>pre-allocate it with 0L, you lose the advantage of being
>able to tuck it into an unobtrusive corner of your application's
>heap. At any rate, It ought to work as long as you don't try
>disposing it.

I use it when trying to load data from a disk file when there is no length
information preceding the data.  Disposing it works fine-- it's a perfectly
valid handle, with a master pointer and everything.

You can still tuck it into an unobtrusive corner of the heap with MoveHHi at
a later time (after you have SetHandleSize'd it)

--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
      .sig under construction, like the rest of this campus.

mystone@mondo.engin.umich.edu (Dean Yu) (10/20/90)

  Whether or not NewHandle(0) works or not is kinda moot, since there's a
_NewEmptyHandle call which does the same thing as NewHandle(0).
_NewEmptyHandle has been around since the 128K ROMs and is documented in
Inside Mac IV.

_______________________________________________________________________________
Dean Yu                            | E-mail:    mystone@mondo.engin.umich.edu
Patches 'R' Us                     | Real-mail: Dean Yu
A Division of Cyberite Systems     |            909 Church St Apt C
                                   |            Ann Arbor, MI 48104
I'm not the voice of Reason, much  | Phone:     313 662-4073
    less the voice of Cyberite.    |            313 662-4163
-------------------------------------------------------------------------------

vd09+@andrew.cmu.edu (Vincent M. Del Vecchio) (10/20/90)

In article <32600007@inmet> gregh@inmet.inmet.com writes:
>   Is it permissable to call NewHandle (or NewPtr) with 0 as an argument?

If you're not worried about 64K ROM compatibility, you can also call
NewEmptyHandle, which does the same thing.
+-------------------------------------------------------------------+
| Vincent Del Vecchio     \       #include <stddisclaimer.h>        |
| Box 4834                 \       #include <stdquote.h>            |
| 5125 Margaret Morrison St.\  BITNET: vd09+%andrew@cmuccvma.bitnet |
| Pittsburgh, PA  15213      \  UUCP: harvard!andrew.cmu.edu!vd09   |
| (412) 268-4441              \  Internet: vd09+@andrew.cmu.edu     |
+-------------------------------------------------------------------+

sean_parent.snarkmail_l_z@gateway.qm.apple.com (Sean Parent) (10/20/90)

In article <1990Oct19.204536.4128@engin.umich.edu> 
mystone@mondo.engin.umich.edu (Dean Yu) writes:
>  Whether or not NewHandle(0) works or not is kinda moot, since there's a
> _NewEmptyHandle call which does the same thing as NewHandle(0).
> _NewEmptyHandle has been around since the 128K ROMs and is documented in
> Inside Mac IV.
Wrong! (Sorry Dean.) NewEmptyHandle creates a handle to a nil master 
pointer. This is not the same as a handle to an empty block. In order to 
use a handle to nil master pointer (either one created by calling 
NewEmptyHandle or one created by call EmptyHandle on an existing handle) 
you must first reallocate the memory by call ReallocHandle before calling 
SetHandleSize. But NewHandle(0) does work just fine in all cases (no bugs 
that I know of and I've been using it for years!)

Sean Parent
"Quality unattainable in a reasonable amount of time."

lsr@Apple.COM (Larry Rosenstein) (10/20/90)

vd09+@andrew.cmu.edu (Vincent M. Del Vecchio) writes:

>In article <32600007@inmet> gregh@inmet.inmet.com writes:
>>   Is it permissable to call NewHandle (or NewPtr) with 0 as an argument?

>If you're not worried about 64K ROM compatibility, you can also call
>NewEmptyHandle, which does the same thing.

These aren't exactly the same.  NewEmptyHandle allocates the master pointer
but not the heap block.  You will have to call ReallocHandle when you really
need the storage.  

In that sense, it is better than calling NewHandle(0L), which will allocate
at least 8 bytes in the heap.  (Those bytes will be moved around if
necessary.)  You do have to be aware of the differences, however.
-- 
		 Larry Rosenstein,  Object Specialist
 Apple Computer, Inc.  20525 Mariani Ave, MS 3-PK  Cupertino, CA 95014
	    AppleLink:Rosenstein1    domain:lsr@Apple.COM
		UUCP:{sun,voder,nsc,decwrl}!apple!lsr

ts@cup.portal.com (Tim W Smith) (10/20/90)

< Why call a handle up before you need to? If you 

If I'm going the be appending some dymanically calculated things
to a list, I do something like this:

	if ( myHandle == 0 )
	{
		myHandle = NewHandle(0);
		mySize = 0;
	}
	else
		mySize = SizeHandle( myHandle );

	for ( blah blah )
		while ( blah blah )
			if ( blah blah )
			{
				SetHandleSize( myHandle, mySize + whatever );
				mySize += whatever;
				append the data
			}

(I'm doing this from memory, so I might have the names of the handle
manipulation traps wrong).

By using the 0 size handle, we get rid of a special case in the loop.
This makes the loop simpler (we don't have so many levels of nesting),
and it makes the code more efficient (we don't have to check for
myHandle being 0 each time through the loop).

In general, whenever I need a list whose size is not known in advance,
I like to start with a zero length handle.

							Tim Smith

russotto@eng.umd.edu (Matthew T. Russotto) (10/23/90)

In article <1990Oct19.204536.4128@engin.umich.edu> mystone@mondo.engin.umich.edu writes:
>
>  Whether or not NewHandle(0) works or not is kinda moot, since there's a
>_NewEmptyHandle call which does the same thing as NewHandle(0).
>_NewEmptyHandle has been around since the 128K ROMs and is documented in
>Inside Mac IV.

NewEmptyHandle gives you a pointer to a NULL. NewHandle(0L) gives you a pointer
to a pointer to a zero-length block.
--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
      .sig under construction, like the rest of this campus.