aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/legacy.c4
-rw-r--r--src/loadext.c2
-rw-r--r--src/main.c29
-rw-r--r--src/printf.c29
-rw-r--r--src/sqlite.h.in14
-rw-r--r--src/sqlite3ext.h6
-rw-r--r--src/sqliteInt.h2
-rw-r--r--src/table.c16
-rw-r--r--src/test3.c3
9 files changed, 68 insertions, 37 deletions
diff --git a/src/legacy.c b/src/legacy.c
index d724e8a42..39e1361fa 100644
--- a/src/legacy.c
+++ b/src/legacy.c
@@ -14,7 +14,7 @@
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
-** $Id: legacy.c,v 1.14 2006/03/06 20:55:46 drh Exp $
+** $Id: legacy.c,v 1.15 2006/06/26 21:35:45 drh Exp $
*/
#include "sqliteInt.h"
@@ -123,7 +123,7 @@ exec_out:
rc = sqlite3ApiExit(0, rc);
if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
- *pzErrMsg = malloc(1+strlen(sqlite3_errmsg(db)));
+ *pzErrMsg = sqlite3_malloc(1+strlen(sqlite3_errmsg(db)));
if( *pzErrMsg ){
strcpy(*pzErrMsg, sqlite3_errmsg(db));
}
diff --git a/src/loadext.c b/src/loadext.c
index 71eb89bd3..6b277675f 100644
--- a/src/loadext.c
+++ b/src/loadext.c
@@ -157,6 +157,7 @@ const sqlite3_api_routines sqlite3_api = {
sqlite3_last_insert_rowid,
sqlite3_libversion,
sqlite3_libversion_number,
+ sqlite3_malloc,
sqlite3_mprintf,
sqlite3_open,
sqlite3_open16,
@@ -164,6 +165,7 @@ const sqlite3_api_routines sqlite3_api = {
sqlite3_prepare16,
sqlite3_profile,
sqlite3_progress_handler,
+ sqlite3_realloc,
sqlite3_reset,
sqlite3_result_blob,
sqlite3_result_double,
diff --git a/src/main.c b/src/main.c
index 6a5cac321..c5d50bd50 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,7 +14,7 @@
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
-** $Id: main.c,v 1.348 2006/06/24 11:51:33 danielk1977 Exp $
+** $Id: main.c,v 1.349 2006/06/26 21:35:45 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
@@ -385,14 +385,29 @@ void sqlite3_interrupt(sqlite3 *db){
}
/*
-** Windows systems should call this routine to free memory that
-** is returned in the in the errmsg parameter of sqlite3_open() when
-** SQLite is a DLL. For some reason, it does not work to call free()
-** directly.
+** Memory allocation routines that use SQLites internal memory
+** memory allocator. Depending on how SQLite is compiled, the
+** internal memory allocator might be just an alias for the
+** system default malloc/realloc/free. Or the built-in allocator
+** might do extra stuff like put sentinals around buffers to
+** check for overruns or look for memory leaks.
**
-** Note that we need to call free() not sqliteFree() here.
+** Use sqlite3_free() to free memory returned by sqlite3_mprintf().
*/
-void sqlite3_free(char *p){ free(p); }
+void sqlite3_free(void *p){ if( p ) sqlite3OsFree(p); }
+void *sqlite3_malloc(int nByte){ return nByte>0 ? sqlite3OsMalloc(nByte) : 0; }
+void *sqlite3_realloc(void *pOld, int nByte){
+ if( pOld ){
+ if( nByte>0 ){
+ return sqlite3OsRealloc(pOld, nByte);
+ }else{
+ sqlite3OsFree(pOld);
+ return 0;
+ }
+ }else{
+ return sqlite3_malloc(nByte);
+ }
+}
/*
** This function is exactly the same as sqlite3_create_function(), except
diff --git a/src/printf.c b/src/printf.c
index 7e62c9c38..b4c37fb61 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -806,29 +806,28 @@ char *sqlite3MPrintf(const char *zFormat, ...){
}
/*
-** Print into memory obtained from malloc(). Do not use the internal
-** %-conversion extensions. This routine is for use by external users.
+** Print into memory obtained from sqlite3_malloc(). Omit the internal
+** %-conversion extensions.
+*/
+char *sqlite3_vmprintf(const char *zFormat, va_list ap){
+ char zBase[SQLITE_PRINT_BUF_SIZE];
+ return base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
+}
+
+/*
+** Print into memory obtained from sqlite3_malloc()(). Omit the internal
+** %-conversion extensions.
*/
char *sqlite3_mprintf(const char *zFormat, ...){
va_list ap;
char *z;
- char zBuf[200];
-
- va_start(ap,zFormat);
- z = base_vprintf((void*(*)(void*,int))realloc, 0,
- zBuf, sizeof(zBuf), zFormat, ap);
+ char zBase[SQLITE_PRINT_BUF_SIZE];
+ va_start(ap, zFormat);
+ z = base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
va_end(ap);
return z;
}
-/* This is the varargs version of sqlite3_mprintf.
-*/
-char *sqlite3_vmprintf(const char *zFormat, va_list ap){
- char zBuf[200];
- return base_vprintf((void*(*)(void*,int))realloc, 0,
- zBuf, sizeof(zBuf), zFormat, ap);
-}
-
/*
** sqlite3_snprintf() works like snprintf() except that it ignores the
** current locale settings. This is important for SQLite because we
diff --git a/src/sqlite.h.in b/src/sqlite.h.in
index de2fcdc7c..86876555f 100644
--- a/src/sqlite.h.in
+++ b/src/sqlite.h.in
@@ -12,7 +12,7 @@
** This header file defines the interface that the SQLite library
** presents to client programs.
**
-** @(#) $Id: sqlite.h.in,v 1.183 2006/06/26 19:10:32 drh Exp $
+** @(#) $Id: sqlite.h.in,v 1.184 2006/06/26 21:35:45 drh Exp $
*/
#ifndef _SQLITE3_H_
#define _SQLITE3_H_
@@ -405,9 +405,19 @@ void sqlite3_free_table(char **result);
*/
char *sqlite3_mprintf(const char*,...);
char *sqlite3_vmprintf(const char*, va_list);
-void sqlite3_free(char *z);
char *sqlite3_snprintf(int,char*,const char*, ...);
+/*
+** SQLite uses its own memory allocator. On many installations, this
+** memory allocator is identical to the standard malloc()/realloc()/free()
+** and can be used interchangable. On others, the implementations are
+** different. For maximum portability, it is best not to mix calls
+** to the standard malloc/realloc/free with the sqlite versions.
+*/
+void *sqlite3_malloc(int);
+void *sqlite3_realloc(void*, int);
+void sqlite3_free(void*);
+
#ifndef SQLITE_OMIT_AUTHORIZATION
/*
** This routine registers a callback with the SQLite library. The
diff --git a/src/sqlite3ext.h b/src/sqlite3ext.h
index a1ed67f86..5088499da 100644
--- a/src/sqlite3ext.h
+++ b/src/sqlite3ext.h
@@ -15,7 +15,7 @@
** as extensions by SQLite should #include this file instead of
** sqlite3.h.
**
-** @(#) $Id: sqlite3ext.h,v 1.3 2006/06/16 21:13:22 drh Exp $
+** @(#) $Id: sqlite3ext.h,v 1.4 2006/06/26 21:35:45 drh Exp $
*/
#ifndef _SQLITE3EXT_H_
#define _SQLITE3EXT_H_
@@ -95,6 +95,7 @@ struct sqlite3_api_routines {
sqlite_int64 (*last_insert_rowid)(sqlite3*);
const char * (*libversion)(void);
int (*libversion_number)(void);
+ void *(*malloc)(int);
char * (*mprintf)(const char*,...);
int (*open)(const char*,sqlite3**);
int (*open16)(const void*,sqlite3**);
@@ -102,6 +103,7 @@ struct sqlite3_api_routines {
int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
+ void *(*realloc)(void*,int);
int (*reset)(sqlite3_stmt*pStmt);
void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_double)(sqlite3_context*,double);
@@ -221,6 +223,7 @@ struct sqlite3_api_routines {
#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
#define sqlite3_libversion sqlite3_api->libversion
#define sqlite3_libversion_number sqlite3_api->libversion_number
+#define sqlite3_malloc sqlite3_api->malloc
#define sqlite3_mprintf sqlite3_api->mprintf
#define sqlite3_open sqlite3_api->open
#define sqlite3_open16 sqlite3_api->open16
@@ -228,6 +231,7 @@ struct sqlite3_api_routines {
#define sqlite3_prepare16 sqlite3_api->prepare16
#define sqlite3_profile sqlite3_api->profile
#define sqlite3_progress_handler sqlite3_api->progress_handler
+#define sqlite3_realloc sqlite3_api->realloc
#define sqlite3_reset sqlite3_api->reset
#define sqlite3_result_blob sqlite3_api->result_blob
#define sqlite3_result_double sqlite3_api->result_double
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index f49de42d3..f973254c8 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
*************************************************************************
** Internal interface definitions for SQLite.
**
-** @(#) $Id: sqliteInt.h,v 1.513 2006/06/26 19:10:32 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.514 2006/06/26 21:35:45 drh Exp $
*/
#ifndef _SQLITEINT_H_
#define _SQLITEINT_H_
diff --git a/src/table.c b/src/table.c
index 8bd9ea71f..5c99c523f 100644
--- a/src/table.c
+++ b/src/table.c
@@ -59,7 +59,7 @@ static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
if( p->nData + need >= p->nAlloc ){
char **azNew;
p->nAlloc = p->nAlloc*2 + need + 1;
- azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc );
+ azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
if( azNew==0 ) goto malloc_failed;
p->azResult = azNew;
}
@@ -73,7 +73,7 @@ static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
if( colv[i]==0 ){
z = 0;
}else{
- z = malloc( strlen(colv[i])+1 );
+ z = sqlite3_malloc( strlen(colv[i])+1 );
if( z==0 ) goto malloc_failed;
strcpy(z, colv[i]);
}
@@ -94,7 +94,7 @@ static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
if( argv[i]==0 ){
z = 0;
}else{
- z = malloc( strlen(argv[i])+1 );
+ z = sqlite3_malloc( strlen(argv[i])+1 );
if( z==0 ) goto malloc_failed;
strcpy(z, argv[i]);
}
@@ -140,7 +140,7 @@ int sqlite3_get_table(
res.nData = 1;
res.nAlloc = 20;
res.rc = SQLITE_OK;
- res.azResult = malloc( sizeof(char*)*res.nAlloc );
+ res.azResult = sqlite3_malloc( sizeof(char*)*res.nAlloc );
if( res.azResult==0 ) return SQLITE_NOMEM;
res.azResult[0] = 0;
rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
@@ -152,7 +152,7 @@ int sqlite3_get_table(
sqlite3_free_table(&res.azResult[1]);
if( res.zErrMsg ){
if( pzErrMsg ){
- free(*pzErrMsg);
+ sqlite3_free(*pzErrMsg);
*pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
}
sqliteFree(res.zErrMsg);
@@ -167,7 +167,7 @@ int sqlite3_get_table(
}
if( res.nAlloc>res.nData ){
char **azNew;
- azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
+ azNew = sqlite3_realloc( res.azResult, sizeof(char*)*(res.nData+1) );
if( azNew==0 ){
sqlite3_free_table(&res.azResult[1]);
return SQLITE_NOMEM;
@@ -192,8 +192,8 @@ void sqlite3_free_table(
azResult--;
if( azResult==0 ) return;
n = (int)azResult[0];
- for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
- free(azResult);
+ for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
+ sqlite3_free(azResult);
}
}
diff --git a/src/test3.c b/src/test3.c
index bd0ff8a96..3d4c086f2 100644
--- a/src/test3.c
+++ b/src/test3.c
@@ -13,7 +13,7 @@
** is not included in the SQLite library. It is used for automated
** testing of the SQLite library.
**
-** $Id: test3.c,v 1.65 2006/01/20 10:55:05 danielk1977 Exp $
+** $Id: test3.c,v 1.66 2006/06/26 21:35:46 drh Exp $
*/
#include "sqliteInt.h"
#include "pager.h"
@@ -585,6 +585,7 @@ static int btree_integrity_check(
#else
zResult = 0;
#endif
+ free(aRoot);
if( zResult ){
Tcl_AppendResult(interp, zResult, 0);
sqliteFree(zResult);