1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-10-27 08:52:26 +03:00

Move malloc(), free(), realloc() and allocationSize() into the Os vtbl. (CVS 2819)

FossilOrigin-Name: 81a41f66370ea7f6810dfb323ba5cea60f240edd
This commit is contained in:
danielk1977
2005-12-15 10:50:53 +00:00
parent 13a68c3f61
commit c529f52046
7 changed files with 94 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
C Add\sthe\ssqlite3_os_routine_set()/get()\sfunctions.\s(CVS\s2818) C Move\smalloc(),\sfree(),\srealloc()\sand\sallocationSize()\sinto\sthe\sOs\svtbl.\s(CVS\s2819)
D 2005-12-15T10:11:31 D 2005-12-15T10:50:54
F Makefile.in e3c6b3a38d734d41574c04f2fc90d18de2b87102 F Makefile.in e3c6b3a38d734d41574c04f2fc90d18de2b87102
F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -51,13 +51,13 @@ F src/legacy.c 59757d857ab95fcbb0ac27692d3201e35f093dd7
F src/main.c a12aa72335036bb22249418e222124abfc581b85 F src/main.c a12aa72335036bb22249418e222124abfc581b85
F src/md5.c c5fdfa5c2593eaee2e32a5ce6c6927c986eaf217 F src/md5.c c5fdfa5c2593eaee2e32a5ce6c6927c986eaf217
F src/os.c 7b4a002d9c9421580276db55d2329636a604e8ef F src/os.c 7b4a002d9c9421580276db55d2329636a604e8ef
F src/os.h df6babd4fd74bb659470c31fa5a27de31b1c474e F src/os.h e941992043b127fdb1bd114f0b4319ae1c4562a7
F src/os_common.h d74a11728ad2444b6b695b94c28c06881f049e49 F src/os_common.h a4ad0448f112bb124e5df9eb41c377e770aee7b2
F src/os_test.c 49833426101f99aee4bb5f6a44b7c4b2029fda1c F src/os_test.c 49833426101f99aee4bb5f6a44b7c4b2029fda1c
F src/os_test.h 903c93554c23d88f34f667f1979e4a1cee792af3 F src/os_test.h 903c93554c23d88f34f667f1979e4a1cee792af3
F src/os_unix.c df6e4972099aa5263183009fb947083633236dd3 F src/os_unix.c 6394d2fa3a8bfbceb227579b44b4b343b5b54a8f
F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e
F src/os_win.c 2da77ddc03de8c51688cca648ced766b5dad8a6a F src/os_win.c 9feb97f49b93d451f8ef7c5dd388e05a44647dc6
F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b
F src/pager.c 49f63a54b57164a70df0b1539141003fd27856c6 F src/pager.c 49f63a54b57164a70df0b1539141003fd27856c6
F src/pager.h e7b41ce8e7b5f629d456708b7ad9a8c8ede37140 F src/pager.h e7b41ce8e7b5f629d456708b7ad9a8c8ede37140
@@ -82,7 +82,7 @@ F src/tokenize.c 7a3a3d3cc734f684a77c4dfd09eb46fcee25394c
F src/trigger.c 2925ba96d964d9b717e74006bf7e64b8a6b70d97 F src/trigger.c 2925ba96d964d9b717e74006bf7e64b8a6b70d97
F src/update.c ec8e540617b116725b5a55c8d6b4db8bc67fdd7d F src/update.c ec8e540617b116725b5a55c8d6b4db8bc67fdd7d
F src/utf.c d2360f55ecd666f3e472738191f8dae717b95e5e F src/utf.c d2360f55ecd666f3e472738191f8dae717b95e5e
F src/util.c 06ab728430ae55191076cdf4075be36f621a7183 F src/util.c 8bb5e0553692d36c769d6cd033eb7f68a7586648
F src/vacuum.c fbfdd3967fd34e2f260fafed88dcbf3c10856b94 F src/vacuum.c fbfdd3967fd34e2f260fafed88dcbf3c10856b94
F src/vdbe.c d09c185f4badac6c79f2a919cbf661e7b5618293 F src/vdbe.c d09c185f4badac6c79f2a919cbf661e7b5618293
F src/vdbe.h 8729a4ee16ff9aeab2af9667df3cf300ff978e13 F src/vdbe.h 8729a4ee16ff9aeab2af9667df3cf300ff978e13
@@ -327,7 +327,7 @@ F www/tclsqlite.tcl ddcf912ea48695603c8ed7efb29f0812ef8d1b49
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
P ad292e27336b8c5afc0acdf111944a456bd23c32 P c1ed79f594fb85009c2e9e5e281cbe66a9d2fa17
R 079783b8421fc6a8eada9a4aa8fe00c7 R e31df3de6303938f8025bb0d1c87c829
U danielk1977 U danielk1977
Z 21e315959d3bb7c348f1a6f926708fc0 Z d08eb08fd8a87ac6ccdc5f99bd5ce249

