~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Tags: innodb-plugin-1.0.1
Imported 1.0.1 with clean - with no changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
Various utilities
 
3
 
 
4
(c) 1994, 1995 Innobase Oy
 
5
 
 
6
Created 1/20/1994 Heikki Tuuri
 
7
***********************************************************************/
 
8
 
 
9
#ifndef ut0ut_h
 
10
#define ut0ut_h
 
11
 
 
12
#include "univ.i"
 
13
#include <time.h>
 
14
#ifndef MYSQL_SERVER
 
15
#include <ctype.h>
 
16
#endif
 
17
 
 
18
#define TEMP_INDEX_PREFIX       '\377'  /* Index name prefix in fast index
 
19
                                        creation */
 
20
 
 
21
typedef time_t  ib_time_t;
 
22
 
 
23
/*************************************************************************
 
24
Delays execution for at most max_wait_us microseconds or returns earlier
 
25
if cond becomes true; cond is evaluated every 2 ms. */
 
26
 
 
27
#define UT_WAIT_FOR(cond, max_wait_us)                          \
 
28
do {                                                            \
 
29
        ullint  start_us;                                       \
 
30
        start_us = ut_time_us(NULL);                            \
 
31
        while (!(cond)                                          \
 
32
               && ut_time_us(NULL) - start_us < (max_wait_us)) {\
 
33
                                                                \
 
34
                os_thread_sleep(2000 /* 2 ms */);               \
 
35
        }                                                       \
 
36
} while (0)
 
37
 
 
38
/************************************************************
 
39
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
 
40
but since there seem to be compiler bugs in both gcc and Visual C++,
 
41
we do this by a special conversion. */
 
42
UNIV_INTERN
 
43
ulint
 
44
ut_get_high32(
 
45
/*==========*/
 
46
                        /* out: a >> 32 */
 
47
        ulint   a);     /* in: ulint */
 
48
/**********************************************************
 
49
Calculates the minimum of two ulints. */
 
50
UNIV_INLINE
 
51
ulint
 
52
ut_min(
 
53
/*===*/
 
54
                        /* out: minimum */
 
55
        ulint    n1,    /* in: first number */
 
56
        ulint    n2);   /* in: second number */
 
57
/**********************************************************
 
58
Calculates the maximum of two ulints. */
 
59
UNIV_INLINE
 
60
ulint
 
61
ut_max(
 
62
/*===*/
 
63
                        /* out: maximum */
 
64
        ulint    n1,    /* in: first number */
 
65
        ulint    n2);   /* in: second number */
 
66
/********************************************************************
 
67
Calculates minimum of two ulint-pairs. */
 
68
UNIV_INLINE
 
69
void
 
