~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/ut/ut0ut.c

  • Committer: Monty Taylor
  • Date: 2009-09-30 07:01:32 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20090930070132-b1ol1xu1rpajdddy
Small namespace cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************
2
 
Various utilities for Innobase.
3
 
 
4
 
(c) 1994, 1995 Innobase Oy
5
 
 
6
 
Created 5/11/1994 Heikki Tuuri
7
 
********************************************************************/
8
 
 
9
 
#include "ut0ut.h"
10
 
 
11
 
#ifdef UNIV_NONINL
12
 
#include "ut0ut.ic"
13
 
#endif
14
 
 
15
 
#include <stdarg.h>
16
 
#include <string.h>
17
 
#include <ctype.h>
18
 
 
19
 
#include "trx0trx.h"
20
 
#include "ha_prototypes.h"
21
 
#ifndef UNIV_HOTBACKUP
22
 
# if defined(BUILD_DRIZZLE)
23
 
#  include <drizzled/common.h>
24
 
#  if TIME_WITH_SYS_TIME
25
 
#   include <sys/time.h>
26
 
#   include <time.h>
27
 
#  else
28
 
#   if HAVE_SYS_TIME_H
29
 
#    include <sys/time.h>
30
 
#   else
31
 
#    include <time.h>
32
 
#   endif
33
 
#  endif
34
 
# else
35
 
#  include "mysql_com.h" /* NAME_LEN */
36
 
# endif /* DRIZZLE */
37
 
#endif /* UNIV_HOTBACKUP */
38
 
 
39
 
UNIV_INTERN ibool       ut_always_false = FALSE;
40
 
 
41
 
#ifdef __WIN__
42
 
/*********************************************************************
43
 
NOTE: The Windows epoch starts from 1601/01/01 whereas the Unix
44
 
epoch starts from 1970/1/1. For selection of constant see:
45
 
http://support.microsoft.com/kb/167296/ */
46
 
#define WIN_TO_UNIX_DELTA_USEC  ((ib_int64_t) 11644473600000000ULL)
47
 
 
48
 
 
49
 
/*********************************************************************
50
 
This is the Windows version of gettimeofday(2).*/
51
 
static
52
 
int
53
 
ut_gettimeofday(
54
 
/*============*/
55
 
                        /* out: 0 if all OK else -1 */
56
 
        struct timeval* tv,     /* out: Values are relative to Unix epoch */
57
 
        void*           tz)     /* in: not used */
58
 
{
59
 
        FILETIME        ft;
60
 
        ib_int64_t      tm;
61
 
 
62
 
        if (!tv) {
63
 
                errno = EINVAL;
64
 
                return(-1);
65
 
        }
66
 
 
67
 
        GetSystemTimeAsFileTime(&ft);
68
 
 
69
 
        tm = (ib_int64_t) ft.dwHighDateTime << 32;
70
 
        tm |= ft.dwLowDateTime;
71
 
 
72
 
        ut_a(tm >= 0);  /* If tm wraps over to negative, the quotient / 10
73
 
                        does not work */
74
 
 
75
 
        tm /= 10;       /* Convert from 100 nsec periods to usec */
76
 
 
77
 
        /* If we don't convert to the Unix epoch the value for
78
 
        struct timeval::tv_sec will overflow.*/
79
 
        tm -= WIN_TO_UNIX_DELTA_USEC;
80
 
 
81
 
        tv->tv_sec  = (long) (tm / 1000000L);
82
 
        tv->tv_usec = (long) (tm % 1000000L);
83
 
 
84
 
        return(0);
85
 
}
86
 
#else
87
 
#define ut_gettimeofday         gettimeofday
88
 
#endif
89
 
 
90
 
/************************************************************
91
 
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
92
 
but since there seem to be compiler bugs in both gcc and Visual C++,
93
 
we do this by a special conversion. */
94
 
UNIV_INTERN
95
 
ulint
96
 
ut_get_high32(
97
 
/*==========*/
98
 
                        /* out: a >> 32 */
99
 
        ulint   a)      /* in: ulint */
100
 
{
101
 
        ib_int64_t      i;
102
 
 
103
 
        i = (ib_int64_t)a;
104
 
 
105
 
        i = i >> 32;
106
 
 
107
 
        return((ulint)i);
108
 
}
109
 
 
110
 