View File

@@ -1 +1 @@
c1ed79f594fb85009c2e9e5e281cbe66a9d2fa17 81a41f66370ea7f6810dfb323ba5cea60f240edd

View File

@@ -220,9 +220,15 @@ extern struct sqlite3OsVtbl {
int (*xRandomSeed)(char*); int (*xRandomSeed)(char*);
int (*xSleep)(int ms); int (*xSleep)(int ms);
int (*xCurrentTime)(double*); int (*xCurrentTime)(double*);
void (*xEnterMutex)(void); void (*xEnterMutex)(void);
void (*xLeaveMutex)(void); void (*xLeaveMutex)(void);
void *(*xThreadSpecificData)(int); void *(*xThreadSpecificData)(int);
void *(*xMalloc)(int);
void *(*xRealloc)(void *, int);
void (*xFree)(void *);
int (*xAllocationSize)(void *);
} sqlite3Os; } sqlite3Os;

View File

@@ -122,3 +122,40 @@ int sqlite3_open_file_count = 0;
#else #else
#define OpenCounter(X) #define OpenCounter(X)
#endif #endif
/*
** genericMalloc
** genericRealloc
** genericOsFree
** genericAllocationSize
**
** Implementation of the os level dynamic memory allocation interface in terms
** of the standard malloc(), realloc() and free() found in many operating
** systems. No rocket science here.
*/
static void *genericMalloc(int n){
char *p = (char *)malloc(n+8);
assert(n>0);
assert(sizeof(int)<=8);
if( p ){
*(int *)p = n;
}
return (void *)(p + 8);
}
static void *genericRealloc(void *p, int n){
char *p2 = ((char *)p - 8);
assert(n>0);
p2 = realloc(p2, n+8);
if( p2 ){
*(int *)p2 = n;
}
return (void *)((char *)p2 + 8);
}
static void genericFree(void *p){
assert(p);
free((void *)((char *)p - 8));
}
static int genericAllocationSize(void *p){
return *(int *)((char *)p - 8);
}

View File

