From 6011a75c0c9265401e1d562ee93edc52703dbc93 Mon Sep 17 00:00:00 2001 From: danielk1977 Date: Wed, 1 Apr 2009 16:25:32 +0000 Subject: [PATCH] Remove an unreachable branch from allocateSpace() in btree.c. Add comments and asserts to the same function. (CVS 6422) FossilOrigin-Name: f8e15a542df67fd9dc1c91c7d9e1c4df59acb82b --- manifest | 12 ++++---- manifest.uuid | 2 +- src/btree.c | 78 ++++++++++++++++++++++++++------------------------- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/manifest b/manifest index ef1d96bd61..7b7e75f0d0 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\ssome\sassert()\sstatements\sto\squerySharedCacheTableLock().\s(CVS\s6421) -D 2009-04-01T09:41:54 +C Remove\san\sunreachable\sbranch\sfrom\sallocateSpace()\sin\sbtree.c.\sAdd\scomments\sand\sasserts\sto\sthe\ssame\sfunction.\s(CVS\s6422) +D 2009-04-01T16:25:33 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 583e87706abc3026960ed759aff6371faf84c211 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -103,7 +103,7 @@ F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627 F src/backup.c 0082d0e5a63f04e88faee0dff0a7d63d3e92a78d F src/bitvec.c 44f7059ac1f874d364b34af31b9617e52223ba75 F src/btmutex.c 341502bc496dc0840dcb00cde65680fb0e85c3ab -F src/btree.c afb15be98c01b07528072dd6e18b7f644859ada0 +F src/btree.c cfa70f8e31c01e2278478740896620bf32ffd837 F src/btree.h e302c5747494067cd4f5763000fbe7bca767d816 F src/btreeInt.h df64030d632f8c8ac217ed52e8b6b3eacacb33a5 F src/build.c 72357fd75ef036d0afbf1756edab6d62c56fcf4b @@ -714,7 +714,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P 1b9da6d73f603e66d078463c3571dda86ceedfb3 -R 14b12ad716bad78f9281344d4310ecc6 +P 3e3b5e861aeff2e4ef568c422236fdf7fa22bed3 +R 3682da7f79f43466521202126b407df7 U danielk1977 -Z 50c3252197be65f8aa74e6bb679653bc +Z 15174c0778c01e00d98a68566fd435b4 diff --git a/manifest.uuid b/manifest.uuid index 9b8025b568..fedb2812e3 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -3e3b5e861aeff2e4ef568c422236fdf7fa22bed3 \ No newline at end of file +f8e15a542df67fd9dc1c91c7d9e1c4df59acb82b \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 0d3106a264..eaa04a83fd 100644 --- a/src/btree.c +++ b/src/btree.c @@ -9,7 +9,7 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.583 2009/04/01 09:41:54 danielk1977 Exp $ +** $Id: btree.c,v 1.584 2009/04/01 16:25:33 danielk1977 Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** See the header comment on "btreeInt.h" for additional information. @@ -865,72 +865,74 @@ static int defragmentPage(MemPage *pPage){ } /* -** Allocate nByte bytes of space on a page. +** Allocate nByte bytes of space from within the B-Tree page passed +** as the first argument. Return the index into pPage->aData[] of the +** first byte of allocated space. ** -** Return the index into pPage->aData[] of the first byte of -** the new allocation. The caller guarantees that there is enough -** space. This routine will never fail. +** The caller guarantees that the space between the end of the cell-offset +** array and the start of the cell-content area is at least nByte bytes +** in size. So this routine can never fail. ** -** If the page contains nBytes of free space but does not contain -** nBytes of contiguous free space, then this routine automatically -** calls defragmentPage() to consolidate all free space before -** allocating the new chunk. +** If there are already 60 or more bytes of fragments within the page, +** the page is defragmented before returning. If this were not done there +** is a chance that the number of fragmented bytes could eventually +** overflow the single-byte field of the page-header in which this value +** is stored. */ static int allocateSpace(MemPage *pPage, int nByte){ - int addr, pc, hdr; - int size; - int nFrag; + const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ + u8 * const data = pPage->aData; /* Local cache of pPage->aData */ + int nFrag; /* Number of fragmented bytes on pPage */ int top; - int nCell; - int cellOffset; - unsigned char *data; - data = pPage->aData; assert( sqlite3PagerIswriteable(pPage->pDbPage) ); assert( pPage->pBt ); assert( sqlite3_mutex_held(pPage->pBt->mutex) ); assert( nByte>=0 ); /* Minimum cell size is 4 */ assert( pPage->nFree>=nByte ); assert( pPage->nOverflow==0 ); - pPage->nFree -= (u16)nByte; - hdr = pPage->hdrOffset; + /* Assert that the space between the cell-offset array and the + ** cell-content area is greater than nByte bytes. + */ + assert( nByte <= ( + get2byte(&data[hdr+5])-(hdr+8+(pPage->leaf?0:4)+2*get2byte(&data[hdr+3])) + )); + + pPage->nFree -= (u16)nByte; nFrag = data[hdr+7]; - if( nFrag<60 ){ - /* Search the freelist looking for a slot big enough to satisfy the - ** space request. */ - addr = hdr+1; - while( (pc = get2byte(&data[addr]))>0 ){ - size = get2byte(&data[pc+2]); + if( nFrag>=60 ){ + defragmentPage(pPage); + }else{ + /* Search the freelist looking for a free slot big enough to satisfy + ** the request. The allocation is made from the first free slot in + ** the list that is large enough to accomadate it. + */ + int pc, addr; + for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ + int size = get2byte(&data[pc+2]); /* Size of free slot */ if( size>=nByte ){ int x = size - nByte; - if( sizecellOffset; - if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ - defragmentPage(pPage); - top = get2byte(&data[hdr+5]); - } - top -= nByte; - assert( cellOffset + 2*nCell <= top ); + top = get2byte(&data[hdr+5]) - nByte; put2byte(&data[hdr+5], top); - assert( sqlite3PagerIswriteable(pPage->pDbPage) ); return top; }