Thrill  0.1
new_alloc.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/common/new_alloc.hpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
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 FOXXLL_COMMON_NEW_ALLOC_HEADER
15 #define FOXXLL_COMMON_NEW_ALLOC_HEADER
16 
17 #include <limits>
18 #include <memory>
19 
20 namespace foxxll {
21 
22 template <class Type>
23 class new_alloc;
24 
25 template <typename Type, typename Rebind>
27 
28 template <typename Type>
31 };
32 
33 template <typename Type, typename Rebind>
34 struct new_alloc_rebind {
35  using other = std::allocator<Rebind>;
36 };
37 
38 // designed for typed_block (to use with std::vector)
39 template <class Type>
40 class new_alloc
41 {
42 public:
43  // type definitions
44  using value_type = Type;
45  using pointer = Type *;
46  using const_pointer = const Type *;
47  using reference = Type &;
48  using const_reference = const Type &;
49  using size_type = std::size_t;
50  using difference_type = std::ptrdiff_t;
51 
52  // rebind allocator to type Rebind, use new_alloc only if Rebind == Type
53  template <class Rebind>
54  struct rebind {
56  };
57 
58  // return address of values
60  {
61  return &value;
62  }
64  {
65  return &value;
66  }
67 
68  new_alloc() noexcept { }
69  new_alloc(const new_alloc&) noexcept { }
70  template <class Rebind>
71  new_alloc(const new_alloc<Rebind>&) noexcept { }
72  ~new_alloc() noexcept { }
73 
74  template <class Rebind>
75  operator std::allocator<Rebind>()
76  {
77  static std::allocator<Rebind> helper_allocator;
78  return helper_allocator;
79  }
80 
81  // return maximum number of elements that can be allocated
82  size_type max_size() const noexcept
83  {
84  return std::numeric_limits<size_type>::max() / sizeof(Type);
85  }
86 
87  // allocate but don't initialize num elements of type Type
88  pointer allocate(size_type num, const void* = 0)
89  {
90  return static_cast<Type*>(Type::operator new (num * sizeof(Type)));
91  }
92 
93  // _GLIBCXX_RESOLVE_LIB_DEFECTS
94  // 402. wrong new expression in [some_] allocator::construct
95  // initialize elements of allocated storage p with value value
96  void construct(pointer p, const Type& value)
97  {
98  // initialize memory with placement new
99  ::new (static_cast<void*>(p))Type(value);
100  }
101 
102  template <typename... Args>
103  void construct(pointer p, Args&& ... args)
104  {
105  ::new (static_cast<void*>(p))Type(std::forward<Args>(args) ...);
106  }
107 
108  // destroy elements of initialized storage p
109  void destroy(pointer p)
110  {
111  // destroy objects by calling their destructor
112  p->~Type();
113  }
114 
115  // deallocate storage p of deleted elements
116  void deallocate(pointer p, size_type /*num*/)
117  {
118  Type::operator delete (p);
119  }
120 };
121 
122 // return that all specializations of this allocator are interchangeable
123 template <class Type1, class Type2>
124 inline bool operator == (
125  const new_alloc<Type1>&, const new_alloc<Type2>&) noexcept
126 {
127  return true;
128 }
129 
130 template <class Type1, class Type2>
131 inline bool operator != (
132  const new_alloc<Type1>&, const new_alloc<Type2>&) noexcept
133 {
134  return false;
135 }
136 
137 } // namespace foxxll
138 
139 #endif // !FOXXLL_COMMON_NEW_ALLOC_HEADER
140 
141 /**************************************************************************/
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
bool operator!=(const new_alloc< Type1 > &, const new_alloc< Type2 > &) noexcept
Definition: new_alloc.hpp:131
const Type * const_pointer
Definition: new_alloc.hpp:46
Type
VFS object type.
Definition: file_io.hpp:52
pointer allocate(size_type num, const void *=0)
Definition: new_alloc.hpp:88
typename new_alloc_rebind< Type, Rebind >::other other
Definition: new_alloc.hpp:55
void construct(pointer p, const Type &value)
Definition: new_alloc.hpp:96
size_type max_size() const noexcept
Definition: new_alloc.hpp:82
int value
Definition: gen_data.py:41
void deallocate(pointer p, size_type)
Definition: new_alloc.hpp:116
FOXXLL library namespace
new_alloc() noexcept
Definition: new_alloc.hpp:68
std::size_t size_type
Definition: new_alloc.hpp:49
new_alloc(const new_alloc< Rebind > &) noexcept
Definition: new_alloc.hpp:71
std::allocator< Rebind > other
Definition: new_alloc.hpp:35
const Type & const_reference
Definition: new_alloc.hpp:48
void construct(pointer p, Args &&... args)
Definition: new_alloc.hpp:103
pointer address(reference value) const
Definition: new_alloc.hpp:59
std::ptrdiff_t difference_type
Definition: new_alloc.hpp:50
~new_alloc() noexcept
Definition: new_alloc.hpp:72
void destroy(pointer p)
Definition: new_alloc.hpp:109
bool operator==(const new_alloc< Type1 > &, const new_alloc< Type2 > &) noexcept
Definition: new_alloc.hpp:124
const_pointer address(const_reference value) const
Definition: new_alloc.hpp:63
new_alloc(const new_alloc &) noexcept
Definition: new_alloc.hpp:69