MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
innodb_utility.c
1 /*****************************************************************************
2 
3 Copyright (c) 1994, 2009, Oracle and/or its affiliates. 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.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 /**************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include "innodb_utility.h"
30 
31 #define UT_HASH_RANDOM_MASK 1463735687
32 #define UT_HASH_RANDOM_MASK2 1653893711
33 #define UT_RANDOM_1 1.0412321
34 #define UT_RANDOM_2 1.1131347
35 #define UT_RANDOM_3 1.0132677
36 
37 /*************************************************************/
40 static
42 ut_fold_ib_ulint_t_pair(
43 /*====================*/
44  ib_ulint_t n1,
45  ib_ulint_t n2)
46 {
47  return(((((n1 ^ n2 ^ UT_HASH_RANDOM_MASK2) << 8) + n1)
48  ^ UT_HASH_RANDOM_MASK) + n2);
49 }
50 
51 /*************************************************************/
55 ut_fold_string(
56 /*===========*/
57  const char* str)
58 {
59  ib_ulint_t fold = 0;
60 
61  while (*str != '\0') {
62  fold = ut_fold_ib_ulint_t_pair(fold, (ib_ulint_t)(*str));
63  str++;
64  }
65 
66  return(fold);
67 }
68 
69 /***********************************************************/
73 static
76 /*==========*/
77  ib_ulint_t n)
78 {
79  ib_ulint_t pow2;
80  ib_ulint_t i;
81 
82  n += 100;
83 
84  pow2 = 1;
85  while (pow2 * 2 < n) {
86  pow2 = 2 * pow2;
87  }
88 
89  if ((double) n < 1.05 * (double) pow2) {
90  n = (ib_ulint_t) ((double) n * UT_RANDOM_1);
91  }
92 
93  pow2 = 2 * pow2;
94 
95  if ((double) n > 0.95 * (double) pow2) {
96  n = (ib_ulint_t) ((double) n * UT_RANDOM_2);
97  }
98 
99  if (n > pow2 - 20) {
100  n += 30;
101  }
102 
103  /* Now we have n far enough from powers of 2. To make
104  n more random (especially, if it was not near
105  a power of 2), we then multiply it by a random number. */
106 
107  n = (ib_ulint_t) ((double) n * UT_RANDOM_3);
108 
109  for (;; n++) {
110  i = 2;
111  while (i * i <= n) {
112  if (n % i == 0) {
113  goto next_n;
114  }
115  i++;
116  }
117 
118  /* Found a prime */
119  break;
120 next_n: ;
121  }
122 
123  return(n);
124 }
125 
126 /*************************************************************/
131 hash_create(
132 /*========*/
133  ib_ulint_t n)
134 {
135  hash_cell_t* array;
136  ib_ulint_t prime;
138 
139  prime = ut_find_prime(n);
140 
141  table = (hash_table_t*) malloc(sizeof(hash_table_t));
142 
143  array = (hash_cell_t*) malloc(sizeof(hash_cell_t) * prime);
144 
145  /* The default type of hash_table is HASH_TABLE_SYNC_NONE i.e.:
146  the caller is responsible for access control to the table. */
147  table->array = array;
148  table->n_cells = prime;
149 
150  /* Initialize the cell array */
151  memset(table->array, 0x0, table->n_cells * sizeof(*table->array));
152 
153  return(table);
154 }
155 
156 /*******************************************************/
161 static
164 /*==========*/
165  ib_ulint_t key,
166  ib_ulint_t table_size)
167 {
168  key = key ^ UT_HASH_RANDOM_MASK2;
169 
170  return(key % table_size);
171 }
172 
173 /**************************************************************/
178 /*===========*/
179  ib_ulint_t fold,
180  hash_table_t* table)
181 {
182  return(ut_hash_ulint(fold, table->n_cells));
183 }
184 
185 /************************************************************/
188 inline
191 /*==============*/
192  hash_table_t* table,
193  ib_ulint_t n)
194 {
195  return(table->array + n);
196 }
197