From: | Allan Odgaard |
Date: | 07 Aug 2000 at 21:25:20 |
Subject: | Re: memory allocation fcns |
On 05-Aug-00, Jack York wrote:
> 1) What is the recommended or most generally used fcn of the
> three (AllocPooled, AllocVec, AllocMem)?
AllocPooled.
> 2) How does malloc fall into this comparison? Is it used mainly
> only if portable code is desired or are there other reasons?
malloc is a wrapper, most often it uses AllocPooled (and tags the
allocation with its size), but it depends on the compiler (linker
library).
> 3) Does it make much difference as far as program size/speed
> as to which fcn is used?
Using AllocPool will avoid (or limit) fragmenting the main memory.
Avoiding AllocVec will save the bytes used for tagging the allocation
with its size. Also AllocVec/AllocMem rounds up the size of the
allocation, I'm not sure AllocPool does that (atleast not as much).
AllocPool should be faster during allocation, but if you allocate a lot
of chunks (500+) then it's slow during freeing, unless you just free the
entire pool.
Speedwise your program is only affected if you allocate or free memory
in critical loops, which have hundreds or maybe thousands of
iterations.
> 4) I saw mentioned before that if pools are used, it is better to
> allocate a different pool for each type (one for int, one
> for char, etc.). Is this the case?
hmm... I fail to see why... perhaps it's to avoid fragmentation (in the
pool only, because freeing a structure will ensure that the hole can
always be used again, since all future allocations will be of the exact
same size as the hole).
> 5) Not that I would do this but...if pools are used, is it necessary
> to check if puddles have been allocated once the pool has been
> obtained? That is, do you need to say if ( ptr = AllocPooled (
> instead of just ptr = AllocPooled (
Yes -- pools doesn't solv the problem with limeted memory resources ;-)
Regards Allan