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