~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2009-08-12 06:25:19 UTC
  • mto: (1114.1.1 innodb-plugin-merge)
  • mto: This revision was merged to the branch mainline in revision 1183.
  • Revision ID: mordred@inaugust.com-20090812062519-cij02mrrunvnxblt
Tags: innodb-plugin-1.0.4
InnoDB Plugin 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 
3
3
Copyright (c) 1994, 2009, 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.
4
11
 
5
12
This program is free software; you can redistribute it and/or modify it under
6
13
the terms of the GNU General Public License as published by the Free Software
16
23
 
17
24
*****************************************************************************/
18
25
 
19
 
/*******************************************************************
 
26
/***************************************************************//**
 
27
@file ut/ut0ut.c
20
28
Various utilities for Innobase.
21
29
 
22
30
Created 5/11/1994 Heikki Tuuri
32
40
#include <string.h>
33
41
#include <ctype.h>
34
42
 
35
 
#include "trx0trx.h"
36
 
#include "ha_prototypes.h"
37
43
#ifndef UNIV_HOTBACKUP
 
44
# include "trx0trx.h"
 
45
# include "ha_prototypes.h"
38
46
# include "mysql_com.h" /* NAME_LEN */
39
47
#endif /* UNIV_HOTBACKUP */
40
48
 
 
49
/** A constant to prevent the compiler from optimizing ut_delay() away. */
41
50
UNIV_INTERN ibool       ut_always_false = FALSE;
42
51
 
43
52
#ifdef __WIN__
44
 
/*********************************************************************
 
53
/*****************************************************************//**
45
54
NOTE: The Windows epoch starts from 1601/01/01 whereas the Unix
46
55
epoch starts from 1970/1/1. For selection of constant see:
47
56
http://support.microsoft.com/kb/167296/ */
48
57
#define WIN_TO_UNIX_DELTA_USEC  ((ib_int64_t) 11644473600000000ULL)
49
58
 
50
59
 
51
 
/*********************************************************************
52
 
This is the Windows version of gettimeofday(2).*/
 
60
/*****************************************************************//**
 
61
This is the Windows version of gettimeofday(2).
 
62
@return 0 if all OK else -1 */
53
63
static
54
64
int
55
65
ut_gettimeofday(
56
66
/*============*/
57
 
                        /* out: 0 if all OK else -1 */
58
 
        struct timeval* tv,     /* out: Values are relative to Unix epoch */
59
 
        void*           tz)     /* in: not used */
 
67
        struct timeval* tv,     /*!< out: Values are relative to Unix epoch */
 
68
        void*           tz)     /*!< in: not used */
60
69
{
61
70
        FILETIME        ft;
62
71
        ib_int64_t      tm;
86
95
        return(0);
87
96
}
88
97
#else
 
98
/** An alias for gettimeofday(2).  On Microsoft Windows, we have to
 
99
reimplement this function. */
89
100
#define ut_gettimeofday         gettimeofday
90
101
#endif
91
102
 
92
 
/************************************************************
 
103
/********************************************************//**
93
104
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
94
105
but since there seem to be compiler bugs in both gcc and Visual C++,
95
 
we do this by a special conversion. */
 
106
we do this by a special conversion.
 
107
@return a >> 32 */
96
108
UNIV_INTERN
97
109
ulint
98
110
ut_get_high32(
99
111
/*==========*/
100
 
                        /* out: a >> 32 */
101
 
        ulint   a)      /* in: ulint */
 
112
        ulint   a)      /*!< in: ulint */
102
113
{
103
114
        ib_int64_t      i;
104
115
 
109
120
        return((ulint)i);
110
121
}
111
122
 
112
 
/**************************************************************
 
123
/**********************************************************//**
113
124
Returns system time. We do not specify the format of the time returned:
114
 
the only way to manipulate it is to use the function ut_difftime. */
 
125
the only way to manipulate it is to use the function ut_difftime.
 
126
@return system time */
115
127
UNIV_INTERN
116
128
ib_time_t
117
129
ut_time(void)
120
132
        return(time(NULL));
121
133
}
122
134
 
123
 
