AryaWu/sqlite
0
1/*2** 2001 September 153**4** The author disclaims copyright to this source code. In place of5** a legal notice, here is a blessing:6**7** May you do good and not evil.8** May you find forgiveness for yourself and forgive others.9** May you share freely, never taking more than you give.10**11*************************************************************************12** Internal interface definitions for SQLite.13**14*/15#ifndef SQLITEINT_H16#define SQLITEINT_H17 18/* Special Comments:19**20** Some comments have special meaning to the tools that measure test21** coverage:22**23** NO_TEST - The branches on this line are not24** measured by branch coverage. This is25** used on lines of code that actually26** implement parts of coverage testing.27**28** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false29** and the correct answer is still obtained,30** though perhaps more slowly.31**32** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true33** and the correct answer is still obtained,34** though perhaps more slowly.35**36** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread37** that would be harmless and undetectable38** if it did occur.39**40** In all cases, the special comment must be enclosed in the usual41** slash-asterisk...asterisk-slash comment marks, with no spaces between the42** asterisks and the comment text.43*/44 45/*46** Make sure the Tcl calling convention macro is defined. This macro is47** only used by test code and Tcl integration code.48*/49#ifndef SQLITE_TCLAPI50# define SQLITE_TCLAPI51#endif52 53/*54** Include the header file used to customize the compiler options for MSVC.55** This should be done first so that it can successfully prevent spurious56** compiler warnings due to subsequent content in this file and other files57** that are included by this file.58*/59#include "msvc.h"60 61/*62** Special setup for VxWorks63*/64#include "vxworks.h"65 66/*67** These #defines should enable >2GB file support on POSIX if the68** underlying operating system supports it. If the OS lacks69** large file support, or if the OS is windows, these should be no-ops.70**71** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any72** system #includes. Hence, this block of code must be the very first73** code in all source files.74**75** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch76** on the compiler command line. This is necessary if you are compiling77** on a recent machine (ex: Red Hat 7.2) but you want your code to work78** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.279** without this option, LFS is enable. But LFS does not exist in the kernel80** in Red Hat 6.0, so the code won't work. Hence, for maximum binary81** portability you should omit LFS.82**83** The previous paragraph was written in 2005. (This paragraph is written84** on 2008-11-28.) These days, all Linux kernels support large files, so85** you should probably leave LFS enabled. But some embedded platforms might86** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.87**88** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.89*/90#ifndef SQLITE_DISABLE_LFS91# define _LARGE_FILE 192# ifndef _FILE_OFFSET_BITS93# define _FILE_OFFSET_BITS 6494# endif95# define _LARGEFILE_SOURCE 196#endif97 98/* The GCC_VERSION and MSVC_VERSION macros are used to99** conditionally include optimizations for each of these compilers. A100** value of 0 means that compiler is not being used. The101** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific102** optimizations, and hence set all compiler macros to 0103**104** There was once also a CLANG_VERSION macro. However, we learn that the105** version numbers in clang are for "marketing" only and are inconsistent106** and unreliable. Fortunately, all versions of clang also recognize the107** gcc version numbers and have reasonable settings for gcc version numbers,108** so the GCC_VERSION macro will be set to a correct non-zero value even109** when compiling with clang.110*/111#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)112# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)113#else114# define GCC_VERSION 0115#endif116#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)117# define MSVC_VERSION _MSC_VER118#else119# define MSVC_VERSION 0120#endif121 122/*123** Some C99 functions in "math.h" are only present for MSVC when its version124** is associated with Visual Studio 2013 or higher.125*/126#ifndef SQLITE_HAVE_C99_MATH_FUNCS127# if MSVC_VERSION==0 || MSVC_VERSION>=1800128# define SQLITE_HAVE_C99_MATH_FUNCS (1)129# else130# define SQLITE_HAVE_C99_MATH_FUNCS (0)131# endif132#endif133 134/* Needed for various definitions... */135#if defined(__GNUC__) && !defined(_GNU_SOURCE)136# define _GNU_SOURCE137#endif138 139#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)140# define _BSD_SOURCE141#endif142 143/*144** Macro to disable warnings about missing "break" at the end of a "case".145*/146#if defined(__has_attribute)147# if __has_attribute(fallthrough)148# define deliberate_fall_through __attribute__((fallthrough));149# endif150#endif151#if !defined(deliberate_fall_through)152# define deliberate_fall_through153#endif154 155/*156** For MinGW, check to see if we can include the header file containing its157** version information, among other things. Normally, this internal MinGW158** header file would [only] be included automatically by other MinGW header159** files; however, the contained version information is now required by this160** header file to work around binary compatibility issues (see below) and161** this is the only known way to reliably obtain it. This entire #if block162** would be completely unnecessary if there was any other way of detecting163** MinGW via their preprocessor (e.g. if they customized their GCC to define164** some MinGW-specific macros). When compiling for MinGW, either the165** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be166** defined; otherwise, detection of conditions specific to MinGW will be167** disabled.168*/169#if defined(_HAVE_MINGW_H)170# include "mingw.h"171#elif defined(_HAVE__MINGW_H)172# include "_mingw.h"173#endif174 175/*176** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T177** define is required to maintain binary compatibility with the MSVC runtime178** library in use (e.g. for Windows XP).179*/180#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \181 defined(_WIN32) && !defined(_WIN64) && \182 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \183 defined(__MSVCRT__)184# define _USE_32BIT_TIME_T185#endif186 187/* Optionally #include a user-defined header, whereby compilation options188** may be set prior to where they take effect, but after platform setup.189** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include190** file.191*/192#ifdef SQLITE_CUSTOM_INCLUDE193# define INC_STRINGIFY_(f) #f194# define INC_STRINGIFY(f) INC_STRINGIFY_(f)195# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)196#endif197 198/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear199** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for200** MinGW.201*/202#include "sqlite3.h"203 204/*205** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory.206*/207#define SQLITE_MUTEX_STATIC_TEMPDIR SQLITE_MUTEX_STATIC_VFS1208 209/*210** Include the configuration header output by 'configure' if we're using the211** autoconf-based build212*/213#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)214#include "sqlite_cfg.h"215#define SQLITECONFIG_H 1216#endif217 218#include "sqliteLimit.h"219 220/* Disable nuisance warnings on Borland compilers */221#if defined(__BORLANDC__)222#pragma warn -rch /* unreachable code */223#pragma warn -ccc /* Condition is always true or false */224#pragma warn -aus /* Assigned value is never used */225#pragma warn -csu /* Comparing signed and unsigned */226#pragma warn -spa /* Suspicious pointer arithmetic */227#endif228 229/*230** A few places in the code require atomic load/store of aligned231** integer values.232*/233#ifndef __has_extension234# define __has_extension(x) 0 /* compatibility with non-clang compilers */235#endif236#if GCC_VERSION>=4007000 || __has_extension(c_atomic)237# define SQLITE_ATOMIC_INTRINSICS 1238# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED)239# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)240#else241# define SQLITE_ATOMIC_INTRINSICS 0242# define AtomicLoad(PTR) (*(PTR))243# define AtomicStore(PTR,VAL) (*(PTR) = (VAL))244#endif245 246/*247** Include standard header files as necessary248*/249#ifdef HAVE_STDINT_H250#include <stdint.h>251#endif252#ifdef HAVE_INTTYPES_H253#include <inttypes.h>254#endif255 256/*257** The following macros are used to cast pointers to integers and258** integers to pointers. The way you do this varies from one compiler259** to the next, so we have developed the following set of #if statements260** to generate appropriate macros for a wide range of compilers.261**262** The correct "ANSI" way to do this is to use the intptr_t type.263** Unfortunately, that typedef is not available on all compilers, or264** if it is available, it requires an #include of specific headers265** that vary from one machine to the next.266**267** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on268** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).269** So we have to define the macros in different ways depending on the270** compiler.271*/272#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */273# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))274# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))275#elif defined(__PTRDIFF_TYPE__) /* This case should work for GCC */276# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))277# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))278#elif !defined(__GNUC__) /* Works for compilers other than LLVM */279# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])280# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))281#else /* Generates a warning - but it always works */282# define SQLITE_INT_TO_PTR(X) ((void*)(X))283# define SQLITE_PTR_TO_INT(X) ((int)(X))284#endif285 286/*287** Macros to hint to the compiler that a function should or should not be288** inlined.289*/290#if defined(__GNUC__)291# define SQLITE_NOINLINE __attribute__((noinline))292# define SQLITE_INLINE __attribute__((always_inline)) inline293#elif defined(_MSC_VER) && _MSC_VER>=1310294# define SQLITE_NOINLINE __declspec(noinline)295# define SQLITE_INLINE __forceinline296#else297# define SQLITE_NOINLINE298# define SQLITE_INLINE299#endif300#if defined(SQLITE_COVERAGE_TEST) || defined(__STRICT_ANSI__)301# undef SQLITE_INLINE302# define SQLITE_INLINE303#endif304 305/*306** Make sure that the compiler intrinsics we desire are enabled when307** compiling with an appropriate version of MSVC unless prevented by308** the SQLITE_DISABLE_INTRINSIC define.309*/310#if !defined(SQLITE_DISABLE_INTRINSIC)311# if defined(_MSC_VER) && _MSC_VER>=1400312# if !defined(_WIN32_WCE)313# include <intrin.h>314# pragma intrinsic(_byteswap_ushort)315# pragma intrinsic(_byteswap_ulong)316# pragma intrinsic(_byteswap_uint64)317# pragma intrinsic(_ReadWriteBarrier)318# else319# include <cmnintrin.h>320# endif321# endif322#endif323 324/*325** Enable SQLITE_USE_SEH by default on MSVC builds. Only omit326** SEH support if the -DSQLITE_OMIT_SEH option is given.327*/328#if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH)329# define SQLITE_USE_SEH 1330#else331# undef SQLITE_USE_SEH332#endif333 334/*335** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly336** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0337*/338#if defined(SQLITE_DIRECT_OVERFLOW_READ) && SQLITE_DIRECT_OVERFLOW_READ+1==1339 /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */340# undef SQLITE_DIRECT_OVERFLOW_READ341#else342 /* In all other cases, enable */343# define SQLITE_DIRECT_OVERFLOW_READ 1344#endif345 346 347/*348** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.349** 0 means mutexes are permanently disable and the library is never350** threadsafe. 1 means the library is serialized which is the highest351** level of threadsafety. 2 means the library is multithreaded - multiple352** threads can use SQLite as long as no two threads try to use the same353** database connection at the same time.354**355** Older versions of SQLite used an optional THREADSAFE macro.356** We support that for legacy.357**358** To ensure that the correct value of "THREADSAFE" is reported when querying359** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this360** logic is partially replicated in ctime.c. If it is updated here, it should361** also be updated there.362*/363#if !defined(SQLITE_THREADSAFE)364# if defined(THREADSAFE)365# define SQLITE_THREADSAFE THREADSAFE366# else367# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */368# endif369#endif370 371/*372** Powersafe overwrite is on by default. But can be turned off using373** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.374*/375#ifndef SQLITE_POWERSAFE_OVERWRITE376# define SQLITE_POWERSAFE_OVERWRITE 1377#endif378 379/*380** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by381** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in382** which case memory allocation statistics are disabled by default.383*/384#if !defined(SQLITE_DEFAULT_MEMSTATUS)385# define SQLITE_DEFAULT_MEMSTATUS 1386#endif387 388/*389** Exactly one of the following macros must be defined in order to390** specify which memory allocation subsystem to use.391**392** SQLITE_SYSTEM_MALLOC // Use normal system malloc()393** SQLITE_WIN32_MALLOC // Use Win32 native heap API394** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails395** SQLITE_MEMDEBUG // Debugging version of system malloc()396**397** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the398** assert() macro is enabled, each call into the Win32 native heap subsystem399** will cause HeapValidate to be called. If heap validation should fail, an400** assertion will be triggered.401**402** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as403** the default.404*/405#if defined(SQLITE_SYSTEM_MALLOC) \406 + defined(SQLITE_WIN32_MALLOC) \407 + defined(SQLITE_ZERO_MALLOC) \408 + defined(SQLITE_MEMDEBUG)>1409# error "Two or more of the following compile-time configuration options\410 are defined but at most one is allowed:\411 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\412 SQLITE_ZERO_MALLOC"413#endif414#if defined(SQLITE_SYSTEM_MALLOC) \415 + defined(SQLITE_WIN32_MALLOC) \416 + defined(SQLITE_ZERO_MALLOC) \417 + defined(SQLITE_MEMDEBUG)==0418# define SQLITE_SYSTEM_MALLOC 1419#endif420 421/*422** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the423** sizes of memory allocations below this value where possible.424*/425#if !defined(SQLITE_MALLOC_SOFT_LIMIT)426# define SQLITE_MALLOC_SOFT_LIMIT 1024427#endif428 429/*430** We need to define _XOPEN_SOURCE as follows in order to enable431** recursive mutexes on most Unix systems and fchmod() on OpenBSD.432** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit433** it.434*/435#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)436# define _XOPEN_SOURCE 600437#endif438 439/*440** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that441** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,442** make it true by defining or undefining NDEBUG.443**444** Setting NDEBUG makes the code smaller and faster by disabling the445** assert() statements in the code. So we want the default action446** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG447** is set. Thus NDEBUG becomes an opt-in rather than an opt-out448** feature.449*/450#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)451# define NDEBUG 1452#endif453#if defined(NDEBUG) && defined(SQLITE_DEBUG)454# undef NDEBUG455#endif456 457/*458** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.459*/460#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)461# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1462#endif463 464/*465** The testcase() macro is used to aid in coverage testing. When466** doing coverage testing, the condition inside the argument to467** testcase() must be evaluated both true and false in order to468** get full branch coverage. The testcase() macro is inserted469** to help ensure adequate test coverage in places where simple470** condition/decision coverage is inadequate. For example, testcase()471** can be used to make sure boundary values are tested. For472** bitmask tests, testcase() can be used to make sure each bit473** is significant and used at least once. On switch statements474** where multiple cases go to the same block of code, testcase()475** can insure that all cases are evaluated.476*/477#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)478# ifndef SQLITE_AMALGAMATION479 extern unsigned int sqlite3CoverageCounter;480# endif481# define testcase(X) if( X ){ sqlite3CoverageCounter += (unsigned)__LINE__; }482#else483# define testcase(X)484#endif485 486/*487** The TESTONLY macro is used to enclose variable declarations or488** other bits of code that are needed to support the arguments489** within testcase() and assert() macros.490*/491#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)492# define TESTONLY(X) X493#else494# define TESTONLY(X)495#endif496 497/*498** Sometimes we need a small amount of code such as a variable initialization499** to setup for a later assert() statement. We do not want this code to500** appear when assert() is disabled. The following macro is therefore501** used to contain that setup code. The "VVA" acronym stands for502** "Verification, Validation, and Accreditation". In other words, the503** code within VVA_ONLY() will only run during verification processes.504*/505#ifndef NDEBUG506# define VVA_ONLY(X) X507#else508# define VVA_ONLY(X)509#endif510 511/*512** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage513** and mutation testing514*/515#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)516# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1517#endif518 519/*520** The ALWAYS and NEVER macros surround boolean expressions which521** are intended to always be true or false, respectively. Such522** expressions could be omitted from the code completely. But they523** are included in a few cases in order to enhance the resilience524** of SQLite to unexpected behavior - to make the code "self-healing"525** or "ductile" rather than being "brittle" and crashing at the first526** hint of unplanned behavior.527**528** In other words, ALWAYS and NEVER are added for defensive code.529**530** When doing coverage testing ALWAYS and NEVER are hard-coded to531** be true and false so that the unreachable code they specify will532** not be counted as untested code.533*/534#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)535# define ALWAYS(X) (1)536# define NEVER(X) (0)537#elif !defined(NDEBUG)538# define ALWAYS(X) ((X)?1:(assert(0),0))539# define NEVER(X) ((X)?(assert(0),1):0)540#else541# define ALWAYS(X) (X)542# define NEVER(X) (X)543#endif544 545/*546** Some conditionals are optimizations only. In other words, if the547** conditionals are replaced with a constant 1 (true) or 0 (false) then548** the correct answer is still obtained, though perhaps not as quickly.549**550** The following macros mark these optimizations conditionals.551*/552#if defined(SQLITE_MUTATION_TEST)553# define OK_IF_ALWAYS_TRUE(X) (1)554# define OK_IF_ALWAYS_FALSE(X) (0)555#else556# define OK_IF_ALWAYS_TRUE(X) (X)557# define OK_IF_ALWAYS_FALSE(X) (X)558#endif559 560/*561** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is562** defined. We need to defend against those failures when testing with563** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches564** during a normal build. The following macro can be used to disable tests565** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.566*/567#if defined(SQLITE_TEST_REALLOC_STRESS)568# define ONLY_IF_REALLOC_STRESS(X) (X)569#elif !defined(NDEBUG)570# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0)571#else572# define ONLY_IF_REALLOC_STRESS(X) (0)573#endif574 575/*576** Declarations used for tracing the operating system interfaces.577*/578#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \579 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)580 extern int sqlite3OSTrace;581# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X582# define SQLITE_HAVE_OS_TRACE583#else584# define OSTRACE(X)585# undef SQLITE_HAVE_OS_TRACE586#endif587 588/*589** Is the sqlite3ErrName() function needed in the build? Currently,590** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when591** OSTRACE is enabled), and by several "test*.c" files (which are592** compiled using SQLITE_TEST).593*/594#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \595 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)596# define SQLITE_NEED_ERR_NAME597#else598# undef SQLITE_NEED_ERR_NAME599#endif600 601/*602** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN603*/604#ifdef SQLITE_OMIT_EXPLAIN605# undef SQLITE_ENABLE_EXPLAIN_COMMENTS606#endif607 608/*609** SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTERTABLE610*/611#if defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_ALTERTABLE)612# define SQLITE_OMIT_ALTERTABLE613#endif614 615#define SQLITE_DIGIT_SEPARATOR '_'616 617/*618** Return true (non-zero) if the input is an integer that is too large619** to fit in 32-bits. This macro is used inside of various testcase()620** macros to verify that we have tested SQLite for large-file support.621*/622#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)623 624/*625** The macro unlikely() is a hint that surrounds a boolean626** expression that is usually false. Macro likely() surrounds627** a boolean expression that is usually true. These hints could,628** in theory, be used by the compiler to generate better code, but629** currently they are just comments for human readers.630*/631#define likely(X) (X)632#define unlikely(X) (X)633 634#include "hash.h"635#include "parse.h"636#include <stdio.h>637#include <stdlib.h>638#include <string.h>639#include <assert.h>640#include <stddef.h>641#include <ctype.h>642 643/*644** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.645** This allows better measurements of where memcpy() is used when running646** cachegrind. But this macro version of memcpy() is very slow so it647** should not be used in production. This is a performance measurement648** hack only.649*/650#ifdef SQLITE_INLINE_MEMCPY651# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\652 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}653#endif654 655/*656** If compiling for a processor that lacks floating point support,657** substitute integer for floating-point658*/659#ifdef SQLITE_OMIT_FLOATING_POINT660# define double sqlite_int64661# define float sqlite_int64662# define fabs(X) ((X)<0?-(X):(X))663# define sqlite3IsOverflow(X) 0664# ifndef SQLITE_BIG_DBL665# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)666# endif667# define SQLITE_OMIT_DATETIME_FUNCS 1668# define SQLITE_OMIT_TRACE 1669# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT670# undef SQLITE_HAVE_ISNAN671#endif672#ifndef SQLITE_BIG_DBL673# define SQLITE_BIG_DBL (1e99)674#endif675 676/*677** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0678** afterward. Having this macro allows us to cause the C compiler679** to omit code used by TEMP tables without messy #ifndef statements.680*/681#ifdef SQLITE_OMIT_TEMPDB682#define OMIT_TEMPDB 1683#else684#define OMIT_TEMPDB 0685#endif686 687/*688** The "file format" number is an integer that is incremented whenever689** the VDBE-level file format changes. The following macros define the690** the default file format for new databases and the maximum file format691** that the library can read.692*/693#define SQLITE_MAX_FILE_FORMAT 4694#ifndef SQLITE_DEFAULT_FILE_FORMAT695# define SQLITE_DEFAULT_FILE_FORMAT 4696#endif697 698/*699** Determine whether triggers are recursive by default. This can be700** changed at run-time using a pragma.701*/702#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS703# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0704#endif705 706/*707** Provide a default value for SQLITE_TEMP_STORE in case it is not specified708** on the command-line709*/710#ifndef SQLITE_TEMP_STORE711# define SQLITE_TEMP_STORE 1712#endif713 714/*715** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if716** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it717** to zero.718*/719#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0720# undef SQLITE_MAX_WORKER_THREADS721# define SQLITE_MAX_WORKER_THREADS 0722#endif723#ifndef SQLITE_MAX_WORKER_THREADS724# define SQLITE_MAX_WORKER_THREADS 8725#endif726#ifndef SQLITE_DEFAULT_WORKER_THREADS727# define SQLITE_DEFAULT_WORKER_THREADS 0728#endif729#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS730# undef SQLITE_MAX_WORKER_THREADS731# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS732#endif733 734/*735** The default initial allocation for the pagecache when using separate736** pagecaches for each database connection. A positive number is the737** number of pages. A negative number N translations means that a buffer738** of -1024*N bytes is allocated and used for as many pages as it will hold.739**740** The default value of "20" was chosen to minimize the run-time of the741** speedtest1 test program with options: --shrink-memory --reprepare742*/743#ifndef SQLITE_DEFAULT_PCACHE_INITSZ744# define SQLITE_DEFAULT_PCACHE_INITSZ 20745#endif746 747/*748** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.749*/750#ifndef SQLITE_DEFAULT_SORTERREF_SIZE751# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff752#endif753 754/*755** The compile-time options SQLITE_MMAP_READWRITE and756** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.757** You must choose one or the other (or neither) but not both.758*/759#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)760#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE761#endif762 763/*764** GCC does not define the offsetof() macro so we'll have to do it765** ourselves.766*/767#ifndef offsetof768# define offsetof(ST,M) ((size_t)((char*)&((ST*)0)->M - (char*)0))769#endif770 771/*772** Work around C99 "flex-array" syntax for pre-C99 compilers, so as773** to avoid complaints from -fsanitize=strict-bounds.774*/775#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)776# define FLEXARRAY777#else778# define FLEXARRAY 1779#endif780 781/*782** Macros to compute minimum and maximum of two numbers.783*/784#ifndef MIN785# define MIN(A,B) ((A)<(B)?(A):(B))786#endif787#ifndef MAX788# define MAX(A,B) ((A)>(B)?(A):(B))789#endif790 791/*792** Swap two objects of type TYPE.793*/794#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}795 796/*797** Check to see if this machine uses EBCDIC. (Yes, believe it or798** not, there are still machines out there that use EBCDIC.)799*/800#if 'A' == '\301'801# define SQLITE_EBCDIC 1802#else803# define SQLITE_ASCII 1804#endif805 806/*807** Integers of known sizes. These typedefs might change for architectures808** where the sizes very. Preprocessor macros are available so that the809** types can be conveniently redefined at compile-type. Like this:810**811** cc '-DUINTPTR_TYPE=long long int' ...812*/813#ifndef UINT32_TYPE814# ifdef HAVE_UINT32_T815# define UINT32_TYPE uint32_t816# else817# define UINT32_TYPE unsigned int818# endif819#endif820#ifndef UINT16_TYPE821# ifdef HAVE_UINT16_T822# define UINT16_TYPE uint16_t823# else824# define UINT16_TYPE unsigned short int825# endif826#endif827#ifndef INT16_TYPE828# ifdef HAVE_INT16_T829# define INT16_TYPE int16_t830# else831# define INT16_TYPE short int832# endif833#endif834#ifndef UINT8_TYPE835# ifdef HAVE_UINT8_T836# define UINT8_TYPE uint8_t837# else838# define UINT8_TYPE unsigned char839# endif840#endif841#ifndef INT8_TYPE842# ifdef HAVE_INT8_T843# define INT8_TYPE int8_t844# else845# define INT8_TYPE signed char846# endif847#endif848typedef sqlite_int64 i64; /* 8-byte signed integer */849typedef sqlite_uint64 u64; /* 8-byte unsigned integer */850typedef UINT32_TYPE u32; /* 4-byte unsigned integer */851typedef UINT16_TYPE u16; /* 2-byte unsigned integer */852typedef INT16_TYPE i16; /* 2-byte signed integer */853typedef UINT8_TYPE u8; /* 1-byte unsigned integer */854typedef INT8_TYPE i8; /* 1-byte signed integer */855 856/* A bitfield type for use inside of structures. Always follow with :N where857** N is the number of bits.858*/859typedef unsigned bft; /* Bit Field Type */860 861/*862** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value863** that can be stored in a u32 without loss of data. The value864** is 0x00000000ffffffff. But because of quirks of some compilers, we865** have to specify the value in the less intuitive manner shown:866*/867#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)868 869/*870** The datatype used to store estimates of the number of rows in a871** table or index.872*/873typedef u64 tRowcnt;874 875/*876** Estimated quantities used for query planning are stored as 16-bit877** logarithms. For quantity X, the value stored is 10*log2(X). This878** gives a possible range of values of approximately 1.0e986 to 1e-986.879** But the allowed values are "grainy". Not every value is representable.880** For example, quantities 16 and 17 are both represented by a LogEst881** of 40. However, since LogEst quantities are suppose to be estimates,882** not exact values, this imprecision is not a problem.883**884** "LogEst" is short for "Logarithmic Estimate".885**886** Examples:887** 1 -> 0 20 -> 43 10000 -> 132888** 2 -> 10 25 -> 46 25000 -> 146889** 3 -> 16 100 -> 66 1000000 -> 199890** 4 -> 20 1000 -> 99 1048576 -> 200891** 10 -> 33 1024 -> 100 4294967296 -> 320892**893** The LogEst can be negative to indicate fractional values.894** Examples:895**896** 0.5 -> -10 0.1 -> -33 0.0625 -> -40897*/898typedef INT16_TYPE LogEst;899#define LOGEST_MIN (-32768)900#define LOGEST_MAX (32767)901 902/*903** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer904*/905#ifndef SQLITE_PTRSIZE906# if defined(__SIZEOF_POINTER__)907# define SQLITE_PTRSIZE __SIZEOF_POINTER__908# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \909 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \910 (defined(__APPLE__) && defined(__ppc__)) || \911 (defined(__TOS_AIX__) && !defined(__64BIT__))912# define SQLITE_PTRSIZE 4913# else914# define SQLITE_PTRSIZE 8915# endif916#endif917 918/* The uptr type is an unsigned integer large enough to hold a pointer919*/920#if defined(HAVE_STDINT_H)921 typedef uintptr_t uptr;922#elif SQLITE_PTRSIZE==4923 typedef u32 uptr;924#else925 typedef u64 uptr;926#endif927 928/*929** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to930** something between S (inclusive) and E (exclusive).931**932** In other words, S is a buffer and E is a pointer to the first byte after933** the end of buffer S. This macro returns true if P points to something934** contained within the buffer S.935*/936#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))937 938/*939** P is one byte past the end of a large buffer. Return true if a span of bytes940** between S..E crosses the end of that buffer. In other words, return true941** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.942**943** S is the start of the span. E is one byte past the end of end of span.944**945** P946** |-----------------| FALSE947** |-------|948** S E949**950** P951** |-----------------|952** |-------| TRUE953** S E954**955** P956** |-----------------| 957** |-------| FALSE958** S E959*/960#define SQLITE_OVERFLOW(P,S,E) (((uptr)(S)<(uptr)(P))&&((uptr)(E)>(uptr)(P)))961 962/*963** Macros to determine whether the machine is big or little endian,964** and whether or not that determination is run-time or compile-time.965**966** For best performance, an attempt is made to guess at the byte-order967** using C-preprocessor macros. If that is unsuccessful, or if968** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined969** at run-time.970**971** If you are building SQLite on some obscure platform for which the972** following ifdef magic does not work, you can always include either:973**974** -DSQLITE_BYTEORDER=1234975**976** or977**978** -DSQLITE_BYTEORDER=4321979**980** to cause the build to work for little-endian or big-endian processors,981** respectively.982*/983#ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */984# if defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__985# define SQLITE_BYTEORDER 4321986# elif defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__987# define SQLITE_BYTEORDER 1234988# elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1989# define SQLITE_BYTEORDER 4321990# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \991 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \992 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \993 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)994# define SQLITE_BYTEORDER 1234995# elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__)996# define SQLITE_BYTEORDER 4321997# else998# define SQLITE_BYTEORDER 0999# endif1000#endif1001#if SQLITE_BYTEORDER==43211002# define SQLITE_BIGENDIAN 11003# define SQLITE_LITTLEENDIAN 01004# define SQLITE_UTF16NATIVE SQLITE_UTF16BE1005#elif SQLITE_BYTEORDER==12341006# define SQLITE_BIGENDIAN 01007# define SQLITE_LITTLEENDIAN 11008# define SQLITE_UTF16NATIVE SQLITE_UTF16LE1009#else1010# ifdef SQLITE_AMALGAMATION1011 const int sqlite3one = 1;1012# else1013 extern const int sqlite3one;1014# endif1015# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)1016# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)1017# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)1018#endif1019 1020/*1021** Constants for the largest and smallest possible 64-bit signed integers.1022** These macros are designed to work correctly on both 32-bit and 64-bit1023** compilers.1024*/1025#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))1026#define LARGEST_UINT64 (0xffffffff|(((u64)0xffffffff)<<32))1027#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)1028 1029/*1030** Macro SMXV(n) return the maximum value that can be held in variable n,1031** assuming n is a signed integer type. UMXV(n) is similar for unsigned1032** integer types.1033*/1034#define SMXV(n) ((((i64)1)<<(sizeof(n)*8-1))-1)1035#define UMXV(n) ((((i64)1)<<(sizeof(n)*8))-1)1036 1037/*1038** Round up a number to the next larger multiple of 8. This is used1039** to force 8-byte alignment on 64-bit architectures.1040**1041** ROUND8() always does the rounding, for any argument.1042**1043** ROUND8P() assumes that the argument is already an integer number of1044** pointers in size, and so it is a no-op on systems where the pointer1045** size is 8.1046*/1047#define ROUND8(x) (((x)+7)&~7)1048#if SQLITE_PTRSIZE==81049# define ROUND8P(x) (x)1050#else1051# define ROUND8P(x) (((x)+7)&~7)1052#endif1053 1054/*1055** Round down to the nearest multiple of 81056*/1057#define ROUNDDOWN8(x) ((x)&~7)1058 1059/*1060** Assert that the pointer X is aligned to an 8-byte boundary. This1061** macro is used only within assert() to verify that the code gets1062** all alignment restrictions correct.1063**1064** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the1065** underlying malloc() implementation might return us 4-byte aligned1066** pointers. In that case, only verify 4-byte alignment.1067*/1068#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC1069# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)1070#else1071# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)1072#endif1073 1074/*1075** Disable MMAP on platforms where it is known to not work1076*/1077#if defined(__OpenBSD__) || defined(__QNXNTO__)1078# undef SQLITE_MAX_MMAP_SIZE1079# define SQLITE_MAX_MMAP_SIZE 01080#endif1081 1082/*1083** Default maximum size of memory used by memory-mapped I/O in the VFS1084*/1085#ifdef __APPLE__1086# include <TargetConditionals.h>1087#endif1088#ifndef SQLITE_MAX_MMAP_SIZE1089# if defined(__linux__) \1090 || defined(_WIN32) \1091 || (defined(__APPLE__) && defined(__MACH__)) \1092 || defined(__sun) \1093 || defined(__FreeBSD__) \1094 || defined(__DragonFly__)1095# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */1096# else1097# define SQLITE_MAX_MMAP_SIZE 01098# endif1099#endif1100 1101/*1102** The default MMAP_SIZE is zero on all platforms. Or, even if a larger1103** default MMAP_SIZE is specified at compile-time, make sure that it does1104** not exceed the maximum mmap size.1105*/1106#ifndef SQLITE_DEFAULT_MMAP_SIZE1107# define SQLITE_DEFAULT_MMAP_SIZE 01108#endif1109#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE1110# undef SQLITE_DEFAULT_MMAP_SIZE1111# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE1112#endif1113 1114/*1115** TREETRACE_ENABLED will be either 1 or 0 depending on whether or not1116** the Abstract Syntax Tree tracing logic is turned on.1117*/1118#if !defined(SQLITE_AMALGAMATION)1119extern u32 sqlite3TreeTrace;1120#endif1121#if defined(SQLITE_DEBUG) \1122 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \1123 || defined(SQLITE_ENABLE_TREETRACE))1124# define TREETRACE_ENABLED 11125# define TREETRACE(K,P,S,X) \1126 if(sqlite3TreeTrace&(K)) \1127 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\1128 sqlite3DebugPrintf X1129#else1130# define TREETRACE(K,P,S,X)1131# define TREETRACE_ENABLED 01132#endif1133 1134/* TREETRACE flag meanings:1135**1136** 0x00000001 Beginning and end of SELECT processing1137** 0x00000002 WHERE clause processing1138** 0x00000004 Query flattener1139** 0x00000008 Result-set wildcard expansion1140** 0x00000010 Query name resolution1141** 0x00000020 Aggregate analysis1142** 0x00000040 Window functions1143** 0x00000080 Generated column names1144** 0x00000100 Move HAVING terms into WHERE1145** 0x00000200 Count-of-view optimization1146** 0x00000400 Compound SELECT processing1147** 0x00000800 Drop superfluous ORDER BY1148** 0x00001000 LEFT JOIN simplifies to JOIN1149** 0x00002000 Constant propagation1150** 0x00004000 Push-down optimization1151** 0x00008000 After all FROM-clause analysis1152** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing1153** 0x00020000 Transform DISTINCT into GROUP BY1154** 0x00040000 SELECT tree dump after all code has been generated1155** 0x00080000 NOT NULL strength reduction1156** 0x00100000 Pointers are all shown as zero1157** 0x00200000 EXISTS-to-JOIN optimization1158*/1159 1160/*1161** Macros for "wheretrace"1162*/1163extern u32 sqlite3WhereTrace;1164#if defined(SQLITE_DEBUG) \1165 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))1166# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X1167# define WHERETRACE_ENABLED 11168#else1169# define WHERETRACE(K,X)1170#endif1171 1172/*1173** Bits for the sqlite3WhereTrace mask:1174**1175** (---any--) Top-level block structure1176** 0x-------F High-level debug messages1177** 0x----FFF- More detail1178** 0xFFFF---- Low-level debug messages1179**1180** 0x00000001 Code generation1181** 0x00000002 Solver (Use 0x40000 for less detail)1182** 0x00000004 Solver costs1183** 0x00000008 WhereLoop inserts1184**1185** 0x00000010 Display sqlite3_index_info xBestIndex calls1186** 0x00000020 Range an equality scan metrics1187** 0x00000040 IN operator decisions1188** 0x00000080 WhereLoop cost adjustments1189** 0x000001001190** 0x00000200 Covering index decisions1191** 0x00000400 OR optimization1192** 0x00000800 Index scanner1193** 0x00001000 More details associated with code generation1194** 0x000020001195** 0x00004000 Show all WHERE terms at key points1196** 0x00008000 Show the full SELECT statement at key places1197**1198** 0x00010000 Show more detail when printing WHERE terms1199** 0x00020000 Show WHERE terms returned from whereScanNext()1200** 0x00040000 Solver overview messages