hash0hash.c 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*****************************************************************************

Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

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 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., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/

19 20
/**************************************************//**
@file ha/hash0hash.c
osku's avatar
osku committed
21 22 23 24 25 26 27 28 29 30 31 32
The simple hash table utility

Created 5/20/1997 Heikki Tuuri
*******************************************************/

#include "hash0hash.h"
#ifdef UNIV_NONINL
#include "hash0hash.ic"
#endif

#include "mem0mem.h"

33
#ifndef UNIV_HOTBACKUP
34 35 36 37 38

# ifdef UNIV_PFS_MUTEX
UNIV_INTERN mysql_pfs_key_t	hash_table_mutex_key;
# endif /* UNIV_PFS_MUTEX */

39
/************************************************************//**
osku's avatar
osku committed
40
Reserves the mutex for a fold value in a hash table. */
41
UNIV_INTERN
osku's avatar
osku committed
42 43 44
void
hash_mutex_enter(
/*=============*/
45 46
	hash_table_t*	table,	/*!< in: hash table */
	ulint		fold)	/*!< in: fold */
osku's avatar
osku committed
47 48 49 50
{
	mutex_enter(hash_get_mutex(table, fold));
}

51
/************************************************************//**
osku's avatar
osku committed
52
Releases the mutex for a fold value in a hash table. */
53
UNIV_INTERN
osku's avatar
osku committed
54 55 56
void
hash_mutex_exit(
/*============*/
57 58
	hash_table_t*	table,	/*!< in: hash table */
	ulint		fold)	/*!< in: fold */
osku's avatar
osku committed
59 60 61 62
{
	mutex_exit(hash_get_mutex(table, fold));
}

63
/************************************************************//**
osku's avatar
osku committed
64
Reserves all the mutexes of a hash table, in an ascending order. */
65
UNIV_INTERN
osku's avatar
osku committed
66 67 68
void
hash_mutex_enter_all(
/*=================*/
69
	hash_table_t*	table)	/*!< in: hash table */
osku's avatar
osku committed
70 71 72 73 74 75 76 77 78
{
	ulint	i;

	for (i = 0; i < table->n_mutexes; i++) {

		mutex_enter(table->mutexes + i);
	}
}

79
/************************************************************//**
osku's avatar
osku committed
80
Releases all the mutexes of a hash table. */
81
UNIV_INTERN
osku's avatar
osku committed
82 83 84
void
hash_mutex_exit_all(
/*================*/
85
	hash_table_t*	table)	/*!< in: hash table */
osku's avatar
osku committed
86 87 88 89 90 91 92 93
{
	ulint	i;

	for (i = 0; i < table->n_mutexes; i++) {

		mutex_exit(table->mutexes + i);
	}
}
94
#endif /* !UNIV_HOTBACKUP */
osku's avatar
osku committed
95

96
/*************************************************************//**
osku's avatar
osku committed
97
Creates a hash table with >= n array cells. The actual number of cells is
98 99
chosen to be a prime number slightly bigger than n.
@return	own: created table */
100
UNIV_INTERN
osku's avatar
osku committed
101 102 103
hash_table_t*
hash_create(
/*========*/
104
	ulint	n)	/*!< in: number of array cells */
osku's avatar
osku committed
105 106 107 108
{
	hash_cell_t*	array;
	ulint		prime;
	hash_table_t*	table;
109

osku's avatar
osku committed
110 111 112 113 114
	prime = ut_find_prime(n);

	table = mem_alloc(sizeof(hash_table_t));

	array = ut_malloc(sizeof(hash_cell_t) * prime);
115

osku's avatar
osku committed
116 117
	table->array = array;
	table->n_cells = prime;
118 119 120 121
#ifndef UNIV_HOTBACKUP
# if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
	table->adaptive = FALSE;
# endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
osku's avatar
osku committed
122 123 124
	table->n_mutexes = 0;
	table->mutexes = NULL;
	table->heaps = NULL;
125
#endif /* !UNIV_HOTBACKUP */
osku's avatar
osku committed
126
	table->heap = NULL;
127
	ut_d(table->magic_n = HASH_TABLE_MAGIC_N);
128

osku's avatar
osku committed
129
	/* Initialize the cell array */
130
	hash_table_clear(table);
osku's avatar
osku committed
131 132 133 134

	return(table);
}

135
/*************************************************************//**
osku's avatar
osku committed
136
Frees a hash table. */
137
UNIV_INTERN
osku's avatar
osku committed
138 139 140
void
hash_table_free(
/*============*/
141
	hash_table_t*	table)	/*!< in, own: hash table */
osku's avatar
osku committed
142
{
143 144
	ut_ad(table);
	ut_ad(table->magic_n == HASH_TABLE_MAGIC_N);
145
#ifndef UNIV_HOTBACKUP
osku's avatar
osku committed
146
	ut_a(table->mutexes == NULL);
147
#endif /* !UNIV_HOTBACKUP */
osku's avatar
osku committed
148 149 150 151 152

	ut_free(table->array);
	mem_free(table);
}

153
#ifndef UNIV_HOTBACKUP
154
/*************************************************************//**
osku's avatar
osku committed
155
Creates a mutex array to protect a hash table. */
156
UNIV_INTERN
osku's avatar
osku committed
157
void
158 159
hash_create_mutexes_func(
/*=====================*/
160
	hash_table_t*	table,		/*!< in: hash table */
161
#ifdef UNIV_SYNC_DEBUG
162
	ulint		sync_level,	/*!< in: latching order level of the
osku's avatar
osku committed
163
					mutexes: used in the debug version */
164
#endif /* UNIV_SYNC_DEBUG */
165
	ulint		n_mutexes)	/*!< in: number of mutexes, must be a
166
					power of 2 */
osku's avatar
osku committed
167 168 169
{
	ulint	i;

170 171
	ut_ad(table);
	ut_ad(table->magic_n == HASH_TABLE_MAGIC_N);
172 173
	ut_a(n_mutexes > 0);
	ut_a(ut_is_2pow(n_mutexes));
osku's avatar
osku committed
174 175 176 177

	table->mutexes = mem_alloc(n_mutexes * sizeof(mutex_t));

	for (i = 0; i < n_mutexes; i++) {
178 179
		mutex_create(hash_table_mutex_key,
			     table->mutexes + i, sync_level);
osku's avatar
osku committed
180 181 182 183
	}

	table->n_mutexes = n_mutexes;
}
184
#endif /* !UNIV_HOTBACKUP */