R
Robert Frunzke
Hello,
I need to implement a custom allocator to speed up the allocation of a
specific class in my project and instead of hardwiring it, I would
"templatize"(?) it. The allocator should have minimal overhead and be
used for allocation of a large number of "Node" classes which represent
a hierarchical structure.
The custom allocator has to be at least intrusive as possible for the
user. The allocator has to be somehow bound to the class that uses it.
And the user should not take any note of it. Additionally the work to
bind the allocator to the class should be minimal.
My first approach was to use macros.. but templates should do better.
The result:
template < typename T, size_t pool_size, size_t pool_incr >
class PoolAllocator {
/* .. implementation details omitted .. */
public:
void *operator new( size_t );
void operator delete( void *, size_t );
void *operator new[]( size_t );
void operator delete [] ( void *, size_t );
};
example usage:
class Node; // forward decl.
class Node : public PoolAllocator< Node, 4096, 512 > {
/* .. */
};
Advantages:
1. User can write "Node *n = new Node()" as usual
2. Easy to dock with any class
3. Configurable
Drawbacks:
1. user has to call some kind of static "Node::InitAllocator()" method
to create the pool, or alternatively live with the overhead of checking
it in every new call
2. does not allow inheritance of Node (pool should only have equally
sized objects)
?. any further drawbacks?
Is there a better solution without those drawbacks?
Regards,
Robert
I need to implement a custom allocator to speed up the allocation of a
specific class in my project and instead of hardwiring it, I would
"templatize"(?) it. The allocator should have minimal overhead and be
used for allocation of a large number of "Node" classes which represent
a hierarchical structure.
The custom allocator has to be at least intrusive as possible for the
user. The allocator has to be somehow bound to the class that uses it.
And the user should not take any note of it. Additionally the work to
bind the allocator to the class should be minimal.
My first approach was to use macros.. but templates should do better.
The result:
template < typename T, size_t pool_size, size_t pool_incr >
class PoolAllocator {
/* .. implementation details omitted .. */
public:
void *operator new( size_t );
void operator delete( void *, size_t );
void *operator new[]( size_t );
void operator delete [] ( void *, size_t );
};
example usage:
class Node; // forward decl.
class Node : public PoolAllocator< Node, 4096, 512 > {
/* .. */
};
Advantages:
1. User can write "Node *n = new Node()" as usual
2. Easy to dock with any class
3. Configurable
Drawbacks:
1. user has to call some kind of static "Node::InitAllocator()" method
to create the pool, or alternatively live with the overhead of checking
it in every new call
2. does not allow inheritance of Node (pool should only have equally
sized objects)
?. any further drawbacks?
Is there a better solution without those drawbacks?
Regards,
Robert