Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An STL implementation that uses a dynamic/state based allocator?

Tags:

c++

stl

allocator

Does anybody know of an STL implementation that allows for dynamic allocators to be passed in to an instance of a container before use.

The scenario is that we have a general memory allocator that manages a number of pools of memory and for each instance of say stl::vector we want to allocate each instance from a different pool of memory.

The problem with the standard STL implementations is that you can only define the memory pool on a type basis ie all vector's of type int would allocate from the same pool.

I've already swapped out our default stl::allocator for one that has a state ie the pool we want to allocate this instance from but this doesn't work well for say stl::list where it allocates things in the default ctor.

For reasons related to our library we also don't have a valid pool in the ctor for all objects and so we want to call a 'set memory pool' function before users can use the stl container.

Has anybody come across an implementation that supports this kind of thing?

like image 922
user176168 Avatar asked Feb 04 '11 11:02

user176168


People also ask

What is an STL allocator?

An SGI STL allocator consists of a class with 3 required member functions, all of which are static: void * allocate(size_t n) Allocates an object with the indicated size (in bytes). The object must be sufficiently aligned for any object of the requested size.

What is the use of allocator?

Allocators are objects responsible for encapsulating memory management. std::allocator is used when you want to separate allocation and do construction in two steps. It is also used when separate destruction and deallocation is done in two steps.

What is allocator type in C++?

Allocators are used by the C++ Standard Library to handle the allocation and deallocation of elements stored in containers. All C++ Standard Library containers except std::array have a template parameter of type allocator<Type> , where Type represents the type of the container element.

What is container allocator?

Allocators handle all the requests for allocation and deallocation of memory for a given container. The C++ Standard Library provides general-purpose allocators that are used by default, however, custom allocators may also be supplied by the programmer.


3 Answers

I am not so sure about your question precisely. So I'll cover the case of a state-full allocator.

In C++03, any allocator should be able to deallocate resources allocated by another allocator of the same type.

The C++0x standard actually removed this limitation and allows state-full allocators to be passed to STL containers as long as they are Allocator Aware containers (I think it covers all containers packaged with the STL, since they model Sequence).

For example: [allocator.adaptor] $20.10 Class scoped_allocator is now part of the C++0x STL.

like image 57
Matthieu M. Avatar answered Sep 25 '22 07:09

Matthieu M.


The typed allocator can use a general allocator underneath to perform the allocations.

Allocator needs to support these functions:

  pointer address ( reference x ) const;
  const_pointer address ( const_reference x ) const;
  pointer allocate (size_type n, allocator<void>::const_pointer hint=0);
  void deallocate (pointer p, size_type n);
  size_type max_size() const throw();
  void construct ( pointer p, const_reference val );

Assuming you have a mechanism that just allocates memory and deallocates memory, you can use that to implement some of the functions above.

The advantage of allocators being typed is that you know you are going to create lots of items of exactly the same size and can therefore create your "pages" to fit. The biggest issue can be that you are forced by allocate() to return contiguous buffers (and indeed vector needs them).

http://www.cplusplus.com/reference/std/memory/allocator/

Your question is still a bit unclear as to why this is not sufficient. You can initialise the memory-pool with "once" logic. (If it is multi-threaded you can use boost::once to achieve this).

like image 35
CashCow Avatar answered Sep 22 '22 07:09

CashCow


The problem with the standard STL implementations is that you can only define the memory pool on a type basis ie all vector's of type int would allocate from the same pool.

This is not exactly true. You can have different vectors holding int elements each having a different allocator type.

However, regarding the question --

Does anybody know of an STL implementation that allows for dynamic allocators to be passed in to an instance of a container before use.

-- it is simply not supported by the C++ Standard Library (STL), therefore, while it may be that there are implementations where per-object allocators work, it is not portable.

For a detailed analysis of why and how to use custom allocators see Scott Meyers's book Effective STL, specifically Item 11: Understand the legitimate use of custom allocators.

like image 21
Martin Ba Avatar answered Sep 22 '22 07:09

Martin Ba