1
/*****************************************************************************
3
Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
4
Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
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.
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
24
*****************************************************************************/
26
/******************************************************************//**
30
Created 1/20/1994 Heikki Tuuri
31
***********************************************************************/
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"
48
typedef time_t ib_time_t;
50
#if defined(IB_HAVE_PAUSE_INSTRUCTION)
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()
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
61
# define UT_RELAX_CPU() __asm__ __volatile__ ("pause")
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); \
69
# define UT_RELAX_CPU() ((void)0) /* avoid warning for an empty statement */
72
/*********************************************************************//**
73
Delays execution for at most max_wait_us microseconds or returns earlier
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) \
80
start_us = ut_time_us(NULL); \
82
&& ut_time_us(NULL) - start_us < (max_wait_us)) {\
84
os_thread_sleep(2000 /* 2 ms */); \
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.
97
ulint a); /*!< in: ulint */
98
/******************************************************//**
99
Calculates the minimum of two ulints.
105
ulint n1, /*!< in: first number */
106
ulint n2); /*!< in: second number */
107
/******************************************************//**
108
Calculates the maximum of two ulints.
114
ulint n1, /*!< in: first number */
115
ulint n2); /*!< in: second number */
116
/****************************************************************//**
117
Calculates minimum of two ulint-pairs. */
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
/******************************************************//**
130
@return 1 if a > b, 0 if a == b, -1 if a < b */
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 */
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.
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
181
@return logarithm in the base 2, rounded upward */
186
ulint n); /*!< in: number */
187
/*************************************************************//**
188
Calculates 2 to power n.
189
@return 2 to power n */
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 */
202
ulint n) /*!< in: number != 0 */
203
__attribute__((const));
205
/** Determine how many bytes (groups of 8 bits) are needed to
206
store the given number of bits.
208
@return number of bytes (octets) needed to represent b */
209
#define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)
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 */
219
/**********************************************************//**
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
224
@return 0 on success, -1 otherwise */
229
ulint* sec, /*!< out: seconds since the Epoch */
230
ulint* ms); /*!< out: microseconds since the Epoch+*sec */
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 */
241
ullint* tloc); /*!< out: us since epoch, if non-NULL */
243
/**********************************************************//**
244
Returns the difference of two times in seconds.
245
@return time2 - time1 expressed in seconds */
250
ib_time_t time2, /*!< in: time */
251
ib_time_t time1); /*!< in: time */
252
/**********************************************************//**
253
Prints a timestamp to a file. */
258
FILE* file); /*!< in: file where to print */
259
/**********************************************************//**
260
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
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
272
ut_sprintf_timestamp_without_extra_chars(
273
/*=====================================*/
274
char* buf); /*!< in: buffer where to sprintf */
275
/**********************************************************//**
276
Returns current year, month, day. */
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 */
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. */
301
FILE* file, /*!< in: file where to print */
302
const void* buf, /*!< in: memory buffer */
303
ulint len); /*!< in: length of the buffer */
305
/**********************************************************************//**
306
Outputs a NUL-terminated file name, quoted with apostrophes. */
311
FILE* f, /*!< in: output stream */
312
const char* name); /*!< in: name to print */
314
#ifndef UNIV_HOTBACKUP
315
/* Forward declaration of transaction handle */
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. */
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 */
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. */
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 */
349
/**********************************************************************//**
355
FILE* dest, /*!< in: output file */
356
FILE* src); /*!< in: input file to be appended to output */
357
#endif /* !UNIV_HOTBACKUP */
360
/**********************************************************************//**
361
A substitute for snprintf(3), formatted output conversion into
363
@return number of characters that would have been printed if the size
364
were unlimited, not including the terminating '\0'. */
369
char* str, /*!< out: string */
370
size_t size, /*!< in: str size */
371
const char* fmt, /*!< in: format */
372
...); /*!< in: format values */
374
/**********************************************************************//**
375
A wrapper for snprintf(3), formatted output conversion into
377
# define ut_snprintf snprintf