Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Re-enable memory statistics and backtraces.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 75b8ccc0a84cc687d1ab45756f9fe18c76cff5df
User & Date: dan 2013-05-21 18:19:02.713
Context
2013-05-21
20:08
Remove sqlite4_mem_methods from sqlite.h.in. Other modifications so that src4.test works again. check-in: 7091e990cd user: dan tags: trunk
18:19
Re-enable memory statistics and backtraces. check-in: 75b8ccc0a8 user: dan tags: trunk
2013-05-18
19:42
Use the default sqlite4_mm object for malloc() and free(). check-in: e3280f4585 user: dan tags: trunk
Changes
Unified Diff Show Whitespace Changes Patch
Changes to src/env.c.
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
struct sqlite4_env sqlite4DefaultEnv = {
   sizeof(sqlite4_env),       /* nByte */
   1,                         /* iVersion */
   SQLITE4_DEFAULT_MEMSTATUS, /* bMemstat */
   1,                         /* bCoreMutex */
   SQLITE4_THREADSAFE==1,     /* bFullMutex */
   0x7ffffffe,                /* mxStrlen */
   128,                       /* szLookaside */
   500,                       /* nLookaside */
   &sqlite4MMSystem,          /* pMM */
#if 0
   {0,0,0,0,0,0,0,0,0},       /* m */
#endif
   {0,0,0,0,0,0,0,0,0,0},     /* mutex */
   (void*)0,                  /* pHeap */
   0,                         /* nHeap */







|
|







38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
struct sqlite4_env sqlite4DefaultEnv = {
   sizeof(sqlite4_env),       /* nByte */
   1,                         /* iVersion */
   SQLITE4_DEFAULT_MEMSTATUS, /* bMemstat */
   1,                         /* bCoreMutex */
   SQLITE4_THREADSAFE==1,     /* bFullMutex */
   0x7ffffffe,                /* mxStrlen */
   0,                         /* szLookaside */
   0,                         /* nLookaside */
   &sqlite4MMSystem,          /* pMM */
#if 0
   {0,0,0,0,0,0,0,0,0},       /* m */
#endif
   {0,0,0,0,0,0,0,0,0,0},     /* mutex */
   (void*)0,                  /* pHeap */
   0,                         /* nHeap */
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
      *va_arg(ap, sqlite4_mutex_methods*) = pEnv->mutex;
      break;
    }
#endif


    /*
    ** sqlite4_env_config(p, SQLITE4_ENVCONFIG_MALLOC, sqlite4_mem_methods*)
    **
    ** Set the memory allocation routines to be used by this environment.
    */
    case SQLITE4_ENVCONFIG_MALLOC: {
#if 0
      /* Specify an alternative malloc implementation */
      if( pEnv->isInit ) return SQLITE4_MISUSE;
      pEnv->m = *va_arg(ap, sqlite4_mem_methods*);
#endif
      break;
    }

    /*
    ** sqlite4_env_config(p, SQLITE4_ENVCONFIG_GETMALLOC, sqlite4_mem_methods*)
    **
    ** Copy the memory allocation routines in use by this environment
    ** into the structure given in the argument.
    */
    case SQLITE4_ENVCONFIG_GETMALLOC: {
#if 0
      /* Retrieve the current malloc() implementation */
      if( pEnv->m.xMalloc==0 ) sqlite4MemSetDefault(pEnv);
      *va_arg(ap, sqlite4_mem_methods*) = pEnv->m;
#endif
      break;
    }

    /* sqlite4_env_config(p, SQLITE4_ENVCONFIG_MEMSTAT, int onoff);
    **
    ** Enable or disable collection of memory usage statistics according to
    ** the onoff parameter.  







|

|

|
<
<

|
<




|




|
<
<
<
|
<







300
301
302
303
304
305
306
307
308
309
310
311


312
313

314
315
316
317
318
319
320
321
322
323



324

325
326
327
328
329
330
331
      *va_arg(ap, sqlite4_mutex_methods*) = pEnv->mutex;
      break;
    }
#endif


    /*
    ** sqlite4_env_config(p, SQLITE4_ENVCONFIG_SETMM, sqlite4_mm*)
    **
    ** Set the memory allocator to be used by this environment.
    */
    case SQLITE4_ENVCONFIG_SETMM: {


      if( pEnv->isInit ) return SQLITE4_MISUSE;
      pEnv->pMM = va_arg(ap, sqlite4_mm*);

      break;
    }

    /*
    ** sqlite4_env_config(p, SQLITE4_ENVCONFIG_GETMM, sqlite4_mm**)
    **
    ** Copy the memory allocation routines in use by this environment
    ** into the structure given in the argument.
    */
    case SQLITE4_ENVCONFIG_GETMM: {



      *va_arg(ap, sqlite4_mm**) = pEnv->pMM;

      break;
    }

    /* sqlite4_env_config(p, SQLITE4_ENVCONFIG_MEMSTAT, int onoff);
    **
    ** Enable or disable collection of memory usage statistics according to
    ** the onoff parameter.  
Changes to src/mem.c.
281
282
283
284
285
286
287























































































































































































288
289
290
291
292
293
294
    pOvfl->xMemberOfA = pA->pMethods->xMember;
    pOvfl->pA = pA;
    pOvfl->pB = pB;
  }
  return &pOvfl->base;
}

























































































































































































/*************************************************************************
** The SQLITE4_MM_ONESIZE memory allocator.
**
** All memory allocations are rounded up to a single size, "sz".  A request
** for an allocation larger than sz bytes fails.  All allocations come out
** of a single initial buffer with "cnt" chunks of "sz" bytes each.







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
    pOvfl->xMemberOfA = pA->pMethods->xMember;
    pOvfl->pA = pA;
    pOvfl->pB = pB;
  }
  return &pOvfl->base;
}

/*************************************************************************
** The SQLITE4_MM_STATS memory allocator.
*/

/*
** Number of available statistics. Statistics are assigned ids starting at
** one, not zero.
*/
#define MM_STATS_NSTAT 8

struct mmStats {
  sqlite4_mm base;                /* Base class.  Must be first. */
  sqlite4_mm *p;                  /* Underlying allocator object */
  sqlite4_mutex *mutex;           /* Mutex protecting aStat[] (or NULL) */

