C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

custom allocator using templates

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Robert Frunzke
Guest





PostPosted: Sun Jan 29, 2006 2:00 am    Post subject: custom allocator using templates Reply with quote



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
Back to top
Pete Becker
Guest





PostPosted: Sun Jan 29, 2006 4:00 pm    Post subject: Re: custom allocator using templates Reply with quote



Robert Frunzke wrote:
Quote:
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.

Why a template? One class doesn't require a template. Just provide
class-specific operator new and operator delete.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Back to top
Robert Frunzke
Guest





PostPosted: Sun Jan 29, 2006 6:00 pm    Post subject: Re: custom allocator using templates Reply with quote



Ok, you may be right. I'll try it this way.

Thank you,
Robert
Back to top
Pete Becker
Guest





PostPosted: Sun Jan 29, 2006 6:00 pm    Post subject: Re: custom allocator using templates Reply with quote

Robert Frunzke wrote:

Quote:
Yes, but there may be another class in the project which could be
optimized with the same allocator, or one would like to reuse the
allocator in another project. Then a template would be nice Smile Not a
must-have, yes.


If you don't know where it's going to be used you can't design it. If
there's "a specific class" that needs special memory management, write
the memory manager for it. Don't do the extra work of generalizing the
memory manager until you understand it and know what extras you need.
Don't make your work harder than it has to be.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Back to top
Robert Frunzke
Guest





PostPosted: Sun Jan 29, 2006 6:00 pm    Post subject: Re: custom allocator using templates Reply with quote

Yes, but there may be another class in the project which could be
optimized with the same allocator, or one would like to reuse the
allocator in another project. Then a template would be nice Smile Not a
must-have, yes.

Robert
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.