1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2008 by Rich Sposato
5 // Permission to use, copy, modify, distribute and sell this software for any
6 // purpose is hereby granted without fee, provided that the above copyright
7 // notice appear in all copies and that both that copyright notice and this
8 // permission notice appear in supporting documentation.
9 // The author makes no representations about the
10 // suitability of this software for any purpose. It is provided "as is"
11 // without express or implied warranty.
12 ////////////////////////////////////////////////////////////////////////////////
14 #ifndef LOKI_ALLOCATOR_HPP_INCLUDED
15 #define LOKI_ALLOCATOR_HPP_INCLUDED
17 // $Id: Allocator.h 896 2008-08-08 22:20:05Z syntheticpp $
19 // Requires project to be compiled with loki/src/SmallObj.cpp and loki/src/Singleton.cpp
21 #include <loki/SmallObj.h>
28 //-----------------------------------------------------------------------------
30 /** @class LokiAllocator
31 Adapts Loki's Small-Object Allocator for STL container classes.
32 This class provides all the functionality required for STL allocators, but
33 uses Loki's Small-Object Allocator to perform actual memory operations.
34 Implementation comes from a post in Loki forums (by Rasmus Ekman?).
39 typename AllocT = Loki::AllocatorSingleton<>
45 typedef ::std::size_t size_type;
46 typedef ::std::ptrdiff_t difference_type;
47 typedef Type *pointer;
48 typedef const Type *const_pointer;
49 typedef Type &reference;
50 typedef const Type &const_reference;
51 typedef Type value_type;
53 /// Default constructor does nothing.
54 inline LokiAllocator( void ) throw() { }
56 /// Copy constructor does nothing.
57 inline LokiAllocator( const LokiAllocator & ) throw() { }
59 /// Type converting allocator constructor does nothing.
60 template < typename Type1 >
61 inline LokiAllocator( const LokiAllocator< Type1 > & ) throw() { }
63 /// Destructor does nothing.
64 inline ~LokiAllocator() throw() { }
66 /// Convert an allocator<Type> to an allocator <Type1>.
67 template < typename Type1 >
70 typedef LokiAllocator< Type1 > other;
73 /// Return address of reference to mutable element.
74 pointer address( reference elem ) const
79 /// Return address of reference to const element.
80 const_pointer address( const_reference elem ) const
85 /** Allocate an array of count elements. Warning! The true parameter in
86 the call to Allocate means this function can throw exceptions. This is
87 better than not throwing, and returning a null pointer in case the caller
88 assumes the return value is not null.
89 @param count # of elements in array.
90 @param hint Place where caller thinks allocation should occur.
91 @return Pointer to block of memory.
93 pointer allocate( size_type count, const void *hint = 0 )
95 (void)hint; // Ignore the hint.
96 void *p = AllocT::Instance().Allocate( count * sizeof( Type ), true );
97 return reinterpret_cast< pointer >( p );
100 /// Ask allocator to release memory at pointer with size bytes.
101 void deallocate( pointer p, size_type size )
103 AllocT::Instance().Deallocate( p, size * sizeof( Type ) );
106 /// Calculate max # of elements allocator can handle.
107 size_type max_size( void ) const throw()
109 // A good optimizer will see these calculations always produce the same
110 // value and optimize this function away completely.
111 const size_type max_bytes = size_type( -1 );
112 const size_type bytes = max_bytes / sizeof( Type );
116 /// Construct an element at the pointer.
117 void construct( pointer p, const Type &value )
119 // A call to global placement new forces a call to copy constructor.
120 ::new( p ) Type( value );
123 /// Destruct the object at pointer.
124 void destroy( pointer p )
126 // If the Type has no destructor, then some compilers complain about
127 // an unreferenced parameter, so use the void cast trick to prevent
128 // spurious warnings.
135 //-----------------------------------------------------------------------------
137 /** All equality operators return true since LokiAllocator is basically a
138 monostate design pattern, so all instances of it are identical.
140 template < typename Type >
141 inline bool operator == ( const LokiAllocator< Type > &, const LokiAllocator< Type > & )
146 /** All inequality operators return false since LokiAllocator is basically a
147 monostate design pattern, so all instances of it are identical.
149 template < typename Type >
150 inline bool operator != ( const LokiAllocator< Type > & , const LokiAllocator< Type > & )
155 //-----------------------------------------------------------------------------
159 #endif // LOKI_ALLOCATOR_INCLUDED