| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1997, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/hash0hash.ic |
| 29 |
The simple hash table utility |
| 30 |
|
| 31 |
Created 5/20/1997 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "ut0rnd.h" |
| 35 |
|
| 36 |
static inline hash_cell_t *hash_get_nth_cell(hash_table_t *table, size_t n) { |
| 37 |
ut_ad(table); |
| 38 |
ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N); |
| 39 |
ut_ad(n < table->get_n_cells()); |
| 40 |
|
| 41 |
return &table->cells[n]; |
| 42 |
} |
| 43 |
|
| 44 |
/** Clears a hash table so that all the cells become empty. */ |
| 45 |
static inline void hash_table_clear( |
| 46 |
hash_table_t *table) /*!< in/out: hash table */ |
| 47 |
{ |
| 48 |
ut_ad(table); |
| 49 |
ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N); |
| 50 |
memset(table->cells.get(), 0x0, table->get_n_cells() * sizeof(hash_cell_t)); |
| 51 |
} |
| 52 |
|
| 53 |
static inline size_t hash_get_n_cells(hash_table_t const *table) { |
| 54 |
ut_ad(table); |
| 55 |
ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N); |
| 56 |
return table->get_n_cells(); |
| 57 |
} |
| 58 |
|
| 59 |
static inline uint64_t hash_calc_cell_id(uint64_t hash_value, |
| 60 |
hash_table_t const *table) { |
| 61 |
ut_ad(table); |
| 62 |
ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N); |
| 63 |
return hash_value % table->get_n_cells_fast_modulo(); |
| 64 |
} |
| 65 |
|
| 66 |
#ifndef UNIV_HOTBACKUP |
| 67 |
/** Gets the sync object index for a hash value in a hash table. |
| 68 |
@return index */ |
| 69 |
static inline uint64_t hash_get_sync_obj_index( |
| 70 |
hash_table_t *table, /*!< in: hash table */ |
| 71 |
uint64_t hash_value) /*!< in: hash value */ |
| 72 |
{ |
| 73 |
ut_ad(table); |
| 74 |
ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N); |
| 75 |
ut_ad(table->type != HASH_TABLE_SYNC_NONE); |
| 76 |
ut_ad(ut_is_2pow(table->n_sync_obj)); |
| 77 |
return ut_2pow_remainder(hash_calc_cell_id(hash_value, table), |
| 78 |
table->n_sync_obj); |
| 79 |
} |
| 80 |
|
| 81 |
/** Gets the heap for a hash value in a hash table. |
| 82 |
@return mem heap */ |
| 83 |
static inline mem_heap_t *hash_get_heap( |
| 84 |
hash_table_t *table) /*!< in: hash table */ |
| 85 |
{ |
| 86 |
ut_ad(table); |
| 87 |
ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N); |
| 88 |
ut_ad(table->n_sync_obj == 0); |
| 89 |
ut_ad(table->type == HASH_TABLE_SYNC_NONE); |
| 90 |
ut_ad(table->heap != nullptr); |
| 91 |
return table->heap; |
| 92 |
} |
| 93 |
|
| 94 |
/** Gets the nth rw_lock in a hash table. |
| 95 |
@return rw_lock */ |
| 96 |
static inline rw_lock_t *hash_get_nth_lock( |
| 97 |
hash_table_t *table, /*!< in: hash table */ |
| 98 |
size_t i) /*!< in: index of the rw_lock */ |
| 99 |
{ |
| 100 |
ut_ad(table); |
| 101 |
ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N); |
| 102 |
ut_ad(table->type == HASH_TABLE_SYNC_RW_LOCK); |
| 103 |
ut_ad(i < table->n_sync_obj); |
| 104 |
|
| 105 |
return table->rw_locks + i; |
| 106 |
} |
| 107 |
|
| 108 |
/** Gets the rw_lock for a hash value in a hash table. |
| 109 |
@return rw_lock */ |
| 110 |
static inline rw_lock_t *hash_get_lock( |
| 111 |
hash_table_t *table, /*!< in: hash table */ |
| 112 |
uint64_t hash_value) /*!< in: hash value */ |
| 113 |
{ |
| 114 |
ut_ad(table); |
| 115 |
ut_ad(table->type == HASH_TABLE_SYNC_RW_LOCK); |
| 116 |
ut_ad(table->magic_n == hash_table_t::HASH_TABLE_MAGIC_N); |
| 117 |
|
| 118 |
const auto i = hash_get_sync_obj_index(table, hash_value); |
| 119 |
|
| 120 |
return hash_get_nth_lock(table, i); |
| 121 |
} |
| 122 |
|
| 123 |
/** If not appropriate rw_lock for a hash value in a hash table, |
| 124 |
relock S-lock the another rw_lock until appropriate for a hash value. |
| 125 |
@param[in] hash_lock latched rw_lock to be confirmed |
| 126 |
@param[in] table hash table |
| 127 |
@param[in] hash_value hash value |
| 128 |
@return latched rw_lock */ |
| 129 |
static inline rw_lock_t *hash_lock_s_confirm(rw_lock_t *hash_lock, |
| 130 |
hash_table_t *table, |
| 131 |
uint64_t hash_value) { |
| 132 |
ut_ad(rw_lock_own(hash_lock, RW_LOCK_S)); |
| 133 |
|
| 134 |
rw_lock_t *hash_lock_tmp = hash_get_lock(table, hash_value); |
| 135 |
|
| 136 |
while (hash_lock_tmp != hash_lock) { |
| 137 |
rw_lock_s_unlock(hash_lock); |
| 138 |
hash_lock = hash_lock_tmp; |
| 139 |
rw_lock_s_lock(hash_lock, UT_LOCATION_HERE); |
| 140 |
hash_lock_tmp = hash_get_lock(table, hash_value); |
| 141 |
} |
| 142 |
|
| 143 |
return hash_lock; |
| 144 |
} |
| 145 |
|
| 146 |
/** If not appropriate rw_lock for a hash value in a hash table, |
| 147 |
relock X-lock the another rw_lock until appropriate for a hash value. |
| 148 |
@param[in] hash_lock latched rw_lock to be confirmed |
| 149 |
@param[in] table hash table |
| 150 |
@param[in] hash_value hash value |
| 151 |
@return latched rw_lock */ |
| 152 |
static inline rw_lock_t *hash_lock_x_confirm(rw_lock_t *hash_lock, |
| 153 |
hash_table_t *table, |
| 154 |
uint64_t hash_value) { |
| 155 |
ut_ad(rw_lock_own(hash_lock, RW_LOCK_X)); |
| 156 |
|
| 157 |
rw_lock_t *hash_lock_tmp = hash_get_lock(table, hash_value); |
| 158 |
|
| 159 |
while (hash_lock_tmp != hash_lock) { |
| 160 |
rw_lock_x_unlock(hash_lock); |
| 161 |
hash_lock = hash_lock_tmp; |
| 162 |
rw_lock_x_lock(hash_lock, UT_LOCATION_HERE); |
| 163 |
hash_lock_tmp = hash_get_lock(table, hash_value); |
| 164 |
} |
| 165 |
|
| 166 |
return hash_lock; |
| 167 |
} |
| 168 |
#endif /* !UNIV_HOTBACKUP */ |
| 169 |
|
| 170 |
/** Assert that the synchronization object in a hash operation involving |
| 171 |
possible change in the hash table is held. |
| 172 |
We assert that the specific rw_lock is held in exclusive mode. |
| 173 |
@param[in] table The hash table |
| 174 |
@param[in] hash_value The hash value for which the modification is performed |
| 175 |
*/ |
| 176 |
static inline void hash_assert_can_modify(hash_table_t *table, |
| 177 |
uint64_t hash_value) { |
| 178 |
#ifdef UNIV_DEBUG |
| 179 |
if (table->type == HASH_TABLE_SYNC_RW_LOCK) { |
| 180 |
#ifndef UNIV_HOTBACKUP |
| 181 |
rw_lock_t *lock = hash_get_lock(table, hash_value); |
| 182 |
ut_ad(rw_lock_own(lock, RW_LOCK_X)); |
| 183 |
#endif /*!UNIV_HOTBACKUP*/ |
| 184 |
} else { |
| 185 |
ut_ad(table->type == HASH_TABLE_SYNC_NONE); |
| 186 |
} |
| 187 |
#endif /* UNIV_DEBUG */ |
| 188 |
} |
| 189 |
|
| 190 |
/** Assert that the synchronization object in a hash search operation is held. |
| 191 |
We assert that the specific rw_lock is held either in x-mode or s-mode. |
| 192 |
@param[in] table The hash table |
| 193 |
@param[in] hash_value The hash value for which the search is performed |
| 194 |
*/ |
| 195 |
static inline void hash_assert_can_search(hash_table_t *table, |
| 196 |
uint64_t hash_value) { |
| 197 |
#ifdef UNIV_DEBUG |
| 198 |
if (table->type == HASH_TABLE_SYNC_RW_LOCK) { |
| 199 |
#ifndef UNIV_HOTBACKUP |
| 200 |
rw_lock_t *lock = hash_get_lock(table, hash_value); |
| 201 |
ut_ad(rw_lock_own(lock, RW_LOCK_X) || rw_lock_own(lock, RW_LOCK_S)); |
| 202 |
#endif /*!UNIV_HOTBACKUP*/ |
| 203 |
} else { |
| 204 |
ut_ad(table->type == HASH_TABLE_SYNC_NONE); |
| 205 |
} |
| 206 |
#endif |
| 207 |
} |