  i64 nOut;                       /* Number of bytes outstanding */
  i64 nOutHw;                     /* Highwater mark of nOut */
  i64 nUnit;                      /* Number of allocations outstanding */
  i64 nUnitHw;                    /* Highwater mark of nUnit */
  i64 nMaxRequest;                /* Largest request seen so far */
  i64 nFault;                     /* Number of malloc or realloc failures */
};

static void updateStatsMalloc(
  struct mmStats *pStats, 
  void *pNew, 
  sqlite4_size_t iSz
){
  /* Statistic SQLITE4_MMSTAT_SIZE records the largest allocation request
  ** that has been made so far. So if iSz is larger than the current value,
  ** set MMSTAT_SIZE to iSz now. This statistic is updated regardless of 
  ** whether or not the allocation succeeded.  */ 
  if( iSz>pStats->nMaxRequest ){
    pStats->nMaxRequest = iSz;
  }

  /* If the allocation succeeded, increase the number of allocations and
  ** bytes outstanding accordingly. Also update the highwater marks if
  ** required. If the allocation failed, increment the fault count.  */
  if( pNew ){
    pStats->nOut += sqlite4_mm_msize(pStats->p, pNew);
    pStats->nUnit += 1;
    if( pStats->nOut>pStats->nOutHw ) pStats->nOutHw = pStats->nOut;
    if( pStats->nUnit>pStats->nUnitHw ) pStats->nUnitHw = pStats->nUnit;
  }else{
    pStats->nFault++;
  }
}

static void *mmStatsMalloc(sqlite4_mm *pMM, sqlite4_size_t iSz){
  struct mmStats *pStats = (struct mmStats*)pMM;
  void *pRet;

  pRet = sqlite4_mm_malloc(pStats->p, iSz);
  sqlite4_mutex_enter(pStats->mutex);
  updateStatsMalloc(pStats, pRet, iSz);
  sqlite4_mutex_leave(pStats->mutex);
  return pRet;
}

static void mmStatsFree(sqlite4_mm *pMM, void *pOld){
  struct mmStats *pStats = (struct mmStats*)pMM;
  sqlite4_mutex_enter(pStats->mutex);
  if( pOld ){
    sqlite4_size_t nByte = sqlite4_mm_msize(pMM, pOld);
    pStats->nOut -= nByte;
    pStats->nUnit -= 1;
  }
  sqlite4_mutex_leave(pStats->mutex);
  sqlite4_mm_free(pStats->p, pOld);
}

static void *mmStatsRealloc(sqlite4_mm *pMM, void *pOld, sqlite4_size_t iSz){
  struct mmStats *pStats = (struct mmStats*)pMM;
  sqlite4_size_t nOrig = (pOld ? sqlite4_mm_msize(pStats->p, pOld) : 0);
  void *pRet;

  pRet = sqlite4_mm_realloc(pStats->p, pOld, iSz);

  sqlite4_mutex_enter(pStats->mutex);
  if( pRet ){
    pStats->nOut -= nOrig;
    if( pOld ) pStats->nUnit--;
  }
  updateStatsMalloc(pStats, pRet, iSz);
  sqlite4_mutex_leave(pStats->mutex);

  return pRet;
}

static sqlite4_size_t mmStatsMsize(sqlite4_mm *pMM, void *pOld){
  struct mmStats *pStats = (struct mmStats*)pMM;
  return sqlite4_mm_msize(pStats->p, pOld);
}

static int mmStatsMember(sqlite4_mm *pMM, const void *pOld){
  struct mmStats *pStats = (struct mmStats*)pMM;
  return sqlite4_mm_member(pStats->p, pOld);
}


static sqlite4_int64 mmStatsStat(
  sqlite4_mm *pMM, 
  unsigned int eType, 
  unsigned int flags
){
  struct mmStats *pStats = (struct mmStats*)pMM;
  i64 iRet = 0;
  sqlite4_mutex_enter(pStats->mutex);
  switch( eType ){
    case SQLITE4_MMSTAT_OUT: {
      iRet = pStats->nOut;
      break;
    }
    case SQLITE4_MMSTAT_OUT_HW: {
      iRet = pStats->nOutHw;
      if( flags & SQLITE4_MMSTAT_RESET ) pStats->nOutHw = pStats->nOut;
      break;
    }
    case SQLITE4_MMSTAT_UNITS: {
      iRet = pStats->nUnit;
      break;
    }
    case SQLITE4_MMSTAT_UNITS_HW: {
      iRet = pStats->nUnitHw;
      if( flags & SQLITE4_MMSTAT_RESET ) pStats->nUnitHw = pStats->nUnit;
      break;
    }
    case SQLITE4_MMSTAT_SIZE: {
      iRet = pStats->nMaxRequest;
      if( flags & SQLITE4_MMSTAT_RESET ) pStats->nMaxRequest = 0;
      break;
    }
    case SQLITE4_MMSTAT_MEMFAULT:
    case SQLITE4_MMSTAT_FAULT: {
      iRet = pStats->nFault;
      if( flags & SQLITE4_MMSTAT_RESET ) pStats->nFault = 0;
      break;
    }
  }
  sqlite4_mutex_leave(pStats->mutex);
  return iRet;
}

/*
** Destroy the allocator object passed as the first argument.
*/
static void mmStatsFinal(sqlite4_mm *pMM){
  struct mmStats *pStats = (struct mmStats*)pMM;
  sqlite4_mm *p = pStats->p;
  sqlite4_mm_free(p, pStats);
  sqlite4_mm_destroy(p);
}

static const sqlite4_mm_methods mmStatsMethods = {
  /* iVersion */    1,
  /* xMalloc  */    mmStatsMalloc,
  /* xRealloc */    mmStatsRealloc,
  /* xFree    */    mmStatsFree,
  /* xMsize   */    mmStatsMsize,
  /* xMember  */    mmStatsMember,
  /* xBenign  */    0,
  /* xStat    */    mmStatsStat,
  /* xCtrl    */    0,
  /* xFinal   */    mmStatsFinal
};

/*
** Allocate a new stats allocator.
*/
static sqlite4_mm *mmStatsNew(sqlite4_mm *p){
  struct mmStats *pNew;

  pNew = (struct mmStats *)sqlite4_mm_malloc(p, sizeof(*pNew));
  if( pNew ){
    memset(pNew, 0, sizeof(*pNew));
    pNew->p = p;
    pNew->base.pMethods = &mmStatsMethods;
  }

  return (sqlite4_mm *)pNew;
}


/*************************************************************************
** The SQLITE4_MM_ONESIZE memory allocator.
**
** All memory allocations are rounded up to a single size, "sz".  A request
** for an allocation larger than sz bytes fails.  All allocations come out
** of a single initial buffer with "cnt" chunks of "sz" bytes each.
350
351
352
353
354
355
356
357




358
359
360
361
362
363
364
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  return pOld ? pOnesz->sz : 0;  
}
static int mmOneszMember(sqlite4_mm *pMM, const void *pOld){
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  return pOld && pOld>=pOnesz->pSpace && pOld<=pOnesz->pLast;
}
static sqlite4_int64 mmOneszStat(sqlite4_mm *pMM, int eType, unsigned flgs){




  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  sqlite4_int64 x = -1;
  switch( eType ){
    case SQLITE4_MMSTAT_OUT: {
      x = pOnesz->nUsed*pOnesz->sz;
      break;
    }







|
>
>
>
>







533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  return pOld ? pOnesz->sz : 0;  
}
static int mmOneszMember(sqlite4_mm *pMM, const void *pOld){
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  return pOld && pOld>=pOnesz->pSpace && pOld<=pOnesz->pLast;
}
static sqlite4_int64 mmOneszStat(
  sqlite4_mm *pMM, 
  unsigned int eType, 
  unsigned int flgs
){
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  sqlite4_int64 x = -1;
  switch( eType ){
    case SQLITE4_MMSTAT_OUT: {
      x = pOnesz->nUsed*pOnesz->sz;
      break;
    }
510
511
512
513
514
515
516





517
518
519
520
521
522
523
524
525
    }
    case SQLITE4_MM_ONESIZE: {
      void *pSpace = va_arg(ap, void*);
      int sz = va_arg(ap, int);
      int cnt = va_arg(ap, int);
      pMM = mmOneszNew(pSpace, sz, cnt);
      break;





    }
    default: {
      pMM = 0;
      break;
    }
  }
  va_end(ap);
  return pMM;
}







>
>
>
>
>









697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
    }
    case SQLITE4_MM_ONESIZE: {
      void *pSpace = va_arg(ap, void*);
      int sz = va_arg(ap, int);
      int cnt = va_arg(ap, int);
      pMM = mmOneszNew(pSpace, sz, cnt);
      break;
    }
    case SQLITE4_MM_STATS: {
      sqlite4_mm *p = va_arg(ap, sqlite4_mm*);
      pMM = mmStatsNew(p);
      break;
    }
    default: {
      pMM = 0;
      break;
    }
  }
  va_end(ap);
  return pMM;
}
Changes to src/prepare.c.
592
593
594
595
596
597
598



599
600
601
602
603
604
605
  Vdbe *pOld,               /* VM being reprepared */
  sqlite4_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
  int *pnUsed               /* OUT: Bytes read from zSql */
){
  int rc;
  assert( ppStmt!=0 );
  *ppStmt = 0;



  if( !sqlite4SafetyCheckOk(db) ){
    return SQLITE4_MISUSE_BKPT;
  }
  sqlite4_mutex_enter(db->mutex);
  rc = sqlite4Prepare(db, zSql, nBytes, pOld, ppStmt, pnUsed);
  if( rc==SQLITE4_SCHEMA ){
    sqlite4_finalize(*ppStmt);







>
>
>







592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  Vdbe *pOld,               /* VM being reprepared */
  sqlite4_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
  int *pnUsed               /* OUT: Bytes read from zSql */
){
  int rc;
  assert( ppStmt!=0 );
  *ppStmt = 0;
  if( pnUsed ){
    *pnUsed = 0;
  }
  if( !sqlite4SafetyCheckOk(db) ){
    return SQLITE4_MISUSE_BKPT;
  }
  sqlite4_mutex_enter(db->mutex);
  rc = sqlite4Prepare(db, zSql, nBytes, pOld, ppStmt, pnUsed);
  if( rc==SQLITE4_SCHEMA ){
    sqlite4_finalize(*ppStmt);
Changes to src/sqlite.h.in.
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
**
** If pOld is not a valid memory allocation or is a memory allocation that
** has previously been freed, then the result of this routine is undefined.
*/
int sqlite4_mm_member(sqlite4_mm *pMM, const void *pOld);

/*
** Allowed values for the second parameter ("eType") to sqlite4_mm_type().
*/
#define SQLITE4_MMSTAT_OUT        1
#define SQLITE4_MMSTAT_OUT_HW     2
#define SQLITE4_MMSTAT_UNITS      3
#define SQLITE4_MMSTAT_UNITS_HW   4
#define SQLITE4_MMSTAT_SIZE       5
#define SQLITE4_MMSTAT_SZFAULT    6







|







166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
**
** If pOld is not a valid memory allocation or is a memory allocation that
** has previously been freed, then the result of this routine is undefined.
*/
int sqlite4_mm_member(sqlite4_mm *pMM, const void *pOld);

/*
** Allowed values for the second parameter ("eType") to sqlite4_mm_stat().
*/
#define SQLITE4_MMSTAT_OUT        1
#define SQLITE4_MMSTAT_OUT_HW     2
#define SQLITE4_MMSTAT_UNITS      3
#define SQLITE4_MMSTAT_UNITS_HW   4
#define SQLITE4_MMSTAT_SIZE       5
#define SQLITE4_MMSTAT_SZFAULT    6
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
** a memory allocator is unavailable, then -1 is returned.
*/
sqlite4_int64 sqlite4_mm_stat(sqlite4_mm *pMM, int eType, unsigned flags);

/*
** Send a control message into a memory allocator.
*/
int sqlit4_mm_control(sqlite4_mm *pMM, int eType, ...);

/*
** Enable or disable benign failure mode.  Benign failure mode can be
** nested.  In benign failure mode, OOM errors do not necessarily propagate
** back out to the application but can be dealt with internally.  Memory
** allocations that occur in benign failure mode are considered "optional".
*/







|







193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
** a memory allocator is unavailable, then -1 is returned.
*/
sqlite4_int64 sqlite4_mm_stat(sqlite4_mm *pMM, int eType, unsigned flags);

/*
** Send a control message into a memory allocator.
*/
int sqlite4_mm_control(sqlite4_mm *pMM, int eType, ...);

/*
** Enable or disable benign failure mode.  Benign failure mode can be
** nested.  In benign failure mode, OOM errors do not necessarily propagate
** back out to the application but can be dealt with internally.  Memory
** allocations that occur in benign failure mode are considered "optional".
*/
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
*/
#define SQLITE4_ENVCONFIG_INIT          1   /* size, template */
#define SQLITE4_ENVCONFIG_SINGLETHREAD  2   /* */
#define SQLITE4_ENVCONFIG_MULTITHREAD   3   /* */
#define SQLITE4_ENVCONFIG_SERIALIZED    4   /* */
#define SQLITE4_ENVCONFIG_MUTEX         5   /* sqlite4_mutex_methods* */
#define SQLITE4_ENVCONFIG_GETMUTEX      6   /* sqlite4_mutex_methods* */
#define SQLITE4_ENVCONFIG_MALLOC        7   /* sqlite4_mem_methods* */
#define SQLITE4_ENVCONFIG_GETMALLOC     8   /* sqlite4_mem_methods* */
#define SQLITE4_ENVCONFIG_MEMSTATUS     9   /* boolean */
#define SQLITE4_ENVCONFIG_LOOKASIDE    10   /* size, count */
#define SQLITE4_ENVCONFIG_LOG          11   /* xLog, pArg */
#define SQLITE4_ENVCONFIG_KVSTORE_PUSH 12   /* name, factory */
#define SQLITE4_ENVCONFIG_KVSTORE_POP  13   /* name */
#define SQLITE4_ENVCONFIG_KVSTORE_GET  14   /* name, *factory */








|
|







245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
*/
#define SQLITE4_ENVCONFIG_INIT          1   /* size, template */
#define SQLITE4_ENVCONFIG_SINGLETHREAD  2   /* */
#define SQLITE4_ENVCONFIG_MULTITHREAD   3   /* */
#define SQLITE4_ENVCONFIG_SERIALIZED    4   /* */
#define SQLITE4_ENVCONFIG_MUTEX         5   /* sqlite4_mutex_methods* */
#define SQLITE4_ENVCONFIG_GETMUTEX      6   /* sqlite4_mutex_methods* */
#define SQLITE4_ENVCONFIG_SETMM         7   /* sqlite4_mm* */
#define SQLITE4_ENVCONFIG_GETMM         8   /* sqlite4_mm** */
#define SQLITE4_ENVCONFIG_MEMSTATUS     9   /* boolean */
#define SQLITE4_ENVCONFIG_LOOKASIDE    10   /* size, count */
#define SQLITE4_ENVCONFIG_LOG          11   /* xLog, pArg */
#define SQLITE4_ENVCONFIG_KVSTORE_PUSH 12   /* name, factory */
#define SQLITE4_ENVCONFIG_KVSTORE_POP  13   /* name */
#define SQLITE4_ENVCONFIG_KVSTORE_GET  14   /* name, *factory */

Changes to src/tclsqlite.c.
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
    extern int Sqlitetestrtree_Init(Tcl_Interp*);
    extern int Sqlitequota_Init(Tcl_Interp*);
    extern int SqliteSuperlock_Init(Tcl_Interp*);
    extern int SqlitetestSyscall_Init(Tcl_Interp*);
    extern int Sqliteteststorage_Init(Tcl_Interp*);
    extern int Sqliteteststorage2_Init(Tcl_Interp*);
    extern int SqlitetestLsm_Init(Tcl_Interp*);
    extern int Sqlitetest_mem_Init(Tcl_Interp*);

    extern void sqlite4TestInit(Tcl_Interp*);

    Sqliteconfig_Init(interp);
    Sqlitetest1_Init(interp);
    Sqlitetest4_Init(interp);
    Sqlitetest5_Init(interp);
    Sqlitetest9_Init(interp);
    Sqlitetest_hexio_Init(interp);
    Sqlitetest_malloc_Init(interp);
    Sqlitetest_mutex_Init(interp);
    SqlitetestThread_Init(interp);
    Sqliteteststorage_Init(interp);
    Sqliteteststorage2_Init(interp);
    SqlitetestLsm_Init(interp);
    Sqlitetest_mem_Init(interp);
    sqlite4TestInit(interp);


    Tcl_CreateObjCommand(
        interp, "load_testfixture_extensions", init_all_cmd, 0, 0
    );
    Tcl_CreateObjCommand(
        interp, "db_use_legacy_prepare", db_use_legacy_prepare_cmd, 0, 0
    );







<















<

<







2875
2876
2877
2878
2879
2880
2881

2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896

2897

2898
2899
2900
2901
2902
2903
2904
    extern int Sqlitetestrtree_Init(Tcl_Interp*);
    extern int Sqlitequota_Init(Tcl_Interp*);
    extern int SqliteSuperlock_Init(Tcl_Interp*);
    extern int SqlitetestSyscall_Init(Tcl_Interp*);
    extern int Sqliteteststorage_Init(Tcl_Interp*);
    extern int Sqliteteststorage2_Init(Tcl_Interp*);
    extern int SqlitetestLsm_Init(Tcl_Interp*);


    extern void sqlite4TestInit(Tcl_Interp*);

    Sqliteconfig_Init(interp);
    Sqlitetest1_Init(interp);
    Sqlitetest4_Init(interp);
    Sqlitetest5_Init(interp);
    Sqlitetest9_Init(interp);
    Sqlitetest_hexio_Init(interp);
    Sqlitetest_malloc_Init(interp);
    Sqlitetest_mutex_Init(interp);
    SqlitetestThread_Init(interp);
    Sqliteteststorage_Init(interp);
    Sqliteteststorage2_Init(interp);
    SqlitetestLsm_Init(interp);

    sqlite4TestInit(interp);


    Tcl_CreateObjCommand(
        interp, "load_testfixture_extensions", init_all_cmd, 0, 0
    );
    Tcl_CreateObjCommand(
        interp, "db_use_legacy_prepare", db_use_legacy_prepare_cmd, 0, 0
    );
Changes to test/testInt.h.
21
22
23
24
25
26
27



28
29

/* test_main.c */
void sqlite4TestInit(Tcl_Interp*);
void *sqlite4TestTextToPtr(const char *z);
int sqlite4TestDbHandle(Tcl_Interp *, Tcl_Obj *, sqlite4 **);
int sqlite4TestSetResult(Tcl_Interp *interp, int rc);




#endif








>
>
>


21
22
23
24
25
26
27
28
29
30
31
32

/* test_main.c */
void sqlite4TestInit(Tcl_Interp*);
void *sqlite4TestTextToPtr(const char *z);
int sqlite4TestDbHandle(Tcl_Interp *, Tcl_Obj *, sqlite4 **);
int sqlite4TestSetResult(Tcl_Interp *interp, int rc);

sqlite4_mm *test_mm_debug(sqlite4_mm *p);
void test_mm_debug_report(sqlite4_mm *p, FILE *pFile);

#endif

Changes to test/test_malloc.c.
15
16
17
18
19
20
21


22
23
24
25
26
27
28
*/
#include "sqliteInt.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>



/*
** This structure is used to encapsulate the global state variables used 
** by malloc() fault simulation.
*/
static struct MemFault {
  int iCountdown;         /* Number of pending successes before a failure */
  int nRepeat;            /* Number of times to repeat the failure */







>
>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
*/
#include "sqliteInt.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "testInt.h"

/*
** This structure is used to encapsulate the global state variables used 
** by malloc() fault simulation.
*/
static struct MemFault {
  int iCountdown;         /* Number of pending successes before a failure */
  int nRepeat;            /* Number of times to repeat the failure */
178
179
180
181
182
183
184

185
186
187
188
189
190
191
}

/*
** Add or remove the fault-simulation layer using sqlite4_env_config(). If
** the argument is non-zero, the 
*/
static int faultsimInstall(int install){

  static struct sqlite4_mem_methods m = {
    faultsimMalloc,                   /* xMalloc */
    faultsimFree,                     /* xFree */
    faultsimRealloc,                  /* xRealloc */
    faultsimSize,                     /* xSize */
    faultsimInit,                     /* xInit */
    faultsimShutdown,                 /* xShutdown */







>







180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}

/*
** Add or remove the fault-simulation layer using sqlite4_env_config(). If
** the argument is non-zero, the 
*/
static int faultsimInstall(int install){
#if 0
  static struct sqlite4_mem_methods m = {
    faultsimMalloc,                   /* xMalloc */
    faultsimFree,                     /* xFree */
    faultsimRealloc,                  /* xRealloc */
    faultsimSize,                     /* xSize */
    faultsimInit,                     /* xInit */
    faultsimShutdown,                 /* xShutdown */
223
224
225
226
227
228
229

230
231
232
233
234
235
236
    rc = sqlite4_env_config(0, SQLITE4_ENVCONFIG_MALLOC, &memfault.m);
  }

  if( rc==SQLITE4_OK ){
    memfault.isInstalled = 1;
  }
  return rc;

}

#ifdef SQLITE4_TEST

/*
** This function is implemented in test1.c. Returns a pointer to a static
** buffer containing the symbolic SQLite error code that corresponds to







>







226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    rc = sqlite4_env_config(0, SQLITE4_ENVCONFIG_MALLOC, &memfault.m);
  }

  if( rc==SQLITE4_OK ){
    memfault.isInstalled = 1;
  }
  return rc;
#endif
}

#ifdef SQLITE4_TEST

/*
** This function is implemented in test1.c. Returns a pointer to a static
** buffer containing the symbolic SQLite error code that corresponds to
1116
1117
1118
1119
1120
1121
1122






































































































































1123
1124
1125
1126
1127
1128
1129
#ifdef SQLITE4_ENABLE_MEMSYS3
  const sqlite4_mem_methods *sqlite4MemGetMemsys3(void);
  rc = sqlite4_env_config(0, SQLITE4_ENVCONFIG_MALLOC, sqlite4MemGetMemsys3());
#endif
  Tcl_SetResult(interp, (char *)sqlite4TestErrorName(rc), TCL_VOLATILE);
  return TCL_OK;
}







































































































































/*
** Register commands with the TCL interpreter.
*/
int Sqlitetest_malloc_Init(Tcl_Interp *interp){
  static struct {
     char *zName;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
#ifdef SQLITE4_ENABLE_MEMSYS3
  const sqlite4_mem_methods *sqlite4MemGetMemsys3(void);
  rc = sqlite4_env_config(0, SQLITE4_ENVCONFIG_MALLOC, sqlite4MemGetMemsys3());
#endif
  Tcl_SetResult(interp, (char *)sqlite4TestErrorName(rc), TCL_VOLATILE);
  return TCL_OK;
}

static sqlite4_mm *pMMDebug = 0;

/*
** tclcmd: test_mm_install ?debug? ?backtrace? ?stats?
*/
static int test_mm_install(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  const char *azOpt[] = { 
    "debug",                      /* 0 */
    "backtrace",                  /* 1 */
    "stats",                      /* 2 */
    0 
  };
  sqlite4_mm *pMM = 0;
  int i;
  int bDebug = 0;
  int bBacktrace = 0;
  int bStats = 0;

  for(i=1; i<objc; i++){
    int iOpt = 0;
    int rc = Tcl_GetIndexFromObj(interp, objv[i], azOpt, "option", 0, &iOpt); 
    if( rc!=TCL_OK ) return rc;

    switch( iOpt ){
      case 0:
        bDebug = 1;
        break;
      case 1:
        bBacktrace = 1;
        break;
      case 2:
        bStats = 1;
        break;
    }
  }

  sqlite4_env_config(0, SQLITE4_ENVCONFIG_GETMM, &pMM);
  if( pMM==sqlite4_mm_default() ){
    if( bDebug ){
      pMMDebug = pMM = test_mm_debug(pMM);
    }
    if( bStats ){
      pMM = sqlite4_mm_new(SQLITE4_MM_STATS, pMM);
    }

    sqlite4_env_config(0, SQLITE4_ENVCONFIG_SETMM, pMM);
  }

  return TCL_OK;
}

/*
** tclcmd: test_mm_stat ?-reset? STATISTIC
*/
static int test_mm_stat(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  int flags = 0;
  sqlite4_mm *pMM = 0;
  sqlite4_int64 iRet = -1;
  Tcl_Obj *pOpt;

  if( objc!=2 && objc!=3 ){
    Tcl_WrongNumArgs(interp, 1, objv, "?-reset? STATISTIC");
    return TCL_ERROR;
  }

  if( objc==3 ){
    const char *azOpt[] = { "-reset", 0 };
    int iOpt = 0;
    int rc = Tcl_GetIndexFromObj(interp, objv[1], azOpt, "option", 0, &iOpt); 
    if( rc!=TCL_OK ) return rc;
    flags = SQLITE4_MMSTAT_RESET;
  }
  pOpt = objv[objc-1];

  sqlite4_env_config(0, SQLITE4_ENVCONFIG_GETMM, &pMM);
  if( pMM ){
    const char *azOpt[] = {
      "out", "out_hw", "units", "units_hw", 
      "size", "szfault", "memfault", "fault", 
      0
    };

    int iOpt = 0;
    int rc = Tcl_GetIndexFromObj(interp, pOpt, azOpt, "option", 0, &iOpt); 
    if( rc!=TCL_OK ) return rc;

    iRet = sqlite4_mm_stat(pMM, iOpt+1, flags);
  }

  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iRet));
  return TCL_OK;
}

/*
** tclcmd: test_mm_report FILENAME
*/
static int test_mm_report(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  FILE *pFile;
  const char *zFile;

  if( objc!=2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "FILE");
    return TCL_ERROR;
  }

  if( pMMDebug ){
    zFile = Tcl_GetString(objv[1]);
    pFile = fopen(zFile, "w");
    if( pFile==0 ){
      Tcl_AppendResult(interp, "Failed to open file: ", zFile, 0);
      return TCL_ERROR;
    }

    test_mm_debug_report(pMMDebug, pFile);
  }
  return TCL_OK;
}


/*
** Register commands with the TCL interpreter.
*/
int Sqlitetest_malloc_Init(Tcl_Interp *interp){
  static struct {
     char *zName;
1146
1147
1148
1149
1150
1151
1152




1153
1154
1155
1156
1157
1158
1159
1160
1161
     { "sqlite4_db_status",              test_db_status                ,0 },
     { "install_malloc_faultsim",        test_install_malloc_faultsim  ,0 },
     { "sqlite4_env_config_memstatus",   test_config_memstatus         ,0 },
     { "sqlite4_envconfig_lookaside",    test_envconfig_lookaside      ,0 },
     { "sqlite4_config_error",           test_config_error             ,0 },
     { "sqlite4_db_config_lookaside",    test_db_config_lookaside      ,0 },
     { "sqlite4_install_memsys3",        test_install_memsys3          ,0 },




  };
  int i;
  for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
    ClientData c = (ClientData)SQLITE4_INT_TO_PTR(aObjCmd[i].clientData);
    Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, c, 0);
  }
  return TCL_OK;
}
#endif







>
>
>
>









1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
     { "sqlite4_db_status",              test_db_status                ,0 },
     { "install_malloc_faultsim",        test_install_malloc_faultsim  ,0 },
     { "sqlite4_env_config_memstatus",   test_config_memstatus         ,0 },
     { "sqlite4_envconfig_lookaside",    test_envconfig_lookaside      ,0 },
     { "sqlite4_config_error",           test_config_error             ,0 },
     { "sqlite4_db_config_lookaside",    test_db_config_lookaside      ,0 },
     { "sqlite4_install_memsys3",        test_install_memsys3          ,0 },

     { "test_mm_install",                test_mm_install               ,0 },
     { "test_mm_stat",                   test_mm_stat                  ,0 },
     { "test_mm_report",                 test_mm_report                ,0 },
  };
  int i;
  for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
    ClientData c = (ClientData)SQLITE4_INT_TO_PTR(aObjCmd[i].clientData);
    Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, c, 0);
  }
  return TCL_OK;
}
#endif
Changes to test/test_mem.c.
9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
**    May you share freely, never taking more than you give.
**
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "sqlite4.h"


