From 4efc4754f349772f883f2aef49fa272eb01cde84 Mon Sep 17 00:00:00 2001 From: drh Date: Fri, 16 Jan 2004 15:55:37 +0000 Subject: [PATCH] Incremental code compaction in expr.c. Now about 4% smaller. Lots more work to do. (CVS 1183) FossilOrigin-Name: d292ba5148059dce3893b80eabdca4af9f75194e --- manifest | 14 +++---- manifest.uuid | 2 +- src/expr.c | 102 +++++++++++++++++++++++------------------------- src/sqliteInt.h | 6 ++- 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/manifest b/manifest index 896ac5261a..4ce91d165f 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\sbuffer-overflow\sproblem\sin\sthe\srandStr\sfunction\s(used\sonly\sfor\stesting).\s(CVS\s1182) -D 2004-01-16T13:58:18 +C Incremental\scode\scompaction\sin\sexpr.c.\s\sNow\sabout\s4%\ssmaller.\nLots\smore\swork\sto\sdo.\s(CVS\s1183) +D 2004-01-16T15:55:38 F Makefile.in 0515ff9218ad8d5a8f6220f0494b8ef94c67013b F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906 F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd @@ -31,7 +31,7 @@ F src/copy.c 9e47975ea96751c658bcf1a0c4f0bb7c6ee61e73 F src/date.c bb89fdb9c89e367b9a728c58cb96e4823974a2c1 F src/delete.c 0f81e6799c089487615d38e042a2de4d2d6192bc F src/encode.c 9e70ea1e4e746f23f18180949e94f1bb1c2220d3 -F src/expr.c 866a6d7aacc2825aa13056ccbea1a16f436a1ca5 +F src/expr.c 5ecbce47b7f163bb1f64bc5e19fbb115ab468f98 F src/func.c 564c0bbe93c290774b305c0199237b8e8bcbda53 F src/hash.c 9b56ef3b291e25168f630d5643a4264ec011c70e F src/hash.h 3247573ab95b9dd90bcca0307a75d9a16da1ccc7 @@ -49,7 +49,7 @@ F src/random.c 19e8e00fe0df32a742f115773f57651be327cabe F src/select.c 2712bd4d311ebe7dbf8fd7596a809042657db85f F src/shell.c 3b067edc098c45caca164bcad1fa79192c3ec5ae F src/sqlite.h.in c70d8533cd5a5ae8af580597dbc726693ef82de9 -F src/sqliteInt.h 99df38d3c16cc727030c218a3fc61de08c071d08 +F src/sqliteInt.h c5b727d5d07b88654c204c0fc1ae79c9f635a008 F src/table.c d845cb101b5afc1f7fea083c99e3d2fa7998d895 F src/tclsqlite.c 85810fc4a850e2178d71aa5efe576dcd331db596 F src/test1.c e8652055d04d241d4fb437b5c33ff07d9f13b4b4 @@ -180,7 +180,7 @@ F www/speed.tcl 2f6b1155b99d39adb185f900456d1d592c4832b3 F www/sqlite.tcl 3c83b08cf9f18aa2d69453ff441a36c40e431604 F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1 -P 5e85025be7aa4a03b0cfb4d0f28a2e44653b9d3f -R aaef1a0aff5b12b7d4f36cc6a72499f1 +P 42c79edc2e8d1051b3bca915b4b205c601b8077f +R 36f16e7773dddd52ae0de77fcf909446 U drh -Z ebbe741602d66e22ec8fa796a8084911 +Z 7d1bc38e295a20482fad97622944af0e diff --git a/manifest.uuid b/manifest.uuid index 42f947c657..0ec3fbc59a 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -42c79edc2e8d1051b3bca915b4b205c601b8077f \ No newline at end of file +d292ba5148059dce3893b80eabdca4af9f75194e \ No newline at end of file diff --git a/src/expr.c b/src/expr.c index 4db369d833..513cc6f3eb 100644 --- a/src/expr.c +++ b/src/expr.c @@ -12,7 +12,7 @@ ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** -** $Id: expr.c,v 1.104 2004/01/14 03:12:42 drh Exp $ +** $Id: expr.c,v 1.105 2004/01/16 15:55:38 drh Exp $ */ #include "sqliteInt.h" #include @@ -26,8 +26,7 @@ Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ Expr *pNew; pNew = sqliteMalloc( sizeof(Expr) ); if( pNew==0 ){ - sqliteExprDelete(pLeft); - sqliteExprDelete(pRight); + /* When malloc fails, we leak memory from pLeft and pRight */ return 0; } pNew->op = op; @@ -38,9 +37,9 @@ Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ pNew->token = *pToken; pNew->span = *pToken; }else{ - pNew->token.dyn = 0; - pNew->token.z = 0; - pNew->token.n = 0; + assert( pNew->token.dyn==0 ); + assert( pNew->token.z==0 ); + assert( pNew->token.n==0 ); if( pLeft && pRight ){ sqliteExprSpan(pNew, &pLeft->span, &pRight->span); }else{ @@ -55,14 +54,15 @@ Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ ** text between the two given tokens. */ void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ - if( pExpr && pRight && pRight->z && pLeft && pLeft->z ){ + assert( pRight!=0 ); + assert( pLeft!=0 ); + /* Note: pExpr might be NULL due to a prior malloc failure */ + if( pExpr && pRight->z && pLeft->z ){ if( pLeft->dyn==0 && pRight->dyn==0 ){ pExpr->span.z = pLeft->z; pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); }else{ pExpr->span.z = 0; - pExpr->span.n = 0; - pExpr->span.dyn = 0; } } } @@ -75,18 +75,16 @@ Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ Expr *pNew; pNew = sqliteMalloc( sizeof(Expr) ); if( pNew==0 ){ - sqliteExprListDelete(pList); + /* sqliteExprListDelete(pList); // Leak pList when malloc fails */ return 0; } pNew->op = TK_FUNCTION; pNew->pList = pList; - pNew->token.dyn = 0; if( pToken ){ assert( pToken->dyn==0 ); pNew->token = *pToken; }else{ pNew->token.z = 0; - pNew->token.n = 0; } pNew->span = pNew->token; return pNew; @@ -97,12 +95,12 @@ Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ */ void sqliteExprDelete(Expr *p){ if( p==0 ) return; - if( p->span.dyn && p->span.z ) sqliteFree((char*)p->span.z); - if( p->token.dyn && p->token.z ) sqliteFree((char*)p->token.z); - if( p->pLeft ) sqliteExprDelete(p->pLeft); - if( p->pRight ) sqliteExprDelete(p->pRight); - if( p->pList ) sqliteExprListDelete(p->pList); - if( p->pSelect ) sqliteSelectDelete(p->pSelect); + if( p->span.dyn ) sqliteFree((char*)p->span.z); + if( p->token.dyn ) sqliteFree((char*)p->token.z); + sqliteExprDelete(p->pLeft); + sqliteExprDelete(p->pRight); + sqliteExprListDelete(p->pList); + sqliteSelectDelete(p->pSelect); sqliteFree(p); } @@ -129,13 +127,9 @@ Expr *sqliteExprDup(Expr *p){ pNew->token.z = sqliteStrDup(p->token.z); pNew->token.dyn = 1; }else{ - pNew->token.z = 0; - pNew->token.n = 0; - pNew->token.dyn = 0; + assert( pNew->token.z==0 ); } pNew->span.z = 0; - pNew->span.n = 0; - pNew->span.dyn = 0; pNew->pLeft = sqliteExprDup(p->pLeft); pNew->pRight = sqliteExprDup(p->pRight); pNew->pList = sqliteExprListDup(p->pList); @@ -149,9 +143,7 @@ void sqliteTokenCopy(Token *pTo, Token *pFrom){ pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); pTo->dyn = 1; }else{ - pTo->n = 0; pTo->z = 0; - pTo->dyn = 0; } } ExprList *sqliteExprListDup(ExprList *p){ @@ -187,19 +179,21 @@ SrcList *sqliteSrcListDup(SrcList *p){ int nByte; if( p==0 ) return 0; nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); - pNew = sqliteMalloc( nByte ); + pNew = sqliteMallocRaw( nByte ); if( pNew==0 ) return 0; pNew->nSrc = pNew->nAlloc = p->nSrc; for(i=0; inSrc; i++){ - pNew->a[i].zDatabase = sqliteStrDup(p->a[i].zDatabase); - pNew->a[i].zName = sqliteStrDup(p->a[i].zName); - pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias); - pNew->a[i].jointype = p->a[i].jointype; - pNew->a[i].iCursor = p->a[i].iCursor; - pNew->a[i].pTab = 0; - pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect); - pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn); - pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing); + struct SrcList_item *pNewItem = &pNew->a[i]; + struct SrcList_item *pOldItem = &p->a[i]; + pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); + pNewItem->zName = sqliteStrDup(pOldItem->zName); + pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); + pNewItem->jointype = pOldItem->jointype; + pNewItem->iCursor = pOldItem->iCursor; + pNewItem->pTab = 0; + pNewItem->pSelect = sqliteSelectDup(pOldItem->pSelect); + pNewItem->pOn = sqliteExprDup(pOldItem->pOn); + pNewItem->pUsing = sqliteIdListDup(pOldItem->pUsing); } return pNew; } @@ -207,21 +201,23 @@ IdList *sqliteIdListDup(IdList *p){ IdList *pNew; int i; if( p==0 ) return 0; - pNew = sqliteMalloc( sizeof(*pNew) ); + pNew = sqliteMallocRaw( sizeof(*pNew) ); if( pNew==0 ) return 0; pNew->nId = pNew->nAlloc = p->nId; - pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) ); + pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); if( pNew->a==0 ) return 0; for(i=0; inId; i++){ - pNew->a[i].zName = sqliteStrDup(p->a[i].zName); - pNew->a[i].idx = p->a[i].idx; + struct IdList_item *pNewItem = &pNew->a[i]; + struct IdList_item *pOldItem = &p->a[i]; + pNewItem->zName = sqliteStrDup(pOldItem->zName); + pNewItem->idx = pOldItem->idx; } return pNew; } Select *sqliteSelectDup(Select *p){ Select *pNew; if( p==0 ) return 0; - pNew = sqliteMalloc( sizeof(*p) ); + pNew = sqliteMallocRaw( sizeof(*p) ); if( pNew==0 ) return 0; pNew->isDistinct = p->isDistinct; pNew->pEList = sqliteExprListDup(p->pEList); @@ -250,28 +246,28 @@ ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ if( pList==0 ){ pList = sqliteMalloc( sizeof(ExprList) ); if( pList==0 ){ - sqliteExprDelete(pExpr); + /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */ return 0; } - pList->nAlloc = 0; + assert( pList->nAlloc==0 ); } if( pList->nAlloc<=pList->nExpr ){ - struct ExprList_item *a; pList->nAlloc = pList->nAlloc*2 + 4; - a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); - if( a==0 ){ - sqliteExprDelete(pExpr); + pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); + if( pList->a==0 ){ + /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */ + pList->nExpr = pList->nAlloc = 0; return pList; } - pList->a = a; } - if( pList->a && (pExpr || pName) ){ - i = pList->nExpr++; - memset(&pList->a[i], 0, sizeof(pList->a[i])); - pList->a[i].pExpr = pExpr; + assert( pList->a!=0 ); + if( pExpr || pName ){ + struct ExprList_item *pItem = &pList->a[pList->nExpr++]; + memset(pItem, 0, sizeof(*pItem)); + pItem->pExpr = pExpr; if( pName ){ - sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); - sqliteDequote(pList->a[i].zName); + sqliteSetNString(&pItem->zName, pName->z, pName->n, 0); + sqliteDequote(pItem->zName); } } return pList; diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 9894786077..381b538e6e 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.208 2004/01/15 02:44:03 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.209 2004/01/16 15:55:38 drh Exp $ */ #include "config.h" #include "sqlite.h" @@ -587,6 +587,10 @@ struct Index { /* ** Each token coming out of the lexer is an instance of ** this structure. Tokens are also used as part of an expression. +** +** Note if Token.z==0 then Token.dyn and Token.n are undefined and +** may contain random values. Do not make any assuptions about Token.dyn +** and Token.n when Token.z==0. */ struct Token { const char *z; /* Text of the token. Not NULL-terminated! */