blob: 478c6c5f8560a5467d0d3ab6c325aca2344f598d [file] [log] [blame]
drh2f999a62007-08-15 19:16:43 +00001/*
2** 2007 August 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file contains code used to implement test interfaces to the
14** memory allocation subsystem.
15**
drhed138fb2007-08-22 22:04:37 +000016** $Id: test_malloc.c,v 1.3 2007/08/22 22:04:37 drh Exp $
drh2f999a62007-08-15 19:16:43 +000017*/
18#include "sqliteInt.h"
19#include "tcl.h"
20#include <stdlib.h>
21#include <string.h>
22#include <assert.h>
23
24/*
25** Transform pointers to text and back again
26*/
27static void pointerToText(void *p, char *z){
28 static const char zHex[] = "0123456789abcdef";
29 int i, k;
30 sqlite3_uint64 n = (sqlite3_uint64)p;
31 for(i=0, k=sizeof(p)*2-1; i<sizeof(p)*2; i++, k--){
32 z[k] = zHex[n&0xf];
33 n >>= 4;
34 }
35 z[sizeof(p)*2] = 0;
36}
37static int hexToInt(int h){
38 if( h>='0' && h<='9' ){
39 return h - '0';
40 }else if( h>='a' && h<='f' ){
41 return h - 'a' + 10;
42 }else{
43 return -1;
44 }
45}
46static int textToPointer(const char *z, void **pp){
47 sqlite3_uint64 n = 0;
48 int i;
49 for(i=0; i<sizeof(void*)*2 && z[0]; i++){
50 int v;
51 v = hexToInt(*z++);
52 if( v<0 ) return TCL_ERROR;
53 n = n*16 + v;
54 }
55 if( *z!=0 ) return TCL_ERROR;
56 *pp = (void*)n;
57 return TCL_OK;
58}
59
60/*
61** Usage: sqlite3_malloc NBYTES
62**
63** Raw test interface for sqlite3_malloc().
64*/
65static int test_malloc(
66 void * clientData,
67 Tcl_Interp *interp,
68 int objc,
69 Tcl_Obj *CONST objv[]
70){
71 int nByte;
72 void *p;
73 char zOut[100];
74 if( objc!=2 ){
75 Tcl_WrongNumArgs(interp, 1, objv, "NBYTES");
76 return TCL_ERROR;
77 }
78 if( Tcl_GetIntFromObj(interp, objv[1], &nByte) ) return TCL_ERROR;
79 p = sqlite3_malloc((unsigned)nByte);
80 pointerToText(p, zOut);
81 Tcl_AppendResult(interp, zOut, NULL);
82 return TCL_OK;
83}
84
85/*
86** Usage: sqlite3_realloc PRIOR NBYTES
87**
88** Raw test interface for sqlite3_realloc().
89*/
90static int test_realloc(
91 void * clientData,
92 Tcl_Interp *interp,
93 int objc,
94 Tcl_Obj *CONST objv[]
95){
96 int nByte;
97 void *pPrior, *p;
98 char zOut[100];
99 if( objc!=3 ){
100 Tcl_WrongNumArgs(interp, 1, objv, "PRIOR NBYTES");
101 return TCL_ERROR;
102 }
103 if( Tcl_GetIntFromObj(interp, objv[2], &nByte) ) return TCL_ERROR;
104 if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){
105 Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0);
106 return TCL_ERROR;
107 }
108 p = sqlite3_realloc(pPrior, (unsigned)nByte);
109 pointerToText(p, zOut);
110 Tcl_AppendResult(interp, zOut, NULL);
111 return TCL_OK;
112}
113
114
115/*
116** Usage: sqlite3_free PRIOR
117**
118** Raw test interface for sqlite3_free().
119*/
120static int test_free(
121 void * clientData,
122 Tcl_Interp *interp,
123 int objc,
124 Tcl_Obj *CONST objv[]
125){
126 void *pPrior;
127 if( objc!=2 ){
128 Tcl_WrongNumArgs(interp, 1, objv, "PRIOR");
129 return TCL_ERROR;
130 }
131 if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){
132 Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0);
133 return TCL_ERROR;
134 }
135 sqlite3_free(pPrior);
136 return TCL_OK;
137}
138
139/*
140** Usage: sqlite3_memory_used
141**
142** Raw test interface for sqlite3_memory_used().
143*/
144static int test_memory_used(
145 void * clientData,
146 Tcl_Interp *interp,
147 int objc,
148 Tcl_Obj *CONST objv[]
149){
150 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3_memory_used()));
151 return TCL_OK;
152}
153
154/*
155** Usage: sqlite3_memory_highwater ?RESETFLAG?
156**
157** Raw test interface for sqlite3_memory_highwater().
158*/
159static int test_memory_highwater(
160 void * clientData,
161 Tcl_Interp *interp,
162 int objc,
163 Tcl_Obj *CONST objv[]
164){
165 int resetFlag = 0;
166 if( objc!=1 && objc!=2 ){
167 Tcl_WrongNumArgs(interp, 1, objv, "?RESET?");
168 return TCL_ERROR;
169 }
170 if( objc==2 ){
171 if( Tcl_GetBooleanFromObj(interp, objv[1], &resetFlag) ) return TCL_ERROR;
172 }
173 Tcl_SetObjResult(interp,
174 Tcl_NewWideIntObj(sqlite3_memory_highwater(resetFlag)));
175 return TCL_OK;
176}
177
178/*
179** Usage: sqlite3_memdebug_backtrace DEPTH
180**
181** Set the depth of backtracing. If SQLITE_MEMDEBUG is not defined
182** then this routine is a no-op.
183*/
184static int test_memdebug_backtrace(
185 void * clientData,
186 Tcl_Interp *interp,
187 int objc,
188 Tcl_Obj *CONST objv[]
189){
190 int depth;
191 if( objc!=2 ){
192 Tcl_WrongNumArgs(interp, 1, objv, "DEPT");
193 return TCL_ERROR;
194 }
195 if( Tcl_GetIntFromObj(interp, objv[1], &depth) ) return TCL_ERROR;
196#ifdef SQLITE_MEMDEBUG
197 {
198 extern void sqlite3_memdebug_backtrace(int);
199 sqlite3_memdebug_backtrace(depth);
200 }
201#endif
202 return TCL_OK;
203}
204
205/*
206** Usage: sqlite3_memdebug_dump FILENAME
207**
208** Write a summary of unfreed memory to FILENAME.
209*/
210static int test_memdebug_dump(
211 void * clientData,
212 Tcl_Interp *interp,
213 int objc,
214 Tcl_Obj *CONST objv[]
215){
216 if( objc!=2 ){
217 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
218 return TCL_ERROR;
219 }
220#ifdef SQLITE_MEMDEBUG
221 {
222 extern void sqlite3_memdebug_dump(const char*);
223 sqlite3_memdebug_dump(Tcl_GetString(objv[1]));
224 }
225#endif
226 return TCL_OK;
227}
228
229
230/*
drhed138fb2007-08-22 22:04:37 +0000231** Usage: sqlite3_memdebug_fail COUNTER ?REPEAT?
drh0e6f1542007-08-15 20:41:28 +0000232**
233** Arrange for a simulated malloc() failure after COUNTER successes.
234** If REPEAT is 1 then all subsequent malloc()s fail. If REPEAT is
235** 0 then only a single failure occurs.
236**
237** Each call to this routine overrides the prior counter value.
238** This routine returns the number of simulated failures that have
239** happened since the previous call to this routine.
240**
241** To disable simulated failures, use a COUNTER of -1.
242*/
243static int test_memdebug_fail(
244 void * clientData,
245 Tcl_Interp *interp,
246 int objc,
247 Tcl_Obj *CONST objv[]
248){
249 int iFail;
250 int iRepeat;
251 int nFail = 0;
drhed138fb2007-08-22 22:04:37 +0000252 if( objc!=3 && objc!=2 ){
253 Tcl_WrongNumArgs(interp, 1, objv, "COUNTER ?REPEAT?");
drh0e6f1542007-08-15 20:41:28 +0000254 return TCL_ERROR;
255 }
256 if( Tcl_GetIntFromObj(interp, objv[1], &iFail) ) return TCL_ERROR;
drhed138fb2007-08-22 22:04:37 +0000257 if( objc==3 ){
258 if( Tcl_GetIntFromObj(interp, objv[2], &iRepeat) ) return TCL_ERROR;
259 }else{
260 iRepeat = -1;
261 }
drh0e6f1542007-08-15 20:41:28 +0000262#ifdef SQLITE_MEMDEBUG
263 {
264 extern int sqlite3_memdebug_fail(int,int);
265 nFail = sqlite3_memdebug_fail(iFail, iRepeat);
266 }
267#endif
268 Tcl_SetObjResult(interp, Tcl_NewIntObj(nFail));
269 return TCL_OK;
270}
271
272
273/*
drhed138fb2007-08-22 22:04:37 +0000274** Usage: sqlite3_memdebug_pending
275**
276** Return the number of successful mallocs remaining before the
277** next simulated failure. Return -1 if no simulated failure is
278** currently scheduled.
279*/
280static int test_memdebug_pending(
281 void * clientData,
282 Tcl_Interp *interp,
283 int objc,
284 Tcl_Obj *CONST objv[]
285){
286 extern int sqlite3_memdebug_pending(void);
287 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_memdebug_pending()));
288 return TCL_OK;
289}
290
291
292/*
drh2f999a62007-08-15 19:16:43 +0000293** Register commands with the TCL interpreter.
294*/
295int Sqlitetest_malloc_Init(Tcl_Interp *interp){
296 static struct {
297 char *zName;
298 Tcl_ObjCmdProc *xProc;
299 } aObjCmd[] = {
300 { "sqlite3_malloc", test_malloc },
301 { "sqlite3_realloc", test_realloc },
302 { "sqlite3_free", test_free },
303 { "sqlite3_memory_used", test_memory_used },
304 { "sqlite3_memory_highwater", test_memory_highwater },
305 { "sqlite3_memdebug_backtrace", test_memdebug_backtrace },
306 { "sqlite3_memdebug_dump", test_memdebug_dump },
drh0e6f1542007-08-15 20:41:28 +0000307 { "sqlite3_memdebug_fail", test_memdebug_fail },
drhed138fb2007-08-22 22:04:37 +0000308 { "sqlite3_memdebug_pending", test_memdebug_pending },
drh2f999a62007-08-15 19:16:43 +0000309 };
310 int i;
311 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
312 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
313 }
314 return TCL_OK;
315}