Suppose we have some fixed-length data type such as a C struct:
struct T
{
...;
}
One way to allocate T would be:
T* create_t() { return (T*) malloc(sizeof(T)); }
and deallocate:
void destroy_t(T* t) { free(t); }
Under the hood malloc uses a memory allocation algorithm that deals with different size blocks in different ways.
Suppose we were writing a program that called create_t and destroy_t frequently and had very many T items allocated at once, (and in pseudo-random order).
Given that the memory required is of fixed size elements, is it possible to write a custom memory allocation scheme that is superior to the generic implementation of malloc.
For example we could preallocate a huge array with elements of size T and then use those, but what is the best method to keep track of which elements have been allocated and which haven't?
What algorithm does malloc on Linux end up using when called with a huge number of allocations of the same size?
Roughly how will the performance of this custom method compare to the generic malloc?