1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

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:
drh
2005-05-01 22:52:42 +00:00
parent 7e62779a58
commit 07d183da90
3 changed files with 48 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
C Prevent\sa\ssegfault\sdescribed\sby\sticket\s#1229.\s(CVS\s2450) 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-04-29T02:10:00 D 2005-05-01T22:52:42
F Makefile.in 5c00d0037104de2a50ac7647a5f12769795957a3 F Makefile.in 5c00d0037104de2a50ac7647a5f12769795957a3
F Makefile.linux-gcc 06be33b2a9ad4f005a5f42b22c4a19dab3cbb5c7 F Makefile.linux-gcc 06be33b2a9ad4f005a5f42b22c4a19dab3cbb5c7
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -30,7 +30,7 @@ F sqlite3.pc.in 985b9bf34192a549d7d370e0f0b6b34a4f61369a
F src/alter.c 9570af388bc99471ea6e1258817fbf06e3120030 F src/alter.c 9570af388bc99471ea6e1258817fbf06e3120030
F src/attach.c 3615dbe960cbee4aa5ea300b8a213dad36527b0f F src/attach.c 3615dbe960cbee4aa5ea300b8a213dad36527b0f
F src/auth.c 18c5a0befe20f3a58a41e3ddd78f372faeeefe1f F src/auth.c 18c5a0befe20f3a58a41e3ddd78f372faeeefe1f
F src/btree.c 25770f8cf1fe778757b69cbe0b84c02615c6e1f5 F src/btree.c 659cbf8b9ab4fa3d33fa46f9a8d7fa60ae5d802a
F src/btree.h 41a71ce027db9ddee72cb43df2316bbe3a1d92af F src/btree.h 41a71ce027db9ddee72cb43df2316bbe3a1d92af
F src/build.c 8afb06c791adcde7787f157bbc55aeef27fb27c1 F src/build.c 8afb06c791adcde7787f157bbc55aeef27fb27c1
F src/date.c 2134ef4388256e8247405178df8a61bd60dc180a F src/date.c 2134ef4388256e8247405178df8a61bd60dc180a
@@ -279,7 +279,7 @@ F www/tclsqlite.tcl 425be741b8ae664f55cb1ef2371aab0a75109cf9
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
F www/whentouse.tcl 528299b8316726dbcc5548e9aa0648c8b1bd055b F www/whentouse.tcl 528299b8316726dbcc5548e9aa0648c8b1bd055b
P 90f4cf2ad57309dbd20954fc7fd60859bc44bcf4 P 0667eae9a97059125a77bd90452d19dc17c30a12
R 19065ec8289228f6ac9c4c1c0322b483 R aa09f451ce42a99806e3bada16ee996b
U drh U drh
Z 34c74af76dd24aa096c57e614d4ab655 Z dd1e0985c973b9299d9ae9a3aabf02aa

View File

@@ -1 +1 @@
0667eae9a97059125a77bd90452d19dc17c30a12 535523e1be692adc940d256a7b3d23c62a4cc947

View File

