MySQL 8.4.3
Source Code Documentation
mem_root_allocator.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 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 MEM_ROOT_ALLOCATOR_INCLUDED
25#define MEM_ROOT_ALLOCATOR_INCLUDED
26
27#include <assert.h>
28#include <limits>
29#include <new>
30#include <utility> // std::forward
31
32#include "my_alloc.h"
33
34/**
35 Mem_root_allocator is a C++ STL memory allocator based on MEM_ROOT.
36
37 No deallocation is done by this allocator. Calling the constructor
38 and destructor on the supplied MEM_ROOT is the responsibility of
39 the caller. Do *not* call Clear() or ~MEM_ROOT until the destructor
40 of any objects using this allocator has completed. This includes iterators.
41
42 Example of use:
43 vector<int, Mem_root_allocator<int> > v((Mem_root_allocator<int>(&mem_root)));
44
45 @note allocate() throws std::bad_alloc() similarly to the default
46 STL memory allocator. This is necessary - STL functions which allocate
47 memory expect it. Otherwise these functions will try to use the memory,
48 leading to seg faults if memory allocation was not successful.
49
50 @note This allocator cannot be used for std::basic_string with RHEL 6/7
51 because of this bug:
52 https://bugzilla.redhat.com/show_bug.cgi?id=1546704
53 "Define _GLIBCXX_USE_CXX11_ABI gets ignored by gcc in devtoolset-7"
54
55 @note C++98 says that STL implementors can assume that allocator objects
56 of the same type always compare equal. This will only be the case for
57 two Mem_root_allocators that use the same MEM_ROOT. Care should be taken
58 when this is not the case. Especially:
59 - Using list::splice() on two lists with allocators using two different
60 MEM_ROOTs causes undefined behavior. Most implementations seem to give
61 runtime errors in such cases.
62 - swap() on two collections with allocators using two different MEM_ROOTs
63 is not well defined. At least some implementations also swap allocators,
64 but this should not be depended on.
65*/
66
67template <class T>
69 // This cannot be const if we want to be able to swap.
71
72 public:
73 typedef T value_type;
74 typedef size_t size_type;
75 typedef ptrdiff_t difference_type;
76
77 typedef T *pointer;
78 typedef const T *const_pointer;
79
80 typedef T &reference;
81 typedef const T &const_reference;
82
83 pointer address(reference r) const { return &r; }
85
87
89
90 template <class U>
92 : m_memroot(other.memroot()) {}
93
94 template <class U>
96 [[maybe_unused]]) {
97 assert(m_memroot == other.memroot()); // Don't swap memroot.
98 }
99
100 pointer allocate(size_type n, const_pointer hint [[maybe_unused]] = nullptr) {
101 if (n == 0) return nullptr;
102 if (n > max_size()) throw std::bad_alloc();
103
104 pointer p = static_cast<pointer>(m_memroot->Alloc(n * sizeof(T)));
105 if (p == nullptr) throw std::bad_alloc();
106 return p;
107 }
108
110
111 template <class U, class... Args>
112 void construct(U *p, Args &&...args) {
113 assert(p != nullptr);
114 try {
115 ::new ((void *)p) U(std::forward<Args>(args)...);
116 } catch (...) {
117 assert(false); // Constructor should not throw an exception.
118 }
119 }
120
122 assert(p != nullptr);
123 try {
124 p->~T();
125 } catch (...) {
126 assert(false); // Destructor should not throw an exception
127 }
128 }
129
131 return std::numeric_limits<size_t>::max() / sizeof(T);
132 }
133
134 template <class U>
135 struct rebind {
137 };
138
139 MEM_ROOT *memroot() const { return m_memroot; }
140};
141
142template <class T>
144 const Mem_root_allocator<T> &a2) {
145 return a1.memroot() == a2.memroot();
146}
147
148template <class T>
150 const Mem_root_allocator<T> &a2) {
151 return a1.memroot() != a2.memroot();
152}
153
154#endif // MEM_ROOT_ALLOCATOR_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Mem_root_allocator is a C++ STL memory allocator based on MEM_ROOT.
Definition: mem_root_allocator.h:68
const T * const_pointer
Definition: mem_root_allocator.h:78
MEM_ROOT * m_memroot
Definition: mem_root_allocator.h:70
Mem_root_allocator()
Definition: mem_root_allocator.h:88
Mem_root_allocator(MEM_ROOT *memroot)
Definition: mem_root_allocator.h:86
pointer allocate(size_type n, const_pointer hint=nullptr)
Definition: mem_root_allocator.h:100
size_t size_type
Definition: mem_root_allocator.h:74
pointer address(reference r) const
Definition: mem_root_allocator.h:83
const_pointer address(const_reference r) const
Definition: mem_root_allocator.h:84
T & reference
Definition: mem_root_allocator.h:80
size_type max_size() const
Definition: mem_root_allocator.h:130
Mem_root_allocator & operator=(const Mem_root_allocator< U > &other)
Definition: mem_root_allocator.h:95
void construct(U *p, Args &&...args)
Definition: mem_root_allocator.h:112
Mem_root_allocator(const Mem_root_allocator< U > &other)
Definition: mem_root_allocator.h:91
ptrdiff_t difference_type
Definition: mem_root_allocator.h:75
T value_type
Definition: mem_root_allocator.h:73
const T & const_reference
Definition: mem_root_allocator.h:81
void deallocate(pointer, size_type)
Definition: mem_root_allocator.h:109
void destroy(pointer p)
Definition: mem_root_allocator.h:121
T * pointer
Definition: mem_root_allocator.h:77
MEM_ROOT * memroot() const
Definition: mem_root_allocator.h:139
const char * p
Definition: ctype-mb.cc:1235
#define U
Definition: ctype-tis620.cc:74
bool operator==(const Mem_root_allocator< T > &a1, const Mem_root_allocator< T > &a2)
Definition: mem_root_allocator.h:143
bool operator!=(const Mem_root_allocator< T > &a1, const Mem_root_allocator< T > &a2)
Definition: mem_root_allocator.h:149
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:145
Definition: mem_root_allocator.h:135
Mem_root_allocator< U > other
Definition: mem_root_allocator.h:136
Definition: dtoa.cc:588
int n
Definition: xcom_base.cc:509