~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2008-09-16 00:00:48 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916000048-3rvrv3gv9l0ad3gs
Fixed copyright headers in drizzled/

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
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
 
#include "db0err.h"
39
 
 
40
 
#ifndef UNIV_HOTBACKUP
41
 
# include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
42
 
#endif /* UNIV_HOTBACKUP */
43
 
 
44
 
#include <time.h>
45
 
#ifndef MYSQL_SERVER
46
 
#include <ctype.h>
47
 
#endif
48
 
 
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"
53
 
 
54
 
/** Time stamp */
55
 
typedef time_t  ib_time_t;
56
 
 
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
 
/*********************************************************************//**
81
 
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 */
85
 
#define UT_WAIT_FOR(cond, max_wait_us)                          \
86
 
do {                                                            \
87
 
        ullint  start_us;                                       \
88
 
        start_us = ut_time_us(NULL);                            \
89
 
        while (!(cond)                                          \
90
 
               && ut_time_us(NULL) - start_us < (max_wait_us)) {\
91
 
                                                                \
92
 
                os_thread_sleep(2000 /* 2 ms */);               \
93
 
        }                                                       \
94
 
} while (0)
95
 
#endif /* !UNIV_HOTBACKUP */
96
 
 
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.
101
 
@return a >> 32 */
102
 
UNIV_INTERN
103
 
ulint
104
 
ut_get_high32(
105
 
/*==========*/
106
 
        ulint   a);     /*!< in: ulint */
107
 
/******************************************************//**
108
 
Calculates the minimum of two ulints.
109
 
@return minimum */
110
 
UNIV_INLINE
111
 
ulint
112
 
ut_min(
113
 
/*===*/
114
 
        ulint    n1,    /*!< in: first number */
115
 
        ulint    n2);   /*!< in: second number */
116
 
/******************************************************//**
117
 
Calculates the maximum of two ulints.
118
 
@return maximum */
119
 
UNIV_INLINE
120
 
ulint
121
 
ut_max(
122
 
/*===*/
123
 
        ulint    n1,    /*!< in: first number */
124
 
        ulint    n2);   /*!< in: second number */
125
 
/****************************************************************//**
126
 
Calculates minimum of two ulint-pairs. */
127
 
UNIV_INLINE
128
 
void
129
 
ut_pair_min(
130
 
/*========*/
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 */
140
 
UNIV_INLINE
141
 
int
142
 
ut_ulint_cmp(
143
 
/*=========*/
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 */
149
 
UNIV_INLINE
150
 
int
151
 
ut_pair_cmp(
152
 
/*========*/
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 */
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
189
 
integer.
190
 
@return logarithm in the base 2, rounded upward */
191
 
UNIV_INLINE
192
 
ulint
193
 
ut_2_log(
194
 
/*=====*/
195
 
        ulint   n);     /*!< in: number */
196
 
/*************************************************************//**
197
 
Calculates 2 to power n.
198
 
@return 2 to power n */
199
 
UNIV_INLINE
200
 
ulint
201
 
ut_2_exp(
202
 
/*=====*/
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 */
207
 
UNIV_INTERN
208
 
ulint
209
 
ut_2_power_up(
210
 
/*==========*/
211
 
        ulint   n)      /*!< in: number != 0 */
212
 
        __attribute__((const));
213
 
 
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 */
218
 
#define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)
219
 
 
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 */
224
 
UNIV_INTERN
225
 
ib_time_t
226
 
ut_time(void);
227
 
/*=========*/
228
 
#ifndef UNIV_HOTBACKUP
229
 
/**********************************************************//**
230
 
Returns system time.
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
233
 
error.
234
 
@return 0 on success, -1 otherwise */
235
 
UNIV_INTERN
236
 
int
237
 
ut_usectime(
238
 
/*========*/
239
 
        ulint*  sec,    /*!< out: seconds since the Epoch */
240
 
        ulint*  ms);    /*!< out: microseconds since the Epoch+*sec */
241
 
 
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 */
247
 
UNIV_INTERN
248
 
ullint
249
 
ut_time_us(
250
 
/*=======*/
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 */
262
 
 
263
 
/**********************************************************//**
264
 
Returns the difference of two times in seconds.
265
 
@return time2 - time1 expressed in seconds */
266
 
UNIV_INTERN
267
 
double
268
 
ut_difftime(
269
 
/*========*/
270
 
        ib_time_t       time2,  /*!< in: time */
271
 
        ib_time_t       time1); /*!< in: time */
272
 
/**********************************************************//**
273
 
Prints a timestamp to a file. */
274
 
UNIV_INTERN
275
 
void
276
 
ut_print_timestamp(
277
 
/*===============*/
278
 
        FILE*  file); /*!< in: file where to print */
279
 
/**********************************************************//**
280
 
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
281
 
UNIV_INTERN
282
 
void
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
289
 
replaced by '_'. */
290
 
UNIV_INTERN
291
 
void
292
 
ut_sprintf_timestamp_without_extra_chars(
293
 
/*=====================================*/
294
 
        char*   buf); /*!< in: buffer where to sprintf */
295
 
/**********************************************************//**
296
 
Returns current year, month, day. */
297
 
UNIV_INTERN
298
 
void
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 */
309
 
UNIV_INTERN
310
 
ulint
311
 
ut_delay(
312
 
/*=====*/
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. */
317
 
UNIV_INTERN
318
 
void
319
 
ut_print_buf(
320
 
/*=========*/
321
 
        FILE*           file,   /*!< in: file where to print */
322
 
        const void*     buf,    /*!< in: memory buffer */
323
 
        ulint           len);   /*!< in: length of the buffer */
324
 
 
325
 
/**********************************************************************//**
326
 
Outputs a NUL-terminated file name, quoted with apostrophes. */
327
 
UNIV_INTERN
328
 
void
329
 
ut_print_filename(
330
 
/*==============*/
331
 
        FILE*           f,      /*!< in: output stream */
332
 
        const char*     name);  /*!< in: name to print */
333
 
 
334
 
#ifndef UNIV_HOTBACKUP
335
 
/* Forward declaration of transaction handle */
336
 
struct trx_struct;
337
 
 
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. */
343
 
UNIV_INTERN
344
 
void
345
 
ut_print_name(
346
 
/*==========*/
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 */
352
 
 
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. */
358
 
UNIV_INTERN
359
 
void
360
 
ut_print_namel(
361
 
/*===========*/
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 */
368
 
 
369
 
/**********************************************************************//**
370
 
Catenate files. */
371
 
UNIV_INTERN
372
 
void
373
 
ut_copy_file(
374
 
/*=========*/
375
 
        FILE*   dest,   /*!< in: output file */
376
 
        FILE*   src);   /*!< in: input file to be appended to output */
377
 
#endif /* !UNIV_HOTBACKUP */
378
 
 
379
 
#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
 
int
387
 
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 */
393
 
#else
394
 
/**********************************************************************//**
395
 
A wrapper for snprintf(3), formatted output conversion into
396
 
a limited buffer. */
397
 
# define ut_snprintf    snprintf
398
 
#endif /* __WIN__ */
399
 
 
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
 
#ifndef UNIV_NONINL
411
 
#include "ut0ut.ic"
412
 
#endif
413
 
 
414
 
#endif
415