J
jacob navia
Within the container library I have ignored (more or less) the problem
of the allocator and memory allocation.
I have a simple macro that has two values: either you use the garbage
collector and you have GC_malloc as allocator, or you use the
malloc/free system for maximum portability.
The problem with this simple approach is that many other solutions
exist. For instance you could want to pass a pointer to an allocator
of your own, or whatever...
I could make things more flexible by adding the allocator system
to every container by putting it in the container header.
Or I could add it to the virtual functions table of every container,
supposing tat you wouldn't want to change allocation for single
containers but for a class of containers, for instance all lists
or all hash tables, etc.
Obviously I do not want to make the interface heavier than it is now,
so the constructor functions will use a default allocator that will be
either GC_malloc or malloc according to how the library was compiled.
You can change the default allocator later.
If you have the allocator in the header you just do:
List->Allocator = myNewAllocator;
and only THAT list will use the myNewAllocator. There would be no way
to change the behavior of all lists.
If you have the allocator in the virtual functions table, you do
List->lpVtbl->Allocator = myNewAllocator;
and ALL lists will use the "myNewAllocator" function, even the new ones
since you have changed the value in the master vtable. That change will
be valid until the program ends (or you restore the old value of course)
If that is too sweeping change, you can still do:
(1) Make a copy of the vtable
(2) change the copy
(3) Put the copy in the lists you want to use the new allocator.
I think the second behavior is better design. t is more flexible, and
uses less space.
What do you think?
Thanks in advance for your attention.
P.S. the library of GNU (libavl) has an allocator pointer in each
container.
of the allocator and memory allocation.
I have a simple macro that has two values: either you use the garbage
collector and you have GC_malloc as allocator, or you use the
malloc/free system for maximum portability.
The problem with this simple approach is that many other solutions
exist. For instance you could want to pass a pointer to an allocator
of your own, or whatever...
I could make things more flexible by adding the allocator system
to every container by putting it in the container header.
Or I could add it to the virtual functions table of every container,
supposing tat you wouldn't want to change allocation for single
containers but for a class of containers, for instance all lists
or all hash tables, etc.
Obviously I do not want to make the interface heavier than it is now,
so the constructor functions will use a default allocator that will be
either GC_malloc or malloc according to how the library was compiled.
You can change the default allocator later.
If you have the allocator in the header you just do:
List->Allocator = myNewAllocator;
and only THAT list will use the myNewAllocator. There would be no way
to change the behavior of all lists.
If you have the allocator in the virtual functions table, you do
List->lpVtbl->Allocator = myNewAllocator;
and ALL lists will use the "myNewAllocator" function, even the new ones
since you have changed the value in the master vtable. That change will
be valid until the program ends (or you restore the old value of course)
If that is too sweeping change, you can still do:
(1) Make a copy of the vtable
(2) change the copy
(3) Put the copy in the lists you want to use the new allocator.
I think the second behavior is better design. t is more flexible, and
uses less space.
What do you think?
Thanks in advance for your attention.
P.S. the library of GNU (libavl) has an allocator pointer in each
container.