~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2010-01-27 18:58:12 UTC
  • Revision ID: brian@gaz-20100127185812-n62n0vwetnx8jrjy
Remove dead code.

Show diffs side-by-side

added added

removed removed

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