]> git.cworth.org Git - vogl/blob - src/extlib/loki/include/loki/Allocator.h
Initial vogl checkin
[vogl] / src / extlib / loki / include / loki / Allocator.h
1 ////////////////////////////////////////////////////////////////////////////////
2 // The Loki Library
3 // Copyright (c) 2008 by Rich Sposato
4 //
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 ////////////////////////////////////////////////////////////////////////////////
13
14 #ifndef LOKI_ALLOCATOR_HPP_INCLUDED
15 #define LOKI_ALLOCATOR_HPP_INCLUDED
16
17 // $Id: Allocator.h 896 2008-08-08 22:20:05Z syntheticpp $
18
19 // Requires project to be compiled with loki/src/SmallObj.cpp and loki/src/Singleton.cpp
20
21 #include <loki/SmallObj.h>
22
23
24 namespace Loki
25 {
26
27
28 //-----------------------------------------------------------------------------
29
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?).
35  */
36 template
37 <
38 typename Type,
39          typename AllocT = Loki::AllocatorSingleton<>
40          >
41 class LokiAllocator
42 {
43 public:
44
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;
52
53         /// Default constructor does nothing.
54         inline LokiAllocator( void ) throw() { }
55
56         /// Copy constructor does nothing.
57         inline LokiAllocator( const LokiAllocator & ) throw() { }
58
59         /// Type converting allocator constructor does nothing.
60         template < typename Type1 >
61         inline LokiAllocator( const LokiAllocator< Type1 > & ) throw() { }
62
63         /// Destructor does nothing.
64         inline ~LokiAllocator() throw() { }
65
66         /// Convert an allocator<Type> to an allocator <Type1>.
67         template < typename Type1 >
68         struct rebind
69         {
70                 typedef LokiAllocator< Type1 > other;
71         };
72
73         /// Return address of reference to mutable element.
74         pointer address( reference elem ) const
75         {
76                 return &elem;
77         }
78
79         /// Return address of reference to const element.
80         const_pointer address( const_reference elem ) const
81         {
82                 return &elem;
83         }
84
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.
92          */
93         pointer allocate( size_type count, const void *hint = 0 )
94         {
95                 (void)hint;  // Ignore the hint.
96                 void *p = AllocT::Instance().Allocate( count * sizeof( Type ), true );
97                 return reinterpret_cast< pointer >( p );
98         }
99
100         /// Ask allocator to release memory at pointer with size bytes.
101         void deallocate( pointer p, size_type size )
102         {
103                 AllocT::Instance().Deallocate( p, size * sizeof( Type ) );
104         }
105
106         /// Calculate max # of elements allocator can handle.
107         size_type max_size( void ) const throw()
108         {
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 );
113                 return bytes;
114         }
115
116         /// Construct an element at the pointer.
117         void construct( pointer p, const Type &value )
118         {
119                 // A call to global placement new forces a call to copy constructor.
120                 ::new( p ) Type( value );
121         }
122
123         /// Destruct the object at pointer.
124         void destroy( pointer p )
125         {
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.
129                 (void)p;
130                 p->~Type();
131         }
132
133 };
134
135 //-----------------------------------------------------------------------------
136
137 /** All equality operators return true since LokiAllocator is basically a
138  monostate design pattern, so all instances of it are identical.
139  */
140 template < typename Type >
141 inline bool operator == ( const LokiAllocator< Type > &, const LokiAllocator< Type > & )
142 {
143         return true;
144 }
145
146 /** All inequality operators return false since LokiAllocator is basically a
147  monostate design pattern, so all instances of it are identical.
148  */
149 template < typename Type >
150 inline bool operator != ( const LokiAllocator< Type > & , const LokiAllocator< Type > & )
151 {
152         return false;
153 }
154
155 //-----------------------------------------------------------------------------
156
157 } // namespace Loki
158
159 #endif // LOKI_ALLOCATOR_INCLUDED