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

Add the sqlite3VdbeMemClearAndResize() interface to be used in place of

sqlite3VdbeMemGrow().

FossilOrigin-Name: 5b9b8987797abf7c68d2c3154f6657be9b8b1c8f
This commit is contained in:
drh
2014-09-19 00:43:39 +00:00
parent 17bcb10299
commit 322f2852f2
8 changed files with 53 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
C Add\sthe\sMem.szMalloc\selement\sto\sthe\sMem\sobject\sand\suse\sit\sto\skeep\strack\sof\nthe\ssize\sof\sthe\sMem.zMalloc\sallocation. C Add\sthe\ssqlite3VdbeMemClearAndResize()\sinterface\sto\sbe\sused\sin\splace\sof\nsqlite3VdbeMemGrow().
D 2014-09-18T21:25:33.845 D 2014-09-19T00:43:39.899
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in cf57f673d77606ab0f2d9627ca52a9ba1464146a F Makefile.in cf57f673d77606ab0f2d9627ca52a9ba1464146a
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -288,14 +288,14 @@ F src/update.c 729f6f18fc27740591d085e1172cebe311144bf0
F src/utf.c fc6b889ba0779b7722634cdeaa25f1930d93820c F src/utf.c fc6b889ba0779b7722634cdeaa25f1930d93820c
F src/util.c 4006c01772bd8d8ac4306d523bbcee41d3e392d8 F src/util.c 4006c01772bd8d8ac4306d523bbcee41d3e392d8
F src/vacuum.c 59f03f92bcff57faa6a8ca256eb29ccddfb0614a F src/vacuum.c 59f03f92bcff57faa6a8ca256eb29ccddfb0614a
F src/vdbe.c fb490f5b1b2ee2f33f60c7dc678c0d0b70f2e0cb F src/vdbe.c 7bc115e540282b1bfff9b43205cbf6528e6398a6
F src/vdbe.h 09f5b4e3719fa454f252322b1cdab5cf1f361327 F src/vdbe.h 09f5b4e3719fa454f252322b1cdab5cf1f361327
F src/vdbeInt.h 1ac536d1fa1260d72f81003ff5b283e8f3c40442 F src/vdbeInt.h f177bed1ec8d4eb5c7089f012aeb95f374745735
F src/vdbeapi.c e088ed70b6cc42ed68985ab064397ebd452286d6 F src/vdbeapi.c e9e33b59834e3edc8790209765e069874c269d9d
F src/vdbeaux.c b3230032238df611aefee5907ea792786362a55d F src/vdbeaux.c ecf0bb835f7c809ba8e22ba7d3a587b7a087ac52
F src/vdbeblob.c 848238dc73e93e48432991bb5651bf87d865eca4 F src/vdbeblob.c 848238dc73e93e48432991bb5651bf87d865eca4
F src/vdbemem.c 3aea3831a981378368ca058cd8fc700b1982772d F src/vdbemem.c 0678ba6214a810539e51c6d8f263634d405b990f
F src/vdbesort.c 09efa5e5098d1a159cd21f588eb118e4fe87cfde F src/vdbesort.c 75c66c2fc02d450b67b4816873fba8088feaf12c
F src/vdbetrace.c 4f29b04edb0cec3d5fcd9b566d9f0e75c8984362 F src/vdbetrace.c 4f29b04edb0cec3d5fcd9b566d9f0e75c8984362
F src/vtab.c 019dbfd0406a7447c990e1f7bd1dfcdb8895697f F src/vtab.c 019dbfd0406a7447c990e1f7bd1dfcdb8895697f
F src/wal.c 10e7de7ce90865a68153f001a61f1d985cd17983 F src/wal.c 10e7de7ce90865a68153f001a61f1d985cd17983
@@ -1198,7 +1198,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 55879932116d373c95a5f32ec44b53a9c3f4db24 P 9c09ac353df6041808cace41880f4729ee73f5e1
R 508057a57cef866ce62608d1eb2d9e8e R eeadad19cadf3315e345f0babe4222b0
U drh U drh
Z 7d8fd1db974daf115bf085d0714b6de9 Z fd83cfbfc71d89fd149fcf4e06924224

View File

@@ -1 +1 @@
9c09ac353df6041808cace41880f4729ee73f5e1 5b9b8987797abf7c68d2c3154f6657be9b8b1c8f

View File

@@ -209,7 +209,7 @@ static VdbeCursor *allocateCursor(
sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
p->apCsr[iCur] = 0; p->apCsr[iCur] = 0;
} }
if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){ if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
memset(pCx, 0, sizeof(VdbeCursor)); memset(pCx, 0, sizeof(VdbeCursor));
pCx->iDb = iDb; pCx->iDb = iDb;
@@ -2633,9 +2633,9 @@ case OP_MakeRecord: {
/* Make sure the output register has a buffer large enough to store /* Make sure the output register has a buffer large enough to store
** the new record. The output register (pOp->p3) is not allowed to ** the new record. The output register (pOp->p3) is not allowed to
** be one of the input registers (because the following call to ** be one of the input registers (because the following call to
** sqlite3VdbeMemGrow() could clobber the value before it is used). ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
*/ */
if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){ if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
goto no_mem; goto no_mem;
} }
zNewRecord = (u8 *)pOut->z; zNewRecord = (u8 *)pOut->z;
@@ -4339,7 +4339,7 @@ case OP_RowData: {
goto too_big; goto too_big;
} }
} }
if( sqlite3VdbeMemGrow(pOut, n, 0) ){ if( sqlite3VdbeMemClearAndResize(pOut, n) ){
goto no_mem; goto no_mem;
} }
pOut->n = n; pOut->n = n;

View File

