MySQL  8.0.21
Source Code Documentation
mem_root_allocator.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, 2019, 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 MEM_ROOT_ALLOCATOR_INCLUDED
24 #define MEM_ROOT_ALLOCATOR_INCLUDED
25 
26 #include <limits>
27 #include <new>
28 #include <utility> // std::forward
29 
30 #include "my_alloc.h"
31 #include "my_dbug.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 init_sql_alloc()
37  and free_root() on the supplied MEM_ROOT is the responsibility of
38  the caller. Do *not* call free_root() until the destructor of any
39  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>
67 class Mem_root_allocator {
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 
85  explicit Mem_root_allocator(MEM_ROOT *memroot) : m_memroot(memroot) {}
86 
87  explicit Mem_root_allocator() : m_memroot(nullptr) {}
88 
89  template <class U>
91  : m_memroot(other.memroot()) {}
92 
93  template <class U>
95  const Mem_root_allocator<U> &other MY_ATTRIBUTE((unused))) {
96  DBUG_ASSERT(m_memroot == other.memroot()); // Don't swap memroot.
97  }
98 
99  pointer allocate(size_type n,
100  const_pointer hint MY_ATTRIBUTE((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 
109  void deallocate(pointer, size_type) {}
110 
111  template <class U, class... Args>
112  void construct(U *p, Args &&... args) {
113  DBUG_ASSERT(p != nullptr);
114  try {
115  ::new ((void *)p) U(std::forward<Args>(args)...);
116  } catch (...) {
117  DBUG_ASSERT(false); // Constructor should not throw an exception.
118  }
119  }
120 
121  void destroy(pointer p) {
122  DBUG_ASSERT(p != nullptr);
123  try {
124  p->~T();
125  } catch (...) {
126  DBUG_ASSERT(false); // Destructor should not throw an exception
127  }
128  }
129 
130  size_type max_size() const {
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 
142 template <class T>
144  const Mem_root_allocator<T> &a2) {
145  return a1.memroot() == a2.memroot();
146 }
147 
148 template <class T>
150  const Mem_root_allocator<T> &a2) {
151  return a1.memroot() != a2.memroot();
152 }
153 
154 #endif // MEM_ROOT_ALLOCATOR_INCLUDED
#define U
Definition: ctype-tis620.cc:74
Mem_root_allocator< U > other
Definition: mem_root_allocator.h:136
Mem_root_allocator is a C++ STL memory allocator based on MEM_ROOT.
Definition: equi_height.h:92
T * pointer
Definition: mem_root_allocator.h:76
pointer allocate(size_type n, const_pointer hint=nullptr)
Definition: mem_root_allocator.h:99
MEM_ROOT * memroot() const
Definition: mem_root_allocator.h:139
const T & const_reference
Definition: mem_root_allocator.h:80
size_t size_type
Definition: mem_root_allocator.h:73
Mem_root_allocator & operator=(const Mem_root_allocator< U > &other)
Definition: mem_root_allocator.h:94
bool operator!=(const Mem_root_allocator< T > &a1, const Mem_root_allocator< T > &a2)
Definition: mem_root_allocator.h:149
size_type max_size() const
Definition: mem_root_allocator.h:130
const T * const_pointer
Definition: mem_root_allocator.h:77
#define DBUG_ASSERT(A)
Definition: my_dbug.h:199
MEM_ROOT * m_memroot
Definition: mem_root_allocator.h:69
Mem_root_allocator()
Definition: mem_root_allocator.h:87
ptrdiff_t difference_type
Definition: mem_root_allocator.h:74
const_pointer address(const_reference r) const
Definition: mem_root_allocator.h:83
void deallocate(pointer, size_type)
Definition: mem_root_allocator.h:109
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
Mem_root_allocator(MEM_ROOT *memroot)
Definition: mem_root_allocator.h:85
void construct(U *p, Args &&... args)
Definition: mem_root_allocator.h:112
int n
Definition: xcom_base.cc:442
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:134
const char * p
Definition: ctype-mb.cc:1235
void destroy(pointer p)
Definition: mem_root_allocator.h:121
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
pointer address(reference r) const
Definition: mem_root_allocator.h:82
T value_type
Definition: mem_root_allocator.h:72
T & reference
Definition: mem_root_allocator.h:79
Definition: dtoa.cc:601
bool operator==(const Mem_root_allocator< T > &a1, const Mem_root_allocator< T > &a2)
Definition: mem_root_allocator.h:143
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
Mem_root_allocator(const Mem_root_allocator< U > &other)
Definition: mem_root_allocator.h:90
Dialog Client Authentication nullptr
Definition: dialog.cc:353
Definition: mem_root_allocator.h:135