1
/*****************************************************************************
3
Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/**************************************************//**
21
The simple hash table utility
23
Created 5/20/1997 Heikki Tuuri
24
*******************************************************/
26
#include "hash0hash.h"
28
#include "hash0hash.ic"
33
#ifndef UNIV_HOTBACKUP
35
# ifdef UNIV_PFS_MUTEX
36
UNIV_INTERN mysql_pfs_key_t hash_table_mutex_key;
37
# endif /* UNIV_PFS_MUTEX */
39
/************************************************************//**
40
Reserves the mutex for a fold value in a hash table. */
45
hash_table_t* table, /*!< in: hash table */
46
ulint fold) /*!< in: fold */
48
mutex_enter(hash_get_mutex(table, fold));
51
/************************************************************//**
52
Releases the mutex for a fold value in a hash table. */
57
hash_table_t* table, /*!< in: hash table */
58
ulint fold) /*!< in: fold */
60
mutex_exit(hash_get_mutex(table, fold));
63
/************************************************************//**
64
Reserves all the mutexes of a hash table, in an ascending order. */
69
hash_table_t* table) /*!< in: hash table */
73
for (i = 0; i < table->n_mutexes; i++) {
75
mutex_enter(table->mutexes + i);
79
/************************************************************//**
80
Releases all the mutexes of a hash table. */
85
hash_table_t* table) /*!< in: hash table */
89
for (i = 0; i < table->n_mutexes; i++) {
91
mutex_exit(table->mutexes + i);
94
#endif /* !UNIV_HOTBACKUP */
96
/*************************************************************//**
97
Creates a hash table with >= n array cells. The actual number of cells is
98
chosen to be a prime number slightly bigger than n.
99
@return own: created table */
104
ulint n) /*!< in: number of array cells */
110
prime = ut_find_prime(n);
112
table = mem_alloc(sizeof(hash_table_t));
114
array = ut_malloc(sizeof(hash_cell_t) * prime);
116
table->array = array;
117
table->n_cells = prime;
118
#ifndef UNIV_HOTBACKUP
119
# if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
120
table->adaptive = FALSE;
121
# endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
122
table->n_mutexes = 0;
123
table->mutexes = NULL;
125
#endif /* !UNIV_HOTBACKUP */
127
ut_d(table->magic_n = HASH_TABLE_MAGIC_N);
129
/* Initialize the cell array */
130
hash_table_clear(table);
135
/*************************************************************//**
136
Frees a hash table. */
141
hash_table_t* table) /*!< in, own: hash table */
144
ut_ad(table->magic_n == HASH_TABLE_MAGIC_N);
145
#ifndef UNIV_HOTBACKUP
146
ut_a(table->mutexes == NULL);
147
#endif /* !UNIV_HOTBACKUP */
149
ut_free(table->array);
153
#ifndef UNIV_HOTBACKUP
154
/*************************************************************//**
155
Creates a mutex array to protect a hash table. */
158
hash_create_mutexes_func(
159
/*=====================*/
160
hash_table_t* table, /*!< in: hash table */
161
#ifdef UNIV_SYNC_DEBUG
162
ulint sync_level, /*!< in: latching order level of the
163
mutexes: used in the debug version */
164
#endif /* UNIV_SYNC_DEBUG */
165
ulint n_mutexes) /*!< in: number of mutexes, must be a
171
ut_ad(table->magic_n == HASH_TABLE_MAGIC_N);
173
ut_a(ut_is_2pow(n_mutexes));
175
table->mutexes = mem_alloc(n_mutexes * sizeof(mutex_t));
177
for (i = 0; i < n_mutexes; i++) {
178
mutex_create(hash_table_mutex_key,
179
table->mutexes + i, sync_level);
182
table->n_mutexes = n_mutexes;
184
#endif /* !UNIV_HOTBACKUP */