~drizzle-trunk/drizzle/development

1 by brian
clean slate
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>
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
14
#ifndef DRIZZLE_SERVER
1 by brian
clean slate
15
#include <ctype.h>
16
#endif
17
18
typedef time_t	ib_time_t;
19
20
/************************************************************
21
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
22
but since there seem to be compiler bugs in both gcc and Visual C++,
23
we do this by a special conversion. */
24
25
ulint
26
ut_get_high32(
27
/*==========*/
28
			/* out: a >> 32 */
29
	ulint	a);	/* in: ulint */
30
/**********************************************************
31
Calculates the minimum of two ulints. */
32
UNIV_INLINE
33
ulint
34
ut_min(
35
/*===*/
36
			/* out: minimum */
37
	ulint	 n1,	/* in: first number */
38
	ulint	 n2);	/* in: second number */
39
/**********************************************************
40
Calculates the maximum of two ulints. */
41
UNIV_INLINE
42
ulint
43
ut_max(
44
/*===*/
45
			/* out: maximum */
46
	ulint	 n1,	/* in: first number */
47
	ulint	 n2);	/* in: second number */
48
/********************************************************************
49
Calculates minimum of two ulint-pairs. */
50
UNIV_INLINE
51
void
52
ut_pair_min(
53
/*========*/
54
	ulint*	a,	/* out: more significant part of minimum */
55
	ulint*	b,	/* out: less significant part of minimum */
56
	ulint	a1,	/* in: more significant part of first pair */
57
	ulint	b1,	/* in: less significant part of first pair */
58
	ulint	a2,	/* in: more significant part of second pair */
59
	ulint	b2);	/* in: less significant part of second pair */
60
/**********************************************************
61
Compares two ulints. */
62
UNIV_INLINE
63
int
64
ut_ulint_cmp(
65
/*=========*/
66
			/* out: 1 if a > b, 0 if a == b, -1 if a < b */
67
	ulint	a,	/* in: ulint */
68
	ulint	b);	/* in: ulint */
69
/***********************************************************
70
Compares two pairs of ulints. */
71
UNIV_INLINE
72
int
73
ut_pair_cmp(
74
/*========*/
75
			/* out: -1 if a < b, 0 if a == b,
76
			1 if a > b */
77
	ulint	a1,	/* in: more significant part of first pair */
78
	ulint	a2,	/* in: less significant part of first pair */
79
	ulint	b1,	/* in: more significant part of second pair */
80
	ulint	b2);	/* in: less significant part of second pair */
81
/*****************************************************************
82
Calculates fast the remainder when divided by a power of two. */
83
UNIV_INLINE
84
ulint
85
ut_2pow_remainder(
86
/*==============*/	/* out: remainder */
87
	ulint	n,	/* in: number to be divided */
88
	ulint	m);	/* in: divisor; power of 2 */
89
/*****************************************************************
90
Calculates fast value rounded to a multiple of a power of 2. */
91
UNIV_INLINE
92
ulint
93
ut_2pow_round(
94
/*==========*/		/* out: value of n rounded down to nearest
95
			multiple of m */
96
	ulint	n,	/* in: number to be rounded */
97
	ulint	m);	/* in: divisor; power of 2 */
98
/*****************************************************************
99
Calculates fast the 2-logarithm of a number, rounded upward to an
100
integer. */
101
UNIV_INLINE
102
ulint
103
ut_2_log(
104
/*=====*/
105
			/* out: logarithm in the base 2, rounded upward */
106
	ulint	n);	/* in: number */
107
/*****************************************************************
108
Calculates 2 to power n. */
109
UNIV_INLINE
110
ulint
111
ut_2_exp(
112
/*=====*/
113
			/* out: 2 to power n */
114
	ulint	n);	/* in: number */
115
/*****************************************************************
116
Calculates fast the number rounded up to the nearest power of 2. */
117
118
ulint
119
ut_2_power_up(
120
/*==========*/
121
			/* out: first power of 2 which is >= n */
122
	ulint	n)	/* in: number != 0 */
123
	__attribute__((const));
124
125
/* Determine how many bytes (groups of 8 bits) are needed to
126
store the given number of bits. */
127
#define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)
128
129
/****************************************************************
130
Sort function for ulint arrays. */
131
132
void
133
ut_ulint_sort(ulint* arr, ulint* aux_arr, ulint low, ulint high);
134
/*============================================================*/
135
/************************************************************
136
The following function returns elapsed CPU time in milliseconds. */
137
138
ulint
139
ut_clock(void);
140
/**************************************************************
141
Returns system time. We do not specify the format of the time returned:
142
the only way to manipulate it is to use the function ut_difftime. */
143
144
ib_time_t
145
ut_time(void);
146
/*=========*/
147
/**************************************************************
148
Returns system time. */
149
150
void
151
ut_usectime(
152
/*========*/
153
	ulint*	sec,	/* out: seconds since the Epoch */
154
	ulint*	ms);	/* out: microseconds since the Epoch+*sec */
