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