MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
misc.cpp
1 /*
2  Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 /* based on Wei Dai's misc.cpp from CryptoPP */
20 
21 
22 #include "runtime.hpp"
23 #include "misc.hpp"
24 
25 
26 #ifdef __GNUC__
27  #include <signal.h>
28  #include <setjmp.h>
29 #endif
30 
31 #ifdef USE_SYS_STL
32  #include <algorithm>
33 #else
34  #include "algorithm.hpp"
35 #endif
36 
37 namespace STL = STL_NAMESPACE;
38 
39 
40 #ifdef YASSL_PURE_C
41 
42  void* operator new(size_t sz, TaoCrypt::new_t)
43  {
44  void* ptr = malloc(sz ? sz : 1);
45  if (!ptr) abort();
46 
47  return ptr;
48  }
49 
50 
51  void operator delete(void* ptr, TaoCrypt::new_t)
52  {
53  if (ptr) free(ptr);
54  }
55 
56 
57  void* operator new[](size_t sz, TaoCrypt::new_t nt)
58  {
59  return ::operator new(sz, nt);
60  }
61 
62 
63  void operator delete[](void* ptr, TaoCrypt::new_t nt)
64  {
65  ::operator delete(ptr, nt);
66  }
67 
68 
69  /* uncomment to test
70  // make sure not using globals anywhere by forgetting to use overloaded
71  void* operator new(size_t sz);
72 
73  void operator delete(void* ptr);
74 
75  void* operator new[](size_t sz);
76 
77  void operator delete[](void* ptr);
78  */
79 
80 
81  namespace TaoCrypt {
82 
83  new_t tc; // for library new
84 
85  }
86 
87 #ifdef __sun
88 
89 // Handler for pure virtual functions
90 namespace __Crun {
91  void pure_error() {
92  }
93 }
94 
95 #endif
96 
97 #if defined(__ICC) || defined(__INTEL_COMPILER) || (__GNUC__ > 2)
98 
99 extern "C" {
100 
101  int __cxa_pure_virtual() {
102  return 0;
103  }
104 
105 } // extern "C"
106 
107 #endif
108 
109 #endif // YASSL_PURE_C
110 
111 
112 namespace TaoCrypt {
113 
114 
115 inline void XorWords(word* r, const word* a, unsigned int n)
116 {
117  for (unsigned int i=0; i<n; i++)
118  r[i] ^= a[i];
119 }
120 
121 
122 void xorbuf(byte* buf, const byte* mask, unsigned int count)
123 {
124  if (((size_t)buf | (size_t)mask | count) % WORD_SIZE == 0)
125  XorWords((word *)buf, (const word *)mask, count/WORD_SIZE);
126  else
127  {
128  for (unsigned int i=0; i<count; i++)
129  buf[i] ^= mask[i];
130  }
131 }
132 
133 
134 unsigned int BytePrecision(word value)
135 {
136  unsigned int i;
137  for (i=sizeof(value); i; --i)
138  if (value >> (i-1)*8)
139  break;
140 
141  return i;
142 }
143 
144 
145 unsigned int BitPrecision(word value)
146 {
147  if (!value)
148  return 0;
149 
150  unsigned int l = 0,
151  h = 8 * sizeof(value);
152 
153  while (h-l > 1)
154  {
155  unsigned int t = (l+h)/2;
156  if (value >> t)
157  l = t;
158  else
159  h = t;
160  }
161 
162  return h;
163 }
164 
165 
166 word Crop(word value, unsigned int size)
167 {
168  if (size < 8*sizeof(value))
169  return (value & ((1L << size) - 1));
170  else
171  return value;
172 }
173 
174 
175 
176 #ifdef TAOCRYPT_X86ASM_AVAILABLE
177 
178 
179 bool HaveCpuId()
180 {
181 #ifdef _MSC_VER
182  __try
183  {
184  __asm
185  {
186  mov eax, 0
187  cpuid
188  }
189  }
190  __except (1)
191  {
192  return false;
193  }
194  return true;
195 #else
196  word32 eax, ebx;
197  __asm__ __volatile
198  (
199  /* Put EFLAGS in eax and ebx */
200  "pushf;"
201  "pushf;"
202  "pop %0;"
203  "movl %0,%1;"
204 
205  /* Flip the cpuid bit and store back in EFLAGS */
206  "xorl $0x200000,%0;"
207  "push %0;"
208  "popf;"
209 
210  /* Read EFLAGS again */
211  "pushf;"
212  "pop %0;"
213  "popf"
214  : "=r" (eax), "=r" (ebx)
215  :
216  : "cc"
217  );
218 
219  if (eax == ebx)
220  return false;
221  return true;
222 #endif
223 }
224 
225 
226 void CpuId(word32 input, word32 *output)
227 {
228 #ifdef __GNUC__
229  __asm__
230  (
231  // save ebx in case -fPIC is being used
232  "push %%ebx; cpuid; mov %%ebx, %%edi; pop %%ebx"
233  : "=a" (output[0]), "=D" (output[1]), "=c" (output[2]), "=d"(output[3])
234  : "a" (input)
235  );
236 #else
237  __asm
238  {
239  mov eax, input
240  cpuid
241  mov edi, output
242  mov [edi], eax
243  mov [edi+4], ebx
244  mov [edi+8], ecx
245  mov [edi+12], edx
246  }
247 #endif
248 }
249 
250 
251 bool IsPentium()
252 {
253  if (!HaveCpuId())
254  return false;
255 
256  word32 cpuid[4];
257 
258  CpuId(0, cpuid);
259  STL::swap(cpuid[2], cpuid[3]);
260  if (memcmp(cpuid+1, "GenuineIntel", 12) != 0)
261  return false;
262 
263  CpuId(1, cpuid);
264  byte family = ((cpuid[0] >> 8) & 0xf);
265  if (family < 5)
266  return false;
267 
268  return true;
269 }
270 
271 
272 
273 static bool IsMmx()
274 {
275  if (!IsPentium())
276  return false;
277 
278  word32 cpuid[4];
279 
280  CpuId(1, cpuid);
281  if ((cpuid[3] & (1 << 23)) == 0)
282  return false;
283 
284  return true;
285 }
286 
287 
288 bool isMMX = IsMmx();
289 
290 
291 #endif // TAOCRYPT_X86ASM_AVAILABLE
292 
293 
294 
295 
296 } // namespace
297