#define ArraySize(x) ((int)(sizeof(x) / sizeof((x)[0])))

#define MIN(x,y) ((x)<(y) ? (x) : (y))

typedef unsigned int  u32;
typedef unsigned char u8;







|
>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
**    May you share freely, never taking more than you give.
**
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "sqliteInt.h"
#include "testInt.h"

#define ArraySize(x) ((int)(sizeof(x) / sizeof((x)[0])))

#define MIN(x,y) ((x)<(y) ? (x) : (y))

typedef unsigned int  u32;
typedef unsigned char u8;
32
33
34
35
36
37
38
39
40



41

42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# define backtrace(A,B) 1
# define backtrace_symbols_fd(A,B,C)
#endif


typedef struct TmBlockHdr TmBlockHdr;
typedef struct TmAgg TmAgg;
typedef struct TmGlobal TmGlobal;




struct TmGlobal {

  /* Linked list of all currently outstanding allocations. And a table of

  ** all allocations, past and present, indexed by backtrace() info.  */
  TmBlockHdr *pFirst;
#ifdef TM_BACKTRACE
  TmAgg *aHash[10000];
#endif

  /* Underlying malloc/realloc/free functions */
  sqlite4_mem_methods mem;
};

struct TmBlockHdr {
  TmBlockHdr *pNext;
  TmBlockHdr *pPrev;
  int nByte;
#ifdef TM_BACKTRACE







|

>
>
>
|
>
|
>
|
|

|

<
<
<







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53



54
55
56
57
58
59
60
# define backtrace(A,B) 1
# define backtrace_symbols_fd(A,B,C)
#endif


typedef struct TmBlockHdr TmBlockHdr;
typedef struct TmAgg TmAgg;
typedef struct Testmem Testmem;

/*
** The object that implements the sqlite4_mm interface for this allocator.
*/
struct Testmem {
  sqlite4_mm base;                /* Base class.  Must be first. */
  sqlite4_mm *p;                  /* Underlying allocator object */
  sqlite4_mutex *mutex;           /* Mutex protecting this object (or NULL) */

