473,383 Members | 1,748 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,383 software developers and data experts.

Using __pool_alloc or some other allocator?

6
Let’s say I have a hash map from strings to a user-defined object of type X. Thoughout the lifetime of my program, I have millions of these objects allocated/free’d. A friend mentioned that I look into some kind of allocator that will save the ‘template’ of my objects when I free, so that I don’t have to incur the additional heap-searching overhead to allocate my next object. I read something about the __pool_alloc but it is unclear to me how to use it. Google didn’t give me any advice on the matter. Any help would be great.
Sep 18 '09 #1
3 2905
weaknessforcats
9,208 Expert Mod 8TB
You could move the address of the object to a linked list of zombie objects in the destructor of the object.

Next time you create an object just grab one from the list, like the head or tail. Only if the list is empty do you ened to allocate a fresh object.

If you do this I strongly suggest you use a handle class.
Sep 18 '09 #2
khoda
6
Thanks weaknessforcats. That strategy makes sense, but I'm looking for the allocator to do this for me. I'm assuming __pool_alloc does this, though some other allocator may do this better.

My question is: Is __pool_alloc what I'm looking for? If so, how do I use it?
Sep 18 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
If you are trying to avoid allocation time while running you could:

1) at startup create a lot of objects and put their addresses in a linked list.
Then as you neeed objects, get trhe address of one from th list. When you delete an object do so by putting its address back into the list. Delete the list when the program finishes. If the list ever gets exhausted, then allocate new objects individually. When these objects enter the destructor the this pointer can be added to the list.

2) create a private heap using placement new. Allocate from your heap as you would in (1) above but when you finish the program, just delete the private heap. Now you don't need to free your objects. If you are using Windows then about CreateProcessHeap.

3) create your objects as needed but do on a worker thread. Check back later to get the address from that thread.

Note than in C++ you can override std::operator new. If you are using option (1) here's where you work with your linked list.

pool_alloc is a Boost whiz bang that you can use as an STL allocator with the STL first class containeers, like vector and list.

What I am talking about is pre-allocation. That is, allocate at the outset enough objects so that you don't need to allocate them individaully later.

My linked list would be in a Singleton and would be pre-loaded in the Singleton constructor. That way the list would be guaranteed to exist before main() started. Creating the linked list in main() is too late as global objects may need the list before main() starts.

Lastly, do not use your pointers directly, Use aa handle object. Read this:

http://bytes.com/topic/c/insights/65...-smart-pointer
Sep 18 '09 #4

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

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
7
by: sbobrows | last post by:
{Whilst I think much of this is OT for this newsgroup, I think the issue of understanding diagnostics just about gets under the door. -mod} Hi, I'm a C++ newbie trying to use the Boost regex...
2
by: Weddick | last post by:
I decided to try creating a map with microsoft visual C++ 6. When building this small app I get 95 warnings which make no sense to me. Anybody else see this before? Thanks, // CODE SAMPLE...
5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
31
by: anongroupaccount | last post by:
I have an ABC with a protected member that is a reference to an object of an ABC type: class ABC { public: virtual ~ABC() = 0; protected: AnotherABC& _member; }
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
4
by: Robert Frunzke | last post by:
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...
7
by: tehn.yit.chin | last post by:
I am trying to experiment <algorithm>'s find to search for an item in a vector of struct. My bit of test code is shown below. #include <iostream> #include <vector> #include <algorithm>...
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.