/**************************************************************
 
135
/**********************************************************//**
124
136
Returns system time.
125
137
Upon successful completion, the value 0 is returned; otherwise the
126
138
value -1 is returned and the global variable errno is set to indicate the
127
 
error. */
 
139
error.
 
140
@return 0 on success, -1 otherwise */
128
141
UNIV_INTERN
129
142
int
130
143
ut_usectime(
131
144
/*========*/
132
 
                        /* out: 0 on success, -1 otherwise */
133
 
        ulint*  sec,    /* out: seconds since the Epoch */
134
 
        ulint*  ms)     /* out: microseconds since the Epoch+*sec */
 
145
        ulint*  sec,    /*!< out: seconds since the Epoch */
 
146
        ulint*  ms)     /*!< out: microseconds since the Epoch+*sec */
135
147
{
136
148
        struct timeval  tv;
137
149
        int             ret;
162
174
        return(ret);
163
175
}
164
176
 
165
 
/**************************************************************
 
177
/**********************************************************//**
166
178
Returns the number of microseconds since epoch. Similar to
167
179
time(3), the return value is also stored in *tloc, provided
168
 
that tloc is non-NULL. */
 
180
that tloc is non-NULL.
 
181
@return us since epoch */
169
182
UNIV_INTERN
170
183
ullint
171
184
ut_time_us(
172
185
/*=======*/
173
 
                        /* out: us since epoch */
174
 
        ullint* tloc)   /* out: us since epoch, if non-NULL */
 
186
        ullint* tloc)   /*!< out: us since epoch, if non-NULL */
175
187
{
176
188
        struct timeval  tv;
177
189
        ullint          us;
187
199
        return(us);
188
200
}
189
201
 
190
 
/**************************************************************
191
 
Returns the difference of two times in seconds. */
 
202
/**********************************************************//**
 
203
Returns the difference of two times in seconds.
 
204
@return time2 - time1 expressed in seconds */
192
205
UNIV_INTERN
193
206
double
194
207
ut_difftime(
195
208
/*========*/
196
 
                                /* out: time2 - time1 expressed in seconds */
197
 
        ib_time_t       time2,  /* in: time */
198
 
        ib_time_t       time1)  /* in: time */
 
209
        ib_time_t       time2,  /*!< in: time */
 
210
        ib_time_t       time1)  /*!< in: time */
199
211
{
200
212
        return(difftime(time2, time1));
201
213
}
202
214
 
203
 
/**************************************************************
 
215
/**********************************************************//**
204
216
Prints a timestamp to a file. */
205
217
UNIV_INTERN
206
218
void
207
219
ut_print_timestamp(
208
220
/*===============*/
209
 
        FILE*  file) /* in: file where to print */
 
221
        FILE*  file) /*!< in: file where to print */
210
222
{
211
223
#ifdef __WIN__
212
224
        SYSTEMTIME cal_tm;
243
255
#endif
244
256
}
245
257
 
246
 
/**************************************************************
 
258
/**********************************************************//**
247
259
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
248
260
UNIV_INTERN
249
261
void
250
262
ut_sprintf_timestamp(
251
263
/*=================*/
252
 
        char*   buf) /* in: buffer where to sprintf */
 
264
        char*   buf) /*!< in: buffer where to sprintf */
253
265
{
254
266
#ifdef __WIN__
255
267
        SYSTEMTIME cal_tm;
287
299
}
288
300
 
289
301
#ifdef UNIV_HOTBACKUP
290
 
/**************************************************************
 
302
/**********************************************************//**
291
303
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
292
304
replaced by '_'. */
293
305
UNIV_INTERN
294
306
void
295
307
ut_sprintf_timestamp_without_extra_chars(
296
308
/*=====================================*/
297
 
        char*   buf) /* in: buffer where to sprintf */
 
309
        char*   buf) /*!< in: buffer where to sprintf */
298
310
{
299
311
#ifdef __WIN__
300
312
        SYSTEMTIME cal_tm;
331
343
#endif
332
344
}
333
345
 
334
 
