~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/include/ut0ut.h

  • Committer: Brian Aker
  • Date: 2009-12-29 01:38:38 UTC
  • mfrom: (1251.1.1 drizzle)
  • Revision ID: brian@gaz-20091229013838-03kb2z5xbqw03ddt
Merge of Diego fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 
 
3
Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
 
4
Copyright (c) 2009, Sun Microsystems, Inc.
 
5
 
 
6
Portions of this file contain modifications contributed and copyrighted by
 
7
Sun Microsystems, Inc. Those modifications are gratefully acknowledged and
 
8
are described briefly in the InnoDB documentation. The contributions by
 
9
Sun Microsystems are incorporated with their permission, and subject to the
 
10
conditions contained in the file COPYING.Sun_Microsystems.
 
11
 
 
12
This program is free software; you can redistribute it and/or modify it under
 
13
the terms of the GNU General Public License as published by the Free Software
 
14
Foundation; version 2 of the License.
 
15
 
 
16
This program is distributed in the hope that it will be useful, but WITHOUT
 
17
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
18
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
19
 
 
20
You should have received a copy of the GNU General Public License along with
 
21
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
22
Place, Suite 330, Boston, MA 02111-1307 USA
 
23
 
 
24
*****************************************************************************/
 
25
 
 
26
/******************************************************************//**
 
27
@file include/ut0ut.h
 
28
Various utilities
 
29
 
 
30
Created 1/20/1994 Heikki Tuuri
 
31
***********************************************************************/
 
32
 
 
33
#ifndef ut0ut_h
 
34
#define ut0ut_h
 
35
 
 
36
#include "univ.i"
 
37
#include <time.h>
 
38
#ifndef MYSQL_SERVER
 
39
#include <ctype.h>
 
40
#endif
 
41
 
 
42
/** Index name prefix in fast index creation */
 
43
#define TEMP_INDEX_PREFIX       '\377'
 
44
/** Index name prefix in fast index creation, as a string constant */
 
45
#define TEMP_INDEX_PREFIX_STR   "\377"
 
46
 
 
47
/** Time stamp */
 
48
typedef time_t  ib_time_t;
 
49
 
 
50
#if defined(IB_HAVE_PAUSE_INSTRUCTION)
 
51
#  ifdef WIN32
 
52
     /* In the Win32 API, the x86 PAUSE instruction is executed by calling
 
53
     the YieldProcessor macro defined in WinNT.h. It is a CPU architecture-
 
54
     independent way by using YieldProcessor.*/
 
55
#    define UT_RELAX_CPU() YieldProcessor()
 
56
#  else
 
57
     /* According to the gcc info page, asm volatile means that the
 
58
     instruction has important side-effects and must not be removed.
 
59
     Also asm volatile may trigger a memory barrier (spilling all registers
 
60
     to memory). */
 
61
#    define UT_RELAX_CPU() __asm__ __volatile__ ("pause")
 
62
#  endif
 
63
#elif defined(HAVE_ATOMIC_BUILTINS)
 
64
#  define UT_RELAX_CPU() do { \
 
65
     volatile lint      volatile_var; \
 
66
     os_compare_and_swap_lint(&volatile_var, 0, 1); \
 
67
   } while (0)
 
68
#else
 
69
#  define UT_RELAX_CPU() ((void)0) /* avoid warning for an empty statement */
 
70
#endif
 
71
 
 
72
/*********************************************************************//**
 
73
Delays execution for at most max_wait_us microseconds or returns earlier
 
74
if cond becomes true.
 
75
@param cond             in: condition to wait for; evaluated every 2 ms
 
76
@param max_wait_us      in: maximum delay to wait, in microseconds */
 
77
#define UT_WAIT_FOR(cond, max_wait_us)                          \
 
78
do {                                                            \
 
79
        ullint  start_us;                                       \
 
80
        start_us = ut_time_us(NULL);                            \
 
81
        while (!(cond)                                          \
 
82
               && ut_time_us(NULL) - start_us < (max_wait_us)) {\
 
83
                                                                \
 
84
                os_thread_sleep(2000 /* 2 ms */);               \
 
85
        }                                                       \
 
86
} while (0)
 
