~drizzle-trunk/drizzle/development

641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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 */
641.1.4 by Monty Taylor
Merged in InnoDB changes.
139
	__attribute__((__const__));
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
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
/**************************************************************
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
153
Returns system time.
154
Upon successful completion, the value 0 is returned; otherwise the
155
value -1 is returned and the global variable errno is set to indicate the
156
error. */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
157
UNIV_INTERN
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
158
int
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
159
ut_usectime(
160
/*========*/
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
161
			/* out: 0 on success, -1 otherwise */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
162
	ulint*	sec,	/* out: seconds since the Epoch */
163
	ulint*	ms);	/* out: microseconds since the Epoch+*sec */
164
165
/**************************************************************
166
Returns the number of microseconds since epoch. Similar to
167
time(3), the return value is also stored in *tloc, provided
168
that tloc is non-NULL. */
169
UNIV_INTERN
170
ullint
171
ut_time_us(
172
/*=======*/
173
			/* out: us since epoch */
174
	ullint*	tloc);	/* out: us since epoch, if non-NULL */
175
176
/**************************************************************
177
Returns the difference of two times in seconds. */
178
UNIV_INTERN
179
double
180
ut_difftime(
181
/*========*/
182
				/* out: time2 - time1 expressed in seconds */
183
	ib_time_t	time2,	/* in: time */
184
	ib_time_t	time1);	/* in: time */
185
/**************************************************************
186
Prints a timestamp to a file. */
187
UNIV_INTERN
188
void
189
ut_print_timestamp(
190
/*===============*/
191
	FILE*  file); /* in: file where to print */
192
/**************************************************************
193
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
194
UNIV_INTERN
195
void
196
ut_sprintf_timestamp(
197
/*=================*/
198
	char*	buf); /* in: buffer where to sprintf */
199
/**************************************************************
200
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
201
replaced by '_'. */
202
UNIV_INTERN
203
void
204
ut_sprintf_timestamp_without_extra_chars(
205
/*=====================================*/
206
	char*	buf); /* in: buffer where to sprintf */
207
/**************************************************************
208
Returns current year, month, day. */
209
UNIV_INTERN
210
void
211
ut_get_year_month_day(
212
/*==================*/
213
	ulint*	year,	/* out: current year */
214
	ulint*	month,	/* out: month */
215
	ulint*	day);	/* out: day */
216
/*****************************************************************
217
Runs an idle loop on CPU. The argument gives the desired delay
218
in microseconds on 100 MHz Pentium + Visual C++. */
219
UNIV_INTERN
220
ulint
221
ut_delay(
222
/*=====*/
223
			/* out: dummy value */
224
	ulint	delay);	/* in: delay in microseconds on 100 MHz Pentium */
225
/*****************************************************************
226
Prints the contents of a memory buffer in hex and ascii. */
227
UNIV_INTERN
228
void
229
ut_print_buf(
230
/*=========*/
231
	FILE*		file,	/* in: file where to print */
232
	const void*	buf,	/* in: memory buffer */
233
	ulint		len);	/* in: length of the buffer */
234
235
/**************************************************************************
236
Outputs a NUL-terminated file name, quoted with apostrophes. */
237
UNIV_INTERN
238
void
239
ut_print_filename(
240
/*==============*/
241
	FILE*		f,	/* in: output stream */
242
	const char*	name);	/* in: name to print */
243
244
/* Forward declaration of transaction handle */
245
struct trx_struct;
246
247
/**************************************************************************
248
Outputs a fixed-length string, quoted as an SQL identifier.
249
If the string contains a slash '/', the string will be
250
output as two identifiers separated by a period (.),
251
as in SQL database_name.identifier. */
252
UNIV_INTERN
253
void
254
ut_print_name(
255
/*==========*/
256
	FILE*		f,	/* in: output stream */
257
	struct trx_struct*trx,	/* in: transaction */
258
	ibool		table_id,/* in: TRUE=print a table name,
259
				FALSE=print other identifier */
260
	const char*	name);	/* in: name to print */
261
262
/**************************************************************************
263
Outputs a fixed-length string, quoted as an SQL identifier.
264
If the string contains a slash '/', the string will be
265
output as two identifiers separated by a period (.),
266
as in SQL database_name.identifier. */
267
UNIV_INTERN
268
void
269
ut_print_namel(
270
/*===========*/
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,
274
				FALSE=print other identifier */
275
	const char*	name,	/* in: name to print */
276
	ulint		namelen);/* in: length of name */
277
278
/**************************************************************************
279
Catenate files. */
280
UNIV_INTERN
281
void
282
ut_copy_file(
283
/*=========*/
284
	FILE*	dest,	/* in: output file */
285
	FILE*	src);	/* in: input file to be appended to output */
286
287
/**************************************************************************
288
snprintf(). */
289
290
#ifdef __WIN__
291
int
292
ut_snprintf(
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 */
301
#else
302
#define ut_snprintf	snprintf
303
#endif /* __WIN__ */
304
305
#ifndef UNIV_NONINL
306
#include "ut0ut.ic"
307
#endif
308
309
#endif
310