  TmBlockHdr *pFirst;             /* List of all outstanding allocations */
#ifdef TM_BACKTRACE
  TmAgg *aHash[10000];            /* Table of all allocations by backtrace() */
#endif



};

struct TmBlockHdr {
  TmBlockHdr *pNext;
  TmBlockHdr *pPrev;
  int nByte;
#ifdef TM_BACKTRACE
74
75
76
77
78
79
80




81
82

83
84
85
86
87


88

89
90
91
92



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148



149

150
151
152
153
154
155
156
157
158

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

176
177
178
179
180
181
182



183
184
185
186
187
188
189
190
191
192
193

194
195
196








197




198


199



200
201


202
203
204
205
206
207
208
209
210
211
212
213
214
215




216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

343
344
345
346

347
348





349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#define FOREGUARD 0x80F5E153
#define REARGUARD 0xE4676B53
static const u32 rearguard = REARGUARD;

#define ROUND8(x) (((x)+7)&~7)
#define BLOCK_HDR_SIZE (ROUND8( sizeof(TmBlockHdr) ))





static void tmEnterMutex(TmGlobal *pTm){
  /*pTm->xEnterMutex(pTm);*/

}
static void tmLeaveMutex(TmGlobal *pTm){
  /* pTm->xLeaveMutex(pTm); */
}



static void *tmMalloc(TmGlobal *pTm, int nByte){

  TmBlockHdr *pNew;               /* New allocation header block */
  u8 *pUser;                      /* Return value */
  int nReq;                       /* Total number of bytes requested */




  assert( sizeof(rearguard)==4 );
  nReq = BLOCK_HDR_SIZE + nByte + 4;
  pNew = (TmBlockHdr *)pTm->mem.xMalloc(pTm->mem.pMemEnv, nReq);
  memset(pNew, 0, sizeof(TmBlockHdr));

  tmEnterMutex(pTm);

  pNew->iForeGuard = FOREGUARD;
  pNew->nByte = nByte;
  pNew->pNext = pTm->pFirst;

  if( pTm->pFirst ){
    pTm->pFirst->pPrev = pNew;
  }
  pTm->pFirst = pNew;

  pUser = &((u8 *)pNew)[BLOCK_HDR_SIZE];
  memset(pUser, 0x56, nByte);
  memcpy(&pUser[nByte], &rearguard, 4);

#ifdef TM_BACKTRACE
  {
    TmAgg *pAgg;
    int i;
    u32 iHash = 0;
    void *aFrame[TM_BACKTRACE];
    memset(aFrame, 0, sizeof(aFrame));
    backtrace(aFrame, TM_BACKTRACE);

    for(i=0; i<ArraySize(aFrame); i++){
      iHash += (u64)(aFrame[i]) + (iHash<<3);
    }
    iHash = iHash % ArraySize(pTm->aHash);

    for(pAgg=pTm->aHash[iHash]; pAgg; pAgg=pAgg->pNext){
      if( memcmp(pAgg->aFrame, aFrame, sizeof(aFrame))==0 ) break;
    }
    if( !pAgg ){
      pAgg = (TmAgg *)pTm->mem.xMalloc(pTm->mem.pMemEnv, sizeof(TmAgg));
      memset(pAgg, 0, sizeof(TmAgg));
      memcpy(pAgg->aFrame, aFrame, sizeof(aFrame));
      pAgg->pNext = pTm->aHash[iHash];
      pTm->aHash[iHash] = pAgg;
    }
    pAgg->nAlloc++;
    pAgg->nByte += nByte;
    pAgg->nOutAlloc++;
    pAgg->nOutByte += nByte;
    pNew->pAgg = pAgg;
  }
#endif

  tmLeaveMutex(pTm);
  return pUser;
}




static void tmFree(TmGlobal *pTm, void *p){

  if( p ){
    TmBlockHdr *pHdr;
    u8 *pUser = (u8 *)p;

    tmEnterMutex(pTm);
    pHdr = (TmBlockHdr *)&pUser[BLOCK_HDR_SIZE * -1];
    assert( pHdr->iForeGuard==FOREGUARD );
    assert( 0==memcmp(&pUser[pHdr->nByte], &rearguard, 4) );


    if( pHdr->pPrev ){
      assert( pHdr->pPrev->pNext==pHdr );
      pHdr->pPrev->pNext = pHdr->pNext;
    }else{
      assert( pHdr==pTm->pFirst );
      pTm->pFirst = pHdr->pNext;
    }
    if( pHdr->pNext ){
      assert( pHdr->pNext->pPrev==pHdr );
      pHdr->pNext->pPrev = pHdr->pPrev;
    }

#ifdef TM_BACKTRACE
    pHdr->pAgg->nOutAlloc--;
    pHdr->pAgg->nOutByte -= pHdr->nByte;
#endif


    tmLeaveMutex(pTm);
    memset(pUser, 0x58, pHdr->nByte);
    memset(pHdr, 0x57, sizeof(TmBlockHdr));
    pTm->mem.xFree(pTm->mem.pMemEnv, pHdr);
  }
}




static void *tmRealloc(TmGlobal *pTm, void *p, int nByte){
  void *pNew;

  pNew = tmMalloc(pTm, nByte);
  if( pNew && p ){
    TmBlockHdr *pHdr;
    u8 *pUser = (u8 *)p;
    pHdr = (TmBlockHdr *)&pUser[BLOCK_HDR_SIZE * -1];
    memcpy(pNew, p, MIN(nByte, pHdr->nByte));
    tmFree(pTm, p);
  }

  return pNew;
}









static void tmMallocCheck(




  TmGlobal *pTm,


  int *pnLeakAlloc,



  int *pnLeakByte,
  FILE *pFile


){
  TmBlockHdr *pHdr;
  int nLeak = 0;
  int nByte = 0;

  if( pTm==0 ) return;

  for(pHdr=pTm->pFirst; pHdr; pHdr=pHdr->pNext){
    nLeak++; 
    nByte += pHdr->nByte;
  }
  if( pnLeakAlloc ) *pnLeakAlloc = nLeak;
  if( pnLeakByte ) *pnLeakByte = nByte;





#ifdef TM_BACKTRACE
  if( pFile ){
    int i;
    fprintf(pFile, "LEAKS\n");
    for(i=0; i<ArraySize(pTm->aHash); i++){
      TmAgg *pAgg;
      for(pAgg=pTm->aHash[i]; pAgg; pAgg=pAgg->pNext){
        if( pAgg->nOutAlloc ){
          int j;
          fprintf(pFile, "%d %d ", pAgg->nOutByte, pAgg->nOutAlloc);
          for(j=0; j<TM_BACKTRACE; j++){
            fprintf(pFile, "%p ", pAgg->aFrame[j]);
          }
          fprintf(pFile, "\n");
        }
      }
    }
    fprintf(pFile, "\nALLOCATIONS\n");
    for(i=0; i<ArraySize(pTm->aHash); i++){
      TmAgg *pAgg;
      for(pAgg=pTm->aHash[i]; pAgg; pAgg=pAgg->pNext){
        int j;
        fprintf(pFile, "%d %d ", pAgg->nByte, pAgg->nAlloc);
        for(j=0; j<TM_BACKTRACE; j++) fprintf(pFile, "%p ", pAgg->aFrame[j]);
        fprintf(pFile, "\n");
      }
    }
  }
#else
  (void)pFile;

#endif
}


static void *tmLsmEnvXMalloc(void *p, sqlite4_size_t n){
  return tmMalloc( (TmGlobal*) p, (int)n );
}

static void tmLsmEnvXFree(void *p, void *ptr){ 
  tmFree( (TmGlobal *)p, ptr );
}

static void *tmLsmEnvXRealloc(void *ptr, void * mem, int n){
  return tmRealloc((TmGlobal*)ptr, mem, n);
}


static sqlite4_size_t tmLsmXSize(void *p, void *ptr){
  if(NULL==ptr){
    return 0;
  }else{
    unsigned char * pUc = (unsigned char *)ptr;
    TmBlockHdr * pBlock = (TmBlockHdr*)(pUc-BLOCK_HDR_SIZE);
    assert( pBlock->nByte > 0 );
    return (sqlite4_size_t) pBlock->nByte;
  }
}

static int tmInitStub(void* ignored){
  assert("Set breakpoint here.");
  return 0;
}
static void tmVoidStub(void* ignored){}


int testMallocInstall(sqlite4_env *pEnv){
  TmGlobal *pGlobal;              /* Object containing allocation hash */
  sqlite4_mem_methods allocator;  /* This malloc system */
  sqlite4_mem_methods orig;       /* Underlying malloc system */

  /* Allocate and populate a TmGlobal structure. sqlite4_malloc cannot be
  ** used to allocate the TmGlobal struct as this would cause the environment
  ** to move to "initialized" state and the SQLITE4_ENVCONFIG_MALLOC 
  ** to fail. */
  sqlite4_env_config(pEnv, SQLITE4_ENVCONFIG_GETMALLOC, &orig);
  pGlobal = (TmGlobal *)orig.xMalloc(orig.pMemEnv, sizeof(TmGlobal));
  memset(pGlobal, 0, sizeof(TmGlobal));
  memcpy(&pGlobal->mem, &orig, sizeof(orig));

  /* Set up pEnv to the use the new TmGlobal */
  allocator.xRealloc = tmLsmEnvXRealloc;
  allocator.xMalloc = tmLsmEnvXMalloc;
  allocator.xFree = tmLsmEnvXFree;
  allocator.xSize = tmLsmXSize;
  allocator.xInit = tmInitStub;
  allocator.xShutdown = tmVoidStub;
  allocator.xBeginBenign = tmVoidStub;
  allocator.xEndBenign = tmVoidStub;
  allocator.pMemEnv = pGlobal;
  return sqlite4_env_config(pEnv, SQLITE4_ENVCONFIG_MALLOC, &allocator);
}

int testMallocUninstall(sqlite4_env *pEnv){
  TmGlobal *pGlobal;              /* Object containing allocation hash */
  sqlite4_mem_methods allocator;  /* This malloc system */
  int rc;

  sqlite4_env_config(pEnv, SQLITE4_ENVCONFIG_GETMALLOC, &allocator);
  assert( allocator.xMalloc==tmLsmEnvXMalloc );
  pGlobal = (TmGlobal *)allocator.pMemEnv;

  rc = sqlite4_env_config(pEnv, SQLITE4_ENVCONFIG_MALLOC, &pGlobal->mem);
  if( rc==SQLITE4_OK ){
    sqlite4_free(pEnv, pGlobal);
  }
  return rc;
}

void testMallocCheck(
  sqlite4_env *pEnv,
  int *pnLeakAlloc,
  int *pnLeakByte,
  FILE *pFile
){
  TmGlobal *pGlobal;
  sqlite4_mem_methods allocator;  /* This malloc system */

  sqlite4_env_config(pEnv, SQLITE4_ENVCONFIG_GETMALLOC, &allocator);
  assert( allocator.xMalloc==tmLsmEnvXMalloc );
  pGlobal = (TmGlobal *)allocator.pMemEnv;

  tmMallocCheck(pGlobal, pnLeakAlloc, pnLeakByte, pFile);
}

#include <tcl.h>

/*

** testmem install
** testmem uninstall
** testmem report ?FILENAME?
*/

static int testmem_cmd(
  void * clientData,





  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite4_env *pEnv;              /* SQLite 4 environment to work with */
  int iOpt;
  const char *azSub[] = {"install", "uninstall", "report", 0};

  if( objc<2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "sub-command");
    return TCL_ERROR;
  }
  if( Tcl_GetIndexFromObj(interp, objv[1], azSub, "sub-command", 0, &iOpt) ){
    return TCL_ERROR;
  }

