~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2008-12-06 22:41:03 UTC
  • mto: (656.1.7 devel)
  • mto: This revision was merged to the branch mainline in revision 665.
  • Revision ID: monty@inaugust.com-20081206224103-jdouqwt9hb0f01y1
Moved non-working tests into broken suite for easier running of working tests.

Show diffs side-by-side

added added

removed removed

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