1a3152895Sdrh /* 2a3152895Sdrh ** 2004 April 6 3a3152895Sdrh ** 4a3152895Sdrh ** The author disclaims copyright to this source code. In place of 5a3152895Sdrh ** a legal notice, here is a blessing: 6a3152895Sdrh ** 7a3152895Sdrh ** May you do good and not evil. 8a3152895Sdrh ** May you find forgiveness for yourself and forgive others. 9a3152895Sdrh ** May you share freely, never taking more than you give. 10a3152895Sdrh ** 11a3152895Sdrh ************************************************************************* 1260ec914cSpeter.d.reid ** This file implements an external (disk-based) database using BTrees. 13a3152895Sdrh ** For a detailed discussion of BTrees, refer to 14a3152895Sdrh ** 15a3152895Sdrh ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: 16a3152895Sdrh ** "Sorting And Searching", pages 473-480. Addison-Wesley 17a3152895Sdrh ** Publishing Company, Reading, Massachusetts. 18a3152895Sdrh ** 19a3152895Sdrh ** The basic idea is that each page of the file contains N database 20a3152895Sdrh ** entries and N+1 pointers to subpages. 21a3152895Sdrh ** 22a3152895Sdrh ** ---------------------------------------------------------------- 23a3152895Sdrh ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) | 24a3152895Sdrh ** ---------------------------------------------------------------- 25a3152895Sdrh ** 26a3152895Sdrh ** All of the keys on the page that Ptr(0) points to have values less 27a3152895Sdrh ** than Key(0). All of the keys on page Ptr(1) and its subpages have 28a3152895Sdrh ** values greater than Key(0) and less than Key(1). All of the keys 29a3152895Sdrh ** on Ptr(N) and its subpages have values greater than Key(N-1). And 30a3152895Sdrh ** so forth. 31a3152895Sdrh ** 32a3152895Sdrh ** Finding a particular key requires reading O(log(M)) pages from the 33a3152895Sdrh ** disk where M is the number of entries in the tree. 34a3152895Sdrh ** 35a3152895Sdrh ** In this implementation, a single file can hold one or more separate 36a3152895Sdrh ** BTrees. Each BTree is identified by the index of its root page. The 37a3152895Sdrh ** key and data for any entry are combined to form the "payload". A 38a3152895Sdrh ** fixed amount of payload can be carried directly on the database 39a3152895Sdrh ** page. If the payload is larger than the preset amount then surplus 40a3152895Sdrh ** bytes are stored on overflow pages. The payload for an entry 41a3152895Sdrh ** and the preceding pointer are combined to form a "Cell". Each 42a3152895Sdrh ** page has a small header which contains the Ptr(N) pointer and other 43a3152895Sdrh ** information such as the size of key and data. 44a3152895Sdrh ** 45a3152895Sdrh ** FORMAT DETAILS 46a3152895Sdrh ** 47a3152895Sdrh ** The file is divided into pages. The first page is called page 1, 48a3152895Sdrh ** the second is page 2, and so forth. A page number of zero indicates 49b2eced5dSdrh ** "no such page". The page size can be any power of 2 between 512 and 65536. 505bbe5481Sdrh ** Each page can be either a btree page, a freelist page, an overflow 515bbe5481Sdrh ** page, or a pointer-map page. 52a3152895Sdrh ** 53a3152895Sdrh ** The first page is always a btree page. The first 100 bytes of the first 54a3152895Sdrh ** page contain a special header (the "file header") that describes the file. 55a3152895Sdrh ** The format of the file header is as follows: 56a3152895Sdrh ** 57a3152895Sdrh ** OFFSET SIZE DESCRIPTION 58a3152895Sdrh ** 0 16 Header string: "SQLite format 3\000" 59e75fb061Sdrh ** 16 2 Page size in bytes. (1 means 65536) 60a3152895Sdrh ** 18 1 File format write version 61a3152895Sdrh ** 19 1 File format read version 62a3152895Sdrh ** 20 1 Bytes of unused space at the end of each page 63e75fb061Sdrh ** 21 1 Max embedded payload fraction (must be 64) 64e75fb061Sdrh ** 22 1 Min embedded payload fraction (must be 32) 65e75fb061Sdrh ** 23 1 Min leaf payload fraction (must be 32) 66a3152895Sdrh ** 24 4 File change counter 67a3152895Sdrh ** 28 4 Reserved for future use 68a3152895Sdrh ** 32 4 First freelist page 69a3152895Sdrh ** 36 4 Number of freelist pages in the file 70a3152895Sdrh ** 40 60 15 4-byte meta values passed to higher layers 71a3152895Sdrh ** 7227731d7cSdrh ** 40 4 Schema cookie 7327731d7cSdrh ** 44 4 File format of schema layer 7427731d7cSdrh ** 48 4 Size of page cache 7527731d7cSdrh ** 52 4 Largest root-page (auto/incr_vacuum) 7627731d7cSdrh ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be 7727731d7cSdrh ** 60 4 User version 7827731d7cSdrh ** 64 4 Incremental vacuum mode 79e75fb061Sdrh ** 68 4 Application-ID 80e75fb061Sdrh ** 72 20 unused 81e75fb061Sdrh ** 92 4 The version-valid-for number 82e75fb061Sdrh ** 96 4 SQLITE_VERSION_NUMBER 8327731d7cSdrh ** 84a3152895Sdrh ** All of the integer values are big-endian (most significant byte first). 85a3152895Sdrh ** 8680308695Sdrh ** The file change counter is incremented when the database is changed 8780308695Sdrh ** This counter allows other processes to know when the file has changed 8880308695Sdrh ** and thus when they need to flush their cache. 89a3152895Sdrh ** 90a3152895Sdrh ** The max embedded payload fraction is the amount of the total usable 91a3152895Sdrh ** space in a page that can be consumed by a single cell for standard 92a3152895Sdrh ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default 93a3152895Sdrh ** is to limit the maximum cell size so that at least 4 cells will fit 94a3152895Sdrh ** on one page. Thus the default max embedded payload fraction is 64. 95a3152895Sdrh ** 96a3152895Sdrh ** If the payload for a cell is larger than the max payload, then extra 97a3152895Sdrh ** payload is spilled to overflow pages. Once an overflow page is allocated, 98a3152895Sdrh ** as many bytes as possible are moved into the overflow pages without letting 99a3152895Sdrh ** the cell size drop below the min embedded payload fraction. 100a3152895Sdrh ** 101a3152895Sdrh ** The min leaf payload fraction is like the min embedded payload fraction 102a3152895Sdrh ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum 103a3152895Sdrh ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it 104a3152895Sdrh ** not specified in the header. 105a3152895Sdrh ** 106a3152895Sdrh ** Each btree pages is divided into three sections: The header, the 10780308695Sdrh ** cell pointer array, and the cell content area. Page 1 also has a 100-byte 108a3152895Sdrh ** file header that occurs before the page header. 109a3152895Sdrh ** 110a3152895Sdrh ** |----------------| 111a3152895Sdrh ** | file header | 100 bytes. Page 1 only. 112a3152895Sdrh ** |----------------| 113a3152895Sdrh ** | page header | 8 bytes for leaves. 12 bytes for interior nodes 114a3152895Sdrh ** |----------------| 115a3152895Sdrh ** | cell pointer | | 2 bytes per cell. Sorted order. 116a3152895Sdrh ** | array | | Grows downward 117a3152895Sdrh ** | | v 118a3152895Sdrh ** |----------------| 119a3152895Sdrh ** | unallocated | 120a3152895Sdrh ** | space | 121a3152895Sdrh ** |----------------| ^ Grows upwards 122a3152895Sdrh ** | cell content | | Arbitrary order interspersed with freeblocks. 123a3152895Sdrh ** | area | | and free space fragments. 124a3152895Sdrh ** |----------------| 125a3152895Sdrh ** 126a3152895Sdrh ** The page headers looks like this: 127a3152895Sdrh ** 128a3152895Sdrh ** OFFSET SIZE DESCRIPTION 129a3152895Sdrh ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf 130a3152895Sdrh ** 1 2 byte offset to the first freeblock 131a3152895Sdrh ** 3 2 number of cells on this page 132a3152895Sdrh ** 5 2 first byte of the cell content area 133a3152895Sdrh ** 7 1 number of fragmented free bytes 134a3152895Sdrh ** 8 4 Right child (the Ptr(N) value). Omitted on leaves. 135a3152895Sdrh ** 136a3152895Sdrh ** The flags define the format of this btree page. The leaf flag means that 137a3152895Sdrh ** this page has no children. The zerodata flag means that this page carries 13860ec914cSpeter.d.reid ** only keys and no data. The intkey flag means that the key is an integer 139a3152895Sdrh ** which is stored in the key size entry of the cell header rather than in 140a3152895Sdrh ** the payload area. 141a3152895Sdrh ** 142a3152895Sdrh ** The cell pointer array begins on the first byte after the page header. 143a3152895Sdrh ** The cell pointer array contains zero or more 2-byte numbers which are 144a3152895Sdrh ** offsets from the beginning of the page to the cell content in the cell 145a3152895Sdrh ** content area. The cell pointers occur in sorted order. The system strives 146a3152895Sdrh ** to keep free space after the last cell pointer so that new cells can 147a3152895Sdrh ** be easily added without having to defragment the page. 148a3152895Sdrh ** 149a3152895Sdrh ** Cell content is stored at the very end of the page and grows toward the 150a3152895Sdrh ** beginning of the page. 151a3152895Sdrh ** 152a3152895Sdrh ** Unused space within the cell content area is collected into a linked list of 153a3152895Sdrh ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset 154a3152895Sdrh ** to the first freeblock is given in the header. Freeblocks occur in 155a3152895Sdrh ** increasing order. Because a freeblock must be at least 4 bytes in size, 156a3152895Sdrh ** any group of 3 or fewer unused bytes in the cell content area cannot 157a3152895Sdrh ** exist on the freeblock chain. A group of 3 or fewer free bytes is called 158a3152895Sdrh ** a fragment. The total number of bytes in all fragments is recorded. 159a3152895Sdrh ** in the page header at offset 7. 160a3152895Sdrh ** 161a3152895Sdrh ** SIZE DESCRIPTION 162a3152895Sdrh ** 2 Byte offset of the next freeblock 163a3152895Sdrh ** 2 Bytes in this freeblock 164a3152895Sdrh ** 165a3152895Sdrh ** Cells are of variable length. Cells are stored in the cell content area at 166a3152895Sdrh ** the end of the page. Pointers to the cells are in the cell pointer array 167a3152895Sdrh ** that immediately follows the page header. Cells is not necessarily 168a3152895Sdrh ** contiguous or in order, but cell pointers are contiguous and in order. 169a3152895Sdrh ** 170a3152895Sdrh ** Cell content makes use of variable length integers. A variable 171a3152895Sdrh ** length integer is 1 to 9 bytes where the lower 7 bits of each 172a3152895Sdrh ** byte are used. The integer consists of all bytes that have bit 8 set and 173a3152895Sdrh ** the first byte with bit 8 clear. The most significant byte of the integer 174a3152895Sdrh ** appears first. A variable-length integer may not be more than 9 bytes long. 175a3152895Sdrh ** As a special case, all 8 bytes of the 9th byte are used as data. This 176a3152895Sdrh ** allows a 64-bit integer to be encoded in 9 bytes. 177a3152895Sdrh ** 178a3152895Sdrh ** 0x00 becomes 0x00000000 179a3152895Sdrh ** 0x7f becomes 0x0000007f 180a3152895Sdrh ** 0x81 0x00 becomes 0x00000080 181a3152895Sdrh ** 0x82 0x00 becomes 0x00000100 182a3152895Sdrh ** 0x80 0x7f becomes 0x0000007f 183a3152895Sdrh ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 184a3152895Sdrh ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 185a3152895Sdrh ** 186a3152895Sdrh ** Variable length integers are used for rowids and to hold the number of 187a3152895Sdrh ** bytes of key and data in a btree cell. 188a3152895Sdrh ** 189a3152895Sdrh ** The content of a cell looks like this: 190a3152895Sdrh ** 191a3152895Sdrh ** SIZE DESCRIPTION 192a3152895Sdrh ** 4 Page number of the left child. Omitted if leaf flag is set. 193a3152895Sdrh ** var Number of bytes of data. Omitted if the zerodata flag is set. 194a3152895Sdrh ** var Number of bytes of key. Or the key itself if intkey flag is set. 195a3152895Sdrh ** * Payload 196a3152895Sdrh ** 4 First page of the overflow chain. Omitted if no overflow 197a3152895Sdrh ** 198a3152895Sdrh ** Overflow pages form a linked list. Each page except the last is completely 199a3152895Sdrh ** filled with data (pagesize - 4 bytes). The last page can have as little 200a3152895Sdrh ** as 1 byte of data. 201a3152895Sdrh ** 202a3152895Sdrh ** SIZE DESCRIPTION 203a3152895Sdrh ** 4 Page number of next overflow page 204a3152895Sdrh ** * Data 205a3152895Sdrh ** 206a3152895Sdrh ** Freelist pages come in two subtypes: trunk pages and leaf pages. The 20780308695Sdrh ** file header points to the first in a linked list of trunk page. Each trunk 208a3152895Sdrh ** page points to multiple leaf pages. The content of a leaf page is 209a3152895Sdrh ** unspecified. A trunk page looks like this: 210a3152895Sdrh ** 211a3152895Sdrh ** SIZE DESCRIPTION 212a3152895Sdrh ** 4 Page number of next trunk page 213a3152895Sdrh ** 4 Number of leaf pointers on this page 214a3152895Sdrh ** * zero or more pages numbers of leaves 215a3152895Sdrh */ 216a3152895Sdrh #include "sqliteInt.h" 217a3152895Sdrh 218a3152895Sdrh 219a3152895Sdrh /* The following value is the maximum cell size assuming a maximum page 220a3152895Sdrh ** size give above. 221a3152895Sdrh */ 222fcd71b60Sdrh #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8)) 223a3152895Sdrh 224a3152895Sdrh /* The maximum number of cells on a single page of the database. This 225a9121e4cSdrh ** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself 226a9121e4cSdrh ** plus 2 bytes for the index to the cell in the page header). Such 227a9121e4cSdrh ** small cells will be rare, but they are possible. 228a3152895Sdrh */ 229a9121e4cSdrh #define MX_CELL(pBt) ((pBt->pageSize-8)/6) 230a3152895Sdrh 231a3152895Sdrh /* Forward declarations */ 232a3152895Sdrh typedef struct MemPage MemPage; 233a3152895Sdrh typedef struct BtLock BtLock; 2345fa60514Sdrh typedef struct CellInfo CellInfo; 235a3152895Sdrh 236a3152895Sdrh /* 237a3152895Sdrh ** This is a magic string that appears at the beginning of every 238a3152895Sdrh ** SQLite database in order to identify the file as a real database. 239a3152895Sdrh ** 240a3152895Sdrh ** You can change this value at compile-time by specifying a 241a3152895Sdrh ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The 242a3152895Sdrh ** header must be exactly 16 bytes including the zero-terminator so 243a3152895Sdrh ** the string itself should be 15 characters long. If you change 244a3152895Sdrh ** the header, then your custom library will not be able to read 245a3152895Sdrh ** databases generated by the standard tools and the standard tools 246a3152895Sdrh ** will not be able to read databases created by your custom library. 247a3152895Sdrh */ 248a3152895Sdrh #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */ 249a3152895Sdrh # define SQLITE_FILE_HEADER "SQLite format 3" 250a3152895Sdrh #endif 251a3152895Sdrh 252a3152895Sdrh /* 253a3152895Sdrh ** Page type flags. An ORed combination of these flags appear as the 254e53831d6Sdrh ** first byte of on-disk image of every BTree page. 255a3152895Sdrh */ 256a3152895Sdrh #define PTF_INTKEY 0x01 257a3152895Sdrh #define PTF_ZERODATA 0x02 258a3152895Sdrh #define PTF_LEAFDATA 0x04 259a3152895Sdrh #define PTF_LEAF 0x08 260a3152895Sdrh 261a3152895Sdrh /* 262a2ee589cSdrh ** An instance of this object stores information about each a single database 263a2ee589cSdrh ** page that has been loaded into memory. The information in this object 264a2ee589cSdrh ** is derived from the raw on-disk page content. 265a3152895Sdrh ** 266a2ee589cSdrh ** As each database page is loaded into memory, the pager allocats an 267a2ee589cSdrh ** instance of this object and zeros the first 8 bytes. (This is the 268a2ee589cSdrh ** "extra" information associated with each page of the pager.) 269d677b3d6Sdrh ** 270d677b3d6Sdrh ** Access to all fields of this structure is controlled by the mutex 271d677b3d6Sdrh ** stored in MemPage.pBt->mutex. 272a3152895Sdrh */ 273a3152895Sdrh struct MemPage { 274a3152895Sdrh u8 isInit; /* True if previously initialized. MUST BE FIRST! */ 2753e28ff5cSdrh u8 intKey; /* True if table b-trees. False for index b-trees */ 2763e28ff5cSdrh u8 intKeyLeaf; /* True if the leaf of an intKey table */ 277a2ee589cSdrh Pgno pgno; /* Page number for this page */ 278a2ee589cSdrh /* Only the first 8 bytes (above) are zeroed by pager.c when a new page 279a2ee589cSdrh ** is allocated. All fields that follow must be initialized before use */ 2803e28ff5cSdrh u8 leaf; /* True if a leaf page */ 281a3152895Sdrh u8 hdrOffset; /* 100 for page 1. 0 otherwise */ 282a3152895Sdrh u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ 283c9166349Sdrh u8 max1bytePayload; /* min(maxLocal,127) */ 284a2ee589cSdrh u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ 285e53831d6Sdrh u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ 286e53831d6Sdrh u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ 287a3152895Sdrh u16 cellOffset; /* Index in aData of first cell pointer */ 288a941ff71Sdrh int nFree; /* Number of free bytes on the page. -1 for unknown */ 289a3152895Sdrh u16 nCell; /* Number of cells on this page, local and ovfl */ 2901688c86fSdrh u16 maskPage; /* Mask for page offset */ 291a2ee589cSdrh u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th 2922cbd78b7Sdrh ** non-overflow cell */ 293a2ee589cSdrh u8 *apOvfl[4]; /* Pointers to the body of overflow cells */ 294e53831d6Sdrh BtShared *pBt; /* Pointer to BtShared that this page is part of */ 295e53831d6Sdrh u8 *aData; /* Pointer to disk image of the page data */ 296a055abb8Sdrh u8 *aDataEnd; /* One byte past the end of the entire page - not just 297a055abb8Sdrh ** the usable space, the entire page. Used to prevent 298*32135d7eSdrh ** corruption-induced buffer overflow. */ 2993def2357Sdrh u8 *aCellIdx; /* The cell index area */ 300f44890a7Sdrh u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */ 301a3152895Sdrh DbPage *pDbPage; /* Pager page handle */ 30225ada07aSdrh u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */ 3035fa60514Sdrh void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */ 304a3152895Sdrh }; 305a3152895Sdrh 306a3152895Sdrh /* 307602b466eSdanielk1977 ** A linked list of the following structures is stored at BtShared.pLock. 308602b466eSdanielk1977 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 309602b466eSdanielk1977 ** is opened on the table with root page BtShared.iTable. Locks are removed 310602b466eSdanielk1977 ** from this list when a transaction is committed or rolled back, or when 311602b466eSdanielk1977 ** a btree handle is closed. 312602b466eSdanielk1977 */ 313602b466eSdanielk1977 struct BtLock { 314602b466eSdanielk1977 Btree *pBtree; /* Btree handle holding this lock */ 315602b466eSdanielk1977 Pgno iTable; /* Root page of table */ 316602b466eSdanielk1977 u8 eLock; /* READ_LOCK or WRITE_LOCK */ 317602b466eSdanielk1977 BtLock *pNext; /* Next in BtShared.pLock list */ 318602b466eSdanielk1977 }; 319602b466eSdanielk1977 320602b466eSdanielk1977 /* Candidate values for BtLock.eLock */ 321602b466eSdanielk1977 #define READ_LOCK 1 322602b466eSdanielk1977 #define WRITE_LOCK 2 323602b466eSdanielk1977 324e53831d6Sdrh /* A Btree handle 325e53831d6Sdrh ** 326e53831d6Sdrh ** A database connection contains a pointer to an instance of 327e53831d6Sdrh ** this object for every database file that it has open. This structure 328e53831d6Sdrh ** is opaque to the database connection. The database connection cannot 329e53831d6Sdrh ** see the internals of this structure and only deals with pointers to 330e53831d6Sdrh ** this structure. 331e53831d6Sdrh ** 332e53831d6Sdrh ** For some database files, the same underlying database cache might be 333ed1f8787Sdrh ** shared between multiple connections. In that case, each connection 334ed1f8787Sdrh ** has it own instance of this object. But each instance of this object 335e53831d6Sdrh ** points to the same BtShared object. The database cache and the 336e53831d6Sdrh ** schema associated with the database file are all contained within 337e53831d6Sdrh ** the BtShared object. 338abddb0c8Sdrh ** 339d0679edcSdrh ** All fields in this structure are accessed under sqlite3.mutex. 340d0679edcSdrh ** The pBt pointer itself may not be changed while there exists cursors 341d0679edcSdrh ** in the referenced BtShared that point back to this Btree since those 3424fa7d7c0Sdrh ** cursors have to go through this Btree to find their BtShared and 343d0679edcSdrh ** they often do so without holding sqlite3.mutex. 344e53831d6Sdrh */ 345a3152895Sdrh struct Btree { 346e5fe690dSdrh sqlite3 *db; /* The database connection holding this btree */ 347e53831d6Sdrh BtShared *pBt; /* Sharable content of this btree */ 348a3152895Sdrh u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ 349e5fe690dSdrh u8 sharable; /* True if we can share pBt with another db */ 350e5fe690dSdrh u8 locked; /* True if db currently has pBt locked */ 3516918095dSdrh u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */ 352e53831d6Sdrh int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */ 3530410302eSdanielk1977 int nBackup; /* Number of backup operations reading this btree */ 3542b994ce5Sdrh u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */ 355e5fe690dSdrh Btree *pNext; /* List of other sharable Btrees from the same db */ 356e53831d6Sdrh Btree *pPrev; /* Back pointer of the same list */ 35737ccfcfeSdrh #ifdef SQLITE_DEBUG 35837ccfcfeSdrh u64 nSeek; /* Calls to sqlite3BtreeMovetoUnpacked() */ 35937ccfcfeSdrh #endif 360602b466eSdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE 361602b466eSdanielk1977 BtLock lock; /* Object used to lock page 1 */ 362602b466eSdanielk1977 #endif 363a3152895Sdrh }; 364a3152895Sdrh 365a3152895Sdrh /* 366a3152895Sdrh ** Btree.inTrans may take one of the following values. 367a3152895Sdrh ** 368a3152895Sdrh ** If the shared-data extension is enabled, there may be multiple users 369a3152895Sdrh ** of the Btree structure. At most one of these may open a write transaction, 370e53831d6Sdrh ** but any number may have active read transactions. 37199744fa4Sdrh ** 37299744fa4Sdrh ** These values must match SQLITE_TXN_NONE, SQLITE_TXN_READ, and 37399744fa4Sdrh ** SQLITE_TXN_WRITE 374a3152895Sdrh */ 375a3152895Sdrh #define TRANS_NONE 0 376a3152895Sdrh #define TRANS_READ 1 377a3152895Sdrh #define TRANS_WRITE 2 378a3152895Sdrh 37999744fa4Sdrh #if TRANS_NONE!=SQLITE_TXN_NONE 38099744fa4Sdrh # error wrong numeric code for no-transaction 38199744fa4Sdrh #endif 38299744fa4Sdrh #if TRANS_READ!=SQLITE_TXN_READ 38399744fa4Sdrh # error wrong numeric code for read-transaction 38499744fa4Sdrh #endif 38599744fa4Sdrh #if TRANS_WRITE!=SQLITE_TXN_WRITE 38699744fa4Sdrh # error wrong numeric code for write-transaction 38799744fa4Sdrh #endif 38899744fa4Sdrh 38999744fa4Sdrh 390a3152895Sdrh /* 391e53831d6Sdrh ** An instance of this object represents a single database file. 392e53831d6Sdrh ** 393b8a45bbdSdrh ** A single database file can be in use at the same time by two 394e53831d6Sdrh ** or more database connections. When two or more connections are 395e53831d6Sdrh ** sharing the same database file, each connection has it own 396e53831d6Sdrh ** private Btree object for the file and each of those Btrees points 397e53831d6Sdrh ** to this one BtShared object. BtShared.nRef is the number of 398e53831d6Sdrh ** connections currently sharing this database file. 399abddb0c8Sdrh ** 400abddb0c8Sdrh ** Fields in this structure are accessed under the BtShared.mutex 401abddb0c8Sdrh ** mutex, except for nRef and pNext which are accessed under the 402ccb2113aSdrh ** global SQLITE_MUTEX_STATIC_MAIN mutex. The pPager field 403b1ab8ea7Sdrh ** may not be modified once it is initially set as long as nRef>0. 404b1ab8ea7Sdrh ** The pSchema field may be set once under BtShared.mutex and 405b1ab8ea7Sdrh ** thereafter is unchanged as long as nRef>0. 406404ca075Sdanielk1977 ** 407404ca075Sdanielk1977 ** isPending: 408404ca075Sdanielk1977 ** 409404ca075Sdanielk1977 ** If a BtShared client fails to obtain a write-lock on a database 410404ca075Sdanielk1977 ** table (because there exists one or more read-locks on the table), 411404ca075Sdanielk1977 ** the shared-cache enters 'pending-lock' state and isPending is 412404ca075Sdanielk1977 ** set to true. 413404ca075Sdanielk1977 ** 414404ca075Sdanielk1977 ** The shared-cache leaves the 'pending lock' state when either of 415404ca075Sdanielk1977 ** the following occur: 416404ca075Sdanielk1977 ** 417404ca075Sdanielk1977 ** 1) The current writer (BtShared.pWriter) concludes its transaction, OR 418404ca075Sdanielk1977 ** 2) The number of locks held by other connections drops to zero. 419404ca075Sdanielk1977 ** 420404ca075Sdanielk1977 ** while in the 'pending-lock' state, no connection may start a new 421404ca075Sdanielk1977 ** transaction. 422404ca075Sdanielk1977 ** 423404ca075Sdanielk1977 ** This feature is included to help prevent writer-starvation. 424a3152895Sdrh */ 425a3152895Sdrh struct BtShared { 426a3152895Sdrh Pager *pPager; /* The page cache */ 427e5fe690dSdrh sqlite3 *db; /* Database connection currently using this Btree */ 428a3152895Sdrh BtCursor *pCursor; /* A list of all open cursors */ 429a3152895Sdrh MemPage *pPage1; /* First page of the database */ 430d4187c71Sdrh u8 openFlags; /* Flags to sqlite3BtreeOpen() */ 431a3152895Sdrh #ifndef SQLITE_OMIT_AUTOVACUUM 432a3152895Sdrh u8 autoVacuum; /* True if auto-vacuum is enabled */ 433a3152895Sdrh u8 incrVacuum; /* True if incr-vacuum is enabled */ 434bc1a3c6cSdan u8 bDoTruncate; /* True to truncate db on commit */ 435a3152895Sdrh #endif 4362e5de2f2Sdrh u8 inTransaction; /* Transaction state */ 437c9166349Sdrh u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */ 438e937df81Sdrh u8 nReserveWanted; /* Desired number of extra bytes per page */ 439c9166349Sdrh u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ 440f49661a4Sdrh u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ 441f49661a4Sdrh u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ 442f49661a4Sdrh u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ 443f49661a4Sdrh u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ 444b2eced5dSdrh u32 pageSize; /* Total number of bytes on a page */ 445b2eced5dSdrh u32 usableSize; /* Number of usable bytes on each page */ 446a3152895Sdrh int nTransaction; /* Number of open transactions (read + write) */ 447dd3cd977Sdrh u32 nPage; /* Number of pages in the database */ 448a3152895Sdrh void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ 449a3152895Sdrh void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ 450bdaec52cSdrh sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */ 451bea2a948Sdanielk1977 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */ 452a3152895Sdrh #ifndef SQLITE_OMIT_SHARED_CACHE 453abddb0c8Sdrh int nRef; /* Number of references to this structure */ 454abddb0c8Sdrh BtShared *pNext; /* Next on a list of sharable BtShared structs */ 455a3152895Sdrh BtLock *pLock; /* List of locks held on this shared-btree struct */ 456404ca075Sdanielk1977 Btree *pWriter; /* Btree with currently open write transaction */ 457a3152895Sdrh #endif 45892787cf0Sdrh u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */ 4597aae7358Sdan int nPreformatSize; /* Size of last cell written by TransferRow() */ 460a3152895Sdrh }; 461a3152895Sdrh 462a3152895Sdrh /* 463c9166349Sdrh ** Allowed values for BtShared.btsFlags 464c9166349Sdrh */ 465c9166349Sdrh #define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */ 466c9166349Sdrh #define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */ 467c9166349Sdrh #define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */ 468a5907a86Sdrh #define BTS_OVERWRITE 0x0008 /* Overwrite deleted content with zeros */ 469a5907a86Sdrh #define BTS_FAST_SECURE 0x000c /* Combination of the previous two */ 470a5907a86Sdrh #define BTS_INITIALLY_EMPTY 0x0010 /* Database was empty at trans start */ 471a5907a86Sdrh #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */ 472a5907a86Sdrh #define BTS_EXCLUSIVE 0x0040 /* pWriter has an exclusive lock */ 473a5907a86Sdrh #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */ 474c9166349Sdrh 475c9166349Sdrh /* 476a3152895Sdrh ** An instance of the following structure is used to hold information 477a3152895Sdrh ** about a cell. The parseCellPtr() function fills in this structure 478a3152895Sdrh ** based on information extract from the raw disk page. 479a3152895Sdrh */ 480a3152895Sdrh struct CellInfo { 481ab1cc58bSdrh i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */ 482ab1cc58bSdrh u8 *pPayload; /* Pointer to the start of payload */ 483ab1cc58bSdrh u32 nPayload; /* Bytes of payload */ 484ab1cc58bSdrh u16 nLocal; /* Amount of payload held locally, not on overflow */ 485a3152895Sdrh u16 nSize; /* Size of the cell content on the main b-tree page */ 486a3152895Sdrh }; 487a3152895Sdrh 488a3152895Sdrh /* 48971d5d2cdSdanielk1977 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than 49071d5d2cdSdanielk1977 ** this will be declared corrupt. This value is calculated based on a 49171d5d2cdSdanielk1977 ** maximum database size of 2^31 pages a minimum fanout of 2 for a 49271d5d2cdSdanielk1977 ** root-node and 3 for all other internal nodes. 49371d5d2cdSdanielk1977 ** 49471d5d2cdSdanielk1977 ** If a tree that appears to be taller than this is encountered, it is 49571d5d2cdSdanielk1977 ** assumed that the database is corrupt. 49671d5d2cdSdanielk1977 */ 49771d5d2cdSdanielk1977 #define BTCURSOR_MAX_DEPTH 20 49871d5d2cdSdanielk1977 49971d5d2cdSdanielk1977 /* 500e53831d6Sdrh ** A cursor is a pointer to a particular entry within a particular 501e53831d6Sdrh ** b-tree within a database file. 502e53831d6Sdrh ** 503a3152895Sdrh ** The entry is identified by its MemPage and the index in 504a3152895Sdrh ** MemPage.aCell[] of the entry. 505e53831d6Sdrh ** 506b8a45bbdSdrh ** A single database file can be shared by two more database connections, 507e53831d6Sdrh ** but cursors cannot be shared. Each cursor is associated with a 508e5fe690dSdrh ** particular database connection identified BtCursor.pBtree.db. 509abddb0c8Sdrh ** 510d677b3d6Sdrh ** Fields in this structure are accessed under the BtShared.mutex 511d0679edcSdrh ** found at self->pBt->mutex. 512d816e00dSdrh ** 513d816e00dSdrh ** skipNext meaning: 5140c873bf2Sdrh ** The meaning of skipNext depends on the value of eState: 5150c873bf2Sdrh ** 5160c873bf2Sdrh ** eState Meaning of skipNext 5170c873bf2Sdrh ** VALID skipNext is meaningless and is ignored 5180c873bf2Sdrh ** INVALID skipNext is meaningless and is ignored 5190c873bf2Sdrh ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and 5200c873bf2Sdrh ** sqlite3BtreePrevious() is no-op if skipNext<0. 5210c873bf2Sdrh ** REQUIRESEEK restoreCursorPosition() restores the cursor to 5220c873bf2Sdrh ** eState=SKIPNEXT if skipNext!=0 5230c873bf2Sdrh ** FAULT skipNext holds the cursor fault error code. 524a3152895Sdrh */ 525a3152895Sdrh struct BtCursor { 526fe0cf7a1Sdrh u8 eState; /* One of the CURSOR_XXX constants (see below) */ 527fe0cf7a1Sdrh u8 curFlags; /* zero or more BTCF_* flags defined below */ 528fe0cf7a1Sdrh u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */ 529fe0cf7a1Sdrh u8 hints; /* As configured by CursorSetHints() */ 530d816e00dSdrh int skipNext; /* Prev() is noop if negative. Next() is noop if positive. 531d816e00dSdrh ** Error code if eState==CURSOR_FAULT */ 532da6bc679Sdrh Btree *pBtree; /* The Btree to which this cursor belongs */ 533da6bc679Sdrh Pgno *aOverflow; /* Cache of overflow page locations */ 534da6bc679Sdrh void *pKey; /* Saved key that was cursor last known position */ 535408efc06Sdrh /* All fields above are zeroed when the cursor is allocated. See 536408efc06Sdrh ** sqlite3BtreeCursorZero(). Fields that follow must be manually 537408efc06Sdrh ** initialized. */ 538da6bc679Sdrh #define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */ 539da6bc679Sdrh BtShared *pBt; /* The BtShared this cursor points to */ 540da6bc679Sdrh BtCursor *pNext; /* Forms a linked list of all cursors */ 541da6bc679Sdrh CellInfo info; /* A parse of the cell we are pointing at */ 542da6bc679Sdrh i64 nKey; /* Size of pKey, or last integer key */ 543da6bc679Sdrh Pgno pgnoRoot; /* The root page of this tree */ 544408efc06Sdrh i8 iPage; /* Index of current page in apPage */ 545408efc06Sdrh u8 curIntKey; /* Value of apPage[0]->intKey */ 54675e96b31Sdrh u16 ix; /* Current index for apPage[iPage] */ 547b6017a44Sdrh u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */ 548b6017a44Sdrh struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */ 549352a35abSdrh MemPage *pPage; /* Current page */ 550352a35abSdrh MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */ 551a3152895Sdrh }; 552a3152895Sdrh 553a3152895Sdrh /* 554036dbec0Sdrh ** Legal values for BtCursor.curFlags 555036dbec0Sdrh */ 5564c417181Sdrh #define BTCF_WriteFlag 0x01 /* True if a write cursor */ 5574c417181Sdrh #define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */ 5584c417181Sdrh #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */ 5594c417181Sdrh #define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */ 560036dbec0Sdrh #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */ 56127fb746cSdrh #define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */ 5627b14b65dSdrh #define BTCF_Pinned 0x40 /* Cursor is busy and cannot be moved */ 563036dbec0Sdrh 564036dbec0Sdrh /* 565a3152895Sdrh ** Potential values for BtCursor.eState. 566a3152895Sdrh ** 567a3152895Sdrh ** CURSOR_INVALID: 568a3152895Sdrh ** Cursor does not point to a valid entry. This can happen (for example) 569a3152895Sdrh ** because the table is empty or because BtreeCursorFirst() has not been 570a3152895Sdrh ** called. 571a3152895Sdrh ** 5729b47ee3fSdrh ** CURSOR_VALID: 5739b47ee3fSdrh ** Cursor points to a valid entry. getPayload() etc. may be called. 5749b47ee3fSdrh ** 5759b47ee3fSdrh ** CURSOR_SKIPNEXT: 5769b47ee3fSdrh ** Cursor is valid except that the Cursor.skipNext field is non-zero 5779b47ee3fSdrh ** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious() 5789b47ee3fSdrh ** operation should be a no-op. 5799b47ee3fSdrh ** 580a3152895Sdrh ** CURSOR_REQUIRESEEK: 581a3152895Sdrh ** The table that this cursor was opened on still exists, but has been 582a3152895Sdrh ** modified since the cursor was last used. The cursor position is saved 583a3152895Sdrh ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 584a3460585Sdrh ** this state, restoreCursorPosition() can be called to attempt to 585a3152895Sdrh ** seek the cursor to the saved position. 586fb98264aSdrh ** 587fb98264aSdrh ** CURSOR_FAULT: 58860ec914cSpeter.d.reid ** An unrecoverable error (an I/O error or a malloc failure) has occurred 589fb98264aSdrh ** on a different connection that shares the BtShared cache with this 590fb98264aSdrh ** cursor. The error has left the cache in an inconsistent state. 591fb98264aSdrh ** Do nothing else with this cursor. Any attempt to use the cursor 592d816e00dSdrh ** should return the error code stored in BtCursor.skipNext 593a3152895Sdrh */ 594a8519d79Sdrh #define CURSOR_VALID 0 595a8519d79Sdrh #define CURSOR_INVALID 1 5969b47ee3fSdrh #define CURSOR_SKIPNEXT 2 5979b47ee3fSdrh #define CURSOR_REQUIRESEEK 3 5989b47ee3fSdrh #define CURSOR_FAULT 4 599a3152895Sdrh 600bea2a948Sdanielk1977 /* 601bea2a948Sdanielk1977 ** The database page the PENDING_BYTE occupies. This page is never used. 602a3152895Sdrh */ 603584bfcaeSdrh #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1)) 604a3152895Sdrh 605a3152895Sdrh /* 606a3152895Sdrh ** These macros define the location of the pointer-map entry for a 607a3152895Sdrh ** database page. The first argument to each is the number of usable 608a3152895Sdrh ** bytes on each page of the database (often 1024). The second is the 609a3152895Sdrh ** page number to look up in the pointer map. 610a3152895Sdrh ** 611a3152895Sdrh ** PTRMAP_PAGENO returns the database page number of the pointer-map 612a3152895Sdrh ** page that stores the required pointer. PTRMAP_PTROFFSET returns 613a3152895Sdrh ** the offset of the requested map entry. 614a3152895Sdrh ** 615a3152895Sdrh ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, 616a3152895Sdrh ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be 617a3152895Sdrh ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements 618a3152895Sdrh ** this test. 619a3152895Sdrh */ 620a3152895Sdrh #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) 6218c666b16Sdanielk1977 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1)) 622a3152895Sdrh #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) 623a3152895Sdrh 624a3152895Sdrh /* 625a3152895Sdrh ** The pointer map is a lookup table that identifies the parent page for 626a3152895Sdrh ** each child page in the database file. The parent page is the page that 627a3152895Sdrh ** contains a pointer to the child. Every page in the database contains 628a3152895Sdrh ** 0 or 1 parent pages. (In this context 'database page' refers 629a3152895Sdrh ** to any page that is not part of the pointer map itself.) Each pointer map 630a3152895Sdrh ** entry consists of a single byte 'type' and a 4 byte parent page number. 631a3152895Sdrh ** The PTRMAP_XXX identifiers below are the valid types. 632a3152895Sdrh ** 633a3152895Sdrh ** The purpose of the pointer map is to facility moving pages from one 634a3152895Sdrh ** position in the file to another as part of autovacuum. When a page 635a3152895Sdrh ** is moved, the pointer in its parent must be updated to point to the 636a3152895Sdrh ** new location. The pointer map is used to locate the parent page quickly. 637a3152895Sdrh ** 638a3152895Sdrh ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not 639a3152895Sdrh ** used in this case. 640a3152895Sdrh ** 641a3152895Sdrh ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 642a3152895Sdrh ** is not used in this case. 643a3152895Sdrh ** 644a3152895Sdrh ** PTRMAP_OVERFLOW1: The database page is the first page in a list of 645a3152895Sdrh ** overflow pages. The page number identifies the page that 646a3152895Sdrh ** contains the cell with a pointer to this overflow page. 647a3152895Sdrh ** 648a3152895Sdrh ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of 649a3152895Sdrh ** overflow pages. The page-number identifies the previous 650a3152895Sdrh ** page in the overflow page list. 651a3152895Sdrh ** 652a3152895Sdrh ** PTRMAP_BTREE: The database page is a non-root btree page. The page number 653a3152895Sdrh ** identifies the parent page in the btree. 654a3152895Sdrh */ 655a3152895Sdrh #define PTRMAP_ROOTPAGE 1 656a3152895Sdrh #define PTRMAP_FREEPAGE 2 657a3152895Sdrh #define PTRMAP_OVERFLOW1 3 658a3152895Sdrh #define PTRMAP_OVERFLOW2 4 659a3152895Sdrh #define PTRMAP_BTREE 5 660a3152895Sdrh 661a3152895Sdrh /* A bunch of assert() statements to check the transaction state variables 662a3152895Sdrh ** of handle p (type Btree*) are internally consistent. 663a3152895Sdrh */ 664a3152895Sdrh #define btreeIntegrity(p) \ 665a3152895Sdrh assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \ 666a3152895Sdrh assert( p->pBt->inTransaction>=p->inTrans ); 667a3152895Sdrh 668a3152895Sdrh 669a3152895Sdrh /* 670a3152895Sdrh ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine 671a3152895Sdrh ** if the database supports auto-vacuum or not. Because it is used 672a3152895Sdrh ** within an expression that is an argument to another macro 673a3152895Sdrh ** (sqliteMallocRaw), it is not possible to use conditional compilation. 674a3152895Sdrh ** So, this macro is defined instead. 675a3152895Sdrh */ 676a3152895Sdrh #ifndef SQLITE_OMIT_AUTOVACUUM 677a3152895Sdrh #define ISAUTOVACUUM (pBt->autoVacuum) 678a3152895Sdrh #else 679a3152895Sdrh #define ISAUTOVACUUM 0 680a3152895Sdrh #endif 681a3152895Sdrh 682a3152895Sdrh 683a3152895Sdrh /* 684a3152895Sdrh ** This structure is passed around through all the sanity checking routines 685a3152895Sdrh ** in order to keep track of some global state information. 6861235bb18Sdan ** 6871235bb18Sdan ** The aRef[] array is allocated so that there is 1 bit for each page in 6881235bb18Sdan ** the database. As the integrity-check proceeds, for each page used in 6891235bb18Sdan ** the database the corresponding bit is set. This allows integrity-check to 6901235bb18Sdan ** detect pages that are used twice and orphaned pages (both of which 6911235bb18Sdan ** indicate corruption). 692a3152895Sdrh */ 693a3152895Sdrh typedef struct IntegrityCk IntegrityCk; 694a3152895Sdrh struct IntegrityCk { 695a3152895Sdrh BtShared *pBt; /* The tree being checked out */ 696a3152895Sdrh Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ 6971235bb18Sdan u8 *aPgRef; /* 1 bit per page in the db (see above) */ 6982cbd78b7Sdrh Pgno nPage; /* Number of pages in the database */ 699a3152895Sdrh int mxErr; /* Stop accumulating errors when this reaches zero */ 700a3152895Sdrh int nErr; /* Number of messages written to zErrMsg so far */ 7018ddf6358Sdrh int bOomFault; /* A memory allocation error has occurred */ 702867db831Sdrh const char *zPfx; /* Error message prefix */ 703abc38158Sdrh Pgno v1; /* Value for first %u substitution in zPfx */ 704abc38158Sdrh int v2; /* Value for second %d substitution in zPfx */ 705f089aa45Sdrh StrAccum errMsg; /* Accumulate the error message text here */ 706e05b3f8fSdrh u32 *heap; /* Min-heap used for analyzing cell coverage */ 70721f6daa2Sdrh sqlite3 *db; /* Database connection running the check */ 708a3152895Sdrh }; 709a3152895Sdrh 710a3152895Sdrh /* 711b8a45bbdSdrh ** Routines to read or write a two- and four-byte big-endian integer values. 712a3152895Sdrh */ 713246ad31dSdanielk1977 #define get2byte(x) ((x)[0]<<8 | (x)[1]) 714f49661a4Sdrh #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) 715a3152895Sdrh #define get4byte sqlite3Get4byte 716a3152895Sdrh #define put4byte sqlite3Put4byte 717329428e2Sdrh 718329428e2Sdrh /* 719329428e2Sdrh ** get2byteAligned(), unlike get2byte(), requires that its argument point to a 720329428e2Sdrh ** two-byte aligned address. get2bytea() is only used for accessing the 721329428e2Sdrh ** cell addresses in a btree header. 722329428e2Sdrh */ 723329428e2Sdrh #if SQLITE_BYTEORDER==4321 724329428e2Sdrh # define get2byteAligned(x) (*(u16*)(x)) 725a39284bfSdrh #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000 726329428e2Sdrh # define get2byteAligned(x) __builtin_bswap16(*(u16*)(x)) 72730a5831cSdrh #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 728647ca46fSmistachkin # define get2byteAligned(x) _byteswap_ushort(*(u16*)(x)) 729329428e2Sdrh #else 730329428e2Sdrh # define get2byteAligned(x) ((x)[0]<<8 | (x)[1]) 731329428e2Sdrh #endif 732