473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is it ok to create a Simple memory pool using singleton and a queue of pointers?

I have been looking at memory pools and was wondering what would be wrong with creating a singleton which create a "pool" of (allocated) pointers to a given struct/class( in the case below Messages ), and then when a client needs a "new" one pass them one from your free message queue. Any thoughts, potential problems, etc, would be appreciated. Thanks.

Expand|Select|Wrap|Line Numbers
  1. class MsgPool
  2. {
  3. public:
  4.  
  5.     ~MsgPool();
  6.  
  7.     static void recycleMem(Message* msg);
  8.     static Message* newMessage();
  9.     static MsgPool* createInstance();
  10.  
  11. private:
  12.  
  13.     MsgPool();
  14.  
  15.     static MsgPool* msgPoolInstance;
  16.     static Message* msgPtr;
  17.     static std::queue<Message*> msgPtrPool;
  18. };
  19.  
  20. MsgPool* MsgPool::msgPoolInstance = NULL;
  21. std::queue<Message*> MsgPool::msgPtrPool;
  22. Message* MsgPool::msgPtr;
  23.  
  24. //CCONSTRUCTOR/DESTRUCTORS
  25. //----------------------------------------------------------------------------------------------//
  26.  
  27. MsgPool::MsgPool()
  28. {
  29.     while (msgPtrPool.size() < INITPTRPOOL) {
  30.         msgPtr = new Message();
  31.         msgPtrPool.push(msgPtr);
  32.     }
  33. }
  34. MsgPool::~MsgPool()
  35. {
  36.     if (msgPoolInstance != NULL) {
  37.         while (!msgPtrPool.empty()) {
  38.             msgPtr = msgPtrPool.front();
  39.             msgPtrPool.pop();
  40.             delete msgPtr;
  41.         }
  42.  
  43.         delete msgPoolInstance;
  44.         msgPoolInstance = NULL;
  45.     }
  46. }
  47. //----------------------------------------------------------------------------------------------//
  48.  
  49. //INSTANCE CREATION
  50. //----------------------------------------------------------------------------------------------//
  51. //This function creates the one and only instance of the message pool
  52. MsgPool* MsgPool::createInstance()
  53. {
  54.     if (msgPoolInstance == NULL) {
  55.         msgPoolInstance = new MsgPool();
  56.     }
  57.         return msgPoolInstance;
  58. }
  59. //----------------------------------------------------------------------------------------------//
  60.  
  61. //"ALLOCATION/DEALLOCATION"
  62. //----------------------------------------------------------------------------------------------//
  63.  
  64. //This function puts a given pointer back into the pool
  65. void MsgPool::recycleMem(Message* msg)
  66. {
  67.     if (msg != NULL) {
  68.         msgPtrPool.push(msg);
  69.     }
  70.     else
  71.         std::cerr <<"Passed NULL ptr" << std::endl;
  72. }
  73.  
  74. //This function returns a message pointer
  75. Message* MsgPool::newMessage()
  76. {
  77.     //If out of memory in pool allocate new memory
  78.     if (msgPtrPool.empty()) {
  79.         return (new Message);
  80.     }
  81.     else
  82.     {
  83.         msgPtr = msgPtrPool.front();
  84.         msgPtrPool.pop();
  85.         return msgPtr;
  86.     }
  87.  
  88. }
Jan 31 '11 #1
3 2690
Oralloy
988 Expert 512MB
Of course it's ok to create such a pool.

In some instances, it's sometimes beneficial to application performance, too.

The question is rather - is it appropriate or necessary to create this memory pool to solve your higher level problem.

Also, you might search around for memory pool template classes that you might use. If you multiple pools to implement, it is best if they all behave similarly. You'll have a lot more confidence in your application.

Cheers!
Oralloy
Jan 31 '11 #2
Assuming we are strictly seeking performance, would the implementation used above work as well(better?) as overloading new and delete and creating a "real" memory pool?
Feb 1 '11 #3
Oralloy
988 Expert 512MB
Peter,

What new and [icode]delete[/icode[ will get you, of course, is dynamic C++ objects on top of your memory pool, which might be very valuable for an application that needs to do rapid recovery of pooled objects. (e.g. deleting all objects from a pool is simply accomplished by declaring the pool closed - no destructors get called or anything - a technique used in embedded systems).

Your design, as it stands, does one "important" thing - it prevents the need for continuous construction and destruction of Message objects. Since I don't know how expensive that operation is, I can't comment further.

You might get a bit more optimization by using a self-managed stack of available Message objects, rather than a std::queue<> object.

Also, have you considered what your code should do, when your pool runs out of objects? Or is this a case which cannot happen? [Object use being controlled by the enclosing application, which is beyond the pool's control].

Since you're optimizing time, you get a bit of an improvement by not calling the Message c'tor and d'tor. You will get a bit more by using a self-managed stack of messages without. Is that enough?

May I ask what your real problem is?

The reason I'm asking, is because replacing the C++ memory management mechanism is an expensive optimization that generally results in low gain. Which is not to say that there aren't times when it is appropriate. I would estimate a week or of man time to write, debug, and "prove" that your code is truly working as required. Not only do you have to write your optimized memory pool (which is mostly done), but you probably should write error detection into the Message class, which detects when the pool is not used (for the maintennece guys that come after you).

Luck!
Feb 1 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Philip Lawatsch | last post by:
Hi, I have some questions about whats written in http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14 (Describing some memory pool) #1 From what i understand this will also work for new...
2
by: Abhi | last post by:
Hi, I am looking for some memory pool implementation of new/delete. Is there any freeware/shareware/software out there? Any pointers would be great help. Thanks ......Abhi
8
by: Tron Thomas | last post by:
As part of applying for a programming position at a company, I recently I had submitted some code samples to one of the developers for review. This is the feedback I received: One of his...
5
by: bull | last post by:
hi could any one explain with example the following in a better way to understand 1. what is stack in memory, how the programs are stored in stack , what is the use of stack 2. What is heap in...
8
by: Cuthbert | last post by:
Hi folks, This question is a little deep into memory management of microprocessor. How do we know the memory arrangement using in microprocessors? Top-Bottom or Bottom-Top? For example, the...
5
by: cikkamikka | last post by:
Hi friends, Sorry for such basic question. but I wanted to know where does new operator or malloc operator allocate memory? in actualy physical Main memory or virtual memory?
11
by: Grey Alien | last post by:
Any one know of an open source memory pool library?. I can't seem to find any implemented in C (many available in C++ e.g. Boost). Google is not turning up anything useful ...
11
by: Grey Alien | last post by:
I am looking to write a very simple memory pool library to store only one data type at a time - i.e. to provide a contiguous block of memory to be alloc'd free'd by the calling program. I am I...
16
by: graham.keellings | last post by:
hi, I'm looking for an open source memory pool. It's for use on an embedded system, if that makes any difference. Something with garbage collection/defragmentation would be nice. It should have...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.