  pEnv = sqlite4_env_default();
  switch( iOpt ){

    case 0: {
      int rc;
      if( objc!=2 ){
        Tcl_WrongNumArgs(interp, 2, objv, "");
        return TCL_ERROR;
      }
      rc = testMallocInstall(pEnv);
      if( rc!=SQLITE4_OK ){
        Tcl_AppendResult(interp, "Failed to install testmem wrapper", 0);
        return TCL_ERROR;
      }
      break;
    }

    case 1:
      if( objc!=2 ){
        Tcl_WrongNumArgs(interp, 2, objv, "");
        return TCL_ERROR;
      }
      testMallocUninstall(pEnv);
      break;

    case 2: {
      int nLeakAlloc = 0;
      int nLeakByte = 0;
      FILE *pReport = 0;
      Tcl_Obj *pRes;

      if( objc!=2 && objc!=3 ){
        Tcl_WrongNumArgs(interp, 2, objv, "?filename?");
        return TCL_ERROR;
      }
      if( objc==3 ){
        const char *zFile = Tcl_GetString(objv[2]);
        pReport = fopen(zFile, "w");
        if( !pReport ){
          Tcl_AppendResult(interp, "Failed to open file: ", zFile, 0);
          return TCL_ERROR;
        }
      }

      testMallocCheck(pEnv, &nLeakAlloc, &nLeakByte, pReport);
      if( pReport ) fclose(pReport);

      pRes = Tcl_NewObj();
      Tcl_ListObjAppendElement(interp, pRes, Tcl_NewIntObj(nLeakAlloc));
      Tcl_ListObjAppendElement(interp, pRes, Tcl_NewIntObj(nLeakByte));
      Tcl_SetObjResult(interp, pRes);
      break;
    }
  }