@@ -437,6 +437,7 @@ void sqlite3VdbeMemRelease(Mem *p);
int sqlite3VdbeMemFinalize(Mem*, FuncDef*); int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
const char *sqlite3OpcodeName(int); const char *sqlite3OpcodeName(int);
int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
int sqlite3VdbeCloseStatement(Vdbe *, int); int sqlite3VdbeCloseStatement(Vdbe *, int);
void sqlite3VdbeFrameDelete(VdbeFrame*); void sqlite3VdbeFrameDelete(VdbeFrame*);
int sqlite3VdbeFrameRestore(VdbeFrame *); int sqlite3VdbeFrameRestore(VdbeFrame *);

View File

@@ -665,7 +665,7 @@ static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
sqlite3VdbeMemSetNull(pMem); sqlite3VdbeMemSetNull(pMem);
pMem->z = 0; pMem->z = 0;
}else{ }else{
sqlite3VdbeMemGrow(pMem, nByte, 0); sqlite3VdbeMemClearAndResize(pMem, nByte);
pMem->flags = MEM_Agg; pMem->flags = MEM_Agg;
pMem->u.pDef = p->pFunc; pMem->u.pDef = p->pFunc;
if( pMem->z ){ if( pMem->z ){

View File

@@ -1426,7 +1426,7 @@ int sqlite3VdbeList(
pMem->u.i = pOp->p3; /* P3 */ pMem->u.i = pOp->p3; /* P3 */
pMem++; pMem++;
if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */
assert( p->db->mallocFailed ); assert( p->db->mallocFailed );
return SQLITE_ERROR; return SQLITE_ERROR;
} }
@@ -1442,7 +1442,7 @@ int sqlite3VdbeList(
pMem++; pMem++;
if( p->explain==1 ){ if( p->explain==1 ){
if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
assert( p->db->mallocFailed ); assert( p->db->mallocFailed );
return SQLITE_ERROR; return SQLITE_ERROR;
} }
@@ -1453,7 +1453,7 @@ int sqlite3VdbeList(
pMem++; pMem++;
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
if( sqlite3VdbeMemGrow(pMem, 500, 0) ){ if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
assert( p->db->mallocFailed ); assert( p->db->mallocFailed );
return SQLITE_ERROR; return SQLITE_ERROR;
} }

View File

@@ -107,7 +107,7 @@ int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
** blob if bPreserve is true. If bPreserve is false, any prior content ** blob if bPreserve is true. If bPreserve is false, any prior content
** in pMem->z is discarded. ** in pMem->z is discarded.
*/ */
int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){ SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
assert( sqlite3VdbeCheckMemInvariants(pMem) ); assert( sqlite3VdbeCheckMemInvariants(pMem) );
assert( (pMem->flags&MEM_RowSet)==0 ); assert( (pMem->flags&MEM_RowSet)==0 );
@@ -150,6 +150,33 @@ int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
return SQLITE_OK; return SQLITE_OK;
} }
/*
** Change the pMem->zMalloc allocation to be at least szNew bytes.
** If pMem->zMalloc already meets or exceeds the requested size, this
** routine is a no-op.
**
** Any prior string or blob content in the pMem object may be discarded.
** The pMem->xDel destructor is called, if it exists.
**
** The value of the pMem after this routine returns might be NULL or
** it might retain its prior value, depending on circumstances. The
** caller should make no assumptions about the state of pMem after this
** routine returns, except that pMem->zMalloc is at least the requested
** size and pMem->z==pMem->zMalloc.
**
** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
** if unable to complete the resizing.
*/
int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
if( pMem->szMalloc<szNew || (pMem->flags & MEM_Dyn)!=0 ){
return sqlite3VdbeMemGrow(pMem, szNew, 0);
}
pMem->z = pMem->zMalloc;
pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
// pMem->flags = MEM_Null;
return SQLITE_OK;
}
/* /*
** Make the given Mem object MEM_Dyn. In other words, make it so ** Make the given Mem object MEM_Dyn. In other words, make it so
** that any TEXT or BLOB content is stored in memory obtained from ** that any TEXT or BLOB content is stored in memory obtained from
@@ -262,7 +289,7 @@ int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
assert( EIGHT_BYTE_ALIGNMENT(pMem) ); assert( EIGHT_BYTE_ALIGNMENT(pMem) );
if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){ if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
return SQLITE_NOMEM; return SQLITE_NOMEM;
} }
@@ -866,7 +893,7 @@ int sqlite3VdbeMemSetStr(
if( nByte>iLimit ){ if( nByte>iLimit ){
return SQLITE_TOOBIG; return SQLITE_TOOBIG;
} }
if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){ if( sqlite3VdbeMemClearAndResize(pMem, nAlloc) ){
return SQLITE_NOMEM; return SQLITE_NOMEM;
} }
memcpy(pMem->z, z, nAlloc); memcpy(pMem->z, z, nAlloc);
@@ -944,7 +971,7 @@ int sqlite3VdbeMemFromBtree(
pMem->n = (int)amt; pMem->n = (int)amt;
}else{ }else{
pMem->flags = MEM_Null; pMem->flags = MEM_Null;
if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){ if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
if( key ){ if( key ){
rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
}else{ }else{

View File

@@ -2461,7 +2461,7 @@ int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
void *pKey; int nKey; /* Sorter key to copy into pOut */ void *pKey; int nKey; /* Sorter key to copy into pOut */
pKey = vdbeSorterRowkey(pSorter, &nKey); pKey = vdbeSorterRowkey(pSorter, &nKey);
if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){ if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
return SQLITE_NOMEM; return SQLITE_NOMEM;
} }
pOut->n = nKey; pOut->n = nKey;