[comp.sys.mac.programmer] A Better malloc

mark@intek01.UUCP (Mark McWiggins) (07/12/89)

We have a couple of applications that we've moved over from the Unix/DOS
world that do a lot of memory allocation.  They don't free up what they've
allocated on exit, since Unix and DOS both get that back automatically.
Of course, this is a problem on the Mac since (as I understand it) MPW's
malloc() allocates a nonrelocatable block that just sticks there forever
until explicitly deallocated.

Has somebody reimplemented malloc() to work more smoothly in the Mac world?
What I want is a free_everything() call to use when I exit.  

Any pointers to this (no pun intended) would be GREATLY appreciated.

Thanks in advance,

Mark McWiggins
Integration Technologies, Inc. (Intek)
1400 112th Ave. SE #202
Bellevue WA  98004
(206) 455-9935
uunet!intek01!mark

earleh@eleazar.dartmouth.edu (Earle R. Horton) (07/13/89)

In article <214@intek01.UUCP> mark@intek01.UUCP (Mark McWiggins) writes:
>We have a couple of applications that we've moved over from the Unix/DOS
>world that do a lot of memory allocation.  They don't free up what they've
>allocated on exit, since Unix and DOS both get that back automatically.
>Of course, this is a problem on the Mac since (as I understand it) MPW's
>malloc() allocates a nonrelocatable block that just sticks there forever
>until explicitly deallocated.

     This is just not true.  All storage is reclaimed when you call
exit() or ExitToShell().  There is a page in the MPW C manual
describing exactly how malloc() works.  I suggest you read both this
and the Memory Manager chapter of Inside Macintosh to see what is
going on when you call malloc() on the Macintosh.

     If your programs do a lot of memory allocation, particularly
using small blocks, then you might want to consider using relocatable
blocks for this purpose.  These are extremely helpful if you find
yourself free-ing and reallocating memory a lot.  Under these
conditions, using nonrelocatable blocks such as those obtained from
malloc() can promote heap fragmentation.

>Has somebody reimplemented malloc() to work more smoothly in the Mac world?
>What I want is a free_everything() call to use when I exit.  

     The problem is not that malloc() is inefficient on the Mac, but
rather that the use of relocatable blocks, as obtained from
NewHandle(), can be much more efficient when you need to reallocate
space.  If you do not reallocate space, then using MPW malloc() is
perfectly safe.  Exit() will free everything, trust me.

Earle R. Horton
"People forget how fast you did a job, but they remember how well you
did it."  Salada Tag Lines