70
ut_pair_min(
 
71
/*========*/
 
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. */
 
80
UNIV_INLINE
 
81
int
 
82
ut_ulint_cmp(
 
83
/*=========*/
 
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. */
 
89
UNIV_INLINE
 
90
int
 
91
ut_pair_cmp(
 
92
/*========*/
 
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. */
 
101
#define ut_is_2pow(n) UNIV_LIKELY(!((n) & ((n) - 1)))
 
102
/*****************************************************************
 
103
Calculates fast the remainder of n/m when m is a power of two. */
 
104
#define ut_2pow_remainder(n, m) ((n) & ((m) - 1))
 
105
/*****************************************************************
 
106
Calculates the biggest multiple of m that is not bigger than n
 
107
when m is a power of two.  In other words, rounds n down to m * k. */
 
108
#define ut_2pow_round(n, m) ((n) & ~((m) - 1))
 
109
#define ut_calc_align_down(n, m) ut_2pow_round(n, m)
 
110
/************************************************************
 
111
Calculates the smallest multiple of m that is not smaller than n
 
112
when m is a power of two.  In other words, rounds n up to m * k. */
 
113
#define ut_calc_align(n, m) (((n) + ((m) - 1)) & ~((m) - 1))
 
114
/*****************************************************************
 
115
Calculates fast the 2-logarithm of a number, rounded upward to an
 
116
integer. */
 
117
UNIV_INLINE
 
118
ulint
 
119
ut_2_log(
 
120
/*=====*/
 
121
                        /* out: logarithm in the base 2, rounded upward */
 
122
        ulint   n);     /* in: number */
 
123
/*****************************************************************
 
124
Calculates 2 to power n. */
 
125
UNIV_INLINE
 
126
ulint
 
127
ut_2_exp(
 
128
/*=====*/
 
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. */
 
133
UNIV_INTERN
 
134
ulint
 
135
ut_2_power_up(
 
136
/*==========*/
 
137
                        /* out: first power of 2 which is >= n */
 
138
        ulint   n)      /* in: number != 0 */
 
139
        __attribute__((const));
 
140
 
 
141
/* Determine how many bytes (groups of 8 bits) are needed to
 
142
store the given number of bits. */
 
143
#define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)
 
144
 
 
145
/**************************************************************
 
146
Returns system time. We do not specify the format of the time returned:
 
147
the only way to manipulate it is to use the function ut_difftime. */
 
148
UNIV_INTERN
 
149
ib_time_t
 
150
ut_time(void);
 
151
/*=========*/
 
152
/**************************************************************
 
153
Returns system time. */
 
154
UNIV_INTERN
 
155
void
 
156
ut_usectime(
 
157
/*========*/
 
158
        ulint*  sec,    /* out: seconds since the Epoch */
 
159
        ulint*  ms);    /* out: microseconds since the Epoch+*sec */
 
160
 
 
161
/**************************************************************
 
162
Returns the number of microseconds since epoch. Similar to
 
163
time(3), the return value is also stored in *tloc, provided
 
164
that tloc is non-NULL. */
 
165
UNIV_INTERN
 
166
ullint
 
167
ut_time_us(
 
168
/*=======*/
 
169
                        /* out: us since epoch */
 
170
        ullint* tloc);  /* out: us since epoch, if non-NULL */
 
171
 
 
172
/**************************************************************
 
173
Returns the difference of two times in seconds. */
 
174
UNIV_INTERN
 
175
double
 
176
ut_difftime(
 
177
/*========*/
 
178
                                /* out: time2 - time1 expressed in seconds */
 
179
        ib_time_t       time2,  /* in: time */
 
180
        ib_time_t       time1); /* in: time */
 
181
/**************************************************************
 
182
Prints a timestamp to a file. */
 
183
UNIV_INTERN
 
184
void
 
185
ut_print_timestamp(
 
186
/*===============*/
 
187
        FILE*  file); /* in: file where to print */
 
188
/**************************************************************
 
189
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
 
190
UNIV_INTERN
 
191
void
 
192
ut_sprintf_timestamp(
 
193
/*=================*/
 
194
        char*   buf); /* in: buffer where to sprintf */
 
195
/**************************************************************
 
196
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
 
197
replaced by '_'. */
 
198
UNIV_INTERN
 
199
void
 
200
ut_sprintf_timestamp_without_extra_chars(
 
201
/*=====================================*/
 
202
        char*   buf); /* in: buffer where to sprintf */
 
203
/**************************************************************
 
204
Returns current year, month, day. */
 
205
UNIV_INTERN
 
206
void
 
207
ut_get_year_month_day(
 
208
/*==================*/
 
209
        ulint*  year,   /* out: current year */
 
210
        ulint*  month,  /* out: month */
 
211
        ulint*  day);   /* out: day */
 
212
/*****************************************************************
 
213
Runs an idle loop on CPU. The argument gives the desired delay
 
214
in microseconds on 100 MHz Pentium + Visual C++. */
 
215
UNIV_INTERN
 
216
ulint
 
217
ut_delay(
 
218
/*=====*/
 
219
                        /* out: dummy value */
 
220
        ulint   delay); /* in: delay in microseconds on 100 MHz Pentium */
 
221
/*****************************************************************
 
222
Prints the contents of a memory buffer in hex and ascii. */
 
223
UNIV_INTERN
 
224
void
 
225
ut_print_buf(
 
226
/*=========*/
 
227
        FILE*           file,   /* in: file where to print */
 
228
        const void*     buf,    /* in: memory buffer */
 
229
        ulint           len);   /* in: length of the buffer */
 
230
 
 
231
/**************************************************************************
 
232
Outputs a NUL-terminated file name, quoted with apostrophes. */
 
233
UNIV_INTERN
 
234
void
 
235
ut_print_filename(
 
236
/*==============*/
 
237
        FILE*           f,      /* in: output stream */
 
238
        const char*     name);  /* in: name to print */
 
239
 
 
240
/* Forward declaration of transaction handle */
 
241
struct trx_struct;
 
242
 
 
243
/**************************************************************************
 
244
Outputs a fixed-length string, quoted as an SQL identifier.
 
245
If the string contains a slash '/', the string will be
 
246
output as two identifiers separated by a period (.),
 
247
as in SQL database_name.identifier. */
 
248
UNIV_INTERN
 
249
void
 
250
ut_print_name(
 
251
/*==========*/
 
252
        FILE*           f,      /* in: output stream */
 
253
        struct trx_struct*trx,  /* in: transaction */
 
254
        ibool           table_id,/* in: TRUE=print a table name,
 
255
                                FALSE=print other identifier */
 
256
        const char*     name);  /* in: name to print */
 
257
 
 
258
/**************************************************************************
 
259
Outputs a fixed-length string, quoted as an SQL identifier.
 
260
If the string contains a slash '/', the string will be
 
261
output as two identifiers separated by a period (.),
 
262
as in SQL database_name.identifier. */
 
263
UNIV_INTERN
 
264
void
 
265
ut_print_namel(
 
266
/*===========*/
 
267
        FILE*           f,      /* in: output stream */
 
268
        struct trx_struct*trx,  /* in: transaction (NULL=no quotes) */
 
269
        ibool           table_id,/* in: TRUE=print a table name,
 
270
                                FALSE=print other identifier */
 
271
        const char*     name,   /* in: name to print */
 
272
        ulint           namelen);/* in: length of name */
 
273
 
 
274
/**************************************************************************
 
275
Catenate files. */
 
276
UNIV_INTERN
 
277
void
 
278
ut_copy_file(
 
279
/*=========*/
 
280
        FILE*   dest,   /* in: output file */
 
281
        FILE*   src);   /* in: input file to be appended to output */
 
282
 
 
283
/**************************************************************************
 
284
snprintf(). */
 
285
 
 
286
#ifdef __WIN__
 
287
int
 
288
ut_snprintf(
 
289
                                /* out: number of characters that would
 
290
                                have been printed if the size were
 
291
                                unlimited, not including the terminating
 
292
                                '\0'. */
 
293
        char*           str,    /* out: string */
 
294
        size_t          size,   /* in: str size */
 
295
        const char*     fmt,    /* in: format */
 
296
        ...);                   /* in: format values */
 
297
#else
 
298
#define ut_snprintf     snprintf
 
299
#endif /* __WIN__ */
 
300
 
 
301
#ifndef UNIV_NONINL
 
302
#include "ut0ut.ic"
 
303
#endif
 
304
 
 
305
#endif
 
306