Lines Matching defs:pPage

17131   sqlite3_pcache_page *pPage;    /* Pcache object page handle */
17201 SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
20207 void *pPage; /* Page cache memory */
20208 int szPage; /* Size of each page in pPage[] */
20209 int nPage; /* Number of pages in pPage[] */
22940 (void*)0, /* pPage */
30380 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
30382 sqlite3GlobalConfig.pPage = 0;
53394 static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
53397 UNUSED_PARAMETER(pPage);
53685 DbPage *pPage = 0;
53687 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
53689 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
53693 sqlite3PagerUnref(pPage);
54313 if( pLower && ((PgHdr*)pLower)->pPage==0 ){
54403 #define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
54404 #define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
54405 #define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
54408 ** Manage pPage's participation on the dirty list. Bits of the addRemove
54410 ** remove pPage from the dirty list. The 0x02 means add pPage back to
54411 ** the dirty list. Doing both moves pPage to the front of the dirty list.
54413 static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
54414 PCache *p = pPage->pCache;
54418 pPage->pgno));
54420 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
54421 assert( pPage->pDirtyPrev || pPage==p->pDirty );
54424 if( p->pSynced==pPage ){
54425 p->pSynced = pPage->pDirtyPrev;
54428 if( pPage->pDirtyNext ){
54429 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
54431 assert( pPage==p->pDirtyTail );
54432 p->pDirtyTail = pPage->pDirtyPrev;
54434 if( pPage->pDirtyPrev ){
54435 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
54441 assert( pPage==p->pDirty );
54442 p->pDirty = pPage->pDirtyNext;
54451 pPage->pDirtyPrev = 0;
54452 pPage->pDirtyNext = p->pDirty;
54453 if( pPage->pDirtyNext ){
54454 assert( pPage->pDirtyNext->pDirtyPrev==0 );
54455 pPage->pDirtyNext->pDirtyPrev = pPage;
54457 p->pDirtyTail = pPage;
54463 p->pDirty = pPage;
54471 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
54473 p->pSynced = pPage;
54486 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
54722 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
54725 assert( pPage!=0 );
54726 pPgHdr = (PgHdr*)pPage->pExtra;
54727 assert( pPgHdr->pPage==0 );
54729 pPgHdr->pPage = pPage;
54730 pPgHdr->pData = pPage->pBuf;
54736 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
54748 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
54752 assert( pPage!=0 );
54753 pPgHdr = (PgHdr *)pPage->pExtra;
54755 if( !pPgHdr->pPage ){
54756 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
54803 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
54896 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
55712 PgHdr1 *pPage;
55714 while( (pPage = pNext)!=0 ){
55715 unsigned int h = pPage->iKey % nNew;
55716 pNext = pPage->pNext;
55717 pPage->pNext = apNew[h];
55718 apNew[h] = pPage;
55728 ** This function is used internally to remove the page pPage from the
55729 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
55734 static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
55735 assert( pPage!=0 );
55736 assert( PAGE_IS_UNPINNED(pPage) );
55737 assert( pPage->pLruNext );
55738 assert( pPage->pLruPrev );
55739 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
55740 pPage->pLruPrev->pLruNext = pPage->pLruNext;
55741 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
55742 pPage->pLruNext = 0;
55743 /* pPage->pLruPrev = 0;
55745 assert( pPage->isAnchor==0 );
55746 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
55747 pPage->pCache->nRecyclable--;
55748 return pPage;
55759 static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
55761 PCache1 *pCache = pPage->pCache;
55765 h = pPage->iKey % pCache->nHash;
55766 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
55770 if( freeFlag ) pcache1FreePage(pPage);
55827 PgHdr1 *pPage;
55830 while( (pPage = *pp)!=0 ){
55831 if( pPage->iKey>=iLimit ){
55833 *pp = pPage->pNext;
55834 if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
55835 pcache1FreePage(pPage);
55837 pp = &pPage->pNext;
55876 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
55879 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
55890 && sqlite3GlobalConfig.pPage==0
56037 PgHdr1 *pPage = 0;
56061 pPage = pGroup->lru.pLruPrev;
56062 assert( PAGE_IS_UNPINNED(pPage) );
56063 pcache1RemoveFromHash(pPage, 0);
56064 pcache1PinPage(pPage);
56065 pOther = pPage->pCache;
56067 pcache1FreePage(pPage);
56068 pPage = 0;
56077 if( !pPage ){
56078 pPage = pcache1AllocPage(pCache, createFlag==1);
56081 if( pPage ){
56084 pPage->iKey = iKey;
56085 pPage->pNext = pCache->apHash[h];
56086 pPage->pCache = pCache;
56087 pPage->pLruNext = 0;
56088 /* pPage->pLruPrev = 0;
56090 *(void **)pPage->page.pExtra = 0;
56091 pCache->apHash[h] = pPage;
56096 return pPage;
56164 PgHdr1 *pPage = 0;
56167 pPage = pCache->apHash[iKey % pCache->nHash];
56168 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
56174 if( pPage ){
56175 if( PAGE_IS_UNPINNED(pPage) ){
56176 return pcache1PinPage(pPage);
56178 return pPage;
56194 PgHdr1 *pPage;
56197 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
56198 assert( pPage==0 || pCache->iMaxKey>=iKey );
56200 return pPage;
56240 PgHdr1 *pPage = (PgHdr1 *)pPg;
56243 assert( pPage->pCache==pCache );
56249 assert( pPage->pLruNext==0 );
56250 assert( PAGE_IS_PINNED(pPage) );
56253 pcache1RemoveFromHash(pPage, 1);
56257 pPage->pLruPrev = &pGroup->lru;
56258 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
56259 *ppFirst = pPage;
56276 PgHdr1 *pPage = (PgHdr1 *)pPg;
56279 assert( pPage->iKey==iOld );
56280 assert( pPage->pCache==pCache );
56285 assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */
56288 while( (*pp)!=pPage ){
56291 *pp = pPage->pNext;
56295 pPage->iKey = iNew;
56296 pPage->pNext = pCache->apHash[hNew];
56297 pCache->apHash[hNew] = pPage;
56396 if( sqlite3GlobalConfig.pPage==0 ){
58347 ** Return a 32-bit hash of the page data for pPage.
58357 static u32 pager_pagehash(PgHdr *pPage){
58358 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
58360 static void pager_set_pagehash(PgHdr *pPage){
58361 pPage->pageHash = pager_pagehash(pPage);
61181 assert( p->pPage==0 );
62840 sqlite3_pcache_page *pPage;
62844 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
62845 assert( pPage==0 || pPager->hasHeldSharedLock );
62846 if( pPage==0 ) return 0;
62847 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
63258 PgHdr *pPage;
63261 rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
63263 rc = pager_write(pPage);
63264 if( pPage->flags&PGHDR_NEED_SYNC ){
63267 sqlite3PagerUnrefNotNull(pPage);
63270 }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
63271 if( pPage->flags&PGHDR_NEED_SYNC ){
63274 sqlite3PagerUnrefNotNull(pPage);
63287 PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
63288 if( pPage ){
63289 pPage->flags |= PGHDR_NEED_SYNC;
63290 sqlite3PagerUnrefNotNull(pPage);
63926 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
63927 return sqlite3PcachePageRefcount(pPage);
66196 ** pPage into WAL frame iFrame.
68829 PgHdr *pPage, /* The page of the frame to be written */
68836 pData = pPage->pData;
68837 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
70061 MemPage *pPage; /* Current page */
71074 static void releasePage(MemPage *pPage); /* Forward reference */
71075 static void releasePageOne(MemPage *pPage); /* Forward reference */
71076 static void releasePageNotNull(MemPage *pPage); /* Forward reference */
71239 releasePageNotNull(pCur->pPage);
71722 MemPage *pPage, /* Page containing the cell */
71739 minLocal = pPage->minLocal;
71740 maxLocal = pPage->maxLocal;
71741 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
71754 ** page pPage, return the number of bytes of payload stored locally.
71756 static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
71758 maxLocal = pPage->maxLocal;
71764 minLocal = pPage->minLocal;
71765 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
71785 MemPage *pPage, /* Page containing the cell */
71789 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71790 assert( pPage->leaf==0 );
71791 assert( pPage->childPtrSize==4 );
71793 UNUSED_PARAMETER(pPage);
71802 MemPage *pPage, /* Page containing the cell */
71810 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71811 assert( pPage->leaf==0 || pPage->leaf==1 );
71812 assert( pPage->intKeyLeaf );
71813 assert( pPage->childPtrSize==0 );
71874 testcase( nPayload==pPage->maxLocal );
71875 testcase( nPayload==(u32)pPage->maxLocal+1 );
71876 if( nPayload<=pPage->maxLocal ){
71884 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
71888 MemPage *pPage, /* Page containing the cell */
71895 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71896 assert( pPage->leaf==0 || pPage->leaf==1 );
71897 assert( pPage->intKeyLeaf==0 );
71898 pIter = pCell + pPage->childPtrSize;
71911 testcase( nPayload==pPage->maxLocal );
71912 testcase( nPayload==(u32)pPage->maxLocal+1 );
71913 if( nPayload<=pPage->maxLocal ){
71921 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
71925 MemPage *pPage, /* Page containing the cell */
71929 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
71946 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
71957 pPage->xParseCell(pPage, pCell, &debuginfo);
71960 assert( pPage->childPtrSize==4 );
71970 testcase( nSize==pPage->maxLocal );
71971 testcase( nSize==(u32)pPage->maxLocal+1 );
71972 if( nSize<=pPage->maxLocal ){
71976 int minLocal = pPage->minLocal;
71977 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
71978 testcase( nSize==pPage->maxLocal );
71979 testcase( nSize==(u32)pPage->maxLocal+1 );
71980 if( nSize>pPage->maxLocal ){
71988 static u16 cellSizePtrIdxLeaf(MemPage *pPage, u8 *pCell){
71999 pPage->xParseCell(pPage, pCell, &debuginfo);
72002 assert( pPage->childPtrSize==0 );
72012 testcase( nSize==pPage->maxLocal );
72013 testcase( nSize==(u32)pPage->maxLocal+1 );
72014 if( nSize<=pPage->maxLocal ){
72018 int minLocal = pPage->minLocal;
72019 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72020 testcase( nSize==pPage->maxLocal );
72021 testcase( nSize==(u32)pPage->maxLocal+1 );
72022 if( nSize>pPage->maxLocal ){
72030 static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
72040 pPage->xParseCell(pPage, pCell, &debuginfo);
72042 UNUSED_PARAMETER(pPage);
72045 assert( pPage->childPtrSize==4 );
72051 static u16 cellSizePtrTableLeaf(MemPage *pPage, u8 *pCell){
72062 pPage->xParseCell(pPage, pCell, &debuginfo);
72085 testcase( nSize==pPage->maxLocal );
72086 testcase( nSize==(u32)pPage->maxLocal+1 );
72087 if( nSize<=pPage->maxLocal ){
72091 int minLocal = pPage->minLocal;
72092 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72093 testcase( nSize==pPage->maxLocal );
72094 testcase( nSize==(u32)pPage->maxLocal+1 );
72095 if( nSize>pPage->maxLocal ){
72108 static u16 cellSize(MemPage *pPage, int iCell){
72109 return pPage->xCellSize(pPage, findCell(pPage, iCell));
72116 ** of pPage. (pSrc and pPage are often the same.) If pCell contains a
72118 ** the overflow page that will be valid after pCell has been moved to pPage.
72120 static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
72124 pPage->xParseCell(pPage, pCell, &info);
72128 testcase( pSrc!=pPage );
72133 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
72151 static int defragmentPage(MemPage *pPage, int nMaxFrag){
72167 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72168 assert( pPage->pBt!=0 );
72169 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
72170 assert( pPage->nOverflow==0 );
72171 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72172 data = pPage->aData;
72173 hdr = pPage->hdrOffset;
72174 cellOffset = pPage->cellOffset;
72175 nCell = pPage->nCell;
72178 usableSize = pPage->pBt->usableSize;
72187 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
72190 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
72198 return SQLITE_CORRUPT_PAGE(pPage);
72201 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
72203 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
72207 return SQLITE_CORRUPT_PAGE(pPage);
72227 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
72240 return SQLITE_CORRUPT_PAGE(pPage);
72243 size = pPage->xCellSize(pPage, &src[pc]);
72246 return SQLITE_CORRUPT_PAGE(pPage);
72258 assert( pPage->nFree>=0 );
72259 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
72260 return SQLITE_CORRUPT_PAGE(pPage);
72267 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72346 ** as the first argument. Write into *pIdx the index into pPage->aData[]
72357 static SQLITE_INLINE int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
72358 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
72359 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
72365 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72366 assert( pPage->pBt );
72367 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72369 assert( pPage->nFree>=nByte );
72370 assert( pPage->nOverflow==0 );
72371 assert( nByte < (int)(pPage->pBt->usableSize-8) );
72373 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
72374 gap = pPage->cellOffset + 2*pPage->nCell;
72384 if( top==0 && pPage->pBt->usableSize==65536 ){
72387 return SQLITE_CORRUPT_PAGE(pPage);
72389 }else if( top>(int)pPage->pBt->usableSize ){
72390 return SQLITE_CORRUPT_PAGE(pPage);
72401 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
72404 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
72407 return SQLITE_CORRUPT_PAGE(pPage);
72421 assert( pPage->nCell>0 || CORRUPT_DB );
72422 assert( pPage->nFree>=0 );
72423 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
72438 assert( top+nByte <= (int)pPage->pBt->usableSize );
72444 ** Return a section of the pPage->aData to the freelist.
72445 ** The first byte of the new free block is pPage->aData[iStart]
72456 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
72464 unsigned char *data = pPage->aData; /* Page content */
72467 assert( pPage->pBt!=0 );
72468 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72469 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
72470 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
72471 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72473 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 );
72478 hdr = pPage->hdrOffset;
72486 return SQLITE_CORRUPT_PAGE(pPage);
72490 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
72491 return SQLITE_CORRUPT_PAGE(pPage);
72503 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
72505 if( iEnd > pPage->pBt->usableSize ){
72506 return SQLITE_CORRUPT_PAGE(pPage);
72519 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
72525 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
72530 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
72539 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage);
72540 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
72549 pPage->nFree += iOrigSize;
72565 static int decodeFlags(MemPage *pPage, int flagByte){
72566 BtShared *pBt; /* A copy of pPage->pBt */
72568 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
72569 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72570 pBt = pPage->pBt;
72571 pPage->max1bytePayload = pBt->max1bytePayload;
72573 pPage->childPtrSize = 0;
72574 pPage->leaf = 1;
72576 pPage->intKeyLeaf = 1;
72577 pPage->xCellSize = cellSizePtrTableLeaf;
72578 pPage->xParseCell = btreeParseCellPtr;
72579 pPage->intKey = 1;
72580 pPage->maxLocal = pBt->maxLeaf;
72581 pPage->minLocal = pBt->minLeaf;
72583 pPage->intKey = 0;
72584 pPage->intKeyLeaf = 0;
72585 pPage->xCellSize = cellSizePtrIdxLeaf;
72586 pPage->xParseCell = btreeParseCellPtrIndex;
72587 pPage->maxLocal = pBt->maxLocal;
72588 pPage->minLocal = pBt->minLocal;
72590 pPage->intKey = 0;
72591 pPage->intKeyLeaf = 0;
72592 pPage->xCellSize = cellSizePtrIdxLeaf;
72593 pPage->xParseCell = btreeParseCellPtrIndex;
72594 return SQLITE_CORRUPT_PAGE(pPage);
72597 pPage->childPtrSize = 4;
72598 pPage->leaf = 0;
72600 pPage->intKey = 0;
72601 pPage->intKeyLeaf = 0;
72602 pPage->xCellSize = cellSizePtr;
72603 pPage->xParseCell = btreeParseCellPtrIndex;
72604 pPage->maxLocal = pBt->maxLocal;
72605 pPage->minLocal = pBt->minLocal;
72607 pPage->intKeyLeaf = 0;
72608 pPage->xCellSize = cellSizePtrNoPayload;
72609 pPage->xParseCell = btreeParseCellPtrNoPayload;
72610 pPage->intKey = 1;
72611 pPage->maxLocal = pBt->maxLeaf;
72612 pPage->minLocal = pBt->minLeaf;
72614 pPage->intKey = 0;
72615 pPage->intKeyLeaf = 0;
72616 pPage->xCellSize = cellSizePtr;
72617 pPage->xParseCell = btreeParseCellPtrIndex;
72618 return SQLITE_CORRUPT_PAGE(pPage);
72626 ** in the pPage->nFree field.
72628 static int btreeComputeFreeSpace(MemPage *pPage){
72629 int pc; /* Address of a freeblock within pPage->aData[] */
72631 u8 *data; /* Equal to pPage->aData */
72638 assert( pPage->pBt!=0 );
72639 assert( pPage->pBt->db!=0 );
72640 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72641 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
72642 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
72643 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
72644 assert( pPage->isInit==1 );
72645 assert( pPage->nFree<0 );
72647 usableSize = pPage->pBt->usableSize;
72648 hdr = pPage->hdrOffset;
72649 data = pPage->aData;
72654 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
72669 return SQLITE_CORRUPT_PAGE(pPage);
72674 return SQLITE_CORRUPT_PAGE(pPage);
72684 return SQLITE_CORRUPT_PAGE(pPage);
72688 return SQLITE_CORRUPT_PAGE(pPage);
72700 return SQLITE_CORRUPT_PAGE(pPage);
72702 pPage->nFree = (u16)(nFree - iCellFirst);
72710 static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
72715 int pc; /* Address of a freeblock within pPage->aData[] */
72716 u8 *data; /* Equal to pPage->aData */
72720 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
72721 usableSize = pPage->pBt->usableSize;
72723 data = pPage->aData;
72724 cellOffset = pPage->cellOffset;
72725 if( !pPage->leaf ) iCellLast--;
72726 for(i=0; i<pPage->nCell; i++){
72731 return SQLITE_CORRUPT_PAGE(pPage);
72733 sz = pPage->xCellSize(pPage, &data[pc]);
72736 return SQLITE_CORRUPT_PAGE(pPage);
72751 static int btreeInitPage(MemPage *pPage){
72752 u8 *data; /* Equal to pPage->aData */
72755 assert( pPage->pBt!=0 );
72756 assert( pPage->pBt->db!=0 );
72757 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72758 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
72759 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
72760 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
72761 assert( pPage->isInit==0 );
72763 pBt = pPage->pBt;
72764 data = pPage->aData + pPage->hdrOffset;
72767 if( decodeFlags(pPage, data[0]) ){
72768 return SQLITE_CORRUPT_PAGE(pPage);
72771 pPage->maskPage = (u16)(pBt->pageSize - 1);
72772 pPage->nOverflow = 0;
72773 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
72774 pPage->aCellIdx = data + pPage->childPtrSize + 8;
72775 pPage->aDataEnd = pPage->aData + pBt->pageSize;
72776 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
72779 pPage->nCell = get2byte(&data[3]);
72780 if( pPage->nCell>MX_CELL(pBt) ){
72782 return SQLITE_CORRUPT_PAGE(pPage);
72784 testcase( pPage->nCell==MX_CELL(pBt) );
72789 assert( pPage->nCell>0
72792 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
72793 pPage->isInit = 1;
72795 return btreeCellSizeCheck(pPage);
72804 static void zeroPage(MemPage *pPage, int flags){
72805 unsigned char *data = pPage->aData;
72806 BtShared *pBt = pPage->pBt;
72807 u8 hdr = pPage->hdrOffset;
72810 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
72811 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72812 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
72813 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72823 pPage->nFree = (u16)(pBt->usableSize - first);
72824 decodeFlags(pPage, flags);
72825 pPage->cellOffset = first;
72826 pPage->aDataEnd = &data[pBt->pageSize];
72827 pPage->aCellIdx = &data[first];
72828 pPage->aDataOfst = &data[pPage->childPtrSize];
72829 pPage->nOverflow = 0;
72831 pPage->maskPage = (u16)(pBt->pageSize - 1);
72832 pPage->nCell = 0;
72833 pPage->isInit = 1;
72842 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
72843 if( pgno!=pPage->pgno ){
72844 pPage->aData = sqlite3PagerGetData(pDbPage);
72845 pPage->pDbPage = pDbPage;
72846 pPage->pBt = pBt;
72847 pPage->pgno = pgno;
72848 pPage->hdrOffset = pgno==1 ? 100 : 0;
72850 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
72851 return pPage;
72920 MemPage *pPage;
72932 pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
72933 if( pPage->isInit==0 ){
72935 rc = btreeInitPage(pPage);
72937 releasePage(pPage);
72942 assert( pPage->pgno==pgno || CORRUPT_DB );
72943 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
72944 *ppPage = pPage;
72954 static void releasePageNotNull(MemPage *pPage){
72955 assert( pPage->aData );
72956 assert( pPage->pBt );
72957 assert( pPage->pDbPage!=0 );
72958 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72959 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
72960 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72961 sqlite3PagerUnrefNotNull(pPage->pDbPage);
72963 static void releasePage(MemPage *pPage){
72964 if( pPage ) releasePageNotNull(pPage);
72966 static void releasePageOne(MemPage *pPage){
72967 assert( pPage!=0 );
72968 assert( pPage->aData );
72969 assert( pPage->pBt );
72970 assert( pPage->pDbPage!=0 );
72971 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72972 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
72973 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72974 sqlite3PagerUnrefPageOne(pPage->pDbPage);
73016 MemPage *pPage;
73017 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
73019 if( pPage->isInit ){
73020 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73021 pPage->isInit = 0;
73023 /* pPage might not be a btree page; it might be an overflow page
73029 btreeInitPage(pPage);
74328 ** Set the pointer-map entries for all children of page pPage. Also, if
74329 ** pPage contains cells that point to overflow pages, set the pointer
74332 static int setChildPtrmaps(MemPage *pPage){
74334 int nCell; /* Number of cells in page pPage */
74336 BtShared *pBt = pPage->pBt;
74337 Pgno pgno = pPage->pgno;
74339 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
74340 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
74342 nCell = pPage->nCell;
74345 u8 *pCell = findCell(pPage, i);
74347 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
74349 if( !pPage->leaf ){
74355 if( !pPage->leaf ){
74356 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74364 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
74368 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
74369 ** page of pPage.
74371 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
74372 ** page pointed to by one of the cells on pPage.
74374 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
74377 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
74378 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
74379 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
74382 if( get4byte(pPage->aData)!=iFrom ){
74383 return SQLITE_CORRUPT_PAGE(pPage);
74385 put4byte(pPage->aData, iTo);
74391 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
74393 nCell = pPage->nCell;
74396 u8 *pCell = findCell(pPage, i);
74399 pPage->xParseCell(pPage, pCell, &info);
74401 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
74402 return SQLITE_CORRUPT_PAGE(pPage);
74410 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
74411 return SQLITE_CORRUPT_PAGE(pPage);
74422 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
74423 return SQLITE_CORRUPT_PAGE(pPage);
74425 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
75363 btreeParseCell(pCur->pPage, pCur->ix, &info);
75372 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
75427 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
75428 (i64)(pCur->info.pPayload - pCur->pPage->aData);
75492 MemPage *pPage = 0;
75526 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
75527 assert( rc==SQLITE_OK || pPage==0 );
75529 next = get4byte(pPage->aData);
75535 *ppPage = pPage;
75537 releasePage(pPage);
75613 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
75619 assert( pPage );
75622 if( pCur->ix>=pPage->nCell ){
75623 return SQLITE_CORRUPT_PAGE(pPage);
75631 assert( aPayload > pPage->aData );
75632 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
75635 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
75638 return SQLITE_CORRUPT_PAGE(pPage);
75647 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
75793 return SQLITE_CORRUPT_PAGE(pPage);
75818 assert( pCur->iPage>=0 && pCur->pPage );
75855 ** the key if index btrees (pPage->intKey==0) and is the data for
75856 ** table btrees (pPage->intKey==1). The number of bytes of available
75876 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
75880 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
75882 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
75883 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
75885 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
75889 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
75936 pCur->apPage[pCur->iPage] = pCur->pPage;
75939 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
75940 assert( pCur->pPage!=0 || rc!=SQLITE_OK );
75942 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
75944 releasePage(pCur->pPage);
75948 pCur->pPage = pCur->apPage[--pCur->iPage];
75988 assert( pCur->pPage );
75992 pCur->pPage->pgno
75998 pLeaf = pCur->pPage;
75999 pCur->pPage = pCur->apPage[--pCur->iPage];
76037 releasePageNotNull(pCur->pPage);
76041 pRoot = pCur->pPage = pCur->apPage[0];
76056 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
76063 pCur->curIntKey = pCur->pPage->intKey;
76065 pRoot = pCur->pPage;
76080 return SQLITE_CORRUPT_PAGE(pCur->pPage);
76113 MemPage *pPage;
76117 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
76118 assert( pCur->ix<pPage->nCell );
76119 pgno = get4byte(findCell(pPage, pCur->ix));
76138 MemPage *pPage = 0;
76142 while( !(pPage = pCur->pPage)->leaf ){
76143 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76144 pCur->ix = pPage->nCell;
76148 pCur->ix = pPage->nCell-1;
76165 assert( pCur->pPage->nCell>0 );
76169 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) );
76189 return pCur->ix==pCur->pPage->nCell-1 && pCur->pPage->leaf!=0;
76209 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
76304 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
76310 assert( pCur->pPage );
76311 assert( pCur->pPage->isInit );
76313 assert( pCur->pPage->nCell > 0 );
76320 MemPage *pPage = pCur->pPage;
76321 u8 *pCell; /* Pointer to current cell in pPage */
76323 /* pPage->nCell must be greater than zero. If this is the root-page
76326 ** would have already detected db corruption. Similarly, pPage must
76329 assert( pPage->nCell>0 );
76330 assert( pPage->intKey );
76332 upr = pPage->nCell-1;
76337 pCell = findCellPastPtr(pPage, idx);
76338 if( pPage->intKeyLeaf ){
76340 if( pCell>=pPage->aDataEnd ){
76341 return SQLITE_CORRUPT_PAGE(pPage);
76355 if( !pPage->leaf ){
76369 assert( lwr==upr+1 || !pPage->leaf );
76370 assert( pPage->isInit );
76371 if( pPage->leaf ){
76372 assert( pCur->ix<pCur->pPage->nCell );
76379 if( lwr>=pPage->nCell ){
76380 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76382 chldPg = get4byte(findCell(pPage, lwr));
76416 MemPage *pPage = pCur->pPage;
76419 u8 *pCell = findCellPastPtr(pPage, idx);
76422 if( nCell<=pPage->max1bytePayload ){
76426 testcase( pCell+nCell+1==pPage->aDataEnd );
76429 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
76433 testcase( pCell+nCell+2==pPage->aDataEnd );
76451 MemPage *pPage = pCur->apPage[i];
76452 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
76520 && pCur->pPage->leaf
76524 if( pCur->ix==pCur->pPage->nCell-1
76536 if( !pCur->pPage->isInit ){
76547 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
76555 assert( pCur->pPage );
76556 assert( pCur->pPage->isInit );
76558 assert( pCur->pPage->nCell > 0 );
76564 MemPage *pPage = pCur->pPage;
76565 u8 *pCell; /* Pointer to current cell in pPage */
76567 /* pPage->nCell must be greater than zero. If this is the root-page
76570 ** would have already detected db corruption. Similarly, pPage must
76573 assert( pPage->nCell>0 );
76574 assert( pPage->intKey==0 );
76576 upr = pPage->nCell-1;
76580 pCell = findCellPastPtr(pPage, idx);
76591 if( nCell<=pPage->max1bytePayload ){
76595 testcase( pCell+nCell+1==pPage->aDataEnd );
76598 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
76602 testcase( pCell+nCell+2==pPage->aDataEnd );
76615 u8 * const pCellBody = pCell - pPage->childPtrSize;
76617 pPage->xParseCell(pPage, pCellBody, &pCur->info);
76624 rc = SQLITE_CORRUPT_PAGE(pPage);
76663 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
76664 assert( pPage->isInit );
76665 if( pPage->leaf ){
76666 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
76672 if( lwr>=pPage->nCell ){
76673 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76675 chldPg = get4byte(findCell(pPage, lwr));
76690 pCur->apPage[pCur->iPage] = pCur->pPage;
76693 rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags);
76695 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
76697 releasePage(pCur->pPage);
76701 pCur->pPage = pCur->apPage[--pCur->iPage];
76745 if( NEVER(pCur->pPage->leaf==0) ) return -1;
76747 n = pCur->pPage->nCell;
76777 MemPage *pPage;
76795 pPage = pCur->pPage;
76797 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
76798 if( !pPage->isInit ){
76802 if( idx>=pPage->nCell ){
76803 if( !pPage->leaf ){
76804 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
76814 pPage = pCur->pPage;
76815 }while( pCur->ix>=pPage->nCell );
76816 if( pPage->intKey ){
76822 if( pPage->leaf ){
76829 MemPage *pPage;
76836 pPage = pCur->pPage;
76837 if( (++pCur->ix)>=pPage->nCell ){
76841 if( pPage->leaf ){
76870 MemPage *pPage;
76889 pPage = pCur->pPage;
76890 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
76891 if( !pPage->isInit ){
76894 if( !pPage->leaf ){
76896 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
76911 pPage = pCur->pPage;
76912 if( pPage->intKey && !pPage->leaf ){
76928 || pCur->pPage->leaf==0
77284 MemPage *pPage; /* Page being freed. May be NULL. */
77296 pPage = pMemPage;
77297 sqlite3PagerRef(pPage->pDbPage);
77299 pPage = btreePageLookup(pBt, iPage);
77312 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
77313 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
77317 memset(pPage->aData, 0, pPage->pBt->pageSize);
77378 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
77379 sqlite3PagerDontWrite(pPage->pDbPage);
77383 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
77394 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
77397 rc = sqlite3PagerWrite(pPage->pDbPage);
77401 put4byte(pPage->aData, iTrunk);
77402 put4byte(&pPage->aData[4], 0);
77404 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
77407 if( pPage ){
77408 pPage->isInit = 0;
77410 releasePage(pPage);
77414 static void freePage(MemPage *pPage, int *pRC){
77416 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
77424 MemPage *pPage, /* The page that contains the Cell */
77434 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77436 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
77437 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
77438 if( pCell + pInfo->nSize > pPage->aDataEnd ){
77440 return SQLITE_CORRUPT_PAGE(pPage);
77443 pBt = pPage->pBt;
77497 #define BTREE_CLEAR_CELL(rc, pPage, pCell, sInfo) \
77498 pPage->xParseCell(pPage, pCell, &sInfo); \
77500 rc = clearCellOverflow(pPage, pCell, &sInfo); \
77507 ** Create the byte sequence used to represent a cell on page pPage
77513 ** Note that pCell does not necessary need to point to the pPage->aData
77515 ** be constructed in this temporary area then copied into pPage->aData
77519 MemPage *pPage, /* The page that contains the cell */
77535 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77537 /* pPage is not necessarily writeable since pCell might be auxiliary
77538 ** buffer space that is separate from the pPage buffer area */
77539 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
77540 || sqlite3PagerIswriteable(pPage->pDbPage) );
77543 nHeader = pPage->childPtrSize;
77544 if( pPage->intKey ){
77548 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
77560 if( nPayload<=pPage->maxLocal ){
77581 mn = pPage->minLocal;
77582 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
77583 testcase( n==pPage->maxLocal );
77584 testcase( n==pPage->maxLocal+1 );
77585 if( n > pPage->maxLocal ) n = mn;
77591 pBt = pPage->pBt;
77608 pPage->xParseCell(pPage, pCell, &info);
77625 /* If pPayload is part of the data area of pPage, then make sure pPage
77627 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
77628 || sqlite3PagerIswriteable(pPage->pDbPage) );
77685 /* If pPrior is part of the data area of pPage, then make sure pPage
77687 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
77688 || sqlite3PagerIswriteable(pPage->pDbPage) );
77704 ** Remove the i-th cell from pPage. This routine effects pPage only.
77707 ** removes the reference to the cell from pPage.
77711 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
77713 u8 *data; /* pPage->aData */
77720 assert( idx<pPage->nCell );
77721 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
77722 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77723 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77724 assert( pPage->nFree>=0 );
77725 data = pPage->aData;
77726 ptr = &pPage->aCellIdx[2*idx];
77727 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
77729 hdr = pPage->hdrOffset;
77731 testcase( pc+sz==pPage->pBt->usableSize );
77732 if( pc+sz > pPage->pBt->usableSize ){
77736 rc = freeSpace(pPage, pc, sz);
77741 pPage->nCell--;
77742 if( pPage->nCell==0 ){
77745 put2byte(&data[hdr+5], pPage->pBt->usableSize);
77746 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
77747 - pPage->childPtrSize - 8;
77749 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
77750 put2byte(&data[hdr+3], pPage->nCell);
77751 pPage->nFree += 2;
77756 ** Insert a new cell on pPage at cell index "i". pCell points to the
77762 ** in pPage->apOvfl[] and make it point to the cell content (either
77764 ** Allocating a new entry in pPage->aCell[] implies that
77765 ** pPage->nOverflow is incremented.
77776 MemPage *pPage, /* Page into which we are copying */
77786 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
77788 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
77789 assert( MX_CELL(pPage->pBt)<=10921 );
77790 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
77791 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
77792 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
77793 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77794 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
77795 assert( pPage->nFree>=0 );
77797 if( pPage->nOverflow || sz+2>pPage->nFree ){
77803 j = pPage->nOverflow++;
77807 assert( j < ArraySize(pPage->apOvfl)-1 );
77808 pPage->apOvfl[j] = pCell;
77809 pPage->aiOvfl[j] = (u16)i;
77816 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
77817 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
77819 int rc = sqlite3PagerWrite(pPage->pDbPage);
77823 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77824 data = pPage->aData;
77825 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
77826 rc = allocateSpace(pPage, sz, &idx);
77831 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
77832 assert( idx+sz <= (int)pPage->pBt->usableSize );
77833 pPage->nFree -= (u16)(2 + sz);
77841 pIns = pPage->aCellIdx + i*2;
77842 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
77844 pPage->nCell++;
77846 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
77847 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
77849 if( pPage->pBt->autoVacuum ){
77854 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
77873 MemPage *pPage, /* Page into which we are copying */
77881 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
77883 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
77884 assert( MX_CELL(pPage->pBt)<=10921 );
77885 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
77886 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
77887 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
77888 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77889 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
77890 assert( pPage->nFree>=0 );
77891 assert( pPage->nOverflow==0 );
77892 if( sz+2>pPage->nFree ){
77893 j = pPage->nOverflow++;
77897 assert( j < ArraySize(pPage->apOvfl)-1 );
77898 pPage->apOvfl[j] = pCell;
77899 pPage->aiOvfl[j] = (u16)i;
77906 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
77907 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
77909 int rc = sqlite3PagerWrite(pPage->pDbPage);
77913 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77914 data = pPage->aData;
77915 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
77916 rc = allocateSpace(pPage, sz, &idx);
77921 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
77922 assert( idx+sz <= (int)pPage->pBt->usableSize );
77923 pPage->nFree -= (u16)(2 + sz);
77925 pIns = pPage->aCellIdx + i*2;
77926 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
77928 pPage->nCell++;
77930 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
77931 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
77933 if( pPage->pBt->autoVacuum ){
77938 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
77963 #define NN 1 /* Number of neighbors on either side of pPage */
78434 ** pPage is the leaf page which is the right-most page in the tree.
78435 ** pParent is its parent. pPage must have a single overflow entry
78444 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
78445 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
78450 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
78452 assert( pPage->nOverflow==1 );
78454 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
78455 assert( pPage->nFree>=0 );
78459 ** pPage. Make the parent page writable, so that the new divider cell
78467 u8 *pCell = pPage->apOvfl[0];
78468 u16 szCell = pPage->xCellSize(pPage, pCell);
78473 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
78476 b.pRef = pPage;
78479 b.apEnd[0] = pPage->aDataEnd;
78505 ** consists of a 4-byte page number (the page number of pPage) and
78507 ** largest key on pPage).
78509 ** To find the largest key value on pPage, first find the right-most
78510 ** cell on pPage. The first two fields of this cell are the
78515 ** cell on pPage into the pSpace buffer.
78517 pCell = findCell(pPage, pPage->nCell-1);
78526 0, pPage->pgno);
78551 MemPage *pPage = apPage[i];
78552 BtShared *pBt = pPage->pBt;
78553 assert( pPage->isInit );
78555 for(j=0; j<pPage->nCell; j++){
78559 z = findCell(pPage, j);
78560 pPage->xParseCell(pPage, z, &info);
78564 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
78566 if( !pPage->leaf ){
78569 assert( n==pPage->pgno && e==PTRMAP_BTREE );
78572 if( !pPage->leaf ){
78573 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
78575 assert( n==pPage->pgno && e==PTRMAP_BTREE );
78695 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
78696 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
78697 int usableSpace; /* Bytes in pPage beyond the header */
78698 int pageFlags; /* Value of pPage->aData[0] */
78702 MemPage *apOld[NB]; /* pPage and up to two siblings */
78703 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
78736 ** either side of pPage. More siblings are taken from one side, however,
78856 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
78857 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
79085 ** (2) pPage is a virtual root page. A virtual root page is when
79487 ** page that will become the new right-child of pPage. Copy the contents
79540 && pOther->pPage==pCur->pPage
79542 return SQLITE_CORRUPT_PAGE(pCur->pPage);
79568 MemPage *pPage = pCur->pPage;
79570 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
79571 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
79578 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
79586 rc = balance_deeper(pPage, &pCur->apPage[1]);
79591 pCur->apPage[0] = pPage;
79592 pCur->pPage = pCur->apPage[1];
79593 assert( pCur->pPage->nOverflow );
79598 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
79602 rc = SQLITE_CORRUPT_PAGE(pPage);
79613 if( pPage->intKeyLeaf
79614 && pPage->nOverflow==1
79615 && pPage->aiOvfl[0]==pPage->nCell
79619 /* Call balance_quick() to create a new sibling of pPage on which
79634 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
79639 ** between pPage and up to 2 of its sibling pages. This involves
79673 pPage->nOverflow = 0;
79676 releasePage(pPage);
79679 pCur->pPage = pCur->apPage[pCur->iPage];
79693 MemPage *pPage, /* MemPage on which writing will occur */
79705 int rc = sqlite3PagerWrite(pPage->pDbPage);
79713 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
79719 int rc = sqlite3PagerWrite(pPage->pDbPage);
79743 MemPage *pPage = pCur->pPage; /* Page being written */
79751 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79760 pBt = pPage->pBt;
79763 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
79765 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
79766 rc = SQLITE_CORRUPT_PAGE(pPage);
79769 ovflPgno = get4byte(pPage->aData);
79773 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
79776 sqlite3PagerUnref(pPage->pDbPage);
79789 MemPage *pPage = pCur->pPage; /* Page being written */
79791 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
79792 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
79794 return SQLITE_CORRUPT_PAGE(pPage);
79798 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79847 MemPage *pPage;
79992 pPage = pCur->pPage;
79993 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
79994 assert( pPage->leaf || !pPage->intKey );
79995 if( pPage->nFree<0 ){
79998 rc = SQLITE_CORRUPT_PAGE(pPage);
80000 rc = btreeComputeFreeSpace(pPage);
80006 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
80008 assert( pPage->isInit || CORRUPT_DB );
80019 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
80021 pPage->xParseCell(pPage, newCell, &info);
80024 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
80029 rc = fillInCell(pPage, newCell, pX, &szNew);
80032 assert( szNew==pPage->xCellSize(pPage, newCell) );
80039 if( idx>=pPage->nCell ){
80040 return SQLITE_CORRUPT_PAGE(pPage);
80042 rc = sqlite3PagerWrite(pPage->pDbPage);
80046 oldCell = findCell(pPage, idx);
80047 if( !pPage->leaf ){
80050 BTREE_CLEAR_CELL(rc, pPage, oldCell, info);
80054 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
80066 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
80067 return SQLITE_CORRUPT_PAGE(pPage);
80069 if( oldCell+szNew > pPage->aDataEnd ){
80070 return SQLITE_CORRUPT_PAGE(pPage);
80075 dropCell(pPage, idx, info.nSize, &rc);
80077 }else if( loc<0 && pPage->nCell>0 ){
80078 assert( pPage->leaf );
80082 assert( pPage->leaf );
80084 rc = insertCellFast(pPage, idx, newCell, szNew);
80085 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
80086 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
80088 /* If no error has occurred and pPage has an overflow cell, call balance()
80108 if( pPage->nOverflow ){
80117 pCur->pPage->nOverflow = 0;
80134 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
80171 if( aIn+nIn>pSrc->pPage->aDataEnd ){
80172 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80175 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
80188 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
80196 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
80197 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80273 MemPage *pPage; /* Page to delete cell from */
80300 pPage = pCur->pPage;
80301 if( pPage->nCell<=iCellIdx ){
80302 return SQLITE_CORRUPT_PAGE(pPage);
80304 pCell = findCell(pPage, iCellIdx);
80305 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
80306 return SQLITE_CORRUPT_PAGE(pPage);
80308 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
80309 return SQLITE_CORRUPT_PAGE(pPage);
80330 if( !pPage->leaf
80331 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
80333 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
80351 if( !pPage->leaf ){
80373 rc = sqlite3PagerWrite(pPage->pDbPage);
80375 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
80376 dropCell(pPage, iCellIdx, info.nSize, &rc);
80384 if( !pPage->leaf ){
80385 MemPage *pLeaf = pCur->pPage;
80397 n = pCur->pPage->pgno;
80407 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
80428 assert( pCur->pPage->nOverflow==0 );
80429 assert( pCur->pPage->nFree>=0 );
80430 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
80438 releasePageNotNull(pCur->pPage);
80443 pCur->pPage = pCur->apPage[pCur->iPage];
80450 assert( pPage==pCur->pPage || CORRUPT_DB );
80451 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
80453 if( iCellIdx>=pPage->nCell ){
80455 pCur->ix = pPage->nCell-1;
80645 MemPage *pPage;
80656 rc = getAndInitPage(pBt, pgno, &pPage, 0);
80659 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
80661 rc = SQLITE_CORRUPT_PAGE(pPage);
80664 hdr = pPage->hdrOffset;
80665 for(i=0; i<pPage->nCell; i++){
80666 pCell = findCell(pPage, i);
80667 if( !pPage->leaf ){
80671 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
80674 if( !pPage->leaf ){
80675 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
80677 if( pPage->intKey ) pnChange = 0;
80680 testcase( !pPage->intKey );
80681 *pnChange += pPage->nCell;
80684 freePage(pPage, &rc);
80685 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
80686 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
80690 releasePage(pPage);
80758 MemPage *pPage = 0;
80770 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
80772 releasePage(pPage);
80779 freePage(pPage, &rc);
80780 releasePage(pPage);
80790 freePage(pPage, &rc);
80791 releasePage(pPage);
80801 releasePage(pPage);
80835 freePage(pPage, &rc);
80836 releasePage(pPage);
80947 MemPage *pPage; /* Current page of the b-tree */
80953 pPage = pCur->pPage;
80954 if( pPage->leaf || !pPage->intKey ){
80955 nEntry += pPage->nCell;
80958 /* pPage is a leaf node. This loop navigates the cursor so that it
80968 if( pPage->leaf ){
80976 }while ( pCur->ix>=pCur->pPage->nCell );
80979 pPage = pCur->pPage;
80983 ** points at. This is the right-child if (iIdx==pPage->nCell).
80986 if( iIdx==pPage->nCell ){
80987 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
80989 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
81289 MemPage *pPage = 0; /* The page being analyzed */
81304 BtShared *pBt; /* The BtShared object that owns pPage */
81325 if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){
81334 savedIsInit = pPage->isInit;
81335 pPage->isInit = 0;
81336 if( (rc = btreeInitPage(pPage))!=0 ){
81342 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
81347 data = pPage->aData;
81348 hdr = pPage->hdrOffset;
81358 assert( pPage->nCell==nCell );
81359 if( pPage->leaf || pPage->intKey==0 ){
81365 cellStart = hdr + 12 - 4*pPage->leaf;
81366 assert( pPage->aCellIdx==&data[cellStart] );
81369 if( !pPage->leaf ){
81404 pPage->xParseCell(pPage, pCell, &info);
81412 if( pPage->intKey ){
81435 if( !pPage->leaf ){
81463 if( !pPage->leaf ){
81469 size = pPage->xCellSize(pPage, &data[pc]);
81537 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
81538 releasePage(pPage);
81911 assert( pCsr->pPage->intKey );
180085 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
180312 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
244881 Fts5PageWriter *pPage = &pWriter->writer;
244884 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) );
244887 assert( 0==fts5GetU16(&pPage->buf.p[2]) );
244888 fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
244892 assert( pPage->pgidx.n==0 );
244896 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
244900 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
244901 fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
244904 fts5BufferZero(&pPage->buf);
244905 fts5BufferZero(&pPage->pgidx);
244906 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
244907 pPage->iPrevPgidx = 0;
244908 pPage->pgno++;
244931 Fts5PageWriter *pPage = &pWriter->writer;
244933 int nMin = MIN(pPage->term.n, nTerm);
244936 assert( pPage->buf.n>=4 );
244937 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage );
244940 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
244941 if( pPage->buf.n>4 ){
244945 fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
244950 &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
244952 pPage->iPrevPgidx = pPage->buf.n;
244954 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
244960 if( pPage->pgno!=1 ){
244969 ** Usually, the previous term is available in pPage->term. The exception
244975 if( pPage->term.n ){
244976 n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm);
244980 pPage = &pWriter->writer;
244983 nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm);
244984 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
244989 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
244990 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
244993 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
245000 pWriter->aDlidx[0].pgno = pPage->pgno;
245012 Fts5PageWriter *pPage = &pWriter->writer;
245014 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
245022 fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
245028 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
245031 fts5BufferAppendVarint(&p->rc, &pPage->buf,
245047 Fts5PageWriter *pPage = &pWriter->writer;
245053 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
245055 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
245061 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
245067 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);