/**************************************************************
 
346
/**********************************************************//**
335
347
Returns current year, month, day. */
336
348
UNIV_INTERN
337
349
void
338
350
ut_get_year_month_day(
339
351
/*==================*/
340
 
        ulint*  year,   /* out: current year */
341
 
        ulint*  month,  /* out: month */
342
 
        ulint*  day)    /* out: day */
 
352
        ulint*  year,   /*!< out: current year */
 
353
        ulint*  month,  /*!< out: month */
 
354
        ulint*  day)    /*!< out: day */
343
355
{
344
356
#ifdef __WIN__
345
357
        SYSTEMTIME cal_tm;
369
381
}
370
382
#endif /* UNIV_HOTBACKUP */
371
383
 
372
 
/*****************************************************************
 
384
#ifndef UNIV_HOTBACKUP
 
385
/*************************************************************//**
373
386
Runs an idle loop on CPU. The argument gives the desired delay
374
 
in microseconds on 100 MHz Pentium + Visual C++. */
 
387
in microseconds on 100 MHz Pentium + Visual C++.
 
388
@return dummy value */
375
389
UNIV_INTERN
376
390
ulint
377
391
ut_delay(
378
392
/*=====*/
379
 
                        /* out: dummy value */
380
 
        ulint   delay)  /* in: delay in microseconds on 100 MHz Pentium */
 
393
        ulint   delay)  /*!< in: delay in microseconds on 100 MHz Pentium */
381
394
{
382
395
        ulint   i, j;
383
396
 
385
398
 
386
399
        for (i = 0; i < delay * 50; i++) {
387
400
                j += i;
 
401
                UT_RELAX_CPU();
388
402
        }
389
403
 
390
404
        if (ut_always_false) {
393
407
 
394
408
        return(j);
395
409
}
 
410
#endif /* !UNIV_HOTBACKUP */
396
411
 
397
 
/*****************************************************************
 
412
/*************************************************************//**
398
413
Prints the contents of a memory buffer in hex and ascii. */
399
414
UNIV_INTERN
400
415
void
401
416
ut_print_buf(
402
417
/*=========*/
403
 
        FILE*           file,   /* in: file where to print */
404
 
        const void*     buf,    /* in: memory buffer */
405
 
        ulint           len)    /* in: length of the buffer */
 
418
        FILE*           file,   /*!< in: file where to print */
 
419
        const void*     buf,    /*!< in: memory buffer */
 
420
        ulint           len)    /*!< in: length of the buffer */
406
421
{
407
422
        const byte*     data;
408
423
        ulint           i;
427
442
        putc(';', file);
428
443
}
429
444
 
430
 
/*****************************************************************
431
 
Calculates fast the number rounded up to the nearest power of 2. */
 
445
/*************************************************************//**
 
446
Calculates fast the number rounded up to the nearest power of 2.
 
447
@return first power of 2 which is >= n */
432
448
UNIV_INTERN
433
449
ulint
434
450
ut_2_power_up(
435
451
/*==========*/
436
 
                        /* out: first power of 2 which is >= n */
437
 
        ulint   n)      /* in: number != 0 */
 
452
        ulint   n)      /*!< in: number != 0 */
438
453
{
439
454
        ulint   res;
440
455
 
449
464
        return(res);
450
465
}
451
466
 
452
 
/**************************************************************************
 
467
/**********************************************************************//**
453
468
Outputs a NUL-terminated file name, quoted with apostrophes. */
454
469
UNIV_INTERN
455
470
void
456
471
ut_print_filename(
457
472
/*==============*/
458
 
        FILE*           f,      /* in: output stream */
459
 
        const char*     name)   /* in: name to print */
 
473
        FILE*           f,      /*!< in: output stream */
 
474
        const char*     name)   /*!< in: name to print */
