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
34
/************************************************************//**
35
Reserves the mutex for a fold value in a hash table. */
40
hash_table_t* table, /*!< in: hash table */
41
ulint fold) /*!< in: fold */
43
mutex_enter(hash_get_mutex(table, fold));
46
/************************************************************//**
47
Releases the mutex for a fold value in a hash table. */
52
hash_table_t* table, /*!< in: hash table */
53
ulint fold) /*!< in: fold */
55
mutex_exit(hash_get_mutex(table, fold));
58
/************************************************************//**
59
Reserves all the mutexes of a hash table, in an ascending order. */
64
hash_table_t* table) /*!< in: hash table */
68
for (i = 0; i < table->n_mutexes; i++) {
70
mutex_enter(table->mutexes + i);
74
/************************************************************//**
75
Releases all the mutexes of a hash table. */
80
hash_table_t* table) /*!< in: hash table */
84
for (i = 0; i < table->n_mutexes; i++) {
86
mutex_exit(table->mutexes + i);
89
#endif /* !UNIV_HOTBACKUP */
91
/*************************************************************//**
92
Creates a hash table with >= n array cells. The actual number of cells is
93
chosen to be a prime number slightly bigger than n.
94
@return own: created table */
99
ulint n) /*!< in: number of array cells */
105
prime = ut_find_prime(n);
107
table = mem_alloc(sizeof(hash_table_t));
109
array = ut_malloc(sizeof(hash_cell_t) * prime);
111
table->array = array;
112
table->n_cells = prime;
113
#ifndef UNIV_HOTBACKUP
114
# if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
115
table->adaptive = FALSE;
116
# endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
117
table->n_mutexes = 0;
118
table->mutexes = NULL;
120
#endif /* !UNIV_HOTBACKUP */
122
ut_d(table->magic_n = HASH_TABLE_MAGIC_N);
124
/* Initialize the cell array */
125
hash_table_clear(table);
130
/*************************************************************//**
131
Frees a hash table. */
136
hash_table_t* table) /*!< in, own: hash table */
139
ut_ad(table->magic_n == HASH_TABLE_MAGIC_N);
140
#ifndef UNIV_HOTBACKUP
141
ut_a(table->mutexes == NULL);
142
#endif /* !UNIV_HOTBACKUP */
144
ut_free(table->array);
148
#ifndef UNIV_HOTBACKUP
149
/*************************************************************//**
150
Creates a mutex array to protect a hash table. */
153
hash_create_mutexes_func(
154
/*=====================*/
155
hash_table_t* table, /*!< in: hash table */
156
#ifdef UNIV_SYNC_DEBUG
157
ulint sync_level, /*!< in: latching order level of the
158
mutexes: used in the debug version */
159
#endif /* UNIV_SYNC_DEBUG */
160
ulint n_mutexes) /*!< in: number of mutexes, must be a
166
ut_ad(table->magic_n == HASH_TABLE_MAGIC_N);
168
ut_a(ut_is_2pow(n_mutexes));
170
table->mutexes = mem_alloc(n_mutexes * sizeof(mutex_t));
172
for (i = 0; i < n_mutexes; i++) {
173
mutex_create(table->mutexes + i, sync_level);
176
table->n_mutexes = n_mutexes;
178
#endif /* !UNIV_HOTBACKUP */