  return TCL_OK;
}

int Sqlitetest_mem_Init(Tcl_Interp *interp){
  Tcl_CreateObjCommand(interp, "testmem", testmem_cmd, 0, 0);
  return TCL_OK;
}









>
>
>
>
|
<
>

<
<
|
|
>
>
|
>




>
>
>


|


|



|

|
|

|

















|

|



|


|
|









|



>
>
>
|
>

|


|
|



>




|
|











>
|


|



>
>
>
|


|

<
|
<
|
|

>



>
>
>
>
>
>
>
>
|
>
>
>
>
|
>
>
|
>
>
>
|
|
>
>

<
<
<
|
|
|
<
<
<
|
<
<
|
>
>
>
>

<


|

|











|

|






<


>



|
<
<
<
|
<
<
<
|
<
<
<
|
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
|
<
<
<
<
<
|
<
<
<

>
|
<
<

>
|
|
>
>
>
>
>
|
|
|
<
<
<
<
|
<
<
<
|
<
<
<
|
|
|
>
|
|
<
<
<

<
<
<
|

<
<
<
<
<
<
<
|
<
<
|
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<



77
78
79
80
81
82
83
84
85
86
87
88

89
90


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

208

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240



241
242
243



244


245
246
247
248
249
250

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

276
277
278
279
280
281
282



283



284



285
286










287





288













































289






290





291



292
293
294


295
296
297
298
299
300
301
302
303
304
305
306




307



308



309
310
311
312
313
314



315



316
317







318


319





320












321


322














323
324
325
#define FOREGUARD 0x80F5E153
#define REARGUARD 0xE4676B53
static const u32 rearguard = REARGUARD;

#define ROUND8(x) (((x)+7)&~7)
#define BLOCK_HDR_SIZE (ROUND8( sizeof(TmBlockHdr) ))

/*
** Given a user data pointer, return a pointer to the associated 
** TmBlockHdr structure.
*/
static TmBlockHdr *userToBlock(const void *p){

  return (TmBlockHdr *)(((const u8 *)p) - BLOCK_HDR_SIZE);
}



/*
** sqlite4_mm_methods.xMalloc method.
*/
static void *mmDebugMalloc(sqlite4_mm *pMM, sqlite4_size_t nByte){
  Testmem *pTest = (Testmem *)pMM;
  TmBlockHdr *pNew;               /* New allocation header block */
  u8 *pUser;                      /* Return value */
  int nReq;                       /* Total number of bytes requested */

  /* Allocate space for the users allocation, the TmBlockHdr object that
  ** located immediately before the users allocation in memory, and the
  ** 4-byte 'rearguard' located immediately following it. */
  assert( sizeof(rearguard)==4 );
  nReq = BLOCK_HDR_SIZE + nByte + 4;
  pNew = (TmBlockHdr *)sqlite4_mm_malloc(pTest->p, nReq);
  memset(pNew, 0, sizeof(TmBlockHdr));

  sqlite4_mutex_enter(pTest->mutex);

  pNew->iForeGuard = FOREGUARD;
  pNew->nByte = nByte;
  pNew->pNext = pTest->pFirst;

  if( pTest->pFirst ){
    pTest->pFirst->pPrev = pNew;
  }
  pTest->pFirst = pNew;

  pUser = &((u8 *)pNew)[BLOCK_HDR_SIZE];
  memset(pUser, 0x56, nByte);
  memcpy(&pUser[nByte], &rearguard, 4);

#ifdef TM_BACKTRACE
  {
    TmAgg *pAgg;
    int i;
    u32 iHash = 0;
    void *aFrame[TM_BACKTRACE];
    memset(aFrame, 0, sizeof(aFrame));
    backtrace(aFrame, TM_BACKTRACE);

    for(i=0; i<ArraySize(aFrame); i++){
      iHash += (u64)(aFrame[i]) + (iHash<<3);
    }
    iHash = iHash % ArraySize(pTest->aHash);

    for(pAgg=pTest->aHash[iHash]; pAgg; pAgg=pAgg->pNext){
      if( memcmp(pAgg->aFrame, aFrame, sizeof(aFrame))==0 ) break;
    }
    if( !pAgg ){
      pAgg = (TmAgg *)sqlite4_mm_malloc(pTest->p, sizeof(TmAgg));
      memset(pAgg, 0, sizeof(TmAgg));
      memcpy(pAgg->aFrame, aFrame, sizeof(aFrame));
      pAgg->pNext = pTest->aHash[iHash];
      pTest->aHash[iHash] = pAgg;
    }
    pAgg->nAlloc++;
    pAgg->nByte += nByte;
    pAgg->nOutAlloc++;
    pAgg->nOutByte += nByte;
    pNew->pAgg = pAgg;
  }
#endif

  sqlite4_mutex_leave(pTest->mutex);
  return pUser;
}

/*
** sqlite4_mm_methods.xFree method.
*/
static void mmDebugFree(sqlite4_mm *pMM, void *p){
  Testmem *pTest = (Testmem *)pMM;
  if( p ){
    TmBlockHdr *pHdr = userToBlock(p);
    u8 *pUser = (u8 *)p;

    sqlite4_mutex_enter(pTest->mutex);

    assert( pHdr->iForeGuard==FOREGUARD );
    assert( 0==memcmp(&pUser[pHdr->nByte], &rearguard, 4) );

    /* Unlink the TmBlockHdr object from the (Testmem.pFirst) list. */
    if( pHdr->pPrev ){
      assert( pHdr->pPrev->pNext==pHdr );
      pHdr->pPrev->pNext = pHdr->pNext;
    }else{
      assert( pHdr==pTest->pFirst );
      pTest->pFirst = pHdr->pNext;
    }
    if( pHdr->pNext ){
      assert( pHdr->pNext->pPrev==pHdr );
      pHdr->pNext->pPrev = pHdr->pPrev;
    }

#ifdef TM_BACKTRACE
    pHdr->pAgg->nOutAlloc--;
    pHdr->pAgg->nOutByte -= pHdr->nByte;
#endif

    sqlite4_mutex_leave(pTest->mutex);

    memset(pUser, 0x58, pHdr->nByte);
    memset(pHdr, 0x57, sizeof(TmBlockHdr));
    sqlite4_mm_free(pTest->p, pHdr);
  }
}

/*
** sqlite4_mm_methods.xRealloc method.
*/
static void *mmDebugRealloc(sqlite4_mm *pMM, void *p, int nByte){
  void *pNew;

  pNew = sqlite4_mm_malloc(pMM, nByte);
  if( pNew && p ){

    int nOrig = sqlite4_mm_msize(pMM, p);

    memcpy(pNew, p, MIN(nByte, nOrig));
    sqlite4_mm_free(pMM, p);
  }

  return pNew;
}

/*
** sqlite4_mm_methods.xMsize method.
*/
static sqlite4_size_t mmDebugMsize(sqlite4_mm *pMM, void *p){
  if( p==0 ) return 0;
  TmBlockHdr *pHdr = userToBlock(p);
  return pHdr->nByte;
}

/*
** sqlite4_mm_methods.xMember method.
*/
static int mmDebugMember(sqlite4_mm *pMM, const void *p){
  Testmem *pTest = (Testmem *)pMM;
  return sqlite4_mm_member(pTest->p, (const void *)userToBlock(p));
}

/*
** sqlite4_mm_methods.xStat method.
*/
static sqlite4_int64 mmDebugStat(
  sqlite4_mm *pMM, 
  unsigned int eType, 
  unsigned int flags
){



  Testmem *pTest = (Testmem *)pMM;
  return sqlite4_mm_stat(pTest->p, eType, flags);
}






/*
** Write a report of all mallocs, outstanding and otherwise, to the
** file passed as the second argument. 
*/
static void mmDebugReport(Testmem *pTest, FILE *pFile){
#ifdef TM_BACKTRACE

    int i;
    fprintf(pFile, "LEAKS\n");
  for(i=0; i<ArraySize(pTest->aHash); i++){
      TmAgg *pAgg;
    for(pAgg=pTest->aHash[i]; pAgg; pAgg=pAgg->pNext){
        if( pAgg->nOutAlloc ){
          int j;
          fprintf(pFile, "%d %d ", pAgg->nOutByte, pAgg->nOutAlloc);
          for(j=0; j<TM_BACKTRACE; j++){
            fprintf(pFile, "%p ", pAgg->aFrame[j]);
          }
          fprintf(pFile, "\n");
        }
      }
    }
    fprintf(pFile, "\nALLOCATIONS\n");
  for(i=0; i<ArraySize(pTest->aHash); i++){
      TmAgg *pAgg;
    for(pAgg=pTest->aHash[i]; pAgg; pAgg=pAgg->pNext){
        int j;
        fprintf(pFile, "%d %d ", pAgg->nByte, pAgg->nAlloc);
        for(j=0; j<TM_BACKTRACE; j++) fprintf(pFile, "%p ", pAgg->aFrame[j]);
        fprintf(pFile, "\n");
      }
    }

#else
  (void)pFile;
  (void)pTest;
#endif
}

/*



** Destroy the allocator object passed as the first argument.



*/



static void mmDebugFinal(sqlite4_mm *pMM){
  Testmem *pTest = (Testmem *)pMM;










  sqlite4_mm *p = pTest->p;





  sqlite4_mm_free(p, (void *)pTest);













































  sqlite4_mm_destroy(p);






}









/*
** Create a new debug allocator wrapper around the allocator passed as the
** first argument.


*/
sqlite4_mm *test_mm_debug(sqlite4_mm *p){
  static const sqlite4_mm_methods mmDebugMethods = {
    /* iVersion */    1,
    /* xMalloc  */    mmDebugMalloc,
    /* xRealloc */    mmDebugRealloc,
    /* xFree    */    mmDebugFree,
    /* xMsize   */    mmDebugMsize,
    /* xMember  */    mmDebugMember,
    /* xBenign  */    0,
    /* xStat    */    mmDebugStat,
    /* xCtrl    */    0,




    /* xFinal   */    mmDebugFinal



  };



  Testmem *pTest;
  pTest = (Testmem *)sqlite4_mm_malloc(p, sizeof(Testmem));
  if( pTest ){
    memset(pTest, 0, sizeof(Testmem));
    pTest->base.pMethods = &mmDebugMethods;
    pTest->p = p;



      }



  return (sqlite4_mm *)pTest;
      }










void test_mm_debug_report(sqlite4_mm *p, FILE *pFile){





  Testmem *pTest = (Testmem *)p;












  assert( pTest->base.pMethods->xMalloc==mmDebugMalloc );


  mmDebugReport(pTest, pFile);














}


Changes to test/tester.tcl.
81
82
83
84
85
86
87


88
89
90
91
92
93
94
# 64KB into the database file instead of the one 1GB in. This means
# the code that handles that special case can be tested without creating
# very large database files.
#
set tcl_precision 15
#sqlite4_test_control_pending_byte 0x0010000




# If the pager codec is available, create a wrapper for the [sqlite4] 
# command that appends "-key {xyzzy}" to the command line. i.e. this:
#
#     sqlite4 db test.db
#
# becomes







>
>







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 64KB into the database file instead of the one 1GB in. This means
# the code that handles that special case can be tested without creating
# very large database files.
#
set tcl_precision 15
#sqlite4_test_control_pending_byte 0x0010000

test_mm_install stats debug


# If the pager codec is available, create a wrapper for the [sqlite4] 
# command that appends "-key {xyzzy}" to the command line. i.e. this:
#
#     sqlite4 db test.db
#
# becomes
759
760
761
762
763
764
765











766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786

787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805


806

807
808


809
810
811
812
813
814
815
816
817
    puts "in your TCL build."
    puts "******************************************************************"
  }
  if {$::cmdlinearg(binarylog)} {
    vfslog finalize binarylog
  }
  kvwrap uninstall











