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

« back to all changes in this revision

Viewing changes to storage/innodb_plugin/include/ut0ut.h

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