460
475
{
461
476
        putc('\'', f);
462
477
        for (;;) {
474
489
done:
475
490
        putc('\'', f);
476
491
}
477
 
 
478
 
/**************************************************************************
 
492
#ifndef UNIV_HOTBACKUP
 
493
/**********************************************************************//**
479
494
Outputs a fixed-length string, quoted as an SQL identifier.
480
495
If the string contains a slash '/', the string will be
481
496
output as two identifiers separated by a period (.),
484
499
void
485
500
ut_print_name(
486
501
/*==========*/
487
 
        FILE*           f,      /* in: output stream */
488
 
        trx_t*          trx,    /* in: transaction */
489
 
        ibool           table_id,/* in: TRUE=print a table name,
 
502
        FILE*           f,      /*!< in: output stream */
 
503
        trx_t*          trx,    /*!< in: transaction */
 
504
        ibool           table_id,/*!< in: TRUE=print a table name,
490
505
                                FALSE=print other identifier */
491
 
        const char*     name)   /* in: name to print */
 
506
        const char*     name)   /*!< in: name to print */
492
507
{
493
508
        ut_print_namel(f, trx, table_id, name, strlen(name));
494
509
}
495
510
 
496
 
/**************************************************************************
 
511
/**********************************************************************//**
497
512
Outputs a fixed-length string, quoted as an SQL identifier.
498
513
If the string contains a slash '/', the string will be
499
514
output as two identifiers separated by a period (.),
502
517
void
503
518
ut_print_namel(
504
519
/*===========*/
505
 
        FILE*           f,      /* in: output stream */
506
 
        trx_t*          trx,    /* in: transaction (NULL=no quotes) */
507
 
        ibool           table_id,/* in: TRUE=print a table name,
 
520
        FILE*           f,      /*!< in: output stream */
 
521
        trx_t*          trx,    /*!< in: transaction (NULL=no quotes) */
 
522
        ibool           table_id,/*!< in: TRUE=print a table name,
508
523
                                FALSE=print other identifier */
509
 
        const char*     name,   /* in: name to print */
510
 
        ulint           namelen)/* in: length of name */
 
524
        const char*     name,   /*!< in: name to print */
 
525
        ulint           namelen)/*!< in: length of name */
511
526
{
512
 
#ifdef UNIV_HOTBACKUP
513
 
        fwrite(name, 1, namelen, f);
514
 
#else
515
527
        /* 2 * NAME_LEN for database and table name,
516
528
        and some slack for the #mysql50# prefix and quotes */
517
529
        char            buf[3 * NAME_LEN];
523
535
                                       table_id);
524
536
 
525
537
        fwrite(buf, 1, bufend - buf, f);
526
 
#endif
527
538
}
528
539
 
529
 
/**************************************************************************
 
540
/**********************************************************************//**
530
541
Catenate files. */
531
542
UNIV_INTERN
532
543
void
533
544
ut_copy_file(
534
545
/*=========*/
535
 
        FILE*   dest,   /* in: output file */
536
 
        FILE*   src)    /* in: input file to be appended to output */
 
546
        FILE*   dest,   /*!< in: output file */
 
547
        FILE*   src)    /*!< in: input file to be appended to output */
537
548
{
538
549
        long    len = ftell(src);
539
550
        char    buf[4096];
551
562
                }
552
563
        } while (len > 0);
553
564
}
554
 
 
555
 
/**************************************************************************
556
 
snprintf(). */
 
565
#endif /* !UNIV_HOTBACKUP */
557
566
 
558
567
#ifdef __WIN__
559
 
#include <stdarg.h>
 
568
# include <stdarg.h>
 
569
/**********************************************************************//**
 
570
A substitute for snprintf(3), formatted output conversion into
 
571
a limited buffer.
 
572
@return number of characters that would have been printed if the size
 
573
were unlimited, not including the terminating '\0'. */
 
574
UNIV_INTERN
560
575
int
561
576
ut_snprintf(
562
 
                                /* out: number of characters that would
563
 
                                have been printed if the size were
564
 
                                unlimited, not including the terminating
565
 
                                '\0'. */
566
 
        char*           str,    /* out: string */
567
 
        size_t          size,   /* in: str size */
568
 
        const char*     fmt,    /* in: format */
569
 
        ...)                    /* in: format values */
 
577
/*========*/
 
578
        char*           str,    /*!< out: string */
 
579
        size_t          size,   /*!< in: str size */
 
580
        const char*     fmt,    /*!< in: format */
 
581
        ...)                    /*!< in: format values */
570
582
{
571
583
        int     res;
572
584
        va_list ap1;