STXXL  1.4-dev
 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 
22 
23 template <class Type>
24 class new_alloc;
25 
26 template <typename Type, typename Rebind>
28 
29 template <typename Type>
30 struct new_alloc_rebind<Type, Type>{
32 };
33 
34 template <typename Type, typename Rebind>
35 struct new_alloc_rebind {
36  typedef std::allocator<Rebind> other;
37 };
38 
39 // designed for typed_block (to use with std::vector)
40 template <class Type>
41 class new_alloc
42 {
43 public:
44  // type definitions
45  typedef Type value_type;
46  typedef Type* pointer;
47  typedef const Type* const_pointer;
48  typedef Type& reference;
49  typedef const Type& const_reference;
50  typedef std::size_t size_type;
51  typedef std::ptrdiff_t difference_type;
52 
53  // rebind allocator to type Rebind, use new_alloc only if Rebind == Type
54  template <class Rebind>
55  struct rebind {
57  };
58 
59  // return address of values
60  pointer address(reference value) const
61  {
62  return &value;
63  }
65  {
66  return &value;
67  }
68 
69  new_alloc() throw () { }
70  new_alloc(const new_alloc&) throw () { }
71  template <class Rebind>
72  new_alloc(const new_alloc<Rebind>&) throw () { }
73  ~new_alloc() throw () { }
74 
75  template <class Rebind>
76  operator std::allocator<Rebind>()
77  {
78  static std::allocator<Rebind> helper_allocator;
79  return helper_allocator;
80  }
81 
82  // return maximum number of elements that can be allocated
83  size_type max_size() const throw ()
84  {
85  return std::numeric_limits<size_type>::max() / sizeof(Type);
86  }
87 
88  // allocate but don't initialize num elements of type Type
89  pointer allocate(size_type num, const void* = 0)
90  {
91  return static_cast<Type*>(Type::operator new (num * sizeof(Type)));
92  }
93 
94  // _GLIBCXX_RESOLVE_LIB_DEFECTS
95  // 402. wrong new expression in [some_] allocator::construct
96  // initialize elements of allocated storage p with value value
97  void construct(pointer p, const Type& value)
98  {
99  // initialize memory with placement new
100  ::new ((void*)p)Type(value);
101  }
102 
103 #ifdef __GXX_EXPERIMENTAL_CXX0X__
104  template <typename ... Args>
105  void construct(pointer p, Args&& ... args)
106  {
107  ::new ((void*)p)Type(std::forward<Args>(args) ...);
108  }
109 #endif
110 
111  // destroy elements of initialized storage p
112  void destroy(pointer p)
113  {
114  // destroy objects by calling their destructor
115  p->~Type();
116  }
117 
118  // deallocate storage p of deleted elements
119  void deallocate(pointer p, size_type /*num*/)
120  {
121  Type::operator delete (p);
122  }
123 };
124 
125 // return that all specializations of this allocator are interchangeable
126 template <class Type1, class Type2>
127 inline bool operator == (const new_alloc<Type1>&,
128  const new_alloc<Type2>&) throw ()
129 {
130  return true;
131 }
132 
133 template <class Type1, class Type2>
134 inline bool operator != (const new_alloc<Type1>&,
135  const new_alloc<Type2>&) throw ()
136 {
137  return false;
138 }
139 
141 
142 #endif // !STXXL_COMMON_NEW_ALLOC_HEADER
143 // vim: et:ts=4:sw=4
Type & reference
Definition: new_alloc.h:48
new_alloc(const new_alloc< Rebind > &)
Definition: new_alloc.h:72
new_alloc(const new_alloc &)
Definition: new_alloc.h:70
const Type & const_reference
Definition: new_alloc.h:49
size_type max_size() const
Definition: new_alloc.h:83
void construct(pointer p, const Type &value)
Definition: new_alloc.h:97
pointer address(reference value) const
Definition: new_alloc.h:60
std::size_t size_type
Definition: new_alloc.h:50
new_alloc_rebind< Type, Rebind >::other other
Definition: new_alloc.h:56
const_pointer address(const_reference value) const
Definition: new_alloc.h:64
bool operator!=(const uint_pair &b) const
inequality checking operator
Definition: uint_types.h:198
std::allocator< Rebind > other
Definition: new_alloc.h:36
const Type * const_pointer
Definition: new_alloc.h:47
void destroy(pointer p)
Definition: new_alloc.h:112
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
Type * pointer
Definition: new_alloc.h:46
std::ptrdiff_t difference_type
Definition: new_alloc.h:51
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:241
pointer allocate(size_type num, const void *=0)
Definition: new_alloc.h:89
void deallocate(pointer p, size_type)
Definition: new_alloc.h:119
bool operator==(const uint_pair &b) const
equality checking operator
Definition: uint_types.h:192
#define STXXL_END_NAMESPACE
Definition: namespace.h:17