[comp.sys.amiga.tech] De-Allocating Memory?

pawn@wpi.wpi.edu (Kevin Goroway) (05/08/90)

I think I need some quick tutoring as to how one deallocates memory:

I'm doing an awful lot of mallocs to create a lot of linked lists...
when I free them using
Freeit(object)
  struct obj *object;
{
	if (object->next != NULL)
		Freeit(object->next);
	free(object);
}

I don't get any memory back...
I tried using lattices rbrk() with no luck...
Also, I'd love to use AllocRemember, but the code in the Intuition Manual
and Rom Kernal just don't work. (crash...)

Thanks...

-- 
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
| Worcester Polytechnic Institute   | "It happens sometimes, people just     |
| Pawn@wpi.wpi.edu  Pawn@wpi.bitnet |   explode, natural causes."-Repo Man   |
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

jmeissen@oregon.oacis.org (John Meissen) (05/09/90)

In article <12750@wpi.wpi.edu> pawn@wpi.wpi.edu (Kevin Goroway) writes:
>I think I need some quick tutoring as to how one deallocates memory:
>
>I'm doing an awful lot of mallocs to create a lot of linked lists...

If you are going to be allocating memory that you will need to give back
to the system (the friendliest way, really), use AllocMem and FreeMem
instead of malloc and free. Just be sure you give it all back explicitly
before exiting.
>
>I don't get any memory back...
>I tried using lattices rbrk() with no luck...

I don't remember the rbrk() code, but in general the Lattice routines
manage a local memory pool under the general assumption that it is
usually more expensive to go to the operating system to get memory. This
may not be true for the Amiga, but the Lattice code is used on a lot of
machines. When you free memory, it goes back into the pool, not to the
system.

>Also, I'd love to use AllocRemember, but the code in the Intuition Manual
>and Rom Kernal just don't work. (crash...)

They work fine, I use them a lot. The thing you need to remember is that
you must pass them the ADDRESS of the POINTER to the Remember structure,
not the address of the structure.
-- 
 John Meissen ............................... Oregon Advanced Computing Institute
 jmeissen@oacis.org        (Internet) | "That's the remarkable thing about life;
 ..!sequent!oacis!jmeissen (UUCP)     |  things are never so bad that they can't
 jmeissen                  (BIX)      |  get worse." - Calvin & Hobbes

cmcmanis@stpeter.Eng.Sun.COM (Chuck McManis) (05/09/90)

In article <12750@wpi.wpi.edu> pawn@wpi.wpi.edu (Kevin Goroway) writes:
>I don't get any memory back...

The "free()" function doesn't free memory in the AllocMem sense, it 
returns it to the pool that malloc() will look in. This is to support
the UNIX semantic of reallocating memory you just freed having the
same contents. [It is documented in the Lattice manual btw] 

>Also, I'd love to use AllocRemember, but the code in the Intuition Manual
>and Rom Kernal just don't work. (crash...)

Did you note that these routines take a pointer to a pointer? That is
to say that AllocRemember() is prototyped as :

void *AllocRemember(struct Remember **, long, long);

These routines work fine when used properly. In your case you could
actually use just AllocMem/FreeMem because you know the sizes of the
objects you are freeing. AllocRemember has the advantage that you can
free a whole bunch of memory without having to free each chunk. 


--
--Chuck McManis						    Sun Microsystems
uucp: {anywhere}!sun!cmcmanis   BIX: <none>   Internet: cmcmanis@Eng.Sun.COM
These opinions are my own and no one elses, but you knew that didn't you.
"I tell you this parrot is bleeding deceased!"

mwm@raven.pa.dec.com (Mike (Real Amigas have keyboard garages) Meyer) (05/09/90)

>> I'm doing an awful lot of mallocs to create a lot of linked lists...
>> when I free them using
>> Freeit(object)
>>   struct obj *object;
>> {
>> 	if (object->next != NULL)
>> 		Freeit(object->next);
>> 	free(object);
>> }
>> 
>> I don't get any memory back...
>> I tried using lattices rbrk() with no luck...

Lattice's memory routines allocate a growing pool. Doing a free()
doesn't release the memory back to the OS, but puts it back in the
pool for reuse by the Lattice memory manager.

>> Also, I'd love to use AllocRemember, but the code in the Intuition Manual
>> and Rom Kernal just don't work. (crash...)

They work fine for me. The obvious problem - did you open the
intuition library? The Remember routines are in it, and calling them
will guru if you don't have it open.

	<mike

--
I'm gonna lasso you with my rubberband lazer,		Mike Meyer
Pull you closer to me, and look right to the moon.	mwm@relay.pa.dec.com
Ride side by side when worlds collide,			decwrl!mwm
And slip into the Martian tide.

greg@walt.cc.utexas.edu (Greg Harp) (05/09/90)

In article <MWM.90May8115606@raven.pa.dec.com> mwm@raven.pa.dec.com (Mike (Real Amigas have keyboard garages) Meyer) writes:
>Lattice's memory routines allocate a growing pool. Doing a free()
>doesn't release the memory back to the OS, but puts it back in the
>pool for reuse by the Lattice memory manager.

Sin! Sin! Sin!  PLEASE don't tell me Manx does the same thing!! Memory is too
much of a commodity in a multitasking machine to hog it like that! What 
happens when an otherwise tame, small program wants to allocate a large 
amount of memory just once, use it, and return it so OTHER PROGRAMS CAN HAVE
IT?!?!?  I think I just decided which compiler to invest in, unless the people
over at Manx are just as guilty...

