~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/ha/hash0hash.c

Tags: innodb-plugin-1.0.1
Imported 1.0.1 with clean - with no changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
UNIV_INTERN
 
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
UNIV_INTERN
 
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
UNIV_INTERN
 
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
UNIV_INTERN
 
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
UNIV_INTERN
 
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
 
 
86
        prime = ut_find_prime(n);
 
87
 
 
88
        table = mem_alloc(sizeof(hash_table_t));
 
89
 
 
90
        array = ut_malloc(sizeof(hash_cell_t) * prime);
 
91
 
 
92
#ifdef UNIV_DEBUG
 
93
        table->adaptive = FALSE;
 
94
#endif /* UNIV_DEBUG */
 
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
        hash_table_clear(table);
 
105
 
 
106
        return(table);
 
107
}
 
108
 
 
109
/*****************************************************************
 
110
Frees a hash table. */
 
111
UNIV_INTERN
 
112
void
 
113
hash_table_free(
 
114
/*============*/
 
115
        hash_table_t*   table)  /* in, own: hash table */
 
116
{
 
117
        ut_a(table->mutexes == NULL);
 
118
 
 
119
        ut_free(table->array);
 
120
        mem_free(table);
 
121
}
 
122
 
 
123
/*****************************************************************
 
124
Creates a mutex array to protect a hash table. */
 
125
UNIV_INTERN
 
126
void
 
127
hash_create_mutexes_func(
 
128
/*=====================*/
 
129
        hash_table_t*   table,          /* in: hash table */
 
130
#ifdef UNIV_SYNC_DEBUG
 
131
        ulint           sync_level,     /* in: latching order level of the
 
132
                                        mutexes: used in the debug version */
 
133
#endif /* UNIV_SYNC_DEBUG */
 
134
        ulint           n_mutexes)      /* in: number of mutexes, must be a
 
135
                                        power of 2 */
 
136
{
 
137
        ulint   i;
 
138
 
 
139
        ut_a(n_mutexes > 0);
 
140
        ut_a(ut_is_2pow(n_mutexes));
 
141
 
 
142
        table->mutexes = mem_alloc(n_mutexes * sizeof(mutex_t));
 
143
 
 
144
        for (i = 0; i < n_mutexes; i++) {
 
145
                mutex_create(table->mutexes + i, sync_level);
 
146
        }
 
147
 
 
148
        table->n_mutexes = n_mutexes;
 
149
}