~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/******************************************************
2
The simple hash table utility
3
4
(c) 1997 Innobase Oy
5
6
Created 5/20/1997 Heikki Tuuri
7
*******************************************************/
8
9
#include "hash0hash.h"
10
#ifdef UNIV_NONINL
11
#include "hash0hash.ic"
12
#endif
13
14
#include "mem0mem.h"
15
16
/****************************************************************
17
Reserves the mutex for a fold value in a hash table. */
18
19
void
20
hash_mutex_enter(
21
/*=============*/
22
	hash_table_t*	table,	/* in: hash table */
23
	ulint		fold)	/* in: fold */
24
{
25
	mutex_enter(hash_get_mutex(table, fold));
26
}
27
28
/****************************************************************
29
Releases the mutex for a fold value in a hash table. */
30
31
void
32
hash_mutex_exit(
33
/*============*/
34
	hash_table_t*	table,	/* in: hash table */
35
	ulint		fold)	/* in: fold */
36
{
37
	mutex_exit(hash_get_mutex(table, fold));
38
}
39
40
/****************************************************************
41
Reserves all the mutexes of a hash table, in an ascending order. */
42
43
void
44
hash_mutex_enter_all(
45
/*=================*/
46
	hash_table_t*	table)	/* in: hash table */
47
{
48
	ulint	i;
49
50
	for (i = 0; i < table->n_mutexes; i++) {
51
52
		mutex_enter(table->mutexes + i);
53
	}
54
}
55
56
/****************************************************************
57
Releases all the mutexes of a hash table. */
58
59
void
60
hash_mutex_exit_all(
61
/*================*/
62
	hash_table_t*	table)	/* in: hash table */
63
{
64
	ulint	i;
65
66
	for (i = 0; i < table->n_mutexes; i++) {
67
68
		mutex_exit(table->mutexes + i);
69
	}
70
}
71
72
/*****************************************************************
73
Creates a hash table with >= n array cells. The actual number of cells is
74
chosen to be a prime number slightly bigger than n. */
75
76
hash_table_t*
77
hash_create(
78
/*========*/
79
			/* out, own: created table */
80
	ulint	n)	/* in: number of array cells */
81
{
82
	hash_cell_t*	array;
83
	ulint		prime;
84
	hash_table_t*	table;
85
	ulint		i;
86
	hash_cell_t*	cell;
87
88
	prime = ut_find_prime(n);
89
90
	table = mem_alloc(sizeof(hash_table_t));
91
92
	array = ut_malloc(sizeof(hash_cell_t) * prime);
93
94
	table->adaptive = FALSE;
95
	table->array = array;
96
	table->n_cells = prime;
97
	table->n_mutexes = 0;
98
	table->mutexes = NULL;
99
	table->heaps = NULL;
100
	table->heap = NULL;
101
	table->magic_n = HASH_TABLE_MAGIC_N;
102
103
	/* Initialize the cell array */
104
105
	for (i = 0; i < prime; i++) {
106
107
		cell = hash_get_nth_cell(table, i);
108
		cell->node = NULL;
109
	}
110
111
	return(table);
112
}
113
114
/*****************************************************************
115
Frees a hash table. */
116
117
void
118
hash_table_free(
119
/*============*/
120
	hash_table_t*	table)	/* in, own: hash table */
121
{
122
	ut_a(table->mutexes == NULL);
123
124
	ut_free(table->array);
125
	mem_free(table);
126
}
127
128
/*****************************************************************
129
Creates a mutex array to protect a hash table. */
130
131
void
132
hash_create_mutexes_func(
133
/*=====================*/
134
	hash_table_t*	table,		/* in: hash table */
135
#ifdef UNIV_SYNC_DEBUG
136
	ulint		sync_level,	/* in: latching order level of the
137
					mutexes: used in the debug version */
138
#endif /* UNIV_SYNC_DEBUG */
139
	ulint		n_mutexes)	/* in: number of mutexes, must be a
140
					power of 2 */
141
{
142
	ulint	i;
143
144
	ut_a(n_mutexes == ut_2_power_up(n_mutexes));
145
146
	table->mutexes = mem_alloc(n_mutexes * sizeof(mutex_t));
147
148
	for (i = 0; i < n_mutexes; i++) {
149
		mutex_create(table->mutexes + i, sync_level);
150
	}
151
152
	table->n_mutexes = n_mutexes;
153
}