STL Allocator Problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: STL Allocator Problem

  1. #1
    Join Date
    Apr 2006
    Posts
    3

    Question 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?

    Thanks,

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    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:
    http://www.devx.com/cplus/10MinuteSolution/30508
    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.
    Danny Kalev

  4. #4
    Join Date
    Apr 2006
    Posts
    3
    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.

    thanks,

    If need I can try to write a sample.

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    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.
    Danny Kalev

Similar Threads

  1. Problems with STL string
    By Darkrider in forum C++
    Replies: 2
    Last Post: 12-12-2005, 02:21 PM
  2. STL / C2784 Problem
    By Jerri in forum C++
    Replies: 1
    Last Post: 11-10-2005, 08:23 AM
  3. STL Multimap
    By amitapm in forum C++
    Replies: 12
    Last Post: 06-27-2005, 02:03 PM
  4. xml C++ Compiling using STL
    By eric in forum XML
    Replies: 0
    Last Post: 06-04-2001, 08:58 PM
  5. ANSI/ISO C++ compliance of VC++ 7.0 sucks! ...
    By Shiv Shankar Ramakrishnan in forum .NET
    Replies: 29
    Last Post: 09-01-2000, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center