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., 51 Franklin
22
St, Fifth Floor, Boston, MA 02110-1301 USA
24
*****************************************************************************/
26
/******************************************************************//**
30
Created 1/20/1994 Heikki Tuuri
31
***********************************************************************/
38
#ifndef UNIV_HOTBACKUP
39
# include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
40
#endif /* UNIV_HOTBACKUP */
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"
53
typedef time_t ib_time_t;
55
#ifndef UNIV_HOTBACKUP
56
#if defined(HAVE_IB_PAUSE_INSTRUCTION)
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()
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
67
# define UT_RELAX_CPU() __asm__ __volatile__ ("pause")
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); \
75
# define UT_RELAX_CPU() ((void)0) /* avoid warning for an empty statement */
78
/*********************************************************************//**
79
Delays execution for at most max_wait_us microseconds or returns earlier
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) \
86
start_us = ut_time_us(NULL); \
88
&& ut_time_us(NULL) - start_us < (max_wait_us)) {\
90
os_thread_sleep(2000 /* 2 ms */); \
93
#endif /* !UNIV_HOTBACKUP */
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.
104
ulint a); /*!< in: ulint */
105
/******************************************************//**
106
Calculates the minimum of two ulints.
112
ulint n1, /*!< in: first number */
113
ulint n2); /*!< in: second number */
114
/******************************************************//**
115
Calculates the maximum of two ulints.
121
ulint n1, /*!< in: first number */
122
ulint n2); /*!< in: second number */
123
/****************************************************************//**
124
Calculates minimum of two ulint-pairs. */
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
/******************************************************//**
137
@return 1 if a > b, 0 if a == b, -1 if a < b */
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 */
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.
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
188
@return logarithm in the base 2, rounded upward */
193
ulint n); /*!< in: number */
194
/*************************************************************//**
195
Calculates 2 to power n.
196
@return 2 to power n */
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 */
209
ulint n) /*!< in: number != 0 */
210
__attribute__((const));
212
/** Determine how many bytes (groups of 8 bits) are needed to
213
store the given number of bits.
215
@return number of bytes (octets) needed to represent b */
216
#define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)
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 */
226
#ifndef UNIV_HOTBACKUP
227
/**********************************************************//**
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
232
@return 0 on success, -1 otherwise */
237
ulint* sec, /*!< out: seconds since the Epoch */
238
ulint* ms); /*!< out: microseconds since the Epoch+*sec */
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 */
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
254
@return ms since epoch */
259
#endif /* !UNIV_HOTBACKUP */
261
/**********************************************************//**
262
Returns the difference of two times in seconds.
263
@return time2 - time1 expressed in seconds */
268
ib_time_t time2, /*!< in: time */
269
ib_time_t time1); /*!< in: time */
270
/**********************************************************//**
271
Prints a timestamp to a file. */
276
FILE* file); /*!< in: file where to print */
277
/**********************************************************//**
278
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
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
290
ut_sprintf_timestamp_without_extra_chars(
291
/*=====================================*/
292
char* buf); /*!< in: buffer where to sprintf */
293
/**********************************************************//**
294
Returns current year, month, day. */
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 */
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. */
319
FILE* file, /*!< in: file where to print */
320
const void* buf, /*!< in: memory buffer */
321
ulint len); /*!< in: length of the buffer */
323
/**********************************************************************//**
324
Outputs a NUL-terminated file name, quoted with apostrophes. */
329
FILE* f, /*!< in: output stream */
330
const char* name); /*!< in: name to print */
332
#ifndef UNIV_HOTBACKUP
333
/* Forward declaration of transaction handle */
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. */
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 */
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. */
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 */
367
/**********************************************************************//**
373
FILE* dest, /*!< in: output file */
374
FILE* src); /*!< in: input file to be appended to output */
375
#endif /* !UNIV_HOTBACKUP */
378
/**********************************************************************//**
379
A substitute for snprintf(3), formatted output conversion into
381
@return number of characters that would have been printed if the size
382
were unlimited, not including the terminating '\0'. */
387
char* str, /*!< out: string */
388
size_t size, /*!< in: str size */
389
const char* fmt, /*!< in: format */
390
...); /*!< in: format values */
392
/**********************************************************************//**
393
A wrapper for snprintf(3), formatted output conversion into
395
# define ut_snprintf snprintf