87
 
 
88
/********************************************************//**
 
89
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
 
90
but since there seem to be compiler bugs in both gcc and Visual C++,
 
91
we do this by a special conversion.
 
92
@return a >> 32 */
 
93
UNIV_INTERN
 
94
ulint
 
95
ut_get_high32(
 
96
/*==========*/
 
97
        ulint   a);     /*!< in: ulint */
 
98
/******************************************************//**
 
99
Calculates the minimum of two ulints.
 
100
@return minimum */
 
101
UNIV_INLINE
 
102
ulint
 
103
ut_min(
 
104
/*===*/
 
105
        ulint    n1,    /*!< in: first number */
 
106
        ulint    n2);   /*!< in: second number */
 
107
/******************************************************//**
 
108
Calculates the maximum of two ulints.
 
109
@return maximum */
 
110
UNIV_INLINE
 
111
ulint
 
112
ut_max(
 
113
/*===*/
 
114
        ulint    n1,    /*!< in: first number */
 
115
        ulint    n2);   /*!< in: second number */
 
116
/****************************************************************//**
 
117
Calculates minimum of two ulint-pairs. */
 
118
UNIV_INLINE
 
119
void
 
120
ut_pair_min(
 
121
/*========*/
 
122
        ulint*  a,      /*!< out: more significant part of minimum */
 
123
        ulint*  b,      /*!< out: less significant part of minimum */
 
124
        ulint   a1,     /*!< in: more significant part of first pair */
 
125
        ulint   b1,     /*!< in: less significant part of first pair */
 
126
        ulint   a2,     /*!< in: more significant part of second pair */
 
127
        ulint   b2);    /*!< in: less significant part of second pair */
 
128
/******************************************************//**
 
129
Compares two ulints.
 
130
@return 1 if a > b, 0 if a == b, -1 if a < b */
 
131
UNIV_INLINE
 
132
int
 
133
ut_ulint_cmp(
 
134
/*=========*/
 
135
        ulint   a,      /*!< in: ulint */
 
136
        ulint   b);     /*!< in: ulint */
 
137
/*******************************************************//**
 
138
Compares two pairs of ulints.
 
139
@return -1 if a < b, 0 if a == b, 1 if a > b */
 
140
UNIV_INLINE
 
141
int
 
142
ut_pair_cmp(
 
143
/*========*/
 
144
        ulint   a1,     /*!< in: more significant part of first pair */
 
145
        ulint   a2,     /*!< in: less significant part of first pair */
 
146
        ulint   b1,     /*!< in: more significant part of second pair */
 
147
        ulint   b2);    /*!< in: less significant part of second pair */
 
148
/*************************************************************//**
 
149
Determines if a number is zero or a power of two.
 
150
@param n        in: number
 
151
@return         nonzero if n is zero or a power of two; zero otherwise */
 
152
#define ut_is_2pow(n) UNIV_LIKELY(!((n) & ((n) - 1)))
 
153
/*************************************************************//**
 
154
Calculates fast the remainder of n/m when m is a power of two.
 
155
@param n        in: numerator
 
156
@param m        in: denominator, must be a power of two
 
157
@return         the remainder of n/m */
 
158
#define ut_2pow_remainder(n, m) ((n) & ((m) - 1))
 
159
/*************************************************************//**
 
160
Calculates the biggest multiple of m that is not bigger than n
 
161
when m is a power of two.  In other words, rounds n down to m * k.
 
162
@param n        in: number to round down
 
163
@param m        in: alignment, must be a power of two
 
164
@return         n rounded down to the biggest possible integer multiple of m */
 
165
#define ut_2pow_round(n, m) ((n) & ~((m) - 1))
 
166
/** Align a number down to a multiple of a power of two.
 
167
@param n        in: number to round down
 
168
@param m        in: alignment, must be a power of two
 
169
@return         n rounded down to the biggest possible integer multiple of m */
 
170
#define ut_calc_align_down(n, m) ut_2pow_round(n, m)
 
171
/********************************************************//**
 
172
Calculates the smallest multiple of m that is not smaller than n
 
173
when m is a power of two.  In other words, rounds n up to m * k.
 
174
@param n        in: number to round up
 
175
@param m        in: alignment, must be a power of two
 
176
@return         n rounded up to the smallest possible integer multiple of m */
 
177
#define ut_calc_align(n, m) (((n) + ((m) - 1)) & ~((m) - 1))
 