#  if {[lindex [sqlite4_env_status SQLITE4_ENVSTATUS_MALLOC_COUNT 0] 1]>0 ||
#              [sqlite4_memory_used]>0} {
#    puts "Unfreed memory: [sqlite4_memory_used] bytes in\
#         [lindex [sqlite4_env_status SQLITE4_ENVSTATUS_MALLOC_COUNT 0] 1] allocations"
#    incr nErr
#    ifcapable memdebug||mem5||(mem3&&debug) {
#      puts "Writing unfreed memory log to \"./memleak.txt\""
#      sqlite4_memdebug_dump ./memleak.txt
#    }
#  } else {
#    puts "All memory allocations freed - no leaks"
#    ifcapable memdebug||mem5 {
#      sqlite4_memdebug_dump ./memusage.txt
#    }
#  }
  show_memstats
  #puts "Maximum memory usage: [sqlite4_memory_highwater 1] bytes"
  #puts "Current memory usage: [sqlite4_memory_highwater] bytes"
  if {[info commands sqlite4_memdebug_malloc_count] ne ""} {
    puts "Number of malloc()  : [sqlite4_memdebug_malloc_count] calls"
  }

  if {$::cmdlinearg(malloctrace)} {
    puts "Writing malloc() report to malloc.txt..."
    testmem report malloc.txt
  }
  foreach f [glob -nocomplain test.db-*-journal] {
    forcedelete $f
  }
  foreach f [glob -nocomplain test.db-mj*] {
    forcedelete $f
  }
  exit [expr {$nErr>0}]
}

