mirror of
https://github.com/sqlite/sqlite.git
synced 2025-08-07 02:42:48 +03:00
Test infrastructure for the new memory subsystem. (CVS 4229)
FossilOrigin-Name: 9e506656720fb3a3205b8cc398152272ce56f6f3
This commit is contained in:
251
src/test_malloc.c
Normal file
251
src/test_malloc.c
Normal file
@@ -0,0 +1,251 @@
|
||||
/*
|
||||
** 2007 August 15
|
||||
**
|
||||
** The author disclaims copyright to this source code. In place of
|
||||
** a legal notice, here is a blessing:
|
||||
**
|
||||
** May you do good and not evil.
|
||||
** May you find forgiveness for yourself and forgive others.
|
||||
** May you share freely, never taking more than you give.
|
||||
**
|
||||
*************************************************************************
|
||||
**
|
||||
** This file contains code used to implement test interfaces to the
|
||||
** memory allocation subsystem.
|
||||
**
|
||||
** $Id: test_malloc.c,v 1.1 2007/08/15 19:16:43 drh Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include "tcl.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
/*
|
||||
** Transform pointers to text and back again
|
||||
*/
|
||||
static void pointerToText(void *p, char *z){
|
||||
static const char zHex[] = "0123456789abcdef";
|
||||
int i, k;
|
||||
sqlite3_uint64 n = (sqlite3_uint64)p;
|
||||
for(i=0, k=sizeof(p)*2-1; i<sizeof(p)*2; i++, k--){
|
||||
z[k] = zHex[n&0xf];
|
||||
n >>= 4;
|
||||
}
|
||||
z[sizeof(p)*2] = 0;
|
||||
}
|
||||
static int hexToInt(int h){
|
||||
if( h>='0' && h<='9' ){
|
||||
return h - '0';
|
||||
}else if( h>='a' && h<='f' ){
|
||||
return h - 'a' + 10;
|
||||
}else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
static int textToPointer(const char *z, void **pp){
|
||||
sqlite3_uint64 n = 0;
|
||||
int i;
|
||||
for(i=0; i<sizeof(void*)*2 && z[0]; i++){
|
||||
int v;
|
||||
v = hexToInt(*z++);
|
||||
if( v<0 ) return TCL_ERROR;
|
||||
n = n*16 + v;
|
||||
}
|
||||
if( *z!=0 ) return TCL_ERROR;
|
||||
*pp = (void*)n;
|
||||
return TCL_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** Usage: sqlite3_malloc NBYTES
|
||||
**
|
||||
** Raw test interface for sqlite3_malloc().
|
||||
*/
|
||||
static int test_malloc(
|
||||
void * clientData,
|
||||
Tcl_Interp *interp,
|
||||
int objc,
|
||||
Tcl_Obj *CONST objv[]
|
||||
){
|
||||
int nByte;
|
||||
void *p;
|
||||
char zOut[100];
|
||||
if( objc!=2 ){
|
||||
Tcl_WrongNumArgs(interp, 1, objv, "NBYTES");
|
||||
return TCL_ERROR;
|
||||
}
|
||||
if( Tcl_GetIntFromObj(interp, objv[1], &nByte) ) return TCL_ERROR;
|
||||
p = sqlite3_malloc((unsigned)nByte);
|
||||
pointerToText(p, zOut);
|
||||
Tcl_AppendResult(interp, zOut, NULL);
|
||||
return TCL_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** Usage: sqlite3_realloc PRIOR NBYTES
|
||||
**
|
||||
** Raw test interface for sqlite3_realloc().
|
||||
*/
|
||||
static int test_realloc(
|
||||
void * clientData,
|
||||
Tcl_Interp *interp,
|
||||
int objc,
|
||||
Tcl_Obj *CONST objv[]
|
||||
){
|
||||
int nByte;
|
||||
void *pPrior, *p;
|
||||
char zOut[100];
|
||||
if( objc!=3 ){
|
||||
Tcl_WrongNumArgs(interp, 1, objv, "PRIOR NBYTES");
|
||||
return TCL_ERROR;
|
||||
}
|
||||
if( Tcl_GetIntFromObj(interp, objv[2], &nByte) ) return TCL_ERROR;
|
||||
if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){
|
||||
Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0);
|
||||
return TCL_ERROR;
|
||||
}
|
||||
p = sqlite3_realloc(pPrior, (unsigned)nByte);
|
||||
pointerToText(p, zOut);
|
||||
Tcl_AppendResult(interp, zOut, NULL);
|
||||
return TCL_OK;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Usage: sqlite3_free PRIOR
|
||||
**
|
||||
** Raw test interface for sqlite3_free().
|
||||
*/
|
||||
static int test_free(
|
||||
void * clientData,
|
||||
Tcl_Interp *interp,
|
||||
int objc,
|
||||
Tcl_Obj *CONST objv[]
|
||||
){
|
||||
void *pPrior;
|
||||
if( objc!=2 ){
|
||||
Tcl_WrongNumArgs(interp, 1, objv, "PRIOR");
|
||||
return TCL_ERROR;
|
||||
}
|
||||
if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){
|
||||
Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0);
|
||||
return TCL_ERROR;
|
||||
}
|
||||
sqlite3_free(pPrior);
|
||||
return TCL_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** Usage: sqlite3_memory_used
|
||||
**
|
||||
** Raw test interface for sqlite3_memory_used().
|
||||
*/
|
||||
static int test_memory_used(
|
||||
void * clientData,
|
||||
Tcl_Interp *interp,
|
||||
int objc,
|
||||
Tcl_Obj *CONST objv[]
|
||||
){
|
||||
Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3_memory_used()));
|
||||
return TCL_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** Usage: sqlite3_memory_highwater ?RESETFLAG?
|
||||
**
|
||||
** Raw test interface for sqlite3_memory_highwater().
|
||||
*/
|
||||
static int test_memory_highwater(
|
||||
void * clientData,
|
||||
Tcl_Interp *interp,
|
||||
int objc,
|
||||
Tcl_Obj *CONST objv[]
|
||||
){
|
||||
int resetFlag = 0;
|
||||
if( objc!=1 && objc!=2 ){
|
||||
Tcl_WrongNumArgs(interp, 1, objv, "?RESET?");
|
||||
return TCL_ERROR;
|
||||
}
|
||||
if( objc==2 ){
|
||||
if( Tcl_GetBooleanFromObj(interp, objv[1], &resetFlag) ) return TCL_ERROR;
|
||||
}
|
||||
Tcl_SetObjResult(interp,
|
||||
Tcl_NewWideIntObj(sqlite3_memory_highwater(resetFlag)));
|
||||
return TCL_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** Usage: sqlite3_memdebug_backtrace DEPTH
|
||||
**
|
||||
** Set the depth of backtracing. If SQLITE_MEMDEBUG is not defined
|
||||
** then this routine is a no-op.
|
||||
*/
|
||||
static int test_memdebug_backtrace(
|
||||
void * clientData,
|
||||
Tcl_Interp *interp,
|
||||
int objc,
|
||||
Tcl_Obj *CONST objv[]
|
||||
){
|
||||
int depth;
|
||||
if( objc!=2 ){
|
||||
Tcl_WrongNumArgs(interp, 1, objv, "DEPT");
|
||||
return TCL_ERROR;
|
||||
}
|
||||
if( Tcl_GetIntFromObj(interp, objv[1], &depth) ) return TCL_ERROR;
|
||||
#ifdef SQLITE_MEMDEBUG
|
||||
{
|
||||
extern void sqlite3_memdebug_backtrace(int);
|
||||
sqlite3_memdebug_backtrace(depth);
|
||||
}
|
||||
#endif
|
||||
return TCL_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** Usage: sqlite3_memdebug_dump FILENAME
|
||||
**
|
||||
** Write a summary of unfreed memory to FILENAME.
|
||||
*/
|
||||
static int test_memdebug_dump(
|
||||
void * clientData,
|
||||
Tcl_Interp *interp,
|
||||
int objc,
|
||||
Tcl_Obj *CONST objv[]
|
||||
){
|
||||
if( objc!=2 ){
|
||||
Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
|
||||
return TCL_ERROR;
|
||||
}
|
||||
#ifdef SQLITE_MEMDEBUG
|
||||
{
|
||||
extern void sqlite3_memdebug_dump(const char*);
|
||||
sqlite3_memdebug_dump(Tcl_GetString(objv[1]));
|
||||
}
|
||||
#endif
|
||||
return TCL_OK;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Register commands with the TCL interpreter.
|
||||
*/
|
||||
int Sqlitetest_malloc_Init(Tcl_Interp *interp){
|
||||
static struct {
|
||||
char *zName;
|
||||
Tcl_ObjCmdProc *xProc;
|
||||
} aObjCmd[] = {
|
||||
{ "sqlite3_malloc", test_malloc },
|
||||
{ "sqlite3_realloc", test_realloc },
|
||||
{ "sqlite3_free", test_free },
|
||||
{ "sqlite3_memory_used", test_memory_used },
|
||||
{ "sqlite3_memory_highwater", test_memory_highwater },
|
||||
{ "sqlite3_memdebug_backtrace", test_memdebug_backtrace },
|
||||
{ "sqlite3_memdebug_dump", test_memdebug_dump },
|
||||
};
|
||||
int i;
|
||||
for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
|
||||
Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
|
||||
}
|
||||
return TCL_OK;
|
||||
}
|
Reference in New Issue
Block a user