178
/*************************************************************//**
 
179
Calculates fast the 2-logarithm of a number, rounded upward to an
 
180
integer.
 
181
@return logarithm in the base 2, rounded upward */
 
182
UNIV_INLINE
 
183
ulint
 
184
ut_2_log(
 
185
/*=====*/
 
186
        ulint   n);     /*!< in: number */
 
187
/*************************************************************//**
 
188
Calculates 2 to power n.
 
189
@return 2 to power n */
 
190
UNIV_INLINE
 
191
ulint
 
192
ut_2_exp(
 
193
/*=====*/
 
194
        ulint   n);     /*!< in: number */
 
195
/*************************************************************//**
 
196
Calculates fast the number rounded up to the nearest power of 2.
 
197
@return first power of 2 which is >= n */
 
198
UNIV_INTERN
 
199
ulint
 
200
ut_2_power_up(
 
201
/*==========*/
 
202
        ulint   n)      /*!< in: number != 0 */
 
203
        __attribute__((const));
 
204
 
 
205
/** Determine how many bytes (groups of 8 bits) are needed to
 
206
store the given number of bits.
 
207
@param b        in: bits
 
208
@return         number of bytes (octets) needed to represent b */
 
209
#define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)
 
210
 
 
211
/**********************************************************//**
 
212
Returns system time. We do not specify the format of the time returned:
 
213
the only way to manipulate it is to use the function ut_difftime.
 
214
@return system time */
 
215
UNIV_INTERN
 
216
ib_time_t
 
217
ut_time(void);
 
218
/*=========*/
 
219
/**********************************************************//**
 
220
Returns system time.
 
221
Upon successful completion, the value 0 is returned; otherwise the
 
222
value -1 is returned and the global variable errno is set to indicate the
 
223
error.
 
224
@return 0 on success, -1 otherwise */
 
225
UNIV_INTERN
 
226
int
 
227
ut_usectime(
 
228
/*========*/
 
229
        ulint*  sec,    /*!< out: seconds since the Epoch */
 
230
        ulint*  ms);    /*!< out: microseconds since the Epoch+*sec */
 
231
 
 
232
/**********************************************************//**
 
233
Returns the number of microseconds since epoch. Similar to
 
234
time(3), the return value is also stored in *tloc, provided
 
235
that tloc is non-NULL.
 
236
@return us since epoch */
 
237
UNIV_INTERN
 
238
ullint
 
239
ut_time_us(
 
240
/*=======*/
 
241
        ullint* tloc);  /*!< out: us since epoch, if non-NULL */
 
242
 
 
243
/**********************************************************//**
 
244
Returns the difference of two times in seconds.
 
245
@return time2 - time1 expressed in seconds */
 
246
UNIV_INTERN
 
247
double
 
248
ut_difftime(
 
249
/*========*/
 
250
        ib_time_t       time2,  /*!< in: time */
 
251
        ib_time_t       time1); /*!< in: time */
 
252
/**********************************************************//**
 
253
Prints a timestamp to a file. */
 
254
UNIV_INTERN
 
255
void
 
256
ut_print_timestamp(
 
257
/*===============*/
 
258
        FILE*  file); /*!< in: file where to print */
 
259
/**********************************************************//**
 
260
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
 
261
UNIV_INTERN
 
262
void
 
263
ut_sprintf_timestamp(
 
264
/*=================*/
 
265
        char*   buf); /*!< in: buffer where to sprintf */
 
266
#ifdef UNIV_HOTBACKUP
 
267
/**********************************************************//**
 
268
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
 
269
replaced by '_'. */
 
270
UNIV_INTERN
 
271
void
 
272
ut_sprintf_timestamp_without_extra_chars(
 
273
/*=====================================*/
 
274
        char*   buf); /*!< in: buffer where to sprintf */
 
275
/**********************************************************//**
 
276
Returns current year, month, day. */
 
277
UNIV_INTERN
 
278
void
 
279
ut_get_year_month_day(
 
280
/*==================*/
 
281
        ulint*  year,   /*!< out: current year */
 
282
        ulint*  month,  /*!< out: month */
 
283
        ulint*  day);   /*!< out: day */
 
284
#else /* UNIV_HOTBACKUP */
 