# Display memory statistics for analysis and debugging purposes.
#
proc show_memstats {} {
  set x [sqlite4_env_status SQLITE4_ENVSTATUS_MEMORY_USED 0]
  set y [sqlite4_env_status SQLITE4_ENVSTATUS_MALLOC_SIZE 0]
  set val [format {now %10d  max %10d  max-size %10d} \


              [lindex $x 1] [lindex $x 2] [lindex $y 2]]

  puts "Memory used:          $val"
  set x [sqlite4_env_status SQLITE4_ENVSTATUS_MALLOC_COUNT 0]


  set val [format {now %10d  max %10d} [lindex $x 1] [lindex $x 2]]
  puts "Allocation count:     $val"
  ifcapable yytrackmaxstackdepth {
    set x [sqlite4_env_status SQLITE4_ENVSTATUS_PARSER_STACK 0]
    set val [format {               max %10d} [lindex $x 2]]
    puts "Parser stack depth:    $val"
  }
}








>
>
>
>
>
>
>
>
>
>
>















<


|
|
<
>
















|
|
|
>
>
|
>
|
<
>
>
|
|







761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793

794
795
796
797

798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822

823
824
825
826
827
828
829
830
831
832
833
    puts "in your TCL build."
    puts "******************************************************************"
  }
  if {$::cmdlinearg(binarylog)} {
    vfslog finalize binarylog
  }
  kvwrap uninstall

  set nOut  [test_mm_stat out]
  set nUnit [test_mm_stat units]
  if {$nOut!=0 || $nUnit!=0} {
    puts "Unfreed memory: $nOut bytes in $nUnit allocations"
  } else {
    puts "All memory allocations freed - no leaks"
  }
  show_memstats
  test_mm_report malloc.txt

#  if {[lindex [sqlite4_env_status SQLITE4_ENVSTATUS_MALLOC_COUNT 0] 1]>0 ||
#              [sqlite4_memory_used]>0} {
#    puts "Unfreed memory: [sqlite4_memory_used] bytes in\
#         [lindex [sqlite4_env_status SQLITE4_ENVSTATUS_MALLOC_COUNT 0] 1] allocations"
#    incr nErr
#    ifcapable memdebug||mem5||(mem3&&debug) {
#      puts "Writing unfreed memory log to \"./memleak.txt\""
#      sqlite4_memdebug_dump ./memleak.txt
#    }
#  } else {
#    puts "All memory allocations freed - no leaks"
#    ifcapable memdebug||mem5 {
#      sqlite4_memdebug_dump ./memusage.txt
#    }
#  }

  #puts "Maximum memory usage: [sqlite4_memory_highwater 1] bytes"
  #puts "Current memory usage: [sqlite4_memory_highwater] bytes"
  #if {[info commands sqlite4_memdebug_malloc_count] ne ""} {
  #  puts "Number of malloc()  : [sqlite4_memdebug_malloc_count] calls"

  #}
  if {$::cmdlinearg(malloctrace)} {
    puts "Writing malloc() report to malloc.txt..."
    testmem report malloc.txt
  }
  foreach f [glob -nocomplain test.db-*-journal] {
    forcedelete $f
  }
  foreach f [glob -nocomplain test.db-mj*] {
    forcedelete $f
  }
  exit [expr {$nErr>0}]
}

# Display memory statistics for analysis and debugging purposes.
#
proc show_memstats {} {
  set nOut        [test_mm_stat out]
  set nOutHw      [test_mm_stat out_hw]
  set nMaxRequest [test_mm_stat size]
  set nUnit       [test_mm_stat units]
  set nUnitHw     [test_mm_stat units_hw]

  puts [format                                        \
    "Memory used: now %-10d max %-10d max-size %-10d" \

    $nOut $nOutHw $nMaxRequest                        \
  ]
  puts [format "Allocations: now %-10d max %-10d" $nUnit $nUnitHw]

  ifcapable yytrackmaxstackdepth {
    set x [sqlite4_env_status SQLITE4_ENVSTATUS_PARSER_STACK 0]
    set val [format {               max %10d} [lindex $x 2]]
    puts "Parser stack depth:    $val"
  }
}