-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathtable_histograms.cc
169 lines (145 loc) · 6 KB
/
table_histograms.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright (c) 2022, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file sql/histograms/table_histograms.cc
Table_histograms and Table_histograms_collection implementation.
*/
#include "sql/histograms/table_histograms.h"
#include <array>
#include <cassert>
#include <utility> // std::move, std::make_pair
#include "map_helpers.h" // mem_root_unordered_map
#include "my_alloc.h" // MEM_ROOT
#include "my_compiler.h"
#include "sql/histograms/histogram.h"
#include "sql/psi_memory_key.h"
#include "sql/sql_base.h" // LOCK_open
// Table_histograms
Table_histograms *Table_histograms::create(PSI_memory_key psi_key) noexcept {
MEM_ROOT mem_root(psi_key, 512);
auto *table_histograms = new (&mem_root) Table_histograms();
if (table_histograms == nullptr) return nullptr;
// Move the MEM_ROOT that we used to allocate the object onto the object.
table_histograms->m_mem_root = std::move(mem_root);
try {
// The constructor of mem_root_unordered_map may throw std::bad_alloc.
table_histograms->m_histograms = new (&table_histograms->m_mem_root)
mem_root_unordered_map<unsigned int, const histograms::Histogram *>(
&table_histograms->m_mem_root);
} catch (const std::bad_alloc &) {
table_histograms->destroy();
return nullptr;
}
return table_histograms;
}
void Table_histograms::destroy() {
// Table_histograms is allocated on its own MEM_ROOT m_mem_root. This means
// that m_mem_root is allocated on itself! The MEM_ROOT destructor calls
// MEM_ROOT::Clear() which accesses member variables while clearing memory. In
// order to avoid accessing memory that has been freed, we move-construct the
// MEM_ROOT in the current function scope before calling Clear() which will
// free all the memory used by the Table_histograms object. This same pattern
// is used in TABLE_SHARE::destroy().
MEM_ROOT own_root = std::move(m_mem_root);
own_root.Clear();
}
bool Table_histograms::insert_histogram(
unsigned int field_index, const histograms::Histogram *histogram) {
const histograms::Histogram *histogram_copy = histogram->clone(&m_mem_root);
if (histogram_copy == nullptr) return true;
try {
bool const insertion_happened =
m_histograms->insert(std::make_pair(field_index, histogram_copy))
.second;
if (!insertion_happened) return true; // Duplicate key.
} catch (const std::bad_alloc &) {
return true; // OOM.
}
return false;
}
const histograms::Histogram *Table_histograms::find_histogram(
unsigned int field_index) const {
const auto found = m_histograms->find(field_index);
if (found == m_histograms->end()) return nullptr;
return found->second;
}
// Table_histograms_collection
Table_histograms_collection::Table_histograms_collection() {
m_table_histograms.fill(nullptr);
}
Table_histograms_collection::~Table_histograms_collection() {
for (size_t i = 0; i < kMaxNumberOfTableHistogramsInCollection; ++i) {
if (m_table_histograms[i]) {
assert(m_table_histograms[i]->reference_count() == 0);
free_table_histograms(i);
}
}
}
const Table_histograms *Table_histograms_collection::acquire() {
mysql_mutex_assert_owner(&LOCK_open);
Table_histograms *current_histograms = m_table_histograms[m_current_index];
if (current_histograms == nullptr) return nullptr;
current_histograms->increment_reference_counter();
return current_histograms;
}
void Table_histograms_collection::release(const Table_histograms *histograms) {
mysql_mutex_assert_owner(&LOCK_open);
size_t const idx = histograms->get_index();
assert(m_table_histograms[idx] == histograms);
m_table_histograms[idx]->decrement_reference_counter();
if (m_table_histograms[idx]->reference_count() == 0 &&
idx != m_current_index) {
free_table_histograms(idx);
}
}
MY_COMPILER_DIAGNOSTIC_PUSH()
MY_COMPILER_MSVC_DIAGNOSTIC_IGNORE(28020)
bool Table_histograms_collection::insert(Table_histograms *histograms) {
size_t i = 0;
while (m_table_histograms[i] != nullptr) {
++i;
if (i == kMaxNumberOfTableHistogramsInCollection) return true;
}
// Free the current object if it has a reference count of zero.
if (m_table_histograms[m_current_index] &&
m_table_histograms[m_current_index]->reference_count() == 0) {
free_table_histograms(m_current_index);
}
assert(i < kMaxNumberOfTableHistogramsInCollection);
m_table_histograms[i] = histograms;
m_table_histograms[i]->set_index(i);
m_current_index = i;
return false;
}
MY_COMPILER_DIAGNOSTIC_POP()
size_t Table_histograms_collection::size() const {
size_t size = 0;
for (size_t i = 0; i < kMaxNumberOfTableHistogramsInCollection; ++i) {
if (m_table_histograms[i]) ++size;
}
return size;
}
int Table_histograms_collection::total_reference_count() const {
int count = 0;
for (size_t i = 0; i < kMaxNumberOfTableHistogramsInCollection; ++i) {
if (m_table_histograms[i])
count += m_table_histograms[i]->reference_count();
}
return count;
}