Remove the psAligned value from the BTree structure - the pageSize is now
always aligned to an 8-byte boundary. Add comments on a confusing bit of code. Ticket #1231. (CVS 2451) FossilOrigin-Name: 535523e1be692adc940d256a7b3d23c62a4cc947
This commit is contained in:
parent
7e62779a58
commit
07d183da90
12
manifest
12
manifest
@ -1,5 +1,5 @@
|
||||
C Prevent\sa\ssegfault\sdescribed\sby\sticket\s#1229.\s(CVS\s2450)
|
||||
D 2005-04-29T02:10:00
|
||||
C Remove\sthe\spsAligned\svalue\sfrom\sthe\sBTree\sstructure\s-\sthe\spageSize\sis\snow\nalways\saligned\sto\san\s8-byte\sboundary.\s\sAdd\scomments\son\sa\sconfusing\sbit\nof\scode.\s\sTicket\s#1231.\s(CVS\s2451)
|
||||
D 2005-05-01T22:52:42
|
||||
F Makefile.in 5c00d0037104de2a50ac7647a5f12769795957a3
|
||||
F Makefile.linux-gcc 06be33b2a9ad4f005a5f42b22c4a19dab3cbb5c7
|
||||
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
|
||||
@ -30,7 +30,7 @@ F sqlite3.pc.in 985b9bf34192a549d7d370e0f0b6b34a4f61369a
|
||||
F src/alter.c 9570af388bc99471ea6e1258817fbf06e3120030
|
||||
F src/attach.c 3615dbe960cbee4aa5ea300b8a213dad36527b0f
|
||||
F src/auth.c 18c5a0befe20f3a58a41e3ddd78f372faeeefe1f
|
||||
F src/btree.c 25770f8cf1fe778757b69cbe0b84c02615c6e1f5
|
||||
F src/btree.c 659cbf8b9ab4fa3d33fa46f9a8d7fa60ae5d802a
|
||||
F src/btree.h 41a71ce027db9ddee72cb43df2316bbe3a1d92af
|
||||
F src/build.c 8afb06c791adcde7787f157bbc55aeef27fb27c1
|
||||
F src/date.c 2134ef4388256e8247405178df8a61bd60dc180a
|
||||
@ -279,7 +279,7 @@ F www/tclsqlite.tcl 425be741b8ae664f55cb1ef2371aab0a75109cf9
|
||||
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
|
||||
F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
|
||||
F www/whentouse.tcl 528299b8316726dbcc5548e9aa0648c8b1bd055b
|
||||
P 90f4cf2ad57309dbd20954fc7fd60859bc44bcf4
|
||||
R 19065ec8289228f6ac9c4c1c0322b483
|
||||
P 0667eae9a97059125a77bd90452d19dc17c30a12
|
||||
R aa09f451ce42a99806e3bada16ee996b
|
||||
U drh
|
||||
Z 34c74af76dd24aa096c57e614d4ab655
|
||||
Z dd1e0985c973b9299d9ae9a3aabf02aa
|
||||
|
@ -1 +1 @@
|
||||
0667eae9a97059125a77bd90452d19dc17c30a12
|
||||
535523e1be692adc940d256a7b3d23c62a4cc947
|
76
src/btree.c
76
src/btree.c
@ -9,7 +9,7 @@
|
||||
** May you share freely, never taking more than you give.
|
||||
**
|
||||
*************************************************************************
|
||||
** $Id: btree.c,v 1.256 2005/03/29 13:17:46 drh Exp $
|
||||
** $Id: btree.c,v 1.257 2005/05/01 22:52:42 drh Exp $
|
||||
**
|
||||
** This file implements a external (disk-based) database using BTrees.
|
||||
** For a detailed discussion of BTrees, refer to
|
||||
@ -211,12 +211,6 @@
|
||||
#include "os.h"
|
||||
#include <assert.h>
|
||||
|
||||
/*
|
||||
** This macro rounds values up so that if the value is an address it
|
||||
** is guaranteed to be an address that is aligned to an 8-byte boundary.
|
||||
*/
|
||||
#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
|
||||
|
||||
/* The following value is the maximum cell size assuming a maximum page
|
||||
** size give above.
|
||||
*/
|
||||
@ -308,7 +302,6 @@ struct Btree {
|
||||
u8 autoVacuum; /* True if database supports auto-vacuum */
|
||||
#endif
|
||||
u16 pageSize; /* Total number of bytes on a page */
|
||||
u16 psAligned; /* pageSize rounded up to a multiple of 8 */
|
||||
u16 usableSize; /* Number of usable bytes on each page */
|
||||
int maxLocal; /* Maximum local payload in non-LEAFDATA tables */
|
||||
int minLocal; /* Minimum local payload in non-LEAFDATA tables */
|
||||
@ -714,7 +707,7 @@ static void _pageIntegrity(MemPage *pPage){
|
||||
used = sqliteMallocRaw( pPage->pBt->pageSize );
|
||||
if( used==0 ) return;
|
||||
usableSize = pPage->pBt->usableSize;
|
||||
assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->psAligned] );
|
||||
assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->pageSize] );
|
||||
hdr = pPage->hdrOffset;
|
||||
assert( hdr==(pPage->pgno==1 ? 100 : 0) );
|
||||
assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) );
|
||||
@ -1017,7 +1010,7 @@ static int initPage(
|
||||
assert( pBt!=0 );
|
||||
assert( pParent==0 || pParent->pBt==pBt );
|
||||
assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) );
|
||||
assert( pPage->aData == &((unsigned char*)pPage)[-pBt->psAligned] );
|
||||
assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] );
|
||||
if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
|
||||
/* The parent page should never change unless the file is corrupt */
|
||||
return SQLITE_CORRUPT; /* bkpt-CORRUPT */
|
||||
@ -1085,7 +1078,7 @@ static void zeroPage(MemPage *pPage, int flags){
|
||||
int first;
|
||||
|
||||
assert( sqlite3pager_pagenumber(data)==pPage->pgno );
|
||||
assert( &data[pBt->psAligned] == (unsigned char*)pPage );
|
||||
assert( &data[pBt->pageSize] == (unsigned char*)pPage );
|
||||
assert( sqlite3pager_iswriteable(data) );
|
||||
memset(&data[hdr], 0, pBt->usableSize - hdr);
|
||||
data[hdr] = flags;
|
||||
@ -1114,7 +1107,7 @@ static int getPage(Btree *pBt, Pgno pgno, MemPage **ppPage){
|
||||
MemPage *pPage;
|
||||
rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData);
|
||||
if( rc ) return rc;
|
||||
pPage = (MemPage*)&aData[pBt->psAligned];
|
||||
pPage = (MemPage*)&aData[pBt->pageSize];
|
||||
pPage->aData = aData;
|
||||
pPage->pBt = pBt;
|
||||
pPage->pgno = pgno;
|
||||
@ -1153,7 +1146,7 @@ static void releasePage(MemPage *pPage){
|
||||
if( pPage ){
|
||||
assert( pPage->aData );
|
||||
assert( pPage->pBt );
|
||||
assert( &pPage->aData[pPage->pBt->psAligned]==(unsigned char*)pPage );
|
||||
assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage );
|
||||
sqlite3pager_unref(pPage->aData);
|
||||
}
|
||||
}
|
||||
@ -1164,7 +1157,9 @@ static void releasePage(MemPage *pPage){
|
||||
** happens.
|
||||
*/
|
||||
static void pageDestructor(void *pData, int pageSize){
|
||||
MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)];
|
||||
MemPage *pPage;
|
||||
assert( (pageSize & 7)==0 );
|
||||
pPage = (MemPage*)&((char*)pData)[pageSize];
|
||||
if( pPage->pParent ){
|
||||
MemPage *pParent = pPage->pParent;
|
||||
pPage->pParent = 0;
|
||||
@ -1182,7 +1177,9 @@ static void pageDestructor(void *pData, int pageSize){
|
||||
** page to agree with the restored data.
|
||||
*/
|
||||
static void pageReinit(void *pData, int pageSize){
|
||||
MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)];
|
||||
MemPage *pPage;
|
||||
assert( (pageSize & 7)==0 );
|
||||
pPage = (MemPage*)&((char*)pData)[pageSize];
|
||||
if( pPage->isInit ){
|
||||
pPage->isInit = 0;
|
||||
initPage(pPage, pPage->pParent);
|
||||
@ -1238,7 +1235,8 @@ int sqlite3BtreeOpen(
|
||||
pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager);
|
||||
sqlite3pager_read_fileheader(pBt->pPager, sizeof(zDbHeader), zDbHeader);
|
||||
pBt->pageSize = get2byte(&zDbHeader[16]);
|
||||
if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE ){
|
||||
if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
|
||||
|| ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
|
||||
pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
|
||||
pBt->maxEmbedFrac = 64; /* 25% */
|
||||
pBt->minEmbedFrac = 32; /* 12.5% */
|
||||
@ -1270,7 +1268,7 @@ int sqlite3BtreeOpen(
|
||||
#endif
|
||||
}
|
||||
pBt->usableSize = pBt->pageSize - nReserve;
|
||||
pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize);
|
||||
assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
|
||||
sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize);
|
||||
*ppBtree = pBt;
|
||||
return SQLITE_OK;
|
||||
@ -1357,8 +1355,8 @@ int sqlite3BtreeSetPageSize(Btree *pBt, int pageSize, int nReserve){
|
||||
}
|
||||
if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
|
||||
((pageSize-1)&pageSize)==0 ){
|
||||
assert( (pageSize & 7)==0 );
|
||||
pBt->pageSize = pageSize;
|
||||
pBt->psAligned = FORCE_ALIGNMENT(pageSize);
|
||||
sqlite3pager_set_pagesize(pBt->pPager, pageSize);
|
||||
}
|
||||
pBt->usableSize = pBt->pageSize - nReserve;
|
||||
@ -1418,7 +1416,7 @@ int sqlite3BtreeGetAutoVacuum(Btree *pBt){
|
||||
** if there is a locking protocol violation.
|
||||
*/
|
||||
static int lockBtree(Btree *pBt){
|
||||
int rc;
|
||||
int rc, pageSize;
|
||||
MemPage *pPage1;
|
||||
if( pBt->pPage1 ) return SQLITE_OK;
|
||||
rc = getPage(pBt, 1, &pPage1);
|
||||
@ -1437,12 +1435,16 @@ static int lockBtree(Btree *pBt){
|
||||
if( page1[18]>1 || page1[19]>1 ){
|
||||
goto page1_init_failed;
|
||||
}
|
||||
pBt->pageSize = get2byte(&page1[16]);
|
||||
pBt->usableSize = pBt->pageSize - page1[20];
|
||||
pageSize = get2byte(&page1[16]);
|
||||
if( ((pageSize-1)&pageSize)!=0 ){
|
||||
goto page1_init_failed;
|
||||
}
|
||||
assert( (pageSize & 7)==0 );
|
||||
pBt->pageSize = pageSize;
|
||||
pBt->usableSize = pageSize - page1[20];
|
||||
if( pBt->usableSize<500 ){
|
||||
goto page1_init_failed;
|
||||
}
|
||||
pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize);
|
||||
pBt->maxEmbedFrac = page1[21];
|
||||
pBt->minEmbedFrac = page1[22];
|
||||
pBt->minLeafFrac = page1[23];
|
||||
@ -1509,7 +1511,7 @@ static void unlockBtreeIfUnused(Btree *pBt){
|
||||
if( pBt->inTrans==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
|
||||
if( pBt->pPage1->aData==0 ){
|
||||
MemPage *pPage = pBt->pPage1;
|
||||
pPage->aData = &((char*)pPage)[-pBt->psAligned];
|
||||
pPage->aData = &((char*)pPage)[-pBt->pageSize];
|
||||
pPage->pBt = pBt;
|
||||
pPage->pgno = 1;
|
||||
}
|
||||
@ -3381,7 +3383,7 @@ static int reparentPage(Btree *pBt, Pgno pgno, MemPage *pNewParent, int idx){
|
||||
assert( pBt->pPager!=0 );
|
||||
aData = sqlite3pager_lookup(pBt->pPager, pgno);
|
||||
if( aData ){
|
||||
pThis = (MemPage*)&aData[pBt->psAligned];
|
||||
pThis = (MemPage*)&aData[pBt->pageSize];
|
||||
assert( pThis->aData==aData );
|
||||
if( pThis->isInit ){
|
||||
if( pThis->pParent!=pNewParent ){
|
||||
@ -3895,7 +3897,7 @@ static int balance_nonroot(MemPage *pPage){
|
||||
nMaxCells*sizeof(u8*) /* apCell */
|
||||
+ nMaxCells*sizeof(int) /* szCell */
|
||||
+ sizeof(MemPage)*NB /* aCopy */
|
||||
+ pBt->psAligned*(5+NB) /* aSpace */
|
||||
+ pBt->pageSize*(5+NB) /* aSpace */
|
||||
+ (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */
|
||||
);
|
||||
if( apCell==0 ){
|
||||
@ -3905,12 +3907,12 @@ static int balance_nonroot(MemPage *pPage){
|
||||
szCell = (int*)&apCell[nMaxCells];
|
||||
aCopy[0] = (u8*)&szCell[nMaxCells];
|
||||
for(i=1; i<NB; i++){
|
||||
aCopy[i] = &aCopy[i-1][pBt->psAligned+sizeof(MemPage)];
|
||||
aCopy[i] = &aCopy[i-1][pBt->pageSize+sizeof(MemPage)];
|
||||
}
|
||||
aSpace = &aCopy[NB-1][pBt->psAligned+sizeof(MemPage)];
|
||||
aSpace = &aCopy[NB-1][pBt->pageSize+sizeof(MemPage)];
|
||||
#ifndef SQLITE_OMIT_AUTOVACUUM
|
||||
if( pBt->autoVacuum ){
|
||||
aFrom = &aSpace[5*pBt->psAligned];
|
||||
aFrom = &aSpace[5*pBt->pageSize];
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -3921,10 +3923,14 @@ static int balance_nonroot(MemPage *pPage){
|
||||
** process of being overwritten.
|
||||
*/
|
||||
for(i=0; i<nOld; i++){
|
||||
MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->psAligned];
|
||||
p->aData = &((u8*)p)[-pBt->psAligned];
|
||||
memcpy(p->aData, apOld[i]->aData, pBt->psAligned + sizeof(MemPage));
|
||||
p->aData = &((u8*)p)[-pBt->psAligned];
|
||||
MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize];
|
||||
assert( (((long long unsigned int)p) & 7)==0 );
|
||||
p->aData = &((u8*)p)[-pBt->pageSize];
|
||||
memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage));
|
||||
/* The memcpy() above changes the value of p->aData so we have to
|
||||
** set it again. */
|
||||
assert( (((long long unsigned int)p) & 7)==0 );
|
||||
p->aData = &((u8*)p)[-pBt->pageSize];
|
||||
}
|
||||
|
||||
/*
|
||||
@ -3982,7 +3988,7 @@ static int balance_nonroot(MemPage *pPage){
|
||||
szCell[nCell] = sz;
|
||||
pTemp = &aSpace[iSpace];
|
||||
iSpace += sz;
|
||||
assert( iSpace<=pBt->psAligned*5 );
|
||||
assert( iSpace<=pBt->pageSize*5 );
|
||||
memcpy(pTemp, apDiv[i], sz);
|
||||
apCell[nCell] = pTemp+leafCorrection;
|
||||
#ifndef SQLITE_OMIT_AUTOVACUUM
|
||||
@ -4207,13 +4213,13 @@ static int balance_nonroot(MemPage *pPage){
|
||||
pCell = &aSpace[iSpace];
|
||||
fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz);
|
||||
iSpace += sz;
|
||||
assert( iSpace<=pBt->psAligned*5 );
|
||||
assert( iSpace<=pBt->pageSize*5 );
|
||||
pTemp = 0;
|
||||
}else{
|
||||
pCell -= 4;
|
||||
pTemp = &aSpace[iSpace];
|
||||
iSpace += sz;
|
||||
assert( iSpace<=pBt->psAligned*5 );
|
||||
assert( iSpace<=pBt->pageSize*5 );
|
||||
}
|
||||
rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
|
||||
if( rc!=SQLITE_OK ) goto balance_cleanup;
|
||||
|
Loading…
x
Reference in New Issue
Block a user