J
jacob navia
One of the main arguments being advanced by the people that oppose
the standardization of a container library in C is the fact that
there are many different usage patterns for such a library, and it
would be impossible to satisfy them all.
The solution proposed overcomes this problem in an elegant way
by using a function table.
For example, let's see how that would work in lists, one of the
simplest containers.
As it stands now, the list header structure, with free list, pointer
to first, last, a size_t for the count of the items, and several
other fields could be "top heavy", and bring too much overhead if
the list is very small. For instance, if you know in advance
that you will never store more than 5 items at a time in the
list, such a "top heavy" overhead would be too much.
The solution is to write a specialization of the list
software for very small lists, where (for instance)
you just allocate all the 5 items at once when creating the
list, you eliminate the free list and the heap manager.
You write then, all the functions contained in the interface in a
NEW virtual table (say "small_list.c") and you define a
new virtual table, etc. Then, you just add to the API a
function
newSmallList(size_t element_size);
and you are done. All the code that uses
mylist->lpVtbl->Add(mylist, &data);
will STAY as before identical, without the need to change anything
in it. And one day, when you discover that the list you thought
would be small is no longer small at all, NOTHING must be changed
in the code that uses the list container, only the call to the
normal creation function.
Since all virtual tables are binary compatible (the virtual table
is always the first member) the user code doesn't even need to be
recompiled.
the standardization of a container library in C is the fact that
there are many different usage patterns for such a library, and it
would be impossible to satisfy them all.
The solution proposed overcomes this problem in an elegant way
by using a function table.
For example, let's see how that would work in lists, one of the
simplest containers.
As it stands now, the list header structure, with free list, pointer
to first, last, a size_t for the count of the items, and several
other fields could be "top heavy", and bring too much overhead if
the list is very small. For instance, if you know in advance
that you will never store more than 5 items at a time in the
list, such a "top heavy" overhead would be too much.
The solution is to write a specialization of the list
software for very small lists, where (for instance)
you just allocate all the 5 items at once when creating the
list, you eliminate the free list and the heap manager.
You write then, all the functions contained in the interface in a
NEW virtual table (say "small_list.c") and you define a
new virtual table, etc. Then, you just add to the API a
function
newSmallList(size_t element_size);
and you are done. All the code that uses
mylist->lpVtbl->Add(mylist, &data);
will STAY as before identical, without the need to change anything
in it. And one day, when you discover that the list you thought
would be small is no longer small at all, NOTHING must be changed
in the code that uses the list container, only the call to the
normal creation function.
Since all virtual tables are binary compatible (the virtual table
is always the first member) the user code doesn't even need to be
recompiled.