Click here to Skip to main content
6,293,171 members and growing! (12,354 online)
Email Password   helpLost your password?
Languages » C / C++ Language » Memory Management     Intermediate License: The Code Project Open License (CPOL)

Object Pooling for Generic C++ classes

By Thomas George

Implements a basic object pooling scheme for generic C++ objects.
VC6, VC7Win2K, WinXP, Visual Studio, STL, Dev
Posted:16 Apr 2003
Updated:4 May 2003
Views:40,214
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
17 votes for this article.
Popularity: 4.42 Rating: 3.59 out of 5
2 votes, 11.8%
1
3 votes, 17.6%
2
2 votes, 11.8%
3
5 votes, 29.4%
4
5 votes, 29.4%
5

Introduction

The default memory allocator is not efficient when it comes to frequent new and delete operations. There are a number of general purpose allocators that replace the standard one. There are certain situations when you want to improve performance at the cost of using more memory. This is especially true for small objects that are frequently created and destroyed in processing intensive applications. So, I decided to create a class that pools instances of classes, so that new and delete works on an array of already existing objects.

Usage

To enable pooling for a class, you publically inherit from this class, as shown below:

class myclasstobepooled : public ObjectPool< myclasstobepooled >

Call this function before any objects are created:

myclasstobepooled::initialize();

Call this function after all objects are deleted:

myclasstobepooled::finalize();

Implementation

The ObjectPool class maintains a static vector of the pointers of the template type. The overridden new and delete operators manage the list.

There is an option to enable or disable thread safety. This is provided so that the critical section calls can be excluded, if the class is used in a single threaded environment, or if the new and delete operations are always performed from the same thread.

You can disable multithreading support by commenting the line:

#define ___USE_MT___

The constructor and destructor gets called even when the placement new and delete is used. So, there is no special code required to call the constructor and destructor.

Additional information

Initially I was thinking about allocating the memory using malloc and calling the constructor and destructor manually. But, this would be along the lines of a general purpose memory allocator, where the global new and delete are overridden.

Suppose you have a class myclass.

// allocate

myclass* p = malloc(sizeof(myclass));     

// call the constructor, does not allocate any memory

new (p) myclass(); 


// call the destructor manually

p->~myclass();

Conclusion

If you have a class that you new and delete frequently in a performance critical part of the code, you can get substantial benefits by using this class.

Downloads

A non-STL version version of the class is available in the downloads section. This was provided by Christian Kaiser.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Thomas George


Member

Location: India India

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralBoost Pool Library PinmvpStephen Hewitt14:29 11 Nov '07  
GeneralNon STL version, some problems... PinmemberArmel Asselin22:59 4 May '04  
GeneralTiming tests Pinmemberc2j23:47 30 Apr '03  
GeneralRe: Timing tests PinsupporterThomas George3:58 30 Apr '03  
GeneralRe: Timing tests Pinmemberc2j24:30 30 Apr '03  
GeneralRe: Timing tests PinsupporterThomas George4:34 30 Apr '03  
GeneralIt's a good idea. Pinmembersclzmbie16:50 21 Apr '03  
GeneralAdditional Overhead? PinmemberMike McCann13:33 21 Apr '03  
GeneralRe: Additional Overhead? PinsupporterThomas George13:52 21 Apr '03  
GeneralRe: Additional Overhead? PinmemberJens Nilsson8:11 6 May '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 May 2003
Editor: Smitha Vijayan
Copyright 2003 by Thomas George
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project