Stxxl  1.3.2
new_alloc.h
1 /***************************************************************************
2  * include/stxxl/bits/common/new_alloc.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2006 Roman Dementiev <[email protected]>
7  * Copyright (C) 2007, 2008, 2010 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_NEW_ALLOC_HEADER
15 #define STXXL_NEW_ALLOC_HEADER
16 
17 #include <memory>
18 #include <limits>
19 #include <stxxl/bits/namespace.h>
20 
21 
22 __STXXL_BEGIN_NAMESPACE
23 
24 template <class T>
25 class new_alloc;
26 
27 template <typename T, typename U>
28 struct new_alloc_rebind;
29 
30 template <typename T>
31 struct new_alloc_rebind<T, T>{
32  typedef new_alloc<T> other;
33 };
34 
35 template <typename T, typename U>
36 struct new_alloc_rebind {
37  typedef std::allocator<U> other;
38 };
39 
40 
41 // designed for typed_block (to use with std::vector)
42 template <class T>
43 class new_alloc {
44 public:
45  // type definitions
46  typedef T value_type;
47  typedef T * pointer;
48  typedef const T * const_pointer;
49  typedef T & reference;
50  typedef const T & const_reference;
51  typedef std::size_t size_type;
52  typedef std::ptrdiff_t difference_type;
53 
54  // rebind allocator to type U, use new_alloc only if U == T
55  template <class U>
56  struct rebind {
57  typedef typename new_alloc_rebind<T, U>::other other;
58  };
59 
60  // return address of values
61  pointer address(reference value) const
62  {
63  return &value;
64  }
65  const_pointer address(const_reference value) const
66  {
67  return &value;
68  }
69 
70  new_alloc() throw () { }
71  new_alloc(const new_alloc &) throw () { }
72  template <class U>
73  new_alloc(const new_alloc<U> &) throw () { }
74  ~new_alloc() throw () { }
75 
76  template <class U>
77  operator std::allocator<U>()
78  {
79  static std::allocator<U> helper_allocator;
80  return helper_allocator;
81  }
82 
83  // return maximum number of elements that can be allocated
84  size_type max_size() const throw ()
85  {
86  return (std::numeric_limits<size_type>::max)() / sizeof(T);
87  }
88 
89  // allocate but don't initialize num elements of type T
90  pointer allocate(size_type num, const void * = 0)
91  {
92  return static_cast<T *>(T::operator new (num * sizeof(T)));
93  }
94 
95  // _GLIBCXX_RESOLVE_LIB_DEFECTS
96  // 402. wrong new expression in [some_] allocator::construct
97  // initialize elements of allocated storage p with value value
98  void construct(pointer p, const T & value)
99  {
100  // initialize memory with placement new
101  ::new ((void *)p)T(value);
102  }
103 
104 #ifdef __GXX_EXPERIMENTAL_CXX0X__
105  template <typename ... Args>
106  void construct(pointer p, Args && ... args)
107  {
108  ::new ((void *)p)T(std::forward<Args>(args) ...);
109  }
110 #endif
111 
112  // destroy elements of initialized storage p
113  void destroy(pointer p)
114  {
115  // destroy objects by calling their destructor
116  p->~T();
117  }
118 
119  // deallocate storage p of deleted elements
120  void deallocate(pointer p, size_type /*num*/)
121  {
122  T::operator delete (p);
123  }
124 };
125 
126 // return that all specializations of this allocator are interchangeable
127 template <class T1, class T2>
128 inline bool operator == (const new_alloc<T1> &,
129  const new_alloc<T2> &) throw ()
130 {
131  return true;
132 }
133 
134 template <class T1, class T2>
135 inline bool operator != (const new_alloc<T1> &,
136  const new_alloc<T2> &) throw ()
137 {
138  return false;
139 }
140 
141 __STXXL_END_NAMESPACE
142 
143 #endif // !STXXL_NEW_ALLOC_HEADER
144 // vim: et:ts=4:sw=4