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 MEMROOT_ALLOCATOR_INCLUDED 24 #define MEMROOT_ALLOCATOR_INCLUDED 25 26 #include "sql_alloc.h" 27 28 #include <new> 29 #include <limits> 30 31 32 /** 33 Memroot_allocator is a C++ STL memory allocator based on MEM_ROOT. 34 35 No deallocation is done by this allocator. Calling init_sql_alloc() 36 and free_root() on the supplied MEM_ROOT is the responsibility of 37 the caller. Do *not* call free_root() until the destructor of any 38 objects using this allocator has completed. This includes iterators. 39 40 Example of use: 41 vector<int, Memroot_allocator<int> > v((Memroot_allocator<int>(&mem_root))); 42 43 @note allocate() throws std::bad_alloc() similarly to the default 44 STL memory allocator. This is necessary - STL functions which allocates 45 memory expects it. Otherwise these functions will try to use the memory, 46 leading to seg faults if memory allocation was not successful. 47 48 @note This allocator cannot be used for std::basic_string 49 because of this libstd++ bug: 50 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56437 51 "basic_string assumes that allocators are default-constructible" 52 53 @note C++98 says that STL implementors can assume that allocator objects 54 of the same type always compare equal. This will only be the case for 55 two Memroot_allocators that use the same MEM_ROOT. Care should be taken 56 when this is not the case. Especially: 57 - Using list::splice() on two lists with allocators using two different 58 MEM_ROOTs causes undefined behavior. Most implementations seem to give 59 runtime errors in such cases. 60 - swap() on two collections with allocators using two different MEM_ROOTs 61 is not well defined. At least some implementations also swap allocators, 62 but this should not be depended on. 63 */ 64 65 template <class T> class Memroot_allocator 66 { 67 // This cannot be const if we want to be able to swap. 68 MEM_ROOT *m_memroot; 69 70 public: 71 typedef T value_type; 72 typedef size_t size_type; 73 typedef ptrdiff_t difference_type; 74 75 typedef T* pointer; 76 typedef const T* const_pointer; 77 78 typedef T& reference; 79 typedef const T& const_reference; 80 address(reference r)81 pointer address(reference r) const { return &r; } address(const_reference r)82 const_pointer address(const_reference r) const { return &r; } 83 Memroot_allocator(MEM_ROOT * memroot)84 explicit Memroot_allocator(MEM_ROOT *memroot) : m_memroot(memroot) 85 {} 86 Memroot_allocator(const Memroot_allocator<U> & other)87 template <class U> Memroot_allocator(const Memroot_allocator<U> &other) 88 : m_memroot(other.memroot()) 89 {} 90 91 template <class U> Memroot_allocator & operator= 92 (const Memroot_allocator<U> &other) 93 { 94 assert(m_memroot == other.memroot()); // Don't swap memroot. 95 } 96 ~Memroot_allocator()97 ~Memroot_allocator() 98 {} 99 100 pointer allocate(size_type n, const_pointer hint= 0) 101 { 102 if (n == 0) 103 return NULL; 104 if (n > max_size()) 105 throw std::bad_alloc(); 106 107 pointer p= static_cast<pointer>(alloc_root(m_memroot, n * sizeof(T))); 108 if (p == NULL) 109 throw std::bad_alloc(); 110 return p; 111 } 112 deallocate(pointer p,size_type n)113 void deallocate(pointer p, size_type n) { } 114 construct(pointer p,const T & val)115 void construct(pointer p, const T& val) 116 { 117 assert(p != NULL); 118 try { 119 new(p) T(val); 120 } catch (...) { 121 assert(false); // Constructor should not throw an exception. 122 } 123 } 124 destroy(pointer p)125 void destroy(pointer p) 126 { 127 assert(p != NULL); 128 try { 129 p->~T(); 130 } catch (...) { 131 assert(false); // Destructor should not throw an exception 132 } 133 } 134 max_size()135 size_type max_size() const 136 { 137 return std::numeric_limits<size_t>::max() / sizeof(T); 138 } 139 140 template <class U> struct rebind { typedef Memroot_allocator<U> other; }; 141 memroot()142 MEM_ROOT *memroot() const { return m_memroot; } 143 }; 144 145 template <class T> 146 bool operator== (const Memroot_allocator<T>& a1, const Memroot_allocator<T>& a2) 147 { 148 return a1.memroot() == a2.memroot(); 149 } 150 151 template <class T> 152 bool operator!= (const Memroot_allocator<T>& a1, const Memroot_allocator<T>& a2) 153 { 154 return a1.memroot() != a2.memroot(); 155 } 156 157 #endif // MEMROOT_ALLOCATOR_INCLUDED 158