STL Allocator Problem
I am writing code using an RTOS that provides support for private heaps. I am also using stl::maps & stl::vectors.
I would like my containers to use memory out of private heaps so I started writing custome allocators, but have run into 2 problems and 1 question.
p1) Since there will be more than one instance of the allocator, I wanted to provide arguments to the construct to get the allocator to setup the private heaps. However, the allocator interface requires a default constructor. Does anyone have a nifty solution to this problem? I don't hate mine, but I don't like mine.
p2) The control block for these private heaps cannot be copied. The RTOS is remembering them some how and depends on them for each call to allocate and deallocate space. The allocator copy construtor is giving me fits. If I store the control block in the allocator, then STL is calling the copy constructor and that cause problems. If I store a pointer, then some how I have to get the memory for control block. Since I can't the memory from the private heap (I haven't created it yet), and would prefer to not get it from the public heap, I have ran into another dead lock. Does any one have a nift solution to this problem?
Finally, my question:
q1) Does anyone know where to find some good documentation on what the containers will do when the allocator can't return memory?
As to problem 2 - a way to prevent the creation and use of a default copy constructor is to create a "private" copy constructor and then don't implement it. Then everything will be contolled by your implementation - often through overiding the "=" operator.
I really am confused by yoru questions. What is it exactly that you're trying to do? Allocate memory not on the default heap? You can use placement new for this puprose:
As for all the other questions: there's no "good documentation" for this because all this stuff is very platform specific. Allocators in particular are meant to abstract away memory management so you can't expect them to be porable. But my main concern is this: why go through all this trouble? Is it just because of threading issues? DLLs? There are simpler and neater solutions to all these problems.
Ok. Let me try to clarify.
I am using a stl::map to hold a table of infomation. When I insert into the map, there are times when the map will allocate memory. I would like all of the allocated memory for the map to come from a specific region of memory. To do this I must use an allocator.
To complicate matters, I wanted to use a feature of the OS that allows for the creation/management of heaps (OS supplied functions for managing regions of memory). To use this feature, I have to pass in several arguments to the functions used to create/manage the heap. Since an allocator must have a default constructor, I ran into a problem with creating the heap in the constructor. SInce the routines require a "control block" to use for managing this memory, and a this control block CANNNOT be copied, I ran into problems with the copy contructor.
So I am wondering, if anyone else has ran into problems like this, and what they did to solve them.
If need I can try to write a sample.
Looks like you need to rewrite yoru allocators from scratch. It's not an easy task but doable. Simply reqrite the code of the default allocators, and plug-in your custome allocators to the map objects. The allocator is the last argument of every STL container so you can override the default.
By Darkrider in forum C++
Last Post: 12-12-2005, 02:21 PM
Last Post: 11-10-2005, 08:23 AM
Last Post: 06-27-2005, 02:03 PM
Last Post: 06-04-2001, 08:58 PM
By Shiv Shankar Ramakrishnan in forum .NET
Last Post: 09-01-2000, 04:52 PM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL