Mail Archives: djgpp/1996/08/13/06:41:59
Leath Muller wrote:
>
> I have never heard of alloca before...and as each structure at the moment
> gets allocated 32 bytes at a time (I think the structure takes about 24
> bytes each), this would work quite nicely... :) Will have to look into
> this...
> alloca() is in stdlib.h, to understand it better, read the function code
alloca.c, in libc\compat\stdlib in the djlsr* archive.
If you allocate many small structures of predefined size (as your posting
implies), you might be better off, though, by reserving space for them in
the beginning (either through a malloc, or static allocation), and
redispatching this memory space as you need it :
let mystruct be the name of your structure, you could define an array of
mystruct, allocated at start :
struct mystruct *myarray;
which could contain, in the beginning, say 1024, slots for mystructs
also, you would a variable
int myarray_size=1024;
and
int myarray_empty_slots=myarray_size;
Now, everytime you need to allocate a mystruct, you put it in
myarray[myarray_size-myarray_empty_slots]
and decrement myarray_empty slots by 1. When myarray_empty_slots==0, then
you have to increase the size of my_array (say by 1024...).
This is fairly easy to implement, so far you allocate data of fixed size,
and you do not free anything during execution.
If you want to free some mystructs, and reclaim their space for others,
then you will need to keep track of this, through another array
int *used_slots, of size myarray_size.
containing 1 when a slot is in use, and 0 when it is not in use.
In this case, you just do as before, except that when you free some
mystruct, you put its used_slots value to zero, (but do not update
myarray_free_slots).
Then, when empty_slots approaches zero, you can consider "packing"
myarray, by "filling in" all freed space. This will increase the value of
myarray_free_slots... and you might not need to increase the size of
myarray...
Hope this helps,
Francois
- Raw text -