Zero  0.1.0
stl_pooled_alloc.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-kits -- Benchmark implementations for Shore-MT
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
34 #ifndef __STL_POOLED_ALLOC
35 #define __STL_POOLED_ALLOC
36 
37 #include "stl_pool.h"
38 #include <cassert>
39 #include <vector>
40 #include <list>
41 #include <set>
42 #include <map>
43 
45 template<typename T>
47 public:
48  typedef size_t size_type;
49  typedef ptrdiff_t difference_type;
50 
51  typedef T value_type;
52  typedef T* pointer;
53  typedef T const* const_pointer;
54  typedef T& reference;
55  typedef T const& const_reference;
56 
58  template<typename U>
59  struct rebind {
61  };
62 
64 
66  PooledAllocator(Pool* pool = 0) : m_pool(pool) {
67  assert(!m_pool || sizeof(T) <= m_pool->GetGranularity());
68  }
69 
71 
73  template<typename U>
75  assert(!m_pool || sizeof(T) <= m_pool->GetGranularity());
76  }
77 
79  size_type max_size() const {
80  return 0xffffffff;
81  }
82 
84 
86  pointer allocate(size_type count, std::allocator<void>::const_pointer /*hint*/ = 0) const {
87  if (count == 1 && m_pool) {
88  return reinterpret_cast<T*>( m_pool->Allocate());
89  } else {
90  return reinterpret_cast<T*>( new char[count * sizeof(T)] );
91  }
92  }
93 
95 
99  void deallocate(pointer block, size_type count) const throw() {
100  if (!block) {
101  return;
102  }
103 
104  assert(block && "null pointer argument");
105  if (count == 1 && m_pool) {
106  m_pool->Deallocate(block);
107  } else {
108  delete[] reinterpret_cast<char*>( block );
109  }
110  }
111 
113 
115  void construct(pointer element, T const& arg) {
116  new(element) T(arg);
117  }
118 
120 
122  void destroy(pointer element) {
123  element->~T();
124  // element; // FIXME: genuinely bizarre 'unused parameter' bug with vs2003
125  }
126 
128  pointer address(reference element) const {
129  return &element;
130  }
131 
133  const_pointer address(const_reference element) const {
134  return &element;
135  }
136 
139 };
140 
142 template<>
143 class PooledAllocator<void> {
144 public:
145  typedef size_t size_type;
146  typedef ptrdiff_t difference_type;
147 
148  typedef void value_type;
149  typedef void* pointer;
150  typedef void const* const_pointer;
151 
153  template<typename U>
154  struct rebind {
156  };
157 
159 
162 
164  PooledAllocator(Pool* pool) : m_pool(pool) {}
165 
168 };
169 
171 template<typename T, typename U>
172 bool operator==(PooledAllocator<T> const& left, PooledAllocator<U> const& right) {
173  return left.m_pool == right.m_pool;
174 }
175 
177 template<typename T, typename U>
178 bool operator!=(PooledAllocator<T> const& left, PooledAllocator<U> const& right) {
179  return left.m_pool != right.m_pool;
180 }
181 
182 
183 
185 
186 
188 template<typename Key, typename Value, class Traits = std::less<Key>>
189 struct PooledMap {
190  typedef std::map<Key, Value, Traits, PooledAllocator<std::pair<Key, Value>>> Type;
191 };
192 
194 template<typename Key, typename Value, class Traits = std::less<Key>>
196  typedef std::multimap<Key, Value, Traits, PooledAllocator<std::pair<Key, Value>>> Type;
197 };
198 
200 template<typename Value>
201 struct PooledList {
202  typedef std::list<Value, PooledAllocator<Value>> Type;
203 };
204 
206 template<typename Value, typename Traits = std::less<Value>>
207 struct PooledSet {
208  typedef std::set<Value, Traits, PooledAllocator<Value>> Type;
209 };
210 
212 template<typename Value>
213 struct PooledVec {
214  typedef std::vector<Value, PooledAllocator<Value>> Type;
215 };
216 
217 #endif // __STL_POOLED_ALLOC
std::map< Key, Value, Traits, PooledAllocator< std::pair< Key, Value > > > Type
Definition: stl_pooled_alloc.h:190
Simple pool class.
Definition: stl_pool.h:40
PooledAllocator< U > other
Definition: stl_pooled_alloc.h:60
PooledAllocator()
Creates a pooled allocator with no pool.
Definition: stl_pooled_alloc.h:161
Pool * m_pool
The pool for this allocator.
Definition: stl_pooled_alloc.h:167
void const * const_pointer
Pointer to const void.
Definition: stl_pooled_alloc.h:150
PooledAllocator(Pool *pool)
Creates a pooled allocator to the given pool.
Definition: stl_pooled_alloc.h:164
bool operator==(PooledAllocator< T > const &left, PooledAllocator< U > const &right)
Returns true if objects allocated from one pool can be deallocated from the other.
Definition: stl_pooled_alloc.h:172
Template typedef std::map<..., PooledAllocator> to PooledMap<Key, Value>::Type.
Definition: stl_pooled_alloc.h:189
Template typedef std::vector<..., PooledAllocator> to PooledVec<Value>::Type.
Definition: stl_pooled_alloc.h:213
T & reference
Reference to T.
Definition: stl_pooled_alloc.h:54
std::set< Value, Traits, PooledAllocator< Value > > Type
Definition: stl_pooled_alloc.h:208
size_type max_size() const
The largest value that can meaningfully passed to allocate.
Definition: stl_pooled_alloc.h:79
A struct to construct an allocator for a different type.
Definition: stl_pooled_alloc.h:59
void * pointer
Pointer to void;.
Definition: stl_pooled_alloc.h:149
Template typedef std::multimap<..., PooledAllocator> to PooledMultiMap<Key, Value>::Type.
Definition: stl_pooled_alloc.h:195
ptrdiff_t difference_type
A type that can represent the difference between any two pointers in the allocation model...
Definition: stl_pooled_alloc.h:49
pointer allocate(size_type count, std::allocator< void >::const_pointer=0) const
Memory is allocated for count objects of type T but objects are not constructed.
Definition: stl_pooled_alloc.h:86
Template typedef std::set<..., PooledAllocator> to PooledSet<Value>::Type.
Definition: stl_pooled_alloc.h:207
void deallocate(pointer block, size_type count) const
Deallocates memory allocated by allocate.
Definition: stl_pooled_alloc.h:99
A standards-compliant pooled allocator.
Definition: stl_pooled_alloc.h:46
T * pointer
Pointer to T;.
Definition: stl_pooled_alloc.h:52
PooledAllocator(PooledAllocator< U > const &arg)
Creates a pooled allocator to the argument&#39;s pool.
Definition: stl_pooled_alloc.h:74
PooledAllocator(Pool *pool=0)
Creates a pooled allocator to the given pool.
Definition: stl_pooled_alloc.h:66
const_pointer address(const_reference element) const
Returns the address of the given reference.
Definition: stl_pooled_alloc.h:133
PooledAllocator< U > other
Definition: stl_pooled_alloc.h:155
void construct(pointer element, T const &arg)
Constructs an element of T at the given pointer.
Definition: stl_pooled_alloc.h:115
size_t GetGranularity() const
Gets the pool granularity.
Definition: stl_pool.h:49
size_t size_type
A type that can represent the size of the largest object in the allocation model. ...
Definition: stl_pooled_alloc.h:145
size_t size_type
A type that can represent the size of the largest object in the allocation model. ...
Definition: stl_pooled_alloc.h:48
pointer address(reference element) const
Returns the address of the given reference.
Definition: stl_pooled_alloc.h:128
T const & const_reference
Reference to const T.
Definition: stl_pooled_alloc.h:55
ptrdiff_t difference_type
A type that can represent the difference between any two pointers in the allocation model...
Definition: stl_pooled_alloc.h:146
void value_type
Identical to void.
Definition: stl_pooled_alloc.h:148
void destroy(pointer element)
Destroys an element of T at the given pointer.
Definition: stl_pooled_alloc.h:122
Template typedef std::list<..., PooledAllocator> to PooledList<Value>::Type.
Definition: stl_pooled_alloc.h:201
void * Allocate()
Allocates memory from the pool without construction.
Definition: stl_pool.cpp:64
std::vector< Value, PooledAllocator< Value > > Type
Definition: stl_pooled_alloc.h:214
T const * const_pointer
Pointer to const T.
Definition: stl_pooled_alloc.h:53
bool operator!=(PooledAllocator< T > const &left, PooledAllocator< U > const &right)
Returns true if objects allocated from one pool cannot be deallocated from the other.
Definition: stl_pooled_alloc.h:178
Pool * m_pool
The pool for this allocator.
Definition: stl_pooled_alloc.h:138
T value_type
Identical to T.
Definition: stl_pooled_alloc.h:51
#define T
Definition: w_okvl_inl.h:45
std::list< Value, PooledAllocator< Value > > Type
Definition: stl_pooled_alloc.h:202
void Deallocate(void *block)
Deallocates memory from the pool without destruction.
Definition: stl_pool.cpp:74
std::multimap< Key, Value, Traits, PooledAllocator< std::pair< Key, Value > > > Type
Definition: stl_pooled_alloc.h:196