In the code below is the function make_vector(). It creates a vector and returns it to the caller. I want to be able to specify an allocator for the vector to use, but use the default std::allocator by default. This is because under some circumstances the default allocator is all I need, but other times I need to allocate from some pre-defined memory pools.
The closest I've come is the make_vector2() function template. It works with the std::allocator, but I don't know how to pass the 'arena' argument into my custom allocator.
Hopefull this working c++11 example will explain it better:
#include <malloc.h>
#include <cinttypes>
#include <cstddef>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <vector>
namespace mem
{
// Memory arena to allocate from.
enum class ARENA
{
TEXTURES,
FONTS,
SCRIPTS
};
// Allocate block from specific arena.
void *malloc( const std::size_t size, const ARENA arena )
{
return std::malloc( size /*, arena */ );
}
// Free block from specific arena.
void free( void *ptr, const ARENA arena )
{
std::free( ptr /*, arena */ );
}
// The allocator - forward declaration.
// Not derived from std::allocator - should it be?
// Based on code from here:
// http://drdobbs.com/184403759?pgno=2
template<typename T> class allocator;
// Specialised for void.
template<> class allocator<void>
{
public:
typedef std::size_t size_type;
typedef ptrdiff_t difference_type;
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template<typename U> struct rebind
{
typedef allocator<U> other;
};
};
template<typename T> class allocator
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template<typename U> struct rebind
{
typedef allocator<U> other;
};
allocator( ARENA arena ) noexcept :
arena_( arena )
{}
~allocator() noexcept
{}
pointer address( reference x ) const
{
return &x;
}
const_pointer address( const_reference x ) const
{
return &x;
}
pointer allocate( size_type n, allocator<void>::const_pointer hint = 0 )
{
void *p = mem::malloc( n * sizeof( T ), arena_ );
if ( p == nullptr )
{
throw std::bad_alloc();
}
return static_cast<pointer>( p );
}
void deallocate( pointer p, size_type n )
{
mem::free( p, arena_ );
}
size_type max_size() const noexcept
{
return std::numeric_limits<std::size_t>::max() / sizeof( T );
}
void construct( pointer p, const T& val )
{
new (p) T(val);
}
void destroy( pointer p )
{
p->~T();
}
allocator( const allocator& src ) noexcept
{
arena_ = src.arena_;
}
ARENA arena_;
};
} // namespace mem
template<class T1, class T2> bool operator==( const mem::allocator<T1> &alloc1, const mem::allocator<T2> &alloc2 ) noexcept
{
return alloc1.arena_ == alloc2.arena_;
}
template<class T1, class T2> bool operator!=( const mem::allocator<T1> &alloc1, const mem::allocator<T2> &alloc2 ) noexcept
{
if alloc1.arena_ != alloc2.arena_;
}
// How do I allow the custom allocator to be passed? Function parameter? Template?
std::vector<uint8_t> make_vector()
{
std::vector<uint8_t> vec;
// Do stuff with the vector
return vec;
}
// This template function seems to work with std::allocator
template< typename T > std::vector<uint8_t,T> make_vector2()
{
std::vector<uint8_t,T> vec;
// Do stuff with the vector.
return vec;
}
int main( int argc, char **argv )
{
// vec1 - Allocates from TEXTURES arena
// See the C++11 FAQ by Bjarne Stroustrup here:
// http://www2.research.att.com/~bs/C++0xFAQ.html#scoped-allocator
std::vector<uint8_t, mem::allocator<uint8_t>> vec1( mem::allocator<uint8_t>{mem::ARENA::TEXTURES} );
// vec2 - Make the vector using the default allocator.
auto vec2 = make_vector2< std::allocator<uint8_t> >();
return 0;
}
In main() vec1 is created to use the TEXTURES arena for allocation. The arena to use is passed into the constructor of the allocator. Vec2 is created by the make_vector2() templated function and uses the std::allocator.
Q: How can I define the make_vector() function so it can create a vector that uses the std::allocator or the custom pool allocator above?