Index: src/build.c ================================================================== --- src/build.c +++ src/build.c @@ -20,11 +20,11 @@ ** creating ID lists ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** -** $Id: build.c,v 1.416 2007/03/26 22:05:01 drh Exp $ +** $Id: build.c,v 1.417 2007/03/27 13:36:37 drh Exp $ */ #include "sqliteInt.h" #include /* @@ -80,11 +80,11 @@ return; } } nBytes = sizeof(TableLock) * (pParse->nTableLock+1); - sqliteReallocOrFree(&pParse->aTableLock, nBytes); + pParse->aTableLock = sqliteReallocOrFree(pParse->aTableLock, nBytes); if( pParse->aTableLock ){ p = &pParse->aTableLock[pParse->nTableLock++]; p->iDb = iDb; p->iTab = iTab; p->isWriteLock = isWriteLock; @@ -2781,49 +2781,50 @@ exit_drop_index: sqlite3SrcListDelete(pName); } /* -** ppArray points into a structure where there is an array pointer -** followed by two integers. The first integer is the -** number of elements in the structure array. The second integer -** is the number of allocated slots in the array. -** -** In other words, the structure looks something like this: -** -** struct Example1 { -** struct subElem *aEntry; -** int nEntry; -** int nAlloc; -** } -** -** The pnEntry parameter points to the equivalent of Example1.nEntry. -** -** This routine allocates a new slot in the array, zeros it out, -** and returns its index. If malloc fails a negative number is returned. -** -** szEntry is the sizeof of a single array entry. initSize is the -** number of array entries allocated on the initial allocation. -*/ -int sqlite3ArrayAllocate(void *ppArray, int szEntry, int initSize){ - char *p; - void **pp = (void**)ppArray; - int *an = (int*)&pp[1]; - if( an[0]>=an[1] ){ +** pArray is a pointer to an array of objects. Each object in the +** array is szEntry bytes in size. This routine allocates a new +** object on the end of the array. +** +** *pnEntry is the number of entries already in use. *pnAlloc is +** the previously allocated size of the array. initSize is the +** suggested initial array size allocation. +** +** The index of the new entry is returned in *pIdx. +** +** This routine returns a pointer to the array of objects. This +** might be the same as the pArray parameter or it might be a different +** pointer if the array was resized. +*/ +void *sqlite3ArrayAllocate( + void *pArray, /* Array of objects. Might be reallocated */ + int szEntry, /* Size of each object in the array */ + int initSize, /* Suggested initial allocation, in elements */ + int *pnEntry, /* Number of objects currently in use */ + int *pnAlloc, /* Current size of the allocation, in elements */ + int *pIdx /* Write the index of a new slot here */ +){ + char *z; + if( *pnEntry >= *pnAlloc ){ void *pNew; int newSize; - newSize = an[1]*2 + initSize; - pNew = sqliteRealloc(*pp, newSize*szEntry); + newSize = (*pnAlloc)*2 + initSize; + pNew = sqliteRealloc(pArray, newSize*szEntry); if( pNew==0 ){ - return -1; - } - an[1] = newSize; - *pp = pNew; - } - p = *pp; - memset(&p[an[0]*szEntry], 0, szEntry); - return an[0]++; + *pIdx = -1; + return pArray; + } + *pnAlloc = newSize; + pArray = pNew; + } + z = (char*)pArray; + memset(&z[*pnEntry * szEntry], 0, szEntry); + *pIdx = *pnEntry; + ++*pnEntry; + return pArray; } /* ** Append a new element to the given IdList. Create a new IdList if ** need be. @@ -2835,11 +2836,18 @@ if( pList==0 ){ pList = sqliteMalloc( sizeof(IdList) ); if( pList==0 ) return 0; pList->nAlloc = 0; } - i = sqlite3ArrayAllocate(&pList->a, sizeof(pList->a[0]), 5); + pList->a = sqlite3ArrayAllocate( + pList->a, + sizeof(pList->a[0]), + 5, + &pList->nId, + &pList->nAlloc, + &i + ); if( i<0 ){ sqlite3IdListDelete(pList); return 0; } pList->a[i].zName = sqlite3NameFromToken(pToken); Index: src/expr.c ================================================================== --- src/expr.c +++ src/expr.c @@ -10,11 +10,11 @@ ** ************************************************************************* ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** -** $Id: expr.c,v 1.282 2007/03/26 22:05:01 drh Exp $ +** $Id: expr.c,v 1.283 2007/03/27 13:36:37 drh Exp $ */ #include "sqliteInt.h" #include /* @@ -402,11 +402,11 @@ } if( i>=pParse->nVarExpr ){ pExpr->iTable = ++pParse->nVar; if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; - sqliteReallocOrFree(&pParse->apVarExpr, + pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr, pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ); } if( !sqlite3MallocFailed() ){ assert( pParse->apVarExpr!=0 ); pParse->apVarExpr[pParse->nVarExpr++] = pExpr; @@ -2235,27 +2235,35 @@ ** Add a new element to the pAggInfo->aCol[] array. Return the index of ** the new element. Return a negative number if malloc fails. */ static int addAggInfoColumn(AggInfo *pInfo){ int i; - i = sqlite3ArrayAllocate(&pInfo->aCol, sizeof(pInfo->aCol[0]), 3); - if( i<0 ){ - return -1; - } + pInfo->aCol = sqlite3ArrayAllocate( + pInfo->aCol, + sizeof(pInfo->aCol[0]), + 3, + &pInfo->nColumn, + &pInfo->nColumnAlloc, + &i + ); return i; } /* ** Add a new element to the pAggInfo->aFunc[] array. Return the index of ** the new element. Return a negative number if malloc fails. */ static int addAggInfoFunc(AggInfo *pInfo){ int i; - i = sqlite3ArrayAllocate(&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2); - if( i<0 ){ - return -1; - } + pInfo->aFunc = sqlite3ArrayAllocate( + pInfo->aFunc, + sizeof(pInfo->aFunc[0]), + 3, + &pInfo->nFunc, + &pInfo->nFuncAlloc, + &i + ); return i; } /* ** This is an xFunc for walkExprTree() used to implement Index: src/pager.c ================================================================== --- src/pager.c +++ src/pager.c @@ -16,11 +16,11 @@ ** is separate from the database file. The pager also implements file ** locking to prevent two processes from writing the same database ** file simultaneously, or one process from reading the database while ** another is writing. ** -** @(#) $Id: pager.c,v 1.300 2007/03/26 22:05:02 drh Exp $ +** @(#) $Id: pager.c,v 1.301 2007/03/27 13:36:37 drh Exp $ */ #ifndef SQLITE_OMIT_DISKIO #include "sqliteInt.h" #include "os.h" #include "pager.h" @@ -1826,11 +1826,11 @@ int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){ assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE ); if( !pPager->memDb && pPager->nRef==0 ){ pager_reset(pPager); pPager->pageSize = pageSize; - sqlite3ReallocOrFree(&pPager->pTmpSpace, pageSize); + pPager->pTmpSpace = sqlite3ReallocOrFree(pPager->pTmpSpace, pageSize); } return pPager->pageSize; } /* Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -9,11 +9,11 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.544 2007/03/26 22:05:02 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.545 2007/03/27 13:36:37 drh Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ #if defined(SQLITE_TCL) || defined(TCLSH) @@ -1565,11 +1565,11 @@ void sqlite3Free(void*); void *sqlite3Realloc(void*,int); char *sqlite3StrDup(const char*); char *sqlite3StrNDup(const char*, int); # define sqlite3CheckMemory(a,b) -void sqlite3ReallocOrFree(void*,int); +void *sqlite3ReallocOrFree(void*,int); void sqlite3FreeX(void*); void *sqlite3MallocX(int); int sqlite3AllocSize(void *); char *sqlite3MPrintf(const char*, ...); @@ -1622,11 +1622,11 @@ #endif void sqlite3DropTable(Parse*, SrcList*, int, int); void sqlite3DeleteTable(sqlite3*, Table*); void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int); -int sqlite3ArrayAllocate(void*,int,int); +void *sqlite3ArrayAllocate(void*,int,int,int*,int*,int*); IdList *sqlite3IdListAppend(IdList*, Token*); int sqlite3IdListIndex(IdList*,const char*); SrcList *sqlite3SrcListAppend(SrcList*, Token*, Token*); SrcList *sqlite3SrcListAppendFromTerm(SrcList*, Token*, Token*, Token*, Select*, Expr*, IdList*); Index: src/util.c ================================================================== --- src/util.c +++ src/util.c @@ -12,11 +12,11 @@ ** Utility functions used throughout sqlite. ** ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** -** $Id: util.c,v 1.196 2007/03/26 22:05:02 drh Exp $ +** $Id: util.c,v 1.197 2007/03/27 13:36:37 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include #include @@ -659,17 +659,17 @@ if( p ){ memset(p, 0, n); } return p; } -void sqlite3ReallocOrFree(void *pp, int n){ - char **x = (char**)pp; - void *p = sqlite3Realloc(*x, n); - if( !p ){ - sqlite3FreeX(*x); +void *sqlite3ReallocOrFree(void *p, int n){ + void *pNew; + pNew = sqlite3Realloc(p, n); + if( !pNew ){ + sqlite3FreeX(p); } - *x = p; + return pNew; } /* ** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those ** rare scenarios where sqlite may allocate memory in one thread and free Index: src/vdbe.c ================================================================== --- src/vdbe.c +++ src/vdbe.c @@ -41,11 +41,11 @@ ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.592 2007/03/26 22:05:02 drh Exp $ +** $Id: vdbe.c,v 1.593 2007/03/27 13:36:37 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include #include "vdbeInt.h" @@ -4234,11 +4234,12 @@ assert( i>=0 ); /* FIX ME: This should be allocated as part of the vdbe at compile-time */ if( i>=p->contextStackDepth ){ p->contextStackDepth = i+1; - sqliteReallocOrFree(&p->contextStack, sizeof(Context)*(i+1)); + p->contextStack = sqliteReallocOrFree(p->contextStack, + sizeof(Context)*(i+1)); if( p->contextStack==0 ) goto no_mem; } pContext = &p->contextStack[i]; pContext->lastRowid = db->lastRowid; pContext->nChange = p->nChange; Index: src/vdbeaux.c ================================================================== --- src/vdbeaux.c +++ src/vdbeaux.c @@ -193,11 +193,12 @@ int i; i = p->nLabel++; assert( p->magic==VDBE_MAGIC_INIT ); if( i>=p->nLabelAlloc ){ p->nLabelAlloc = p->nLabelAlloc*2 + 10; - sqliteReallocOrFree(&p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0])); + p->aLabel = sqliteReallocOrFree(p->aLabel, + p->nLabelAlloc*sizeof(p->aLabel[0])); } if( p->aLabel ){ p->aLabel[i] = -1; } return -1-i; Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -14,11 +14,11 @@ ** generating the code that loops through a table looking for applicable ** rows. Indices are selected and used to speed the search when doing ** so is applicable. Because this module is responsible for selecting ** indices, you might also think of this module as the "query optimizer". ** -** $Id: where.c,v 1.240 2007/03/26 22:05:02 drh Exp $ +** $Id: where.c,v 1.241 2007/03/27 13:36:37 drh Exp $ */ #include "sqliteInt.h" /* ** The number of bits in a Bitmask. "BMS" means "BitMask Size". @@ -1705,12 +1705,12 @@ sqlite3CodeSubselect(pParse, pX); iTab = pX->iTable; sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0); VdbeComment((v, "# %.*s", pX->span.n, pX->span.z)); pLevel->nIn++; - sqliteReallocOrFree(&pLevel->aInLoop, - sizeof(pLevel->aInLoop[0])*2*pLevel->nIn); + pLevel->aInLoop = sqliteReallocOrFree(pLevel->aInLoop, + sizeof(pLevel->aInLoop[0])*2*pLevel->nIn); aIn = pLevel->aInLoop; if( aIn ){ aIn += pLevel->nIn*2 - 2; aIn[0] = iTab; aIn[1] = sqlite3VdbeAddOp(v, OP_Column, iTab, 0);