MySQL 8.2.0
Source Code Documentation
stateless_allocator.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2023, Oracle and/or its affiliates.
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 <assert.h>
27#include <stddef.h>
28#include <limits>
29#include <new>
30#include <utility> // std::forward
31
32#include "my_compiler.h"
33
34/**
35 Functor struct which invokes my_free. Declared here as it is used as the
36 default 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
90template <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
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 [[maybe_unused]] = nullptr) {
119 if (n == 0) return nullptr;
120 if (n > max_size()) throw std::bad_alloc();
121
122 pointer p = static_cast<pointer>(ALLOC_FUN()(n * sizeof(T)));
123 if (p == nullptr) 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 assert(p != nullptr);
132 try {
133 ::new ((void *)p) U(std::forward<Args>(args)...);
134 } catch (...) {
135 assert(false); // Constructor should not throw an exception.
136 }
137 }
138
140 assert(p != nullptr);
141 try {
142 p->~T();
143 } catch (...) {
144 assert(false); // Destructor should not throw an exception
145 }
146 }
147
149 return std::numeric_limits<size_t>::max() / sizeof(T);
150 }
151
152 template <class U>
153 struct rebind {
155 };
156};
157
158template <class T, class ALLOC_FUN, class DEALLOC_FUN>
161 return true;
162}
163
164template <class T, class ALLOC_FUN, class DEALLOC_FUN>
167 return false;
168}
169
170#endif // STATELESS_ALLOCATOR_INCLUDED
Stateless_allocator is a C++ STL memory allocator skeleton based on Malloc_allocator,...
Definition: stateless_allocator.h:91
ptrdiff_t difference_type
Definition: stateless_allocator.h:95
T * pointer
Definition: stateless_allocator.h:97
Stateless_allocator(const Stateless_allocator_type< U > &)
Definition: stateless_allocator.h:113
size_t size_type
Definition: stateless_allocator.h:94
T value_type
Definition: stateless_allocator.h:93
void construct(U *p, Args &&... args)
Definition: stateless_allocator.h:130
pointer address(reference r) const
Definition: stateless_allocator.h:109
const_pointer address(const_reference r) const
Definition: stateless_allocator.h:110
void deallocate(pointer p, size_type n)
Definition: stateless_allocator.h:127
Stateless_allocator & operator=(const Stateless_allocator_type< U > &)
Definition: stateless_allocator.h:116
const T & const_reference
Definition: stateless_allocator.h:101
const T * const_pointer
Definition: stateless_allocator.h:98
void destroy(pointer p)
Definition: stateless_allocator.h:139
Stateless_allocator()=default
pointer allocate(size_type n, const_pointer hint=nullptr)
Definition: stateless_allocator.h:118
T & reference
Definition: stateless_allocator.h:100
size_type max_size() const
Definition: stateless_allocator.h:148
const char * p
Definition: ctype-mb.cc:1234
#define U
Definition: ctype-tis620.cc:73
Header for compiler-dependent features.
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
bool operator==(const Stateless_allocator< T, ALLOC_FUN, DEALLOC_FUN > &, const Stateless_allocator< T, ALLOC_FUN, DEALLOC_FUN > &)
Definition: stateless_allocator.h:159
bool operator!=(const Stateless_allocator< T, ALLOC_FUN, DEALLOC_FUN > &, const Stateless_allocator< T, ALLOC_FUN, DEALLOC_FUN > &)
Definition: stateless_allocator.h:165
Functor struct which invokes my_free.
Definition: stateless_allocator.h:38
void operator()(void *p, size_t) const
Definition: stateless_allocator.cc:27
Definition: stateless_allocator.h:153
Stateless_allocator< U, ALLOC_FUN, DEALLOC_FUN > other
Definition: stateless_allocator.h:154
Definition: dtoa.cc:588
int n
Definition: xcom_base.cc:508