/**************************************************************
111
 
Returns system time. We do not specify the format of the time returned:
112
 
the only way to manipulate it is to use the function ut_difftime. */
113
 
UNIV_INTERN
114
 
ib_time_t
115
 
ut_time(void)
116
 
/*=========*/
117
 
{
118
 
        return(time(NULL));
119
 
}
120
 
 
121
 
/**************************************************************
122
 
Returns system time. */
123
 
UNIV_INTERN
124
 
void
125
 
ut_usectime(
126
 
/*========*/
127
 
        ulint*  sec,    /* out: seconds since the Epoch */
128
 
        ulint*  ms)     /* out: microseconds since the Epoch+*sec */
129
 
{
130
 
        struct timeval  tv;
131
 
 
132
 
        ut_gettimeofday(&tv, NULL);
133
 
        *sec = (ulint) tv.tv_sec;
134
 
        *ms  = (ulint) tv.tv_usec;
135
 
}
136
 
 
137
 
/**************************************************************
138
 
Returns the number of microseconds since epoch. Similar to
139
 
time(3), the return value is also stored in *tloc, provided
140
 
that tloc is non-NULL. */
141
 
UNIV_INTERN
142
 
ullint
143
 
ut_time_us(
144
 
/*=======*/
145
 
                        /* out: us since epoch */
146
 
        ullint* tloc)   /* out: us since epoch, if non-NULL */
147
 
{
148
 
        struct timeval  tv;
149
 
        ullint          us;
150
 
 
151
 
        ut_gettimeofday(&tv, NULL);
152
 
 
153
 
        us = (ullint) tv.tv_sec * 1000000 + tv.tv_usec;
154
 
 
155
 
        if (tloc != NULL) {
156
 
                *tloc = us;
157
 
        }
158
 
 
159
 
        return(us);
160
 
}
161
 
 
162
 
/**************************************************************
163
 
Returns the difference of two times in seconds. */
164
 
UNIV_INTERN
165
 
double
166
 
ut_difftime(
167
 
/*========*/
168
 
                                /* out: time2 - time1 expressed in seconds */
169
 
        ib_time_t       time2,  /* in: time */
170
 
        ib_time_t       time1)  /* in: time */
171
 
{
172
 
        return(difftime(time2, time1));
173
 
}
174
 
 
175
 
/**************************************************************
176
 
Prints a timestamp to a file. */
177
 
UNIV_INTERN
178
 
void
179
 
ut_print_timestamp(
180
 
/*===============*/
181
 
        FILE*  file) /* in: file where to print */
182
 
{
183
 
#ifdef __WIN__
184
 
        SYSTEMTIME cal_tm;
185
 
 
186
 
        GetLocalTime(&cal_tm);
187
 
 
188
 
        fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
189
 
                (int)cal_tm.wYear % 100,
190
 
                (int)cal_tm.wMonth,
191
 
                (int)cal_tm.wDay,
192
 
                (int)cal_tm.wHour,
193
 
                (int)cal_tm.wMinute,
194
 
                (int)cal_tm.wSecond);
195
 
#else
196
 
        struct tm  cal_tm;
197
 
        struct tm* cal_tm_ptr;
198
 
        time_t     tm;
199
 
 
200
 
        time(&tm);
201
 
 
202
 
#ifdef HAVE_LOCALTIME_R
203
 
        localtime_r(&tm, &cal_tm);
204
 
        cal_tm_ptr = &cal_tm;
205
 
#else
206
 
        cal_tm_ptr = localtime(&tm);
207
 
#endif
208
 
        fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
209
 
                cal_tm_ptr->tm_year % 100,
210
 
                cal_tm_ptr->tm_mon + 1,
211
 
                cal_tm_ptr->tm_mday,
212
 
                cal_tm_ptr->tm_hour,
213
 
                cal_tm_ptr->tm_min,
214
 
                cal_tm_ptr->tm_sec);
215
 
#endif
216
 
}
217
 
 
218
 
/**************************************************************
219
 
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
220
 
UNIV_INTERN
221
 
void
222
 
ut_sprintf_timestamp(
223
 
/*=================*/
224
 
        char*   buf) /* in: buffer where to sprintf */
