MySQL 8.0.39
Source Code Documentation
component_malloc_allocator.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef COMPONENT_MALLOC_ALLOCATOR_INCLUDED
25#define COMPONENT_MALLOC_ALLOCATOR_INCLUDED
26
27#include <assert.h>
29#include <limits>
30#include <new>
31#include <utility> // std::forward
32#include "my_compiler.h"
33
34/**
35 Component_malloc_allocator is a C++ STL memory allocator based on
36 my_malloc/my_free.
37
38 This allows for P_S instrumentation of memory allocation done
39 internally by STL container classes in components.
40
41 Example usage:
42 vector<int, Component_malloc_allocator<int>>
43 v((Component_malloc_allocator<int>(PSI_NOT_INSTRUMENTED)));
44
45 If the type is complicated, you can just write
46 Component_malloc_allocator<>(psi_key) as a shorthand for
47 Component_malloc_allocator<My_complicated_type>(psi_key), as all
48 Component_malloc_allocator instances are implicitly convertible to each other
49 and there is a default template parameter.
50
51 @note allocate() throws std::bad_alloc() similarly to the default
52 STL memory allocator. This is necessary - STL functions which allocates
53 memory expects it. Otherwise these functions will try to use the memory,
54 leading to segfaults if memory allocation was not successful.
55
56 @note This allocator cannot be used for std::basic_string with RHEL 6/7
57 because of this bug:
58 https://bugzilla.redhat.com/show_bug.cgi?id=1546704
59 "Define _GLIBCXX_USE_CXX11_ABI gets ignored by gcc in devtoolset-7"
60*/
61
62template <class T = void *>
64 // This cannot be const if we want to be able to swap.
66
67 public:
68 typedef T value_type;
69 typedef size_t size_type;
70 typedef ptrdiff_t difference_type;
71
72 typedef T *pointer;
73 typedef const T *const_pointer;
74
75 typedef T &reference;
76 typedef const T &const_reference;
77
78 pointer address(reference r) const { return &r; }
80
82
83 template <class U>
85 : m_key(other.psi_key()) {}
86
87 template <class U>
88 // on release builds assert is not evaluated. So compilation error is seen,
89 // hence used 'unused' attribute.
91 const Component_malloc_allocator<U> &other [[maybe_unused]]) {
92 assert(m_key == other.psi_key()); // Don't swap key.
93 }
94
96 if (n == 0) return nullptr;
97 if (n > max_size()) throw std::bad_alloc();
98
99 pointer p = static_cast<pointer>(my_malloc(m_key, n * sizeof(T), 0));
100 if (p == nullptr) throw std::bad_alloc();
101 return p;
102 }
103
105
106 template <class U, class... Args>
107 void construct(U *p, Args &&... args) {
108 assert(p != nullptr);
109 try {
110 ::new ((void *)p) U(std::forward<Args>(args)...);
111 } catch (...) {
112 assert(false); // Constructor should not throw an exception.
113 }
114 }
115
117 assert(p != nullptr);
118 try {
119 p->~T();
120 } catch (...) {
121 assert(false); // Destructor should not throw an exception
122 }
123 }
124
126 return std::numeric_limits<size_t>::max() / sizeof(T);
127 }
128
129 template <class U>
130 struct rebind {
132 };
133
134 PSI_memory_key psi_key() const { return m_key; }
135};
136
137template <class T>
140 return a1.psi_key() == a2.psi_key();
141}
142
143template <class T>
146 return a1.psi_key() != a2.psi_key();
147}
148
149#endif // COMPONENT_MALLOC_ALLOCATOR_INCLUDED
Component_malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
Definition: component_malloc_allocator.h:63
void destroy(pointer p)
Definition: component_malloc_allocator.h:116
PSI_memory_key psi_key() const
Definition: component_malloc_allocator.h:134
const_pointer address(const_reference r) const
Definition: component_malloc_allocator.h:79
void deallocate(pointer p, size_type)
Definition: component_malloc_allocator.h:104
pointer allocate(size_type n, const_pointer=nullptr)
Definition: component_malloc_allocator.h:95
T value_type
Definition: component_malloc_allocator.h:68
void construct(U *p, Args &&... args)
Definition: component_malloc_allocator.h:107
T & reference
Definition: component_malloc_allocator.h:75
size_t size_type
Definition: component_malloc_allocator.h:69
ptrdiff_t difference_type
Definition: component_malloc_allocator.h:70
const T & const_reference
Definition: component_malloc_allocator.h:76
PSI_memory_key m_key
Definition: component_malloc_allocator.h:65
Component_malloc_allocator(PSI_memory_key key)
Definition: component_malloc_allocator.h:81
Component_malloc_allocator(const Component_malloc_allocator< U > &other)
Definition: component_malloc_allocator.h:84
Component_malloc_allocator & operator=(const Component_malloc_allocator< U > &other)
Definition: component_malloc_allocator.h:90
const T * const_pointer
Definition: component_malloc_allocator.h:73
pointer address(reference r) const
Definition: component_malloc_allocator.h:78
T * pointer
Definition: component_malloc_allocator.h:72
size_type max_size() const
Definition: component_malloc_allocator.h:125
bool operator!=(const Component_malloc_allocator< T > &a1, const Component_malloc_allocator< T > &a2)
Definition: component_malloc_allocator.h:144
bool operator==(const Component_malloc_allocator< T > &a1, const Component_malloc_allocator< T > &a2)
Definition: component_malloc_allocator.h:138
const char * p
Definition: ctype-mb.cc:1237
#define U
Definition: ctype-tis620.cc:75
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
Header for compiler-dependent features.
void * my_malloc(PSI_memory_key key, size_t size, int flags)
Allocates size bytes of memory.
Definition: my_memory.cc:57
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:81
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Definition: component_malloc_allocator.h:130
Component_malloc_allocator< U > other
Definition: component_malloc_allocator.h:131
Definition: dtoa.cc:594
int n
Definition: xcom_base.cc:509