~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.
4
 
 
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.
8
 
 
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.
12
 
 
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
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/**************************************************//**
20
 
@file ha/hash0hash.c
21
 
The simple hash table utility
22
 
 
23
 
Created 5/20/1997 Heikki Tuuri
24
 
*******************************************************/
25
 
 
26
 
#include "hash0hash.h"
27
 
#ifdef UNIV_NONINL
28
 
#include "hash0hash.ic"
29
 
#endif
30
 
 
31
 
#include "mem0mem.h"
32
 
 
33
 
#ifndef UNIV_HOTBACKUP
34
 
 
35
 
# ifdef UNIV_PFS_MUTEX
36
 
UNIV_INTERN mysql_pfs_key_t     hash_table_mutex_key;
37
 
# endif /* UNIV_PFS_MUTEX */
38
 
 
39
 
/************************************************************//**
40
 
Reserves the mutex for a fold value in a hash table. */
41
 
UNIV_INTERN
42
 
void
43
 
hash_mutex_enter(
44
 
/*=============*/
45
 
        hash_table_t*   table,  /*!< in: hash table */
46
 
        ulint           fold)   /*!< in: fold */
47
 
{
48
 
        mutex_enter(hash_get_mutex(table, fold));
49
 
}
50
 
 
51
 
/************************************************************//**
52
 
Releases the mutex for a fold value in a hash table. */
53
 
UNIV_INTERN
54
 
void
55
 
hash_mutex_exit(
56
 
/*============*/
57
 
        hash_table_t*   table,  /*!< in: hash table */
58
 
        ulint           fold)   /*!< in: fold */
59
 
{
60
 
        mutex_exit(hash_get_mutex(table, fold));
61
 
}
62
 
 
63
 
/************************************************************//**
64
 
Reserves all the mutexes of a hash table, in an ascending order. */
65
 
UNIV_INTERN
66
 
void
67
 
hash_mutex_enter_all(
68
 
/*=================*/
69
 
        hash_table_t*   table)  /*!< in: hash table */
70
 
{
71
 
        ulint   i;
72
 
 
73
 
        for (i = 0; i < table->n_mutexes; i++) {
74
 
 
75
 
                mutex_enter(table->mutexes + i);
76
 
        }
77
 
}
78
 
 
79
 
/************************************************************//**
80
 
Releases all the mutexes of a hash table. */
81
 
UNIV_INTERN
82
 
void
83
 
hash_mutex_exit_all(
84
 
/*================*/
85
 
        hash_table_t*   table)  /*!< in: hash table */
86
 
{
87
 
        ulint   i;
88
 
 
89
 
        for (i = 0; i < table->n_mutexes; i++) {
90
 
 
91
 
                mutex_exit(table->mutexes + i);
92
 
        }
93
 
}
94
 
#endif /* !UNIV_HOTBACKUP */
95
 
 
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 */
100
 
UNIV_INTERN
101
 
hash_table_t*
102
 
hash_create(
103
 
/*========*/
104
 
        ulint   n)      /*!< in: number of array cells */
105
 
{
106
 
        hash_cell_t*    array;
107
 
        ulint           prime;
108
 
        hash_table_t*   table;
109
 
 
110
 
        prime = ut_find_prime(n);
111
 
 
112
 
        table = mem_alloc(sizeof(hash_table_t));
113
 
 
114
 
        array = ut_malloc(sizeof(hash_cell_t) * prime);
115
 
 
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;
124
 
        table->heaps = NULL;
125
 
#endif /* !UNIV_HOTBACKUP */
126
 
        table->heap = NULL;
127
 
        ut_d(table->magic_n = HASH_TABLE_MAGIC_N);
128
 
 
129
 
        /* Initialize the cell array */
130
 
        hash_table_clear(table);
131
 
 
132
 
        return(table);
133
 
}
134
 
 
135
 
/*************************************************************//**
136
 
Frees a hash table. */
137
 
UNIV_INTERN
138
 
void
139
 
hash_table_free(
140
 
/*============*/
141
 
        hash_table_t*   table)  /*!< in, own: hash table */
142
 
{
143
 
        ut_ad(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 */
148
 
 
149
 
        ut_free(table->array);
150
 
        mem_free(table);
151
 
}
152
 
 
153
 
#ifndef UNIV_HOTBACKUP
154
 
/*************************************************************//**
155
 
Creates a mutex array to protect a hash table. */
156
 
UNIV_INTERN
157
 
void
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
166
 
                                        power of 2 */
167
 
{
168
 
        ulint   i;
169
 
 
170
 
        ut_ad(table);
171
 
        ut_ad(table->magic_n == HASH_TABLE_MAGIC_N);
172
 
        ut_a(n_mutexes > 0);
173
 
        ut_a(ut_is_2pow(n_mutexes));
174
 
 
175
 
        table->mutexes = mem_alloc(n_mutexes * sizeof(mutex_t));
176
 
 
177
 
        for (i = 0; i < n_mutexes; i++) {
178
 
                mutex_create(hash_table_mutex_key,
179
 
                             table->mutexes + i, sync_level);
180
 
        }
181
 
 
182
 
        table->n_mutexes = n_mutexes;
183
 
}
184
 
#endif /* !UNIV_HOTBACKUP */