225
 
{
226
 
#ifdef __WIN__
227
 
        SYSTEMTIME cal_tm;
228
 
 
229
 
        GetLocalTime(&cal_tm);
230
 
 
231
 
        sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
232
 
                (int)cal_tm.wYear % 100,
233
 
                (int)cal_tm.wMonth,
234
 
                (int)cal_tm.wDay,
235
 
                (int)cal_tm.wHour,
236
 
                (int)cal_tm.wMinute,
237
 
                (int)cal_tm.wSecond);
238
 
#else
239
 
        struct tm  cal_tm;
240
 
        struct tm* cal_tm_ptr;
241
 
        time_t     tm;
242
 
 
243
 
        time(&tm);
244
 
 
245
 
#ifdef HAVE_LOCALTIME_R
246
 
        localtime_r(&tm, &cal_tm);
247
 
        cal_tm_ptr = &cal_tm;
248
 
#else
249
 
        cal_tm_ptr = localtime(&tm);
250
 
#endif
251
 
        sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
252
 
                cal_tm_ptr->tm_year % 100,
253
 
                cal_tm_ptr->tm_mon + 1,
254
 
                cal_tm_ptr->tm_mday,
255
 
                cal_tm_ptr->tm_hour,
256
 
                cal_tm_ptr->tm_min,
257
 
                cal_tm_ptr->tm_sec);
258
 
#endif
259
 
}
260
 
 
261
 
/**************************************************************
262
 
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
263
 
replaced by '_'. */
264
 
UNIV_INTERN
265
 
void
266
 
ut_sprintf_timestamp_without_extra_chars(
267
 
/*=====================================*/
268
 
        char*   buf) /* in: buffer where to sprintf */
269
 
{
270
 
#ifdef __WIN__
271
 
        SYSTEMTIME cal_tm;
272
 
 
273
 
        GetLocalTime(&cal_tm);
274
 
 
275
 
        sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
276
 
                (int)cal_tm.wYear % 100,
277
 
                (int)cal_tm.wMonth,
278
 
                (int)cal_tm.wDay,
279
 
                (int)cal_tm.wHour,
280
 
                (int)cal_tm.wMinute,
281
 
                (int)cal_tm.wSecond);
282
 
#else
283
 
        struct tm  cal_tm;
284
 
        struct tm* cal_tm_ptr;
285
 
        time_t     tm;
286
 
 
287
 
        time(&tm);
288
 
 
289
 
#ifdef HAVE_LOCALTIME_R
290
 
        localtime_r(&tm, &cal_tm);
291
 
        cal_tm_ptr = &cal_tm;
292
 
#else
293
 
        cal_tm_ptr = localtime(&tm);
294
 
#endif
295
 
        sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
296
 
                cal_tm_ptr->tm_year % 100,
297
 
                cal_tm_ptr->tm_mon + 1,
298
 
                cal_tm_ptr->tm_mday,
299
 
                cal_tm_ptr->tm_hour,
300
 
                cal_tm_ptr->tm_min,
301
 
                cal_tm_ptr->tm_sec);
302
 
#endif
303
 
}
304
 
 
305
 
/**************************************************************
306
 
Returns current year, month, day. */
307
 
UNIV_INTERN
308
 
void
309
 
ut_get_year_month_day(
310
 
/*==================*/
311
 
        ulint*  year,   /* out: current year */
312
 
        ulint*  month,  /* out: month */
313
 
        ulint*  day)    /* out: day */
314
 
{
315
 
#ifdef __WIN__
316
 
        SYSTEMTIME cal_tm;
317
 
 
318
 
        GetLocalTime(&cal_tm);
319
 
 
320
 
        *year = (ulint)cal_tm.wYear;
321
 
        *month = (ulint)cal_tm.wMonth;
322
 
        *day = (ulint)cal_tm.wDay;
323
 
#else
324
 
        struct tm  cal_tm;
325
 
        struct tm* cal_tm_ptr;
326
 
        time_t     tm;
327
 
 
328
 
        time(&tm);
329
 
 
330
 
#ifdef HAVE_LOCALTIME_R
331
 
        localtime_r(&tm, &cal_tm);
332
 
        cal_tm_ptr = &cal_tm;
333
 
#else
334
 
        cal_tm_ptr = localtime(&tm);
335
 
#endif
336
 
        *year = (ulint)cal_tm_ptr->tm_year + 1900;
337
 
        *month = (ulint)cal_tm_ptr->tm_mon + 1;
338
 
        *day = (ulint)cal_tm_ptr->tm_mday;
339
 
#endif
340
 
}
341
 
 
342
 
