diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/build.c | 6 | ||||
-rw-r--r-- | src/sqliteInt.h | 21 | ||||
-rw-r--r-- | src/tclsqlite.c | 3 | ||||
-rw-r--r-- | src/tokenize.c | 7 | ||||
-rw-r--r-- | src/util.c | 97 |
5 files changed, 119 insertions, 15 deletions
diff --git a/src/build.c b/src/build.c index dbb227a28..40c4d39c9 100644 --- a/src/build.c +++ b/src/build.c @@ -24,7 +24,7 @@ ** This file contains C code routines that are called by the parser ** when syntax rules are reduced. ** -** $Id: build.c,v 1.5 2000/05/30 03:12:21 drh Exp $ +** $Id: build.c,v 1.6 2000/05/30 13:44:19 drh Exp $ */ #include "sqliteInt.h" @@ -243,7 +243,7 @@ void sqliteAddColumn(Parse *pParse, Token *pName){ char **pz; if( (p = pParse->pNewTable)==0 ) return; if( (p->nCol & 0x7)==0 ){ - p->azCol = sqliteRealloc( p->azCol, (p->nCol+9)*sizeof(p->azCol[0])); + p->azCol = sqliteRealloc( p->azCol, (p->nCol+8)*sizeof(p->azCol[0])); } if( p->azCol==0 ){ p->nCol = 0; @@ -473,7 +473,7 @@ void sqliteCreateIndex( /* ** Allocate the index structure. */ - pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + + pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + sizeof(int)*pList->nId ); if( pIndex==0 ){ sqliteSetString(&pParse->zErrMsg, "out of memory", 0); diff --git a/src/sqliteInt.h b/src/sqliteInt.h index b2bf129de..0ef51f9ba 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -23,7 +23,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.1 2000/05/29 14:26:01 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.2 2000/05/30 13:44:20 drh Exp $ */ #include "sqlite.h" #include "dbbe.h" @@ -35,6 +35,13 @@ #include <string.h> #include <assert.h> +/* #define MEMORY_DEBUG 1 */ +#ifdef MEMORY_DEBUG +# define sqliteMalloc(X) sqliteMalloc_(X,__FILE__,__LINE__) +# define sqliteFree(X) sqliteFree_(X,__FILE__,__LINE__) +# define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__) +#endif + /* ** The number of entries in the in-memory hash table holding the ** schema. @@ -200,9 +207,15 @@ int sqliteStrNICmp(const char *, const char *, int); int sqliteHashNoCase(const char *, int); int sqliteCompare(const char *, const char *); int sqliteSortCompare(const char *, const char *); -void *sqliteMalloc(int); -void sqliteFree(void*); -void *sqliteRealloc(void*,int); +#ifdef MEMORY_DEBUG + void *sqliteMalloc_(int,char*,int); + void sqliteFree_(void*,char*,int); + void *sqliteRealloc_(void*,int,char*,int); +#else + void *sqliteMalloc(int); + void sqliteFree(void*); + void *sqliteRealloc(void*,int); +#endif int sqliteGetToken(const char*, int *); void sqliteSetString(char **, const char *, ...); void sqliteSetNString(char **, ...); diff --git a/src/tclsqlite.c b/src/tclsqlite.c index 2d4d154eb..e261791cc 100644 --- a/src/tclsqlite.c +++ b/src/tclsqlite.c @@ -23,7 +23,7 @@ ************************************************************************* ** A TCL Interface to SQLite ** -** $Id: tclsqlite.c,v 1.2 2000/05/29 20:41:51 drh Exp $ +** $Id: tclsqlite.c,v 1.3 2000/05/30 13:44:20 drh Exp $ */ #include "sqlite.h" #include <tcl.h> @@ -152,6 +152,7 @@ static int DbCmd(void *cd, Tcl_Interp *interp, int argc, char **argv){ } if( argc==5 ){ cbData.interp = interp; + cbData.once = 1; cbData.zArray = argv[3]; cbData.zCode = argv[4]; zErrMsg = 0; diff --git a/src/tokenize.c b/src/tokenize.c index f762b679d..cc7615bac 100644 --- a/src/tokenize.c +++ b/src/tokenize.c @@ -27,10 +27,11 @@ ** individual tokens and sends those tokens one-by-one over to the ** parser for analysis. ** -** $Id: tokenize.c,v 1.1 2000/05/29 14:26:02 drh Exp $ +** $Id: tokenize.c,v 1.2 2000/05/30 13:44:20 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> +#include <stdlib.h> /* ** All the keywords of the SQL language are stored as in a hash @@ -287,7 +288,7 @@ int sqliteRunParser(Parse *pParse, char *zSql, char **pzErrMsg){ extern void sqliteParserTrace(FILE*, char *); i = 0; - pEngine = sqliteParserAlloc(sqliteMalloc); + pEngine = sqliteParserAlloc((void(*)())malloc); if( pEngine==0 ){ sqliteSetString(pzErrMsg, "out of memory", 0); return 1; @@ -354,7 +355,7 @@ int sqliteRunParser(Parse *pParse, char *zSql, char **pzErrMsg){ nErr++; } } - sqliteParserFree(pEngine, sqliteFree); + sqliteParserFree(pEngine, free); if( pParse->zErrMsg ){ if( pzErrMsg ){ *pzErrMsg = pParse->zErrMsg; diff --git a/src/util.c b/src/util.c index 65ccc1ab8..11e4449b6 100644 --- a/src/util.c +++ b/src/util.c @@ -26,19 +26,109 @@ ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** -** $Id: util.c,v 1.4 2000/05/29 23:48:23 drh Exp $ +** $Id: util.c,v 1.5 2000/05/30 13:44:20 drh Exp $ */ #include "sqliteInt.h" #include <stdarg.h> #include <ctype.h> +#ifdef MEMORY_DEBUG + + +/* +** Allocate new memory and set it to zero. Return NULL if +** no memory is available. +*/ +void *sqliteMalloc_(int n, char *zFile, int line){ + void *p; + int *pi; + int k; + k = (n+sizeof(int)-1)/sizeof(int); + pi = malloc( (3+k)*sizeof(int)); + if( pi==0 ) return 0; + pi[0] = 0xdead1122; + pi[1] = n; + pi[k+2] = 0xdead3344; + p = &pi[2]; + memset(p, 0, n); + printf("malloc %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile, line); + return p; +} + +/* +** Free memory previously obtained from sqliteMalloc() +*/ +void sqliteFree_(void *p, char *zFile, int line){ + if( p ){ + int *pi, k, n; + pi = p; + pi -= 2; + if( pi[0]!=0xdead1122 ){ + printf("Low-end memory corruption at 0x%x\n", (int)p); + return; + } + n = pi[1]; + k = (n+sizeof(int)-1)/sizeof(int); + if( pi[k+2]!=0xdead3344 ){ + printf("High-end memory corruption at 0x%x\n", (int)p); + return; + } + memset(pi, 0, (k+3)*sizeof(int)); + printf("free %d bytes at 0x%x from %s:%d\n", n, (int)p, zFile, line); + free(pi); + } +} + +/* +** Resize a prior allocation. If p==0, then this routine +** works just like sqliteMalloc(). If n==0, then this routine +** works just like sqliteFree(). +*/ +void *sqliteRealloc_(void *oldP, int n, char *zFile, int line){ + int *oldPi, *pi, k, oldN, oldK; + void *p; + if( oldP==0 ){ + return sqliteMalloc_(n,zFile,line); + } + if( n==0 ){ + sqliteFree_(oldP,zFile,line); + return 0; + } + oldPi = oldP; + oldPi -= 2; + if( oldPi[0]!=0xdead1122 ){ + printf("Low-end memory corruption in realloc at 0x%x\n", (int)p); + return; + } + oldN = oldPi[1]; + oldK = (oldN+sizeof(int)-1)/sizeof(int); + if( oldPi[oldK+2]!=0xdead3344 ){ + printf("High-end memory corruption in realloc at 0x%x\n", (int)p); + return; + } + k = (n + sizeof(int) - 1)/sizeof(int); + pi = malloc( (k+3)*sizeof(int) ); + pi[0] = 0xdead1122; + pi[1] = n; + pi[k+2] = 0xdead3344; + p = &pi[2]; + memcpy(p, oldP, n>oldN ? oldN : n); + if( n>oldN ){ + memset(&((char*)p)[oldN], 0, n-oldN); + } + memset(oldPi, 0, (oldK+3)*sizeof(int)); + free(oldPi); + printf("realloc %d->%d bytes at 0x%x->0x%x at %s:%d\n", oldN, n, + (int)oldP, (int)p, zFile, line); + return p; +} +#else /* !defined(MEMORY_DEBUG) */ /* ** Allocate new memory and set it to zero. Return NULL if ** no memory is available. */ void *sqliteMalloc(int n){ void *p = malloc(n); - /* printf("alloc 0x%x size: %d bytes\n", (int)p, n); */ if( p==0 ) return 0; memset(p, 0, n); return p; @@ -49,7 +139,6 @@ void *sqliteMalloc(int n){ */ void sqliteFree(void *p){ if( p ){ - /* printf("free 0x%x\n", (int)p); */ free(p); } } @@ -67,9 +156,9 @@ void *sqliteRealloc(void *p, int n){ sqliteFree(p); return 0; } - /* printf("realloc 0x%x size: %d bytes\n", (int)p, n); */ return realloc(p, n); } +#endif /* MEMORY_DEBUG */ /* ** Create a string from the 2nd and subsequent arguments (up to the |