~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

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., 59 Temple
 
15
Place, Suite 330, Boston, MA 02111-1307 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
Reserves the mutex for a fold value in a hash table. */
 
36
UNIV_INTERN
 
37
void
 
38
hash_mutex_enter(
 
39
/*=============*/
 
40
        hash_table_t*   table,  /*!< in: hash table */
 
41
        ulint           fold)   /*!< in: fold */
 
42
{
 
43
        mutex_enter(hash_get_mutex(table, fold));
 
44
}
 
45
 
 
46
/************************************************************//**
 
47
Releases the mutex for a fold value in a hash table. */
 
48
UNIV_INTERN
 
49
void
 
50
hash_mutex_exit(
 
51
/*============*/
 
52
        hash_table_t*   table,  /*!< in: hash table */
 
53
        ulint           fold)   /*!< in: fold */
 
54
{
 
55
        mutex_exit(hash_get_mutex(table, fold));
 
56
}
 
57
 
 
58
/************************************************************//**
 
59
Reserves all the mutexes of a hash table, in an ascending order. */
 
60
UNIV_INTERN
 
61
void
 
62
hash_mutex_enter_all(
 
63
/*=================*/
 
64
        hash_table_t*   table)  /*!< in: hash table */
 
65
{
 
66
        ulint   i;
 
67
 
 
68
        for (i = 0; i < table->n_mutexes; i++) {
 
69
 
 
70
                mutex_enter(table->mutexes + i);
 
71
        }
 
72
}
 
73
 
 
74
/************************************************************//**
 
75
Releases all the mutexes of a hash table. */
 
76
UNIV_INTERN
 
77
void
 
78
hash_mutex_exit_all(
 
79
/*================*/
 
80
        hash_table_t*   table)  /*!< in: hash table */
 
81
{
 
82
        ulint   i;
 
83
 
 
84
        for (i = 0; i < table->n_mutexes; i++) {
 
85
 
 
86
                mutex_exit(table->mutexes + i);
 
87
        }
 
88
}
 
89
#endif /* !UNIV_HOTBACKUP */
 
90
 
 
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 */
 
95
UNIV_INTERN
 
96
hash_table_t*
 
97
hash_create(
 
98
/*========*/
 
99
        ulint   n)      /*!< in: number of array cells */
 
100
{
 
101
        hash_cell_t*    array;
 
102
        ulint           prime;
 
103
        hash_table_t*   table;
 
104
 
 
105
        prime = ut_find_prime(n);
 
106
 
 
107
        table = mem_alloc(sizeof(hash_table_t));
 
108
 
 
109
        array = ut_malloc(sizeof(hash_cell_t) * prime);
 
110
 
 
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;
 
119
        table->heaps = NULL;
 
120
#endif /* !UNIV_HOTBACKUP */
 
121
        table->heap = NULL;
 
122
        table->magic_n = HASH_TABLE_MAGIC_N;
 
123
 
 
124
        /* Initialize the cell array */
 
125
        hash_table_clear(table);
 
126
 
 
127
        return(table);
 
128
}
 
129
 
 
130
/*************************************************************//**
 
131
Frees a hash table. */
 
132
UNIV_INTERN
 
133
void
 
134
hash_table_free(
 
135
/*============*/
 
136
        hash_table_t*   table)  /*!< in, own: hash table */
 
137
{
 
138
#ifndef UNIV_HOTBACKUP
 
139
        ut_a(table->mutexes == NULL);
 
140
#endif /* !UNIV_HOTBACKUP */
 
141
 
 
142
        ut_free(table->array);
 
143
        mem_free(table);
 
144
}
 
145
 
 
146
#ifndef UNIV_HOTBACKUP
 
147
/*************************************************************//**
 
148
Creates a mutex array to protect a hash table. */
 
149
UNIV_INTERN
 
150
void
 
151
hash_create_mutexes_func(
 
152
/*=====================*/
 
153
        hash_table_t*   table,          /*!< in: hash table */
 
154
#ifdef UNIV_SYNC_DEBUG
 
155
        ulint           sync_level,     /*!< in: latching order level of the
 
156
                                        mutexes: used in the debug version */
 
157
#endif /* UNIV_SYNC_DEBUG */
 
158
        ulint           n_mutexes)      /*!< in: number of mutexes, must be a
 
159
                                        power of 2 */
 
160
{
 
161
        ulint   i;
 
162
 
 
163
        ut_a(n_mutexes > 0);
 
164
        ut_a(ut_is_2pow(n_mutexes));
 
165
 
 
166
        table->mutexes = mem_alloc(n_mutexes * sizeof(mutex_t));
 
167
 
 
168
        for (i = 0; i < n_mutexes; i++) {
 
169
                mutex_create(table->mutexes + i, sync_level);
 
170
        }
 
171
 
 
172
        table->n_mutexes = n_mutexes;
 
173
}
 
174
#endif /* !UNIV_HOTBACKUP */