/*****************************************************************
343
 
Runs an idle loop on CPU. The argument gives the desired delay
344
 
in microseconds on 100 MHz Pentium + Visual C++. */
345
 
UNIV_INTERN
346
 
ulint
347
 
ut_delay(
348
 
/*=====*/
349
 
                        /* out: dummy value */
350
 
        ulint   delay)  /* in: delay in microseconds on 100 MHz Pentium */
351
 
{
352
 
        ulint   i, j;
353
 
 
354
 
        j = 0;
355
 
 
356
 
        for (i = 0; i < delay * 50; i++) {
357
 
                j += i;
358
 
        }
359
 
 
360
 
        if (ut_always_false) {
361
 
                ut_always_false = (ibool) j;
362
 
        }
363
 
 
364
 
        return(j);
365
 
}
366
 
 
367
 
/*****************************************************************
368
 
Prints the contents of a memory buffer in hex and ascii. */
369
 
UNIV_INTERN
370
 
void
371
 
ut_print_buf(
372
 
/*=========*/
373
 
        FILE*           file,   /* in: file where to print */
374
 
        const void*     buf,    /* in: memory buffer */
375
 
        ulint           len)    /* in: length of the buffer */
376
 
{
377
 
        const byte*     data;
378
 
        ulint           i;
379
 
 
380
 
        UNIV_MEM_ASSERT_RW(buf, len);
381
 
 
382
 
        fprintf(file, " len %lu; hex ", len);
383
 
 
384
 
        for (data = (const byte*)buf, i = 0; i < len; i++) {
385
 
                fprintf(file, "%02lx", (ulong)*data++);
386
 
        }
387
 
 
388
 
        fputs("; asc ", file);
389
 
 
390
 
        data = (const byte*)buf;
391
 
 
392
 
        for (i = 0; i < len; i++) {
393
 
                int     c = (int) *data++;
394
 
                putc(isprint(c) ? c : ' ', file);
395
 
        }
396
 
 
397
 
        putc(';', file);
398
 
}
399
 
 
400
 
/*****************************************************************
401
 
Calculates fast the number rounded up to the nearest power of 2. */
402
 
UNIV_INTERN
403
 
ulint
404
 
ut_2_power_up(
405
 
/*==========*/
406
 
                        /* out: first power of 2 which is >= n */
407
 
        ulint   n)      /* in: number != 0 */
408
 
{
409
 
        ulint   res;
410
 
 
411
 
        res = 1;
412
 
 
413
 
        ut_ad(n > 0);
414
 
 
415
 
        while (res < n) {
416
 
                res = res * 2;
417
 
        }
418
 
 
419
 
        return(res);
420
 
}
421
 
 
422
 
/**************************************************************************
423
 
Outputs a NUL-terminated file name, quoted with apostrophes. */
424
 
UNIV_INTERN
425
 
void
426
 
ut_print_filename(
427
 
/*==============*/
428
 
        FILE*           f,      /* in: output stream */
429
 
        const char*     name)   /* in: name to print */
430
 
{
431
 
        putc('\'', f);
432
 
        for (;;) {
433
 
                int     c = *name++;
434
 
                switch (c) {
435
 
                case 0:
436
 
                        goto done;
437
 
                case '\'':
438
 
                        putc(c, f);
439
 
                        /* fall through */
440
 
                default:
441
 
                        putc(c, f);
442
 
                }
443
 
        }
444
 
done:
445
 
        putc('\'', f);
446
 
}
447
 
 
448
 
/**************************************************************************
449
 
Outputs a fixed-length string, quoted as an SQL identifier.
450
 
If the string contains a slash '/', the string will be
451
 
output as two identifiers separated by a period (.),
452
 
as in SQL database_name.identifier. */
453
 
UNIV_INTERN
454
 
void
455
 
