MySQL  8.0.18
Source Code Documentation
memroot_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 MEMROOT_ALLOCATOR_INCLUDED
24 #define MEMROOT_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  Memroot_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, Memroot_allocator<int> > v((Memroot_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 Memroot_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 Memroot_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 
86 
87  explicit Memroot_allocator() : m_memroot(nullptr) {}
88 
89  template <class U>
91  : m_memroot(other.memroot()) {}
92 
93  template <class U>
95  const Memroot_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, const_pointer hint MY_ATTRIBUTE((unused)) = 0) {
100  if (n == 0) return NULL;
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 == NULL) throw std::bad_alloc();
105  return p;
106  }
107 
109 
110  template <class U, class... Args>
111  void construct(U *p, Args &&... args) {
112  DBUG_ASSERT(p != NULL);
113  try {
114  ::new ((void *)p) U(std::forward<Args>(args)...);
115  } catch (...) {
116  DBUG_ASSERT(false); // Constructor should not throw an exception.
117  }
118  }
119 
120  void destroy(pointer p) {
121  DBUG_ASSERT(p != NULL);
122  try {
123  p->~T();
124  } catch (...) {
125  DBUG_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 Memroot_allocator<T> &a2) {
144  return a1.memroot() == a2.memroot();
145 }
146 
147 template <class T>
149  const Memroot_allocator<T> &a2) {
150  return a1.memroot() != a2.memroot();
151 }
152 
153 #endif // MEMROOT_ALLOCATOR_INCLUDED
#define U
Definition: ctype-tis620.cc:72
Memroot_allocator< U > other
Definition: memroot_allocator.h:135
ptrdiff_t difference_type
Definition: memroot_allocator.h:74
T * pointer
Definition: memroot_allocator.h:76
T value_type
Definition: memroot_allocator.h:72
const_pointer address(const_reference r) const
Definition: memroot_allocator.h:83
pointer address(reference r) const
Definition: memroot_allocator.h:82
MEM_ROOT * m_memroot
Definition: memroot_allocator.h:69
size_t size_type
Definition: memroot_allocator.h:73
void destroy(pointer p)
Definition: memroot_allocator.h:120
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
void construct(U *p, Args &&... args)
Definition: memroot_allocator.h:111
Memroot_allocator()
Definition: memroot_allocator.h:87
int n
Definition: xcom_base.c:425
T & reference
Definition: memroot_allocator.h:79
size_type max_size() const
Definition: memroot_allocator.h:129
Memroot_allocator(MEM_ROOT *memroot)
Definition: memroot_allocator.h:85
Memroot_allocator(const Memroot_allocator< U > &other)
Definition: memroot_allocator.h:90
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
const T & const_reference
Definition: memroot_allocator.h:80
bool operator==(const Memroot_allocator< T > &a1, const Memroot_allocator< T > &a2)
Definition: memroot_allocator.h:142
bool operator!=(const Memroot_allocator< T > &a1, const Memroot_allocator< T > &a2)
Definition: memroot_allocator.h:148
void deallocate(pointer, size_type)
Definition: memroot_allocator.h:108
Definition: memroot_allocator.h:134
#define NULL
Definition: types.h:55
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:134
pointer allocate(size_type n, const_pointer hint=0)
Definition: memroot_allocator.h:99
const char * p
Definition: ctype-mb.cc:1232
Memroot_allocator & operator=(const Memroot_allocator< U > &other)
Definition: memroot_allocator.h:94
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
const T * const_pointer
Definition: memroot_allocator.h:77
Definition: dtoa.cc:603
MEM_ROOT * memroot() const
Definition: memroot_allocator.h:138
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
Memroot_allocator is a C++ STL memory allocator based on MEM_ROOT.
Definition: equi_height.h:92