@@ -1594,9 +1594,11 @@ static void unixLeaveMutex(){
** such as sqliteMalloc() we use OsFree() and OsMalloc() directly to ** such as sqliteMalloc() we use OsFree() and OsMalloc() directly to
** allocate the thread specific data. ** allocate the thread specific data.
*/ */
#ifdef SQLITE_UNIX_THREADS
static void deleteTsd(void *pTsd){ static void deleteTsd(void *pTsd){
sqlite3OsFree(pTsd); sqlite3Os.xFree(pTsd);
} }
#endif
/* /*
** The first time this function is called from a specific thread, nByte ** The first time this function is called from a specific thread, nByte
@@ -1627,7 +1629,7 @@ static void *unixThreadSpecificData(int nByte){
pTsd = (SqliteTsd *)pthread_getspecific(key); pTsd = (SqliteTsd *)pthread_getspecific(key);
if( !pTsd ){ if( !pTsd ){
pTsd = sqlite3OsMalloc(sizeof(SqliteTsd)); pTsd = sqlite3Os.xMalloc(sizeof(SqliteTsd));
if( pTsd ){ if( pTsd ){
memset(pTsd, 0, sizeof(SqliteTsd)); memset(pTsd, 0, sizeof(SqliteTsd));
pthread_setspecific(key, pTsd); pthread_setspecific(key, pTsd);
@@ -1636,7 +1638,7 @@ static void *unixThreadSpecificData(int nByte){
return pTsd; return pTsd;
#else #else
static char tsd[sizeof(SqliteTsd)]; static char tsd[sizeof(SqliteTsd)];
static isInit = 0; static int isInit = 0;
assert( nByte==sizeof(SqliteTsd) ); assert( nByte==sizeof(SqliteTsd) );
if( !isInit ){ if( !isInit ){
memset(tsd, 0, sizeof(SqliteTsd)); memset(tsd, 0, sizeof(SqliteTsd));
@@ -1704,7 +1706,11 @@ struct sqlite3OsVtbl sqlite3Os = {
unixCurrentTime, unixCurrentTime,
unixEnterMutex, unixEnterMutex,
unixLeaveMutex, unixLeaveMutex,
unixThreadSpecificData unixThreadSpecificData,
genericMalloc,
genericRealloc,
genericFree,
genericAllocationSize
}; };

View File

@@ -1062,7 +1062,11 @@ struct sqlite3OsVtbl sqlite3Os = {
winCurrentTime, winCurrentTime,
winEnterMutex, winEnterMutex,
winLeaveMutex, winLeaveMutex,
winThreadSpecificData winThreadSpecificData,
genericMalloc,
genericRealloc,
genericFree,
genericAllocationSize
}; };
#endif /* OS_WIN */ #endif /* OS_WIN */

View File

@@ -14,7 +14,7 @@
** This file contains functions for allocating memory, comparing ** This file contains functions for allocating memory, comparing
** strings, and stuff like that. ** strings, and stuff like that.
** **
** $Id: util.c,v 1.153 2005/12/15 10:11:32 danielk1977 Exp $ ** $Id: util.c,v 1.154 2005/12/15 10:50:54 danielk1977 Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include "os.h" #include "os.h"
@@ -40,14 +40,14 @@
** level (not in this file). The Os level interface is never accessed directly ** level (not in this file). The Os level interface is never accessed directly
** by code outside of this file. ** by code outside of this file.
** **
** sqlite3OsMalloc() ** sqlite3Os.xMalloc()
** sqlite3OsRealloc() ** sqlite3Os.xRealloc()
** sqlite3OsFree() ** sqlite3Os.xFree()
** sqlite3OsAllocationSize() ** sqlite3Os.xAllocationSize()
** **
** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke ** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke
** sqlite3_release_memory() if a call to sqlite3OsMalloc() or ** sqlite3_release_memory() if a call to sqlite3Os.xMalloc() or
** sqlite3OsRealloc() fails. Function sqlite3Malloc() usually invokes ** sqlite3Os.xRealloc() fails. Function sqlite3Malloc() usually invokes
** sqlite3MallocRaw(). ** sqlite3MallocRaw().
** **
** MALLOC TEST WRAPPER ARCHITECTURE ** MALLOC TEST WRAPPER ARCHITECTURE
@@ -62,42 +62,6 @@
** * Audit outstanding memory allocations (i.e check for leaks). ** * Audit outstanding memory allocations (i.e check for leaks).
*/ */
/*
** sqlite3OsMalloc
** sqlite3OsRealloc
** sqlite3OsOsFree
** sqlite3OsAllocationSize
**
** Implementation of the os level dynamic memory allocation interface in terms
** of the standard malloc(), realloc() and free() found in many operating
** systems. No rocket science here.
*/
void *sqlite3OsMalloc(int n){
char *p = (char *)malloc(n+8);
assert(n>0);
assert(sizeof(int)<=8);
if( p ){
*(int *)p = n;
}
return (void *)(p + 8);
}
void *sqlite3OsRealloc(void *p, int n){
char *p2 = ((char *)p - 8);
assert(n>0);
p2 = realloc(p2, n+8);
if( p2 ){
*(int *)p2 = n;
}
return (void *)((char *)p2 + 8);
}
void sqlite3OsFree(void *p){
assert(p);
free((void *)((char *)p - 8));
}
int sqlite3OsAllocationSize(void *p){
return *(int *)((char *)p - 8);
}
/* /*
** TODO! ** TODO!
*/ */
@@ -167,7 +131,7 @@ const char *sqlite3_malloc_id = 0;
TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \ TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \
) )
#define TESTALLOC_OFFSET_GUARD2(p) ( \ #define TESTALLOC_OFFSET_GUARD2(p) ( \
TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \ TESTALLOC_OFFSET_DATA(p) + sqlite3Os.xAllocationSize(p) - TESTALLOC_OVERHEAD \
) )
#define TESTALLOC_OFFSET_LINENUMBER(p) ( \ #define TESTALLOC_OFFSET_LINENUMBER(p) ( \
TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \ TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \
@@ -226,7 +190,7 @@ static int failMalloc(){
} }
/* /*
** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). ** The argument is a pointer returned by sqlite3Os.xMalloc() or xRealloc().
** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the ** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the
** values set by the applyGuards() function. ** values set by the applyGuards() function.
*/ */
@@ -252,7 +216,7 @@ static void checkGuards(u32 *p)
} }
/* /*
** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). The ** The argument is a pointer returned by sqlite3Os.xMalloc() or Realloc(). The
** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as ** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as
** guard-posts. ** guard-posts.
*/ */
@@ -404,7 +368,7 @@ int sqlite3OutstandingMallocs(Tcl_Interp *interp){
Tcl_Obj *pStack = Tcl_NewObj(); Tcl_Obj *pStack = Tcl_NewObj();
char *z; char *z;
u32 iLine; u32 iLine;
int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD; int nBytes = sqlite3Os.xAllocationSize(p) - TESTALLOC_OVERHEAD;
char *zAlloc = (char *)p; char *zAlloc = (char *)p;
int i; int i;
@@ -439,12 +403,12 @@ int sqlite3OutstandingMallocs(Tcl_Interp *interp){
#endif #endif
/* /*
** This is the test layer's wrapper around sqlite3OsMalloc(). ** This is the test layer's wrapper around sqlite3Os.xMalloc().
*/ */
static void * OSMALLOC(int n){ static void * OSMALLOC(int n){
if( !failMalloc() ){ if( !failMalloc() ){
u32 *p; u32 *p;
p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD); p = (u32 *)sqlite3Os.xMalloc(n + TESTALLOC_OVERHEAD);
assert(p); assert(p);
sqlite3_nMalloc++; sqlite3_nMalloc++;
applyGuards(p); applyGuards(p);
@@ -455,25 +419,25 @@ static void * OSMALLOC(int n){
} }
/* /*
** This is the test layer's wrapper around sqlite3OsFree(). The argument is a ** This is the test layer's wrapper around sqlite3Os.xFree(). The argument is a
** pointer to the space allocated for the application to use. ** pointer to the space allocated for the application to use.
*/ */
void OSFREE(void *pFree){ void OSFREE(void *pFree){
u32 *p = (u32 *)getOsPointer(pFree); /* p points to Os level allocation */ u32 *p = (u32 *)getOsPointer(pFree); /* p points to Os level allocation */
checkGuards(p); checkGuards(p);
unlinkAlloc(p); unlinkAlloc(p);
sqlite3OsFree(p); sqlite3Os.xFree(p);
sqlite3_nFree++; sqlite3_nFree++;
} }
/* /*
** This is the test layer's wrapper around sqlite3OsRealloc(). ** This is the test layer's wrapper around sqlite3Os.xRealloc().
*/ */
void * OSREALLOC(void *pRealloc, int n){ void * OSREALLOC(void *pRealloc, int n){
if( !failMalloc() ){ if( !failMalloc() ){
u32 *p = (u32 *)getOsPointer(pRealloc); u32 *p = (u32 *)getOsPointer(pRealloc);
checkGuards(p); checkGuards(p);
p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD); p = sqlite3Os.xRealloc(p, n + TESTALLOC_OVERHEAD);
applyGuards(p); applyGuards(p);
relinkAlloc(p); relinkAlloc(p);
return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
@@ -487,16 +451,16 @@ void OSMALLOC_FAILED(){
int OSSIZEOF(void *p){ int OSSIZEOF(void *p){
if( p ){ if( p ){
return sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD; return sqlite3Os.xAllocationSize(p) - TESTALLOC_OVERHEAD;
} }
return 0; return 0;
} }
#else #else
#define OSMALLOC(x) sqlite3OsMalloc(x) #define OSMALLOC(x) sqlite3Os.xMalloc(x)
#define OSREALLOC(x,y) sqlite3OsRealloc(x,y) #define OSREALLOC(x,y) sqlite3Os.xRealloc(x,y)
#define OSFREE(x) sqlite3OsFree(x) #define OSFREE(x) sqlite3Os.xFree(x)
#define OSSIZEOF(x) sqlite3OsAllocationSize(x) #define OSSIZEOF(x) sqlite3Os.xAllocationSize(x)
#define OSMALLOC_FAILED() #define OSMALLOC_FAILED()
#endif #endif
/* /*
@@ -505,7 +469,7 @@ int OSSIZEOF(void *p){
/* /*
** The handleSoftLimit() function is called before each call to ** The handleSoftLimit() function is called before each call to
** sqlite3OsMalloc() or sqlite3OsRealloc(). The parameter 'n' is the number of ** sqlite3Os.xMalloc() or xRealloc(). The parameter 'n' is the number of
** extra bytes about to be allocated (for Realloc() this means the size of the ** extra bytes about to be allocated (for Realloc() this means the size of the
** new allocation less the size of the old allocation). If the extra allocation ** new allocation less the size of the old allocation). If the extra allocation
** means that the total memory allocated to SQLite in this thread would exceed ** means that the total memory allocated to SQLite in this thread would exceed
@@ -527,7 +491,7 @@ static void handleSoftLimit(int n){
/* /*
** Allocate and return N bytes of uninitialised memory by calling ** Allocate and return N bytes of uninitialised memory by calling
** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory ** sqlite3Os.xMalloc(). If the Malloc() call fails, attempt to free memory
** by calling sqlite3_release_memory(). ** by calling sqlite3_release_memory().
*/ */
void *sqlite3MallocRaw(int n){ void *sqlite3MallocRaw(int n){
@@ -545,7 +509,7 @@ void *sqlite3MallocRaw(int n){
} }
/* /*
** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The ** Resize the allocation at p to n bytes by calling sqlite3Os.xRealloc(). The
** pointer to the new allocation is returned. If the Realloc() call fails, ** pointer to the new allocation is returned. If the Realloc() call fails,
** attempt to free memory by calling sqlite3_release_memory(). ** attempt to free memory by calling sqlite3_release_memory().
*/ */