1/***************************************************************************** 2 3Copyright (c) 1997, 2009, Oracle and/or its affiliates. All Rights Reserved. 4 5This program is free software; you can redistribute it and/or modify 6it under the terms of the GNU General Public License, version 2.0, 7as published by the Free Software Foundation. 8 9This program is also distributed with certain software (including 10but not limited to OpenSSL) that is licensed under separate terms, 11as designated in a particular file or component or in included license 12documentation. The authors of MySQL hereby grant you an additional 13permission to link the program and your derivative works with the 14separately licensed software that they have included with MySQL. 15 16This program is distributed in the hope that it will be useful, 17but WITHOUT ANY WARRANTY; without even the implied warranty of 18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19GNU General Public License, version 2.0, for more details. 20 21You should have received a copy of the GNU General Public License along with 22this program; if not, write to the Free Software Foundation, Inc., 2351 Franklin Street, Suite 500, Boston, MA 02110-1335 USA 24 25*****************************************************************************/ 26 27/**************************************************//** 28@file include/hash0hash.ic 29The simple hash table utility 30 31Created 5/20/1997 Heikki Tuuri 32*******************************************************/ 33 34#include "ut0rnd.h" 35 36/************************************************************//** 37Gets the nth cell in a hash table. 38@return pointer to cell */ 39UNIV_INLINE 40hash_cell_t* 41hash_get_nth_cell( 42/*==============*/ 43 hash_table_t* table, /*!< in: hash table */ 44 ulint n) /*!< in: cell index */ 45{ 46 ut_ad(table); 47 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 48 ut_ad(n < table->n_cells); 49 50 return(table->array + n); 51} 52 53/*************************************************************//** 54Clears a hash table so that all the cells become empty. */ 55UNIV_INLINE 56void 57hash_table_clear( 58/*=============*/ 59 hash_table_t* table) /*!< in/out: hash table */ 60{ 61 ut_ad(table); 62 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 63 memset(table->array, 0x0, 64 table->n_cells * sizeof(*table->array)); 65} 66 67/*************************************************************//** 68Returns the number of cells in a hash table. 69@return number of cells */ 70UNIV_INLINE 71ulint 72hash_get_n_cells( 73/*=============*/ 74 hash_table_t* table) /*!< in: table */ 75{ 76 ut_ad(table); 77 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 78 return(table->n_cells); 79} 80 81/**************************************************************//** 82Calculates the hash value from a folded value. 83@return hashed value */ 84UNIV_INLINE 85ulint 86hash_calc_hash( 87/*===========*/ 88 ulint fold, /*!< in: folded value */ 89 hash_table_t* table) /*!< in: hash table */ 90{ 91 ut_ad(table); 92 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 93 return(ut_hash_ulint(fold, table->n_cells)); 94} 95 96#ifndef UNIV_HOTBACKUP 97/************************************************************//** 98Gets the sync object index for a fold value in a hash table. 99@return index */ 100UNIV_INLINE 101ulint 102hash_get_sync_obj_index( 103/*====================*/ 104 hash_table_t* table, /*!< in: hash table */ 105 ulint fold) /*!< in: fold */ 106{ 107 ut_ad(table); 108 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 109 ut_ad(table->type != HASH_TABLE_SYNC_NONE); 110 ut_ad(ut_is_2pow(table->n_sync_obj)); 111 return(ut_2pow_remainder(hash_calc_hash(fold, table), 112 table->n_sync_obj)); 113} 114 115/************************************************************//** 116Gets the nth heap in a hash table. 117@return mem heap */ 118UNIV_INLINE 119mem_heap_t* 120hash_get_nth_heap( 121/*==============*/ 122 hash_table_t* table, /*!< in: hash table */ 123 ulint i) /*!< in: index of the heap */ 124{ 125 ut_ad(table); 126 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 127 ut_ad(table->type != HASH_TABLE_SYNC_NONE); 128 ut_ad(i < table->n_sync_obj); 129 130 return(table->heaps[i]); 131} 132 133/************************************************************//** 134Gets the heap for a fold value in a hash table. 135@return mem heap */ 136UNIV_INLINE 137mem_heap_t* 138hash_get_heap( 139/*==========*/ 140 hash_table_t* table, /*!< in: hash table */ 141 ulint fold) /*!< in: fold */ 142{ 143 ulint i; 144 145 ut_ad(table); 146 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 147 148 if (table->heap) { 149 return(table->heap); 150 } 151 152 i = hash_get_sync_obj_index(table, fold); 153 154 return(hash_get_nth_heap(table, i)); 155} 156 157/************************************************************//** 158Gets the nth mutex in a hash table. 159@return mutex */ 160UNIV_INLINE 161ib_mutex_t* 162hash_get_nth_mutex( 163/*===============*/ 164 hash_table_t* table, /*!< in: hash table */ 165 ulint i) /*!< in: index of the mutex */ 166{ 167 ut_ad(table); 168 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 169 ut_ad(table->type == HASH_TABLE_SYNC_MUTEX); 170 ut_ad(i < table->n_sync_obj); 171 172 return(table->sync_obj.mutexes + i); 173} 174 175/************************************************************//** 176Gets the mutex for a fold value in a hash table. 177@return mutex */ 178UNIV_INLINE 179ib_mutex_t* 180hash_get_mutex( 181/*===========*/ 182 hash_table_t* table, /*!< in: hash table */ 183 ulint fold) /*!< in: fold */ 184{ 185 ulint i; 186 187 ut_ad(table); 188 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 189 190 i = hash_get_sync_obj_index(table, fold); 191 192 return(hash_get_nth_mutex(table, i)); 193} 194 195/************************************************************//** 196Gets the nth rw_lock in a hash table. 197@return rw_lock */ 198UNIV_INLINE 199rw_lock_t* 200hash_get_nth_lock( 201/*==============*/ 202 hash_table_t* table, /*!< in: hash table */ 203 ulint i) /*!< in: index of the rw_lock */ 204{ 205 ut_ad(table); 206 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 207 ut_ad(table->type == HASH_TABLE_SYNC_RW_LOCK); 208 ut_ad(i < table->n_sync_obj); 209 210 return(table->sync_obj.rw_locks + i); 211} 212 213/************************************************************//** 214Gets the rw_lock for a fold value in a hash table. 215@return rw_lock */ 216UNIV_INLINE 217rw_lock_t* 218hash_get_lock( 219/*==========*/ 220 hash_table_t* table, /*!< in: hash table */ 221 ulint fold) /*!< in: fold */ 222{ 223 ulint i; 224 225 ut_ad(table); 226 ut_ad(table->type == HASH_TABLE_SYNC_RW_LOCK); 227 ut_ad(table->magic_n == HASH_TABLE_MAGIC_N); 228 229 i = hash_get_sync_obj_index(table, fold); 230 231 return(hash_get_nth_lock(table, i)); 232} 233#endif /* !UNIV_HOTBACKUP */ 234