>I'm gonna lasso you with my rubberband lazer,		Mike Meyer
>Pull you closer to me, and look right to the moon.	mwm@relay.pa.dec.com
>Ride side by side when worlds collide,			decwrl!mwm
>And slip into the Martian tide.


       ////  Disclaimer:  THe opinions expressed above are not my own, but
      ////   the property of some higher-up power, to which I am only a tool.
     //// 
\\\\////     Greg Harp                greg@ccwf.cc.utexas.edu
 \\XX//   

S.J.Raybould@fulcrum.bt.co.uk (Simon Raybould) (05/09/90)

In article <12750@wpi.wpi.edu> pawn@wpi.wpi.edu (Kevin Goroway) writes:
>I think I need some quick tutoring as to how one deallocates memory:
>
>I'm doing an awful lot of mallocs to create a lot of linked lists...
>when I free them using
>Freeit(object)
>  struct obj *object;
>{
>	if (object->next != NULL)
>		Freeit(object->next);
>	free(object);
>}



You need a recursive routine to free the whole list, along the lines of

freelist(ptr)
struct listelement *ptr;
{
	if(ptr->Next)
		freelist(ptr->Next);	/* Recurse */
	if(ptr)
		free(ptr);
}



+-=-=-=-=-=-=-=-=-=-=-=-=-=-=+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
|   British Telecom Fulcrum  | name : Simon John Raybould   {^.^}   |
|   Fordrough Lane           | path : sie@fulcrum.bt.co.uk   \~/    |
|   Birmingham               +-----------+--------------------------|
|   B9 5LD                   |   //      | AMIGA B2000HD 3MB 8088BB |
|   ENGLAND                  | \X/AMIGA  | Lattice C V5.05          |
+-=-=-=-=-=-=-=-=-=-=-=-=-=-=+=-=-=-=-=-=+=-=-=-=-=-=-=-=-=-=-=-=-=-+

karl@sugar.hackercorp.com (Karl Lehenbauer) (05/09/90)

In article <29676@ut-emx.UUCP> greg@walt.cc.utexas.edu (Greg Harp) writes:
>In article <MWM.90May8115606@raven.pa.dec.com> mwm@raven.pa.dec.com (Mike (Real Amigas have keyboard garages) Meyer) writes:
>>Lattice's memory routines allocate a growing pool. Doing a free()
>>doesn't release the memory back to the OS, but puts it back in the
>>pool for reuse by the Lattice memory manager.

>Sin! Sin! Sin!  PLEASE don't tell me Manx does the same thing!! 

OK, it doesn't.  But you don't want to malloc() under Manx anyway, usually,
because it does search through a linearly linked list containing an entry for
each chunk of memory you've malloc'ed to determine whether or not the memory
you're freeing was actually allocated (eliminating a source of gurus, but
doing it silently -- it doesn't report if you tried to free something you
didn't allocate, it just ignores it), which can get quite slow if you've
called malloc a bunch of times.

Besides, the Amiga puts a finer point on memory allocation than malloc 
supports, that is, do you want chip or fast RAM, public or private
(try to use MEMF_PUBLIC where relevant in case there's a protected mode
version of AmigaDOS sometime in the future).

Disclaimer:  This is from examining the library source to 3.6.  I haven't
checked the 5.0 libsrc to see if they've changed their methods.
-- 
-- uunet!sugar!karl
-- Usenet access: (713) 438-5018

jmeissen@oregon.oacis.org (John Meissen) (05/10/90)

In article <X-8#!|_@masalla.fulcrum.bt.co.uk> S.J.Raybould@fulcrum.bt.co.uk (Simon Raybould) writes:
>You need a recursive routine to free the whole list, along the lines of
>
>freelist(ptr)
>struct listelement *ptr;
>{
>	if(ptr->Next)
>		freelist(ptr->Next);	/* Recurse */
>	if(ptr)
>		free(ptr);
>}
This is exactly the routine the original poster had listed, except he didn't
bother to check the validity of 'ptr' before freeing it. In that respect,
your routine has a bug, because if there is a chance of the pointer being
invalid you should check it BEFORE dereferencing it:

	if (ptr)
	   {
	   if (ptr-Next) freelist(ptr-Next);
	   free(ptr);
	   }

-- 
 John Meissen ............................... Oregon Advanced Computing Institute
 jmeissen@oacis.org        (Internet) | "That's the remarkable thing about life;
 ..!sequent!oacis!jmeissen (UUCP)     |  things are never so bad that they can't
 jmeissen                  (BIX)      |  get worse." - Calvin & Hobbes

mcmahan@netcom.UUCP (Dave Mc Mahan) (05/10/90)

In article <29676@ut-emx.UUCP> greg@walt.cc.utexas.edu (Greg Harp) writes:
>In article <MWM.90May8115606@raven.pa.dec.com> mwm@raven.pa.dec.com writes:
>>Lattice's memory routines allocate a growing pool. Doing a free()
>>doesn't release the memory back to the OS.
>
>Sin! Sin! Sin!  PLEASE don't tell me Manx does the same thing!! Memory is too
>much of a commodity in a multitasking machine to hog it like that! What 
>happens when an otherwise tame, small program wants to allocate a large 
>amount of memory just once, use it, and return it so OTHER PROGRAMS CAN HAVE
>IT?!?!?  

That's why C= invented the AllocMem() and FreeMem() function calls.  With not
to much effort, you get EXACTLY what you want.

>\\\\////     Greg Harp                greg@ccwf.cc.utexas.edu


  -dave