ut_print_name(
456
 
/*==========*/
457
 
        FILE*           f,      /* in: output stream */
458
 
        trx_t*          trx,    /* in: transaction */
459
 
        ibool           table_id,/* in: TRUE=print a table name,
460
 
                                FALSE=print other identifier */
461
 
        const char*     name)   /* in: name to print */
462
 
{
463
 
        ut_print_namel(f, trx, table_id, name, strlen(name));
464
 
}
465
 
 
466
 
/**************************************************************************
467
 
Outputs a fixed-length string, quoted as an SQL identifier.
468
 
If the string contains a slash '/', the string will be
469
 
output as two identifiers separated by a period (.),
470
 
as in SQL database_name.identifier. */
471
 
UNIV_INTERN
472
 
void
473
 
ut_print_namel(
474
 
/*===========*/
475
 
        FILE*           f,      /* in: output stream */
476
 
        trx_t*          trx,    /* in: transaction (NULL=no quotes) */
477
 
        ibool           table_id,/* in: TRUE=print a table name,
478
 
                                FALSE=print other identifier */
479
 
        const char*     name,   /* in: name to print */
480
 
        ulint           namelen)/* in: length of name */
481
 
{
482
 
#ifdef UNIV_HOTBACKUP
483
 
        fwrite(name, 1, namelen, f);
484
 
#else
485
 
        /* 2 * NAME_LEN for database and table name,
486
 
        and some slack for the #mysql50# prefix and quotes */
487
 
        char            buf[3 * NAME_LEN];
488
 
        const char*     bufend;
489
 
 
490
 
        bufend = innobase_convert_name(buf, sizeof buf,
491
 
                                       name, namelen,
492
 
                                       trx ? trx->mysql_thd : NULL,
493
 
                                       table_id);
494
 
 
495
 
        ssize_t ret= fwrite(buf, 1, bufend - buf, f);
496
 
        assert(ret==bufend-buf);  
497
 
#endif
498
 
}
499
 
 
500
 
/**************************************************************************
501
 
Catenate files. */
502
 
UNIV_INTERN
503
 
void
504
 
ut_copy_file(
505
 
/*=========*/
506
 
        FILE*   dest,   /* in: output file */
507
 
        FILE*   src)    /* in: input file to be appended to output */
508
 
{
509
 
        long    len = ftell(src);
510
 
        char    buf[4096];
511
 
 
512
 
        rewind(src);
513
 
        do {
514
 
                size_t  maxs = len < (long) sizeof buf
515
 
                        ? (size_t) len
516
 
                        : sizeof buf;
517
 
                size_t  size = fread(buf, 1, maxs, src);
518
 
                size_t ret= fwrite(buf, 1, size, dest);
519
 
                assert(ret==size);
520
 
                len -= (long) size;
521
 
                if (size < maxs) {
522
 
                        break;
523
 
                }
524
 
        } while (len > 0);
525
 
}
526
 
 
527
 
/**************************************************************************
528
 
snprintf(). */
529
 
 
530
 
#ifdef __WIN__
531
 
#include <stdarg.h>
532
 
int
533
 
ut_snprintf(
534
 
                                /* out: number of characters that would
535
 
                                have been printed if the size were
536
 
                                unlimited, not including the terminating
537
 
                                '\0'. */
538
 
        char*           str,    /* out: string */
539
 
        size_t          size,   /* in: str size */
540
 
        const char*     fmt,    /* in: format */
541
 
        ...)                    /* in: format values */
542
 
{
543
 
        int     res;
544
 
        va_list ap1;
545
 
        va_list ap2;
546
 
 
547
 
        va_start(ap1, fmt);
548
 
        va_start(ap2, fmt);
549
 
 
550
 
        res = _vscprintf(fmt, ap1);
551
 
        ut_a(res != -1);
552
 
 
553
 
        if (size > 0) {
554
 
                _vsnprintf(str, size, fmt, ap2);
555
 
 
556
 
                if ((size_t) res >= size) {
557
 
                        str[size - 1] = '\0';
558
 
                }
559
 
        }
560
 
 
561
 
        va_end(ap1);
562
 
        va_end(ap2);
563
 
 
564
 
        return(res);
565
 
}
566
 
#endif /* __WIN__ */