swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory problems mixing zones & malloc


From: Sven N. Thommesen
Subject: Re: Memory problems mixing zones & malloc
Date: Fri, 24 Oct 1997 18:31:53 -0500

Hi Steve!

Don't know if you have all the answers you need by now, but I'll contribute
one or two:

-you can find an example of dynamic memory allocation/deallocation in the
documentation for my random generators. See the file
/swarm-1.0.3/src/random/docs/README.Generators.v07, lines 349ff.

-the basic scheme is:

// 1. determine size of data area needed:
arrSize = #items*sizeof(item); // e.g. 3000*sizeof(double)
// 2. allocate the data from some zone:
myArr = [[self getZone] alloc: arrSize]; // or some other named zone
if (myArr == 0) { }; // error during allocation!
// 3. then if need be, zero the data:
memset(myArr, 0, arrSize);
  // NOTE: if arrSize > LONG_MAX, you dump core here. 
  // On Intel at least, LONG_MAX = 2^31 = 2Gb
// 4. Finally, to deallocate the memory:
[[self getZone] free: myArr];
  // Don't forget to free dynamic arrays before killing the objects 
  // that own them ...

-as for your specific questions, I'll try but get Glen's concurrence before
trusting me:

At 10:50 AM 10/24/97 +0100, you wrote:
>
>Thanks Dwight,
>
>Embarrassing to think that I've avoided dynamic allocation through
>several thousand lines of code just because the C primers I have don't
>explain malloc clearly.
>
>1. The swarm doc entry for alloc doesn't mention whether an exception
>occurs if there isn't enough memory for allocation.
>

I believe it will. See code above.

>2. Am I correct in thinking that the idea with zones is to keep
>objects that frequently pass messages to each other in the same zone
>and in different zones from objects (esp. large) that are infrequently
>used. Is it more efficient to create named zones from globalZone to
>segregate objects and use getZone to group objects - hoping to group
>chatty objects within the machines memory page size?
>

That is one reason. Another is if you need to create and later remove a
bunch of objects during simulation -- just put them all in the same zone
and deallocate the whole zone when done. Easier than deallocating every
object, one by one.

>3. When creating objects from a class the only memory allocated in
>that that holds the instance variables. There is only one copy of
>variables declared within the class but not in the interface - for
>instance large static declared arrays declared before the
>@implementation statement. Is that right?
>

That is my understanding, yes.

>Apologies for the basic questions but although I've downloaded the
>ObjC manual, Swarm docs & people's code this malloc business makes me
>doubt that I know what I'm doing.
>
>Cheers.

Hope the above helped, and good luck!
--Sven


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.
                  ==================================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]