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.
69   MEM_ROOT *m_memroot;
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 
address(reference r)82   pointer address(reference r) const { return &r; }
address(const_reference r)83   const_pointer address(const_reference r) const { return &r; }
84 
Mem_root_allocator(MEM_ROOT * memroot)85   explicit Mem_root_allocator(MEM_ROOT *memroot) : m_memroot(memroot) {}
86 
Mem_root_allocator()87   explicit Mem_root_allocator() : m_memroot(nullptr) {}
88 
89   template <class U>
Mem_root_allocator(const Mem_root_allocator<U> & other)90   Mem_root_allocator(const Mem_root_allocator<U> &other)
91       : m_memroot(other.memroot()) {}
92 
93   template <class U>
94   Mem_root_allocator &operator=(
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 
deallocate(pointer,size_type)109   void deallocate(pointer, size_type) {}
110 
111   template <class U, class... Args>
construct(U * p,Args &&...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 
destroy(pointer p)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 
max_size()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 {
136     typedef Mem_root_allocator<U> other;
137   };
138 
memroot()139   MEM_ROOT *memroot() const { return m_memroot; }
140 };
141 
142 template <class T>
143 bool operator==(const Mem_root_allocator<T> &a1,
144                 const Mem_root_allocator<T> &a2) {
145   return a1.memroot() == a2.memroot();
146 }
147 
148 template <class T>
149 bool operator!=(const Mem_root_allocator<T> &a1,
150                 const Mem_root_allocator<T> &a2) {
151   return a1.memroot() != a2.memroot();
152 }
153 
154 #endif  // MEM_ROOT_ALLOCATOR_INCLUDED
155