155
/**************************************************************
156
Returns the difference of two times in seconds. */
157
158
double
159
ut_difftime(
160
/*========*/
161
				/* out: time2 - time1 expressed in seconds */
162
	ib_time_t	time2,	/* in: time */
163
	ib_time_t	time1);	/* in: time */
164
/**************************************************************
165
Prints a timestamp to a file. */
166
167
void
168
ut_print_timestamp(
169
/*===============*/
170
	FILE*  file); /* in: file where to print */
171
/**************************************************************
172
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
173
174
void
175
ut_sprintf_timestamp(
176
/*=================*/
177
	char*	buf); /* in: buffer where to sprintf */
178
/**************************************************************
179
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
180
replaced by '_'. */
181
182
void
183
ut_sprintf_timestamp_without_extra_chars(
184
/*=====================================*/
185
	char*	buf); /* in: buffer where to sprintf */
186
/**************************************************************
187
Returns current year, month, day. */
188
189
void
190
ut_get_year_month_day(
191
/*==================*/
192
	ulint*	year,	/* out: current year */
193
	ulint*	month,	/* out: month */
194
	ulint*	day);	/* out: day */
195
/*****************************************************************
196
Runs an idle loop on CPU. The argument gives the desired delay
197
in microseconds on 100 MHz Pentium + Visual C++. */
198
199
ulint
200
ut_delay(
201
/*=====*/
202
			/* out: dummy value */
203
	ulint	delay);	/* in: delay in microseconds on 100 MHz Pentium */
204
/*****************************************************************
205
Prints the contents of a memory buffer in hex and ascii. */
206
207
void
208
ut_print_buf(
209
/*=========*/
210
	FILE*		file,	/* in: file where to print */
211
	const void*	buf,	/* in: memory buffer */
212
	ulint		len);	/* in: length of the buffer */
213
214
/**************************************************************************
215
Outputs a NUL-terminated file name, quoted with apostrophes. */
216
217
void
218
ut_print_filename(
219
/*==============*/
220
	FILE*		f,	/* in: output stream */
221
	const char*	name);	/* in: name to print */
222
223
/* Forward declaration of transaction handle */
224
struct trx_struct;
225
226
/**************************************************************************
227
Outputs a fixed-length string, quoted as an SQL identifier.
228
If the string contains a slash '/', the string will be
229
output as two identifiers separated by a period (.),
230
as in SQL database_name.identifier. */
231
232
void
233
ut_print_name(
234
/*==========*/
235
	FILE*		f,	/* in: output stream */
236
	struct trx_struct*trx,	/* in: transaction */
237
	ibool		table_id,/* in: TRUE=print a table name,
238
				FALSE=print other identifier */
239
	const char*	name);	/* in: name to print */
240
241
/**************************************************************************
242
Outputs a fixed-length string, quoted as an SQL identifier.
243
If the string contains a slash '/', the string will be
244
output as two identifiers separated by a period (.),
245
as in SQL database_name.identifier. */
246
247
void
248
ut_print_namel(
249
/*===========*/
250
	FILE*		f,	/* in: output stream */
251
	struct trx_struct*trx,	/* in: transaction (NULL=no quotes) */
252
	ibool		table_id,/* in: TRUE=print a table name,
253
				FALSE=print other identifier */
254
	const char*	name,	/* in: name to print */
255
	ulint		namelen);/* in: length of name */
256
257
/**************************************************************************
258
Catenate files. */
259
260
void
261
ut_copy_file(
262
/*=========*/
263
	FILE*	dest,	/* in: output file */
264
	FILE*	src);	/* in: input file to be appended to output */
265
266
/**************************************************************************
267
snprintf(). */
268
269
#ifdef __WIN__
270
int
271
ut_snprintf(
272
				/* out: number of characters that would
273
				have been printed if the size were
274
				unlimited, not including the terminating
275
				'\0'. */
276
	char*		str,	/* out: string */
277
	size_t		size,	/* in: str size */
278
	const char*	fmt,	/* in: format */
279
	...);			/* in: format values */
280
#else
281
#define ut_snprintf	snprintf
282
#endif /* __WIN__ */
283
284
#ifndef UNIV_NONINL
285
#include "ut0ut.ic"
286
#endif
287
288
#endif
289