@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give. ** 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. ** This file implements a external (disk-based) database using BTrees.
** For a detailed discussion of BTrees, refer to ** For a detailed discussion of BTrees, refer to
@@ -211,12 +211,6 @@
#include "os.h" #include "os.h"
#include <assert.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 /* The following value is the maximum cell size assuming a maximum page
** size give above. ** size give above.
*/ */
@@ -308,7 +302,6 @@ struct Btree {
u8 autoVacuum; /* True if database supports auto-vacuum */ u8 autoVacuum; /* True if database supports auto-vacuum */
#endif #endif
u16 pageSize; /* Total number of bytes on a page */ 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 */ u16 usableSize; /* Number of usable bytes on each page */
int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ int maxLocal; /* Maximum local payload in non-LEAFDATA tables */
int minLocal; /* Minimum 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 ); used = sqliteMallocRaw( pPage->pBt->pageSize );
if( used==0 ) return; if( used==0 ) return;
usableSize = pPage->pBt->usableSize; 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; hdr = pPage->hdrOffset;
assert( hdr==(pPage->pgno==1 ? 100 : 0) ); assert( hdr==(pPage->pgno==1 ? 100 : 0) );
assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) );
@@ -1017,7 +1010,7 @@ static int initPage(
assert( pBt!=0 ); assert( pBt!=0 );
assert( pParent==0 || pParent->pBt==pBt ); assert( pParent==0 || pParent->pBt==pBt );
assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); 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) ){ if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
/* The parent page should never change unless the file is corrupt */ /* The parent page should never change unless the file is corrupt */
return SQLITE_CORRUPT; /* bkpt-CORRUPT */ return SQLITE_CORRUPT; /* bkpt-CORRUPT */
@@ -1085,7 +1078,7 @@ static void zeroPage(MemPage *pPage, int flags){
int first; int first;
assert( sqlite3pager_pagenumber(data)==pPage->pgno ); assert( sqlite3pager_pagenumber(data)==pPage->pgno );
assert( &data[pBt->psAligned] == (unsigned char*)pPage ); assert( &data[pBt->pageSize] == (unsigned char*)pPage );
assert( sqlite3pager_iswriteable(data) ); assert( sqlite3pager_iswriteable(data) );
memset(&data[hdr], 0, pBt->usableSize - hdr); memset(&data[hdr], 0, pBt->usableSize - hdr);
data[hdr] = flags; data[hdr] = flags;
@@ -1114,7 +1107,7 @@ static int getPage(Btree *pBt, Pgno pgno, MemPage **ppPage){
MemPage *pPage; MemPage *pPage;
rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData); rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData);
if( rc ) return rc; if( rc ) return rc;
pPage = (MemPage*)&aData[pBt->psAligned]; pPage = (MemPage*)&aData[pBt->pageSize];
pPage->aData = aData; pPage->aData = aData;
pPage->pBt = pBt; pPage->pBt = pBt;
pPage->pgno = pgno; pPage->pgno = pgno;
@@ -1153,7 +1146,7 @@ static void releasePage(MemPage *pPage){
if( pPage ){ if( pPage ){
assert( pPage->aData ); assert( pPage->aData );
assert( pPage->pBt ); 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); sqlite3pager_unref(pPage->aData);
} }
} }
@@ -1164,7 +1157,9 @@ static void releasePage(MemPage *pPage){
** happens. ** happens.
*/ */
static void pageDestructor(void *pData, int pageSize){ 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 ){ if( pPage->pParent ){
MemPage *pParent = pPage->pParent; MemPage *pParent = pPage->pParent;
pPage->pParent = 0; pPage->pParent = 0;
@@ -1182,7 +1177,9 @@ static void pageDestructor(void *pData, int pageSize){
** page to agree with the restored data. ** page to agree with the restored data.
*/ */
static void pageReinit(void *pData, int pageSize){ 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 ){ if( pPage->isInit ){
pPage->isInit = 0; pPage->isInit = 0;
initPage(pPage, pPage->pParent); initPage(pPage, pPage->pParent);
@@ -1238,7 +1235,8 @@ int sqlite3BtreeOpen(
pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager); pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager);
sqlite3pager_read_fileheader(pBt->pPager, sizeof(zDbHeader), zDbHeader); sqlite3pager_read_fileheader(pBt->pPager, sizeof(zDbHeader), zDbHeader);
pBt->pageSize = get2byte(&zDbHeader[16]); 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->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
pBt->maxEmbedFrac = 64; /* 25% */ pBt->maxEmbedFrac = 64; /* 25% */
pBt->minEmbedFrac = 32; /* 12.5% */ pBt->minEmbedFrac = 32; /* 12.5% */
@@ -1270,7 +1268,7 @@ int sqlite3BtreeOpen(
#endif #endif
} }
pBt->usableSize = pBt->pageSize - nReserve; 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); sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize);
*ppBtree = pBt; *ppBtree = pBt;
return SQLITE_OK; return SQLITE_OK;
@@ -1357,8 +1355,8 @@ int sqlite3BtreeSetPageSize(Btree *pBt, int pageSize, int nReserve){
} }
if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
((pageSize-1)&pageSize)==0 ){ ((pageSize-1)&pageSize)==0 ){
assert( (pageSize & 7)==0 );
pBt->pageSize = pageSize; pBt->pageSize = pageSize;
pBt->psAligned = FORCE_ALIGNMENT(pageSize);
sqlite3pager_set_pagesize(pBt->pPager, pageSize); sqlite3pager_set_pagesize(pBt->pPager, pageSize);
} }
pBt->usableSize = pBt->pageSize - nReserve; pBt->usableSize = pBt->pageSize - nReserve;
@@ -1418,7 +1416,7 @@ int sqlite3BtreeGetAutoVacuum(Btree *pBt){
** if there is a locking protocol violation. ** if there is a locking protocol violation.
*/ */
static int lockBtree(Btree *pBt){ static int lockBtree(Btree *pBt){
int rc; int rc, pageSize;
MemPage *pPage1; MemPage *pPage1;
if( pBt->pPage1 ) return SQLITE_OK; if( pBt->pPage1 ) return SQLITE_OK;
rc = getPage(pBt, 1, &pPage1); rc = getPage(pBt, 1, &pPage1);
@@ -1437,12 +1435,16 @@ static int lockBtree(Btree *pBt){
if( page1[18]>1 || page1[19]>1 ){ if( page1[18]>1 || page1[19]>1 ){
goto page1_init_failed; goto page1_init_failed;
} }
pBt->pageSize = get2byte(&page1[16]); pageSize = get2byte(&page1[16]);
pBt->usableSize = pBt->pageSize - page1[20]; 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 ){ if( pBt->usableSize<500 ){
goto page1_init_failed; goto page1_init_failed;
} }
pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize);
pBt->maxEmbedFrac = page1[21]; pBt->maxEmbedFrac = page1[21];
pBt->minEmbedFrac = page1[22]; pBt->minEmbedFrac = page1[22];
pBt->minLeafFrac = page1[23]; 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->inTrans==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
if( pBt->pPage1->aData==0 ){ if( pBt->pPage1->aData==0 ){
MemPage *pPage = pBt->pPage1; MemPage *pPage = pBt->pPage1;
pPage->aData = &((char*)pPage)[-pBt->psAligned]; pPage->aData = &((char*)pPage)[-pBt->pageSize];
pPage->pBt = pBt; pPage->pBt = pBt;
pPage->pgno = 1; pPage->pgno = 1;
} }
@@ -3381,7 +3383,7 @@ static int reparentPage(Btree *pBt, Pgno pgno, MemPage *pNewParent, int idx){
assert( pBt->pPager!=0 ); assert( pBt->pPager!=0 );
aData = sqlite3pager_lookup(pBt->pPager, pgno); aData = sqlite3pager_lookup(pBt->pPager, pgno);
if( aData ){ if( aData ){
pThis = (MemPage*)&aData[pBt->psAligned]; pThis = (MemPage*)&aData[pBt->pageSize];
assert( pThis->aData==aData ); assert( pThis->aData==aData );
if( pThis->isInit ){ if( pThis->isInit ){
if( pThis->pParent!=pNewParent ){ if( pThis->pParent!=pNewParent ){
@@ -3895,7 +3897,7 @@ static int balance_nonroot(MemPage *pPage){
nMaxCells*sizeof(u8*) /* apCell */ nMaxCells*sizeof(u8*) /* apCell */
+ nMaxCells*sizeof(int) /* szCell */ + nMaxCells*sizeof(int) /* szCell */
+ sizeof(MemPage)*NB /* aCopy */ + sizeof(MemPage)*NB /* aCopy */
+ pBt->psAligned*(5+NB) /* aSpace */ + pBt->pageSize*(5+NB) /* aSpace */
+ (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */
); );
if( apCell==0 ){ if( apCell==0 ){
@@ -3905,12 +3907,12 @@ static int balance_nonroot(MemPage *pPage){
szCell = (int*)&apCell[nMaxCells]; szCell = (int*)&apCell[nMaxCells];
aCopy[0] = (u8*)&szCell[nMaxCells]; aCopy[0] = (u8*)&szCell[nMaxCells];
for(i=1; i<NB; i++){ 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 #ifndef SQLITE_OMIT_AUTOVACUUM
if( pBt->autoVacuum ){ if( pBt->autoVacuum ){
aFrom = &aSpace[5*pBt->psAligned]; aFrom = &aSpace[5*pBt->pageSize];
} }
#endif #endif
@@ -3921,10 +3923,14 @@ static int balance_nonroot(MemPage *pPage){
** process of being overwritten. ** process of being overwritten.
*/ */
for(i=0; i<nOld; i++){ for(i=0; i<nOld; i++){
MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->psAligned]; MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize];
p->aData = &((u8*)p)[-pBt->psAligned]; assert( (((long long unsigned int)p) & 7)==0 );
memcpy(p->aData, apOld[i]->aData, pBt->psAligned + sizeof(MemPage)); p->aData = &((u8*)p)[-pBt->pageSize];
p->aData = &((u8*)p)[-pBt->psAligned]; 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; szCell[nCell] = sz;
pTemp = &aSpace[iSpace]; pTemp = &aSpace[iSpace];
iSpace += sz; iSpace += sz;
assert( iSpace<=pBt->psAligned*5 ); assert( iSpace<=pBt->pageSize*5 );
memcpy(pTemp, apDiv[i], sz); memcpy(pTemp, apDiv[i], sz);
apCell[nCell] = pTemp+leafCorrection; apCell[nCell] = pTemp+leafCorrection;
#ifndef SQLITE_OMIT_AUTOVACUUM #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -4207,13 +4213,13 @@ static int balance_nonroot(MemPage *pPage){
pCell = &aSpace[iSpace]; pCell = &aSpace[iSpace];
fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz); fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz);
iSpace += sz; iSpace += sz;
assert( iSpace<=pBt->psAligned*5 ); assert( iSpace<=pBt->pageSize*5 );
pTemp = 0; pTemp = 0;
}else{ }else{
pCell -= 4; pCell -= 4;
pTemp = &aSpace[iSpace]; pTemp = &aSpace[iSpace];
iSpace += sz; iSpace += sz;
assert( iSpace<=pBt->psAligned*5 ); assert( iSpace<=pBt->pageSize*5 );
} }
rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
if( rc!=SQLITE_OK ) goto balance_cleanup; if( rc!=SQLITE_OK ) goto balance_cleanup;