285
/*************************************************************//**
 
286
Runs an idle loop on CPU. The argument gives the desired delay
 
287
in microseconds on 100 MHz Pentium + Visual C++.
 
288
@return dummy value */
 
289
UNIV_INTERN
 
290
ulint
 
291
ut_delay(
 
292
/*=====*/
 
293
        ulint   delay); /*!< in: delay in microseconds on 100 MHz Pentium */
 
294
#endif /* UNIV_HOTBACKUP */
 
295
/*************************************************************//**
 
296
Prints the contents of a memory buffer in hex and ascii. */
 
297
UNIV_INTERN
 
298
void
 
299
ut_print_buf(
 
300
/*=========*/
 
301
        FILE*           file,   /*!< in: file where to print */
 
302
        const void*     buf,    /*!< in: memory buffer */
 
303
        ulint           len);   /*!< in: length of the buffer */
 
304
 
 
305
/**********************************************************************//**
 
306
Outputs a NUL-terminated file name, quoted with apostrophes. */
 
307
UNIV_INTERN
 
308
void
 
309
ut_print_filename(
 
310
/*==============*/
 
311
        FILE*           f,      /*!< in: output stream */
 
312
        const char*     name);  /*!< in: name to print */
 
313
 
 
314
#ifndef UNIV_HOTBACKUP
 
315
/* Forward declaration of transaction handle */
 
316
struct trx_struct;
 
317
 
 
318
/**********************************************************************//**
 
319
Outputs a fixed-length string, quoted as an SQL identifier.
 
320
If the string contains a slash '/', the string will be
 
321
output as two identifiers separated by a period (.),
 
322
as in SQL database_name.identifier. */
 
323
UNIV_INTERN
 
324
void
 
325
ut_print_name(
 
326
/*==========*/
 
327
        FILE*           f,      /*!< in: output stream */
 
328
        struct trx_struct*trx,  /*!< in: transaction */
 
329
        ibool           table_id,/*!< in: TRUE=print a table name,
 
330
                                FALSE=print other identifier */
 
331
        const char*     name);  /*!< in: name to print */
 
332
 
 
333
/**********************************************************************//**
 
334
Outputs a fixed-length string, quoted as an SQL identifier.
 
335
If the string contains a slash '/', the string will be
 
336
output as two identifiers separated by a period (.),
 
337
as in SQL database_name.identifier. */
 
338
UNIV_INTERN
 
339
void
 
340
ut_print_namel(
 
341
/*===========*/
 
342
        FILE*           f,      /*!< in: output stream */
 
343
        struct trx_struct*trx,  /*!< in: transaction (NULL=no quotes) */
 
344
        ibool           table_id,/*!< in: TRUE=print a table name,
 
345
                                FALSE=print other identifier */
 
346
        const char*     name,   /*!< in: name to print */
 
347
        ulint           namelen);/*!< in: length of name */
 
348
 
 
349
/**********************************************************************//**
 
350
Catenate files. */
 
351
UNIV_INTERN
 
352
void
 
353
ut_copy_file(
 
354
/*=========*/
 
355
        FILE*   dest,   /*!< in: output file */
 
356
        FILE*   src);   /*!< in: input file to be appended to output */
 
357
#endif /* !UNIV_HOTBACKUP */
 
358
 
 
359
#ifdef __WIN__
 
360
/**********************************************************************//**
 
361
A substitute for snprintf(3), formatted output conversion into
 
362
a limited buffer.
 
363
@return number of characters that would have been printed if the size
 
364
were unlimited, not including the terminating '\0'. */
 
365
UNIV_INTERN
 
366
int
 
367
ut_snprintf(
 
368
/*========*/
 
369
        char*           str,    /*!< out: string */
 
370
        size_t          size,   /*!< in: str size */
 
371
        const char*     fmt,    /*!< in: format */
 
372
        ...);                   /*!< in: format values */
 
373
#else
 
374
/**********************************************************************//**
 
375
A wrapper for snprintf(3), formatted output conversion into
 
376
a limited buffer. */
 
377
# define ut_snprintf    snprintf
 
378
#endif /* __WIN__ */
 
379
 
 
380
#ifndef UNIV_NONINL
 
381
#include "ut0ut.ic"
 
382
#endif
 
383
 
 
384
#endif
 
385