1
/*****************************************************************************
3
Copyright (C) 1994, 2010, 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
***********************************************************************/
40
#ifndef UNIV_HOTBACKUP
41
# include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
42
#endif /* UNIV_HOTBACKUP */
49
/** Index name prefix in fast index creation */
50
#define TEMP_INDEX_PREFIX '\377'
51
/** Index name prefix in fast index creation, as a string constant */
52
#define TEMP_INDEX_PREFIX_STR "\377"
55
typedef time_t ib_time_t;
57
#ifndef UNIV_HOTBACKUP
58
#if defined(HAVE_IB_PAUSE_INSTRUCTION)
60
/* In the Win32 API, the x86 PAUSE instruction is executed by calling
61
the YieldProcessor macro defined in WinNT.h. It is a CPU architecture-
62
independent way by using YieldProcessor.*/
63
# define UT_RELAX_CPU() YieldProcessor()
65
/* According to the gcc info page, asm volatile means that the
66
instruction has important side-effects and must not be removed.
67
Also asm volatile may trigger a memory barrier (spilling all registers
69
# define UT_RELAX_CPU() __asm__ __volatile__ ("pause")
71
#elif defined(HAVE_ATOMIC_BUILTINS)
72
# define UT_RELAX_CPU() do { \
73
volatile lint volatile_var; \
74
if (os_compare_and_swap_lint(&volatile_var, 0, 1)) ((void)0); \
77
# define UT_RELAX_CPU() ((void)0) /* avoid warning for an empty statement */
80
/*********************************************************************//**
81
Delays execution for at most max_wait_us microseconds or returns earlier
83
@param cond in: condition to wait for; evaluated every 2 ms
84
@param max_wait_us in: maximum delay to wait, in microseconds */
85
#define UT_WAIT_FOR(cond, max_wait_us) \
88
start_us = ut_time_us(NULL); \
90
&& ut_time_us(NULL) - start_us < (max_wait_us)) {\
92
os_thread_sleep(2000 /* 2 ms */); \
95
#endif /* !UNIV_HOTBACKUP */
97
/********************************************************//**
98
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
99
but since there seem to be compiler bugs in both gcc and Visual C++,
100
we do this by a special conversion.
106
ulint a); /*!< in: ulint */
107
/******************************************************//**
108
Calculates the minimum of two ulints.
114
ulint n1, /*!< in: first number */
115
ulint n2); /*!< in: second number */
116
/******************************************************//**
117
Calculates the maximum of two ulints.
123
ulint n1, /*!< in: first number */
124
ulint n2); /*!< in: second number */
125
/****************************************************************//**
126
Calculates minimum of two ulint-pairs. */
131
ulint* a, /*!< out: more significant part of minimum */
132
ulint* b, /*!< out: less significant part of minimum */
133
ulint a1, /*!< in: more significant part of first pair */
134
ulint b1, /*!< in: less significant part of first pair */
135
ulint a2, /*!< in: more significant part of second pair */
136
ulint b2); /*!< in: less significant part of second pair */
137
/******************************************************//**
139
@return 1 if a > b, 0 if a == b, -1 if a < b */
144
ulint a, /*!< in: ulint */
145
ulint b); /*!< in: ulint */
146
/*******************************************************//**
147
Compares two pairs of ulints.
148
@return -1 if a < b, 0 if a == b, 1 if a > b */
153
ulint a1, /*!< in: more significant part of first pair */
154
ulint a2, /*!< in: less significant part of first pair */
155
ulint b1, /*!< in: more significant part of second pair */
156
ulint b2); /*!< in: less significant part of second pair */
157
/*************************************************************//**
158
Determines if a number is zero or a power of two.
160
@return nonzero if n is zero or a power of two; zero otherwise */
161
#define ut_is_2pow(n) UNIV_LIKELY(!((n) & ((n) - 1)))
162
/*************************************************************//**
163
Calculates fast the remainder of n/m when m is a power of two.
164
@param n in: numerator
165
@param m in: denominator, must be a power of two
166
@return the remainder of n/m */
167
#define ut_2pow_remainder(n, m) ((n) & ((m) - 1))
168
/*************************************************************//**
169
Calculates the biggest multiple of m that is not bigger than n
170
when m is a power of two. In other words, rounds n down to m * k.
171
@param n in: number to round down
172
@param m in: alignment, must be a power of two
173
@return n rounded down to the biggest possible integer multiple of m */
174
#define ut_2pow_round(n, m) ((n) & ~((m) - 1))
175
/** Align a number down to a multiple of a power of two.
176
@param n in: number to round down
177
@param m in: alignment, must be a power of two
178
@return n rounded down to the biggest possible integer multiple of m */
179
#define ut_calc_align_down(n, m) ut_2pow_round(n, m)
180
/********************************************************//**
181
Calculates the smallest multiple of m that is not smaller than n
182
when m is a power of two. In other words, rounds n up to m * k.
183
@param n in: number to round up
184
@param m in: alignment, must be a power of two
185
@return n rounded up to the smallest possible integer multiple of m */
186
#define ut_calc_align(n, m) (((n) + ((m) - 1)) & ~((m) - 1))
187
/*************************************************************//**
188
Calculates fast the 2-logarithm of a number, rounded upward to an
190
@return logarithm in the base 2, rounded upward */
195
ulint n); /*!< in: number */
196
/*************************************************************//**
197
Calculates 2 to power n.
198
@return 2 to power n */
203
ulint n); /*!< in: number */
204
/*************************************************************//**
205
Calculates fast the number rounded up to the nearest power of 2.
206
@return first power of 2 which is >= n */
211
ulint n) /*!< in: number != 0 */
212
__attribute__((const));
214
/** Determine how many bytes (groups of 8 bits) are needed to
215
store the given number of bits.
217
@return number of bytes (octets) needed to represent b */
218
#define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)
220
/**********************************************************//**
221
Returns system time. We do not specify the format of the time returned:
222
the only way to manipulate it is to use the function ut_difftime.
223
@return system time */
228
#ifndef UNIV_HOTBACKUP
229
/**********************************************************//**
231
Upon successful completion, the value 0 is returned; otherwise the
232
value -1 is returned and the global variable errno is set to indicate the
234
@return 0 on success, -1 otherwise */
239
ulint* sec, /*!< out: seconds since the Epoch */
240
ulint* ms); /*!< out: microseconds since the Epoch+*sec */
242
/**********************************************************//**
243
Returns the number of microseconds since epoch. Similar to
244
time(3), the return value is also stored in *tloc, provided
245
that tloc is non-NULL.
246
@return us since epoch */
251
ullint* tloc); /*!< out: us since epoch, if non-NULL */
252
/**********************************************************//**
253
Returns the number of milliseconds since some epoch. The
254
value may wrap around. It should only be used for heuristic
256
@return ms since epoch */
261
#endif /* !UNIV_HOTBACKUP */
263
/**********************************************************//**
264
Returns the difference of two times in seconds.
265
@return time2 - time1 expressed in seconds */
270
ib_time_t time2, /*!< in: time */
271
ib_time_t time1); /*!< in: time */
272
/**********************************************************//**
273
Prints a timestamp to a file. */
278
FILE* file); /*!< in: file where to print */
279
/**********************************************************//**
280
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
283
ut_sprintf_timestamp(
284
/*=================*/
285
char* buf); /*!< in: buffer where to sprintf */
286
#ifdef UNIV_HOTBACKUP
287
/**********************************************************//**
288
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
292
ut_sprintf_timestamp_without_extra_chars(
293
/*=====================================*/
294
char* buf); /*!< in: buffer where to sprintf */
295
/**********************************************************//**
296
Returns current year, month, day. */
299
ut_get_year_month_day(
300
/*==================*/
301
ulint* year, /*!< out: current year */
302
ulint* month, /*!< out: month */
303
ulint* day); /*!< out: day */
304
#else /* UNIV_HOTBACKUP */
305
/*************************************************************//**
306
Runs an idle loop on CPU. The argument gives the desired delay
307
in microseconds on 100 MHz Pentium + Visual C++.
308
@return dummy value */
313
ulint delay); /*!< in: delay in microseconds on 100 MHz Pentium */
314
#endif /* UNIV_HOTBACKUP */
315
/*************************************************************//**
316
Prints the contents of a memory buffer in hex and ascii. */
321
FILE* file, /*!< in: file where to print */
322
const void* buf, /*!< in: memory buffer */
323
ulint len); /*!< in: length of the buffer */
325
/**********************************************************************//**
326
Outputs a NUL-terminated file name, quoted with apostrophes. */
331
FILE* f, /*!< in: output stream */
332
const char* name); /*!< in: name to print */
334
#ifndef UNIV_HOTBACKUP
335
/* Forward declaration of transaction handle */
338
/**********************************************************************//**
339
Outputs a fixed-length string, quoted as an SQL identifier.
340
If the string contains a slash '/', the string will be
341
output as two identifiers separated by a period (.),
342
as in SQL database_name.identifier. */
347
FILE* f, /*!< in: output stream */
348
struct trx_struct*trx, /*!< in: transaction */
349
ibool table_id,/*!< in: TRUE=print a table name,
350
FALSE=print other identifier */
351
const char* name); /*!< in: name to print */
353
/**********************************************************************//**
354
Outputs a fixed-length string, quoted as an SQL identifier.
355
If the string contains a slash '/', the string will be
356
output as two identifiers separated by a period (.),
357
as in SQL database_name.identifier. */
362
FILE* f, /*!< in: output stream */
363
struct trx_struct*trx, /*!< in: transaction (NULL=no quotes) */
364
ibool table_id,/*!< in: TRUE=print a table name,
365
FALSE=print other identifier */
366
const char* name, /*!< in: name to print */
367
ulint namelen);/*!< in: length of name */
369
/**********************************************************************//**
375
FILE* dest, /*!< in: output file */
376
FILE* src); /*!< in: input file to be appended to output */
377
#endif /* !UNIV_HOTBACKUP */
380
/**********************************************************************//**
381
A substitute for snprintf(3), formatted output conversion into
383
@return number of characters that would have been printed if the size
384
were unlimited, not including the terminating '\0'. */
389
char* str, /*!< out: string */
390
size_t size, /*!< in: str size */
391
const char* fmt, /*!< in: format */
392
...); /*!< in: format values */
394
/**********************************************************************//**
395
A wrapper for snprintf(3), formatted output conversion into
397
# define ut_snprintf snprintf
400
/*************************************************************//**
401
Convert an error number to a human readable text message. The
402
returned string is static and should not be freed or modified.
403
@return string, describing the error */
408
enum db_err num); /*!< in: error number */