engine/sqlite/src/os_common.h
author teknolog
Sat, 24 Apr 2010 21:46:17 +0100
changeset 120 31d05afa1a52
parent 2 29cda98b007e
permissions -rw-r--r--
Added video podcast icons, but they don't work correctly yet.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     1
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     2
** 2004 May 22
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     3
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     4
** The author disclaims copyright to this source code.  In place of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     5
** a legal notice, here is a blessing:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     6
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     7
**    May you do good and not evil.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     8
**    May you find forgiveness for yourself and forgive others.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     9
**    May you share freely, never taking more than you give.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    10
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    11
******************************************************************************
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    12
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    13
** This file contains macros and a little bit of code that is common to
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    14
** all of the platform-specific files (os_*.c) and is #included into those
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    15
** files.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    16
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    17
** This file should be #included by the os_*.c files only.  It is not a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    18
** general purpose header file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    19
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    20
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    21
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    22
** At least two bugs have slipped in because we changed the MEMORY_DEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    23
** macro to SQLITE_DEBUG and some older makefiles have not yet made the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    24
** switch.  The following code should catch this problem at compile-time.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    25
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    26
#ifdef MEMORY_DEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    27
# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    28
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    29
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    30
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    31
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    32
 * When testing, this global variable stores the location of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    33
 * pending-byte in the database file.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    34
 */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    35
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    36
unsigned int sqlite3_pending_byte = 0x40000000;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    37
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    38
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    39
#ifdef SQLITE_DEBUG
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    40
int sqlite3_os_trace = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    41
#define OSTRACE1(X)         if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    42
#define OSTRACE2(X,Y)       if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    43
#define OSTRACE3(X,Y,Z)     if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    44
#define OSTRACE4(X,Y,Z,A)   if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    45
#define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    46
#define OSTRACE6(X,Y,Z,A,B,C) \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    47
    if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    48
#define OSTRACE7(X,Y,Z,A,B,C,D) \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    49
    if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    50
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    51
#define OSTRACE1(X)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    52
#define OSTRACE2(X,Y)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    53
#define OSTRACE3(X,Y,Z)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    54
#define OSTRACE4(X,Y,Z,A)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    55
#define OSTRACE5(X,Y,Z,A,B)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    56
#define OSTRACE6(X,Y,Z,A,B,C)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    57
#define OSTRACE7(X,Y,Z,A,B,C,D)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    58
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    59
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    60
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    61
** Macros for performance tracing.  Normally turned off.  Only works
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    62
** on i486 hardware.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    63
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    64
#ifdef SQLITE_PERFORMANCE_TRACE
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    65
__inline__ unsigned long long int hwtime(void){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    66
  unsigned long long int x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    67
  __asm__("rdtsc\n\t"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    68
          "mov %%edx, %%ecx\n\t"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    69
          :"=A" (x));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    70
  return x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    71
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    72
static unsigned long long int g_start;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    73
static unsigned int elapse;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    74
#define TIMER_START       g_start=hwtime()
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    75
#define TIMER_END         elapse=hwtime()-g_start
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    76
#define TIMER_ELAPSED     elapse
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    77
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    78
#define TIMER_START
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    79
#define TIMER_END
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    80
#define TIMER_ELAPSED     0
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    81
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    82
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    83
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    84
** If we compile with the SQLITE_TEST macro set, then the following block
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    85
** of code will give us the ability to simulate a disk I/O error.  This
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    86
** is used for testing the I/O recovery logic.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    87
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    88
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    89
int sqlite3_io_error_hit = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    90
int sqlite3_io_error_pending = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    91
int sqlite3_io_error_persist = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    92
int sqlite3_diskfull_pending = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    93
int sqlite3_diskfull = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    94
#define SimulateIOError(CODE)  \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    95
  if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    96
     if( sqlite3_io_error_pending-- == 1 \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    97
         || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    98
                { local_ioerr(); CODE; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    99
static void local_ioerr(){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   100
  IOTRACE(("IOERR\n"));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   101
  sqlite3_io_error_hit = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   102
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   103
#define SimulateDiskfullError(CODE) \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   104
   if( sqlite3_diskfull_pending ){ \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   105
     if( sqlite3_diskfull_pending == 1 ){ \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   106
       local_ioerr(); \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   107
       sqlite3_diskfull = 1; \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   108
       sqlite3_io_error_hit = 1; \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   109
       CODE; \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   110
     }else{ \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   111
       sqlite3_diskfull_pending--; \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   112
     } \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   113
   }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   114
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   115
#define SimulateIOError(A)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   116
#define SimulateDiskfullError(A)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   117
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   118
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   119
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   120
** When testing, keep a count of the number of open files.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   121
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   122
#ifdef SQLITE_TEST
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   123
int sqlite3_open_file_count = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   124
#define OpenCounter(X)  sqlite3_open_file_count+=(X)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   125
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   126
#define OpenCounter(X)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   127
#endif