MySQL  8.0.19
Source Code Documentation
stateless_allocator.h
Go to the documentation of this file.
1 /* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef STATELESS_ALLOCATOR_INCLUDED
24 #define STATELESS_ALLOCATOR_INCLUDED
25 
26 #include <stddef.h>
27 #include <limits>
28 #include <new>
29 #include <utility> // std::forward
30 
31 #include "my_compiler.h"
32 #include "my_dbug.h"
33 
34 /**
35  Functor struct which invokes my_free. Declared here as it is used as the
36  defalt value for Stateless_allocator's DEALLOC_FUN template parameter.
37 */
39  void operator()(void *p, size_t) const;
40 };
41 
42 /**
43  Stateless_allocator is a C++ STL memory allocator skeleton based on
44  Malloc_allocator, which assumes that a global free function can be
45  used to allocate and deallocate memory, so that no state need to be
46  kept by the allocator object.
47 
48  The allocation and deallocation functions must be provided as
49  callable types (aka functors) which have no state and can be default
50  constructed.
51 
52  Example usage:
53 
54  @verbatim
55  struct My_psi_key_alloc
56  {
57  void* operator(size_t s)()
58  {
59  return my_malloc(My_psi_key, s, MYF(MY_WME | ME_FATALERROR));
60  }
61  };
62 
63  template <class T>
64  using My_psi_key_allocator =
65  Stateless_allocator<T, My_psi_key_alloc>;
66 
67  template < template<class T> class Allocator >
68  using default_string=
69  std::basic_string<char, std::char_traits<char>, Allocator<char> >;
70 
71 
72  typedef default_string<My_psi_key_allocator> My_psi_key_str;
73 
74  My_psi_key_str x("foobar");
75  @endverbatim
76 
77  Since a Stateless_allocator instance is always
78  default-constructible, it can also be used to create instances of
79  std::basic_string, even with compilers that have this libstd++ bug:
80  http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56437 "basic_string
81  assumes that allocators are default-constructible".
82 
83  @note allocate() throws std::bad_alloc() similarly to the default
84  STL memory allocator. This is necessary - STL functions which allocate
85  memory expect it. Otherwise these functions will try to use the memory,
86  leading to seg faults if memory allocation was not successful.
87 
88 */
89 
90 template <class T, class ALLOC_FUN, class DEALLOC_FUN = My_free_functor>
92  public:
93  typedef T value_type;
94  typedef size_t size_type;
95  typedef ptrdiff_t difference_type;
96 
97  typedef T *pointer;
98  typedef const T *const_pointer;
99 
100  typedef T &reference;
101  typedef const T &const_reference;
102 
103  template <class T_>
106 
107  Stateless_allocator() = default;
108 
109  pointer address(reference r) const { return &r; }
111 
112  template <class U>
114 
115  template <class U>
117 
118  pointer allocate(size_type n, const_pointer hint MY_ATTRIBUTE((unused)) = 0) {
119  if (n == 0) return NULL;
120  if (n > max_size()) throw std::bad_alloc();
121 
122  pointer p = static_cast<pointer>(ALLOC_FUN()(n * sizeof(T)));
123  if (p == NULL) throw std::bad_alloc();
124  return p;
125  }
126 
127  void deallocate(pointer p, size_type n) { DEALLOC_FUN()(p, n); }
128 
129  template <class U, class... Args>
130  void construct(U *p, Args &&... args) {
131  DBUG_ASSERT(p != NULL);
132  try {
133  ::new ((void *)p) U(std::forward<Args>(args)...);
134  } catch (...) {
135  DBUG_ASSERT(false); // Constructor should not throw an exception.
136  }
137  }
138 
139  void destroy(pointer p) {
140  DBUG_ASSERT(p != NULL);
141  try {
142  p->~T();
143  } catch (...) {
144  DBUG_ASSERT(false); // Destructor should not throw an exception
145  }
146  }
147 
148  size_type max_size() const {
149  return std::numeric_limits<size_t>::max() / sizeof(T);
150  }
151 
152  template <class U>
153  struct rebind {
155  };
156 };
157 
158 template <class T, class ALLOC_FUN, class DEALLOC_FUN>
161  return true;
162 }
163 
164 template <class T, class ALLOC_FUN, class DEALLOC_FUN>
167  return false;
168 }
169 
170 #endif // STATELESS_ALLOCATOR_INCLUDED
Stateless_allocator
Stateless_allocator is a C++ STL memory allocator skeleton based on Malloc_allocator,...
Definition: stateless_allocator.h:91
Stateless_allocator::allocate
pointer allocate(size_type n, const_pointer hint=0)
Definition: stateless_allocator.h:118
NULL
#define NULL
Definition: types.h:55
Stateless_allocator::pointer
T * pointer
Definition: stateless_allocator.h:97
Stateless_allocator::size_type
size_t size_type
Definition: stateless_allocator.h:94
my_compiler.h
Stateless_allocator::max_size
size_type max_size() const
Definition: stateless_allocator.h:148
my_dbug.h
operator==
bool operator==(const Stateless_allocator< T, ALLOC_FUN, DEALLOC_FUN > &, const Stateless_allocator< T, ALLOC_FUN, DEALLOC_FUN > &)
Definition: stateless_allocator.h:159
Stateless_allocator::address
const_pointer address(const_reference r) const
Definition: stateless_allocator.h:110
Stateless_allocator::rebind
Definition: stateless_allocator.h:153
U
#define U
Definition: ctype-tis620.cc:74
Stateless_allocator::reference
T & reference
Definition: stateless_allocator.h:100
U
Definition: dtoa.cc:603
r
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
Stateless_allocator::difference_type
ptrdiff_t difference_type
Definition: stateless_allocator.h:95
Stateless_allocator::deallocate
void deallocate(pointer p, size_type n)
Definition: stateless_allocator.h:127
operator!=
bool operator!=(const Stateless_allocator< T, ALLOC_FUN, DEALLOC_FUN > &, const Stateless_allocator< T, ALLOC_FUN, DEALLOC_FUN > &)
Definition: stateless_allocator.h:165
Stateless_allocator::value_type
T value_type
Definition: stateless_allocator.h:93
Stateless_allocator::destroy
void destroy(pointer p)
Definition: stateless_allocator.h:139
Stateless_allocator::const_pointer
const typedef T * const_pointer
Definition: stateless_allocator.h:98
Stateless_allocator::construct
void construct(U *p, Args &&... args)
Definition: stateless_allocator.h:130
Stateless_allocator::Stateless_allocator
Stateless_allocator()=default
DBUG_ASSERT
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
Stateless_allocator::const_reference
const typedef T & const_reference
Definition: stateless_allocator.h:101
Stateless_allocator::Stateless_allocator
Stateless_allocator(const Stateless_allocator_type< U > &)
Definition: stateless_allocator.h:113
My_free_functor::operator()
void operator()(void *p, size_t) const
Definition: stateless_allocator.cc:27
Stateless_allocator::operator=
Stateless_allocator & operator=(const Stateless_allocator_type< U > &)
Definition: stateless_allocator.h:116
n
int n
Definition: xcom_base.c:425
My_free_functor
Functor struct which invokes my_free.
Definition: stateless_allocator.h:38
p
const char * p
Definition: ctype-mb.cc:1233
Stateless_allocator::rebind::other
Stateless_allocator< U, ALLOC_FUN, DEALLOC_FUN > other
Definition: stateless_allocator.h:154
Stateless_allocator::address
pointer address(reference r) const
Definition: stateless_allocator.h:109