STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
new_alloc.h
Go to the documentation of this file.
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_COMMON_NEW_ALLOC_HEADER
15 #define STXXL_COMMON_NEW_ALLOC_HEADER
16 
17 #include <memory>
18 #include <limits>
19 #include <stxxl/bits/namespace.h>
20 
21 
23 
24 template <class T>
25 class new_alloc;
26 
27 template <typename T, typename U>
29 
30 template <typename T>
31 struct new_alloc_rebind<T, T>{
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 {
45 public:
46  // type definitions
47  typedef T value_type;
48  typedef T* pointer;
49  typedef const T* const_pointer;
50  typedef T& reference;
51  typedef const T& const_reference;
52  typedef std::size_t size_type;
53  typedef std::ptrdiff_t difference_type;
54 
55  // rebind allocator to type U, use new_alloc only if U == T
56  template <class U>
57  struct rebind {
59  };
60 
61  // return address of values
62  pointer address(reference value) const
63  {
64  return &value;
65  }
67  {
68  return &value;
69  }
70 
71  new_alloc() throw () { }
72  new_alloc(const new_alloc&) throw () { }
73  template <class U>
74  new_alloc(const new_alloc<U>&) throw () { }
75  ~new_alloc() throw () { }
76 
77  template <class U>
78  operator std::allocator<U>()
79  {
80  static std::allocator<U> helper_allocator;
81  return helper_allocator;
82  }
83 
84  // return maximum number of elements that can be allocated
85  size_type max_size() const throw ()
86  {
87  return std::numeric_limits<size_type>::max() / sizeof(T);
88  }
89 
90  // allocate but don't initialize num elements of type T
91  pointer allocate(size_type num, const void* = 0)
92  {
93  return static_cast<T*>(T::operator new (num * sizeof(T)));
94  }
95 
96  // _GLIBCXX_RESOLVE_LIB_DEFECTS
97  // 402. wrong new expression in [some_] allocator::construct
98  // initialize elements of allocated storage p with value value
99  void construct(pointer p, const T& value)
100  {
101  // initialize memory with placement new
102  ::new ((void*)p)T(value);
103  }
104 
105 #ifdef __GXX_EXPERIMENTAL_CXX0X__
106  template <typename ... Args>
107  void construct(pointer p, Args&& ... args)
108  {
109  ::new ((void*)p)T(std::forward<Args>(args) ...);
110  }
111 #endif
112 
113  // destroy elements of initialized storage p
114  void destroy(pointer p)
115  {
116  // destroy objects by calling their destructor
117  p->~T();
118  }
119 
120  // deallocate storage p of deleted elements
121  void deallocate(pointer p, size_type /*num*/)
122  {
123  T::operator delete (p);
124  }
125 };
126 
127 // return that all specializations of this allocator are interchangeable
128 template <class T1, class T2>
129 inline bool operator == (const new_alloc<T1>&,
130  const new_alloc<T2>&) throw ()
131 {
132  return true;
133 }
134 
135 template <class T1, class T2>
136 inline bool operator != (const new_alloc<T1>&,
137  const new_alloc<T2>&) throw ()
138 {
139  return false;
140 }
141 
143 
144 #endif // !STXXL_COMMON_NEW_ALLOC_HEADER
145 // vim: et:ts=4:sw=4
new_alloc_rebind< T, U >::other other
Definition: new_alloc.h:58
void destroy(pointer p)
Definition: new_alloc.h:114
size_type max_size() const
Definition: new_alloc.h:85
void construct(pointer p, const T &value)
Definition: new_alloc.h:99
pointer address(reference value) const
Definition: new_alloc.h:62
const T & const_reference
Definition: new_alloc.h:51
void deallocate(pointer p, size_type)
Definition: new_alloc.h:121
const_pointer address(const_reference value) const
Definition: new_alloc.h:66
bool operator!=(const uint_pair &b) const
inequality checking operator
Definition: uint_types.h:201
std::ptrdiff_t difference_type
Definition: new_alloc.h:53
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
new_alloc(const new_alloc< U > &)
Definition: new_alloc.h:74
pointer allocate(size_type num, const void *=0)
Definition: new_alloc.h:91
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:244
new_alloc(const new_alloc &)
Definition: new_alloc.h:72
std::allocator< U > other
Definition: new_alloc.h:37
const T * const_pointer
Definition: new_alloc.h:49
bool operator==(const uint_pair &b) const
equality checking operator
Definition: uint_types.h:195
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
std::size_t size_type
Definition: new_alloc.h:52