InnoDB Plugin  1.0
ut0ut.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1994, 2014, 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 /******************************************************************/
26 #ifndef ut0ut_h
27 #define ut0ut_h
28 
29 #include "univ.i"
30 
31 #ifndef UNIV_INNOCHECKSUM
32 
33 #include "db0err.h"
34 
35 #ifndef UNIV_HOTBACKUP
36 # include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
37 #endif /* UNIV_HOTBACKUP */
38 
39 #include <time.h>
40 #ifndef MYSQL_SERVER
41 #include <ctype.h>
42 #endif
43 
44 #include <stdarg.h> /* for va_list */
45 
47 #define TEMP_INDEX_PREFIX '\377'
48 
49 #define TEMP_INDEX_PREFIX_STR "\377"
50 
52 typedef time_t ib_time_t;
53 
54 /* In order to call a piece of code, when a function returns or when the
55 scope ends, use this utility class. It will invoke the given function
56 object in its destructor. */
57 template<typename F>
58 struct ut_when_dtor {
59  ut_when_dtor(F& p) : f(p) {}
60  ~ut_when_dtor() {
61  f();
62  }
63 private:
64  F& f;
65 };
66 
67 #ifndef UNIV_HOTBACKUP
68 # if defined(HAVE_PAUSE_INSTRUCTION)
69  /* According to the gcc info page, asm volatile means that the
70  instruction has important side-effects and must not be removed.
71  Also asm volatile may trigger a memory barrier (spilling all registers
72  to memory). */
73 # ifdef __SUNPRO_CC
74 # define UT_RELAX_CPU() asm ("pause" )
75 # else
76 # define UT_RELAX_CPU() __asm__ __volatile__ ("pause")
77 # endif /* __SUNPRO_CC */
78 
79 # elif defined(HAVE_FAKE_PAUSE_INSTRUCTION)
80 # define UT_RELAX_CPU() __asm__ __volatile__ ("rep; nop")
81 # elif defined(HAVE_ATOMIC_BUILTINS)
82 # define UT_RELAX_CPU() do { \
83  volatile lint volatile_var; \
84  os_compare_and_swap_lint(&volatile_var, 0, 1); \
85  } while (0)
86 # elif defined(HAVE_WINDOWS_ATOMICS)
87  /* In the Win32 API, the x86 PAUSE instruction is executed by calling
88  the YieldProcessor macro defined in WinNT.h. It is a CPU architecture-
89  independent way by using YieldProcessor. */
90 # define UT_RELAX_CPU() YieldProcessor()
91 # else
92 # define UT_RELAX_CPU() ((void)0) /* avoid warning for an empty statement */
93 # endif
94 
95 /*********************************************************************/
100 #define UT_WAIT_FOR(cond, max_wait_us) \
101 do { \
102  ullint start_us; \
103  start_us = ut_time_us(NULL); \
104  while (!(cond) \
105  && ut_time_us(NULL) - start_us < (max_wait_us)) {\
106  \
107  os_thread_sleep(2000 /* 2 ms */); \
108  } \
109 } while (0)
110 #endif /* !UNIV_HOTBACKUP */
111 
112 template <class T> T ut_min(T a, T b) { return(a < b ? a : b); }
113 template <class T> T ut_max(T a, T b) { return(a > b ? a : b); }
114 
115 /******************************************************/
118 UNIV_INLINE
119 ulint
120 ut_min(
121 /*===*/
122  ulint n1,
123  ulint n2);
124 /******************************************************/
127 UNIV_INLINE
128 ulint
129 ut_max(
130 /*===*/
131  ulint n1,
132  ulint n2);
133 /****************************************************************/
135 UNIV_INLINE
136 void
138 /*========*/
139  ulint* a,
140  ulint* b,
141  ulint a1,
142  ulint b1,
143  ulint a2,
144  ulint b2);
145 /******************************************************/
148 UNIV_INLINE
149 int
151 /*=========*/
152  ulint a,
153  ulint b);
154 /*******************************************************/
157 UNIV_INLINE
158 int
160 /*========*/
161  ulint a1,
162  ulint a2,
163  ulint b1,
164  ulint b2);
165 /*************************************************************/
169 #define ut_is_2pow(n) UNIV_LIKELY(!((n) & ((n) - 1)))
170 /*************************************************************/
175 #define ut_2pow_remainder(n, m) ((n) & ((m) - 1))
176 /*************************************************************/
182 #define ut_2pow_round(n, m) ((n) & ~((m) - 1))
183 
187 #define ut_calc_align_down(n, m) ut_2pow_round(n, m)
188 /********************************************************/
194 #define ut_calc_align(n, m) (((n) + ((m) - 1)) & ~((m) - 1))
195 /*************************************************************/
199 UNIV_INLINE
200 ulint
201 ut_2_log(
202 /*=====*/
203  ulint n);
204 /*************************************************************/
207 UNIV_INLINE
208 ulint
209 ut_2_exp(
210 /*=====*/
211  ulint n);
212 /*************************************************************/
215 UNIV_INTERN
216 ulint
218 /*==========*/
219  ulint n)
220  __attribute__((const));
221 
226 #define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)
227 
228 /**********************************************************/
232 UNIV_INTERN
233 ib_time_t
234 ut_time(void);
235 /*=========*/
236 #ifndef UNIV_HOTBACKUP
237 /**********************************************************/
243 UNIV_INTERN
244 int
246 /*========*/
247  ulint* sec,
248  ulint* ms);
250 /**********************************************************/
255 UNIV_INTERN
256 ullint
257 ut_time_us(
258 /*=======*/
259  ullint* tloc);
260 /**********************************************************/
265 UNIV_INTERN
266 ulint
267 ut_time_ms(void);
268 /*============*/
269 #endif /* !UNIV_HOTBACKUP */
270 
271 /**********************************************************/
276 UNIV_INTERN
277 ulint
278 ut_time_ms(void);
279 /*============*/
280 
281 /**********************************************************/
284 UNIV_INTERN
285 double
287 /*========*/
288  ib_time_t time2,
289  ib_time_t time1);
291 #endif /* !UNIV_INNOCHECKSUM */
292 
293 /**********************************************************/
295 UNIV_INTERN
296 void
298 /*===============*/
299  FILE* file)
300  UNIV_COLD __attribute__((nonnull));
301 
302 #ifndef UNIV_INNOCHECKSUM
303 
304 /**********************************************************/
306 UNIV_INTERN
307 void
309 /*=================*/
310  char* buf);
311 #ifdef UNIV_HOTBACKUP
312 /**********************************************************/
315 UNIV_INTERN
316 void
317 ut_sprintf_timestamp_without_extra_chars(
318 /*=====================================*/
319  char* buf);
320 /**********************************************************/
322 UNIV_INTERN
323 void
324 ut_get_year_month_day(
325 /*==================*/
326  ulint* year,
327  ulint* month,
328  ulint* day);
329 #else /* UNIV_HOTBACKUP */
330 /*************************************************************/
334 UNIV_INTERN
335 ulint
336 ut_delay(
337 /*=====*/
338  ulint delay);
339 #endif /* UNIV_HOTBACKUP */
340 /*************************************************************/
342 UNIV_INTERN
343 void
345 /*=========*/
346  FILE* file,
347  const void* buf,
348  ulint len);
350 /**********************************************************************/
352 UNIV_INTERN
353 void
355 /*==============*/
356  FILE* f,
357  const char* name);
359 #ifndef UNIV_HOTBACKUP
360 /* Forward declaration of transaction handle */
361 struct trx_t;
362 
363 /**********************************************************************/
368 UNIV_INTERN
369 void
371 /*==========*/
372  FILE* f,
373  const trx_t* trx,
374  ibool table_id,
376  const char* name);
378 /**********************************************************************/
383 UNIV_INTERN
384 void
386 /*===========*/
387  FILE* f,
388  const trx_t* trx,
389  ibool table_id,
391  const char* name,
392  ulint namelen);
394 /**********************************************************************/
399 UNIV_INTERN
400 char*
402 /*===========*/
403  const char* name,
405  ibool is_table,
407  char* formatted,
409  ulint formatted_size);
412 /**********************************************************************/
414 UNIV_INTERN
415 void
417 /*=========*/
418  FILE* dest,
419  FILE* src);
420 #endif /* !UNIV_HOTBACKUP */
421 
422 #ifdef __WIN__
423 /**********************************************************************/
430 UNIV_INTERN
431 void
433 /*=========*/
434  char* str,
435  size_t size,
436  const char* fmt,
437  va_list ap);
439 /**********************************************************************/
444 UNIV_INTERN
445 int
447 /*========*/
448  char* str,
449  size_t size,
450  const char* fmt,
451  ...);
452 #else
453 /**********************************************************************/
460 # define ut_vsnprintf(buf, size, fmt, ap) \
461  ((void) vsnprintf(buf, size, fmt, ap))
462 /**********************************************************************/
465 # define ut_snprintf snprintf
466 #endif /* __WIN__ */
467 
468 /*************************************************************/
472 UNIV_INTERN
473 const char*
474 ut_strerr(
475 /*======*/
476  dberr_t num);
478 /****************************************************************
479 Sort function for ulint arrays. */
480 UNIV_INTERN
481 void
483 /*==========*/
484  ulint* arr,
485  ulint* aux_arr,
486  ulint low,
487  ulint high)
488  __attribute__((nonnull));
489 
490 #ifndef UNIV_NONINL
491 #include "ut0ut.ic"
492 #endif
493 
494 #endif /* !UNIV_INNOCHECKSUM */
495 
496 #endif
497