/ Check-in [3e51696c]
Login

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

Overview
Comment:Add a malloc size histogram to the debugging malloc implementation in mem2.c. (CVS 4490)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3e51696cb878063e4ebfdcc2a61ba94c9bebdfe3
User & Date: drh 2007-10-15 19:34:32
Context
2007-10-16
19:45
A cosmetic changes to btree.c which (we are told) works around a bug in MSVC++. (CVS 4491) check-in: 329dd014 user: drh tags: trunk
2007-10-15
19:34
Add a malloc size histogram to the debugging malloc implementation in mem2.c. (CVS 4490) check-in: 3e51696c user: drh tags: trunk
07:08
Fix for OMIT_AUTHORIZATION builds. (CVS 4489) check-in: 260711a1 user: danielk1977 tags: trunk
Changes
Hide Diffs Unified Diffs Show Whitespace Changes Patch

Changes to src/mem2.c.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
80
81
82
83
84
85
86





87
88
89
90
91
92
93
...
145
146
147
148
149
150
151







152
153
154
155
156
157
158
...
268
269
270
271
272
273
274





275
276
277
278
279
280
281
...
436
437
438
439
440
441
442

443
444
445
446
447
448
449
...
455
456
457
458
459
460
461









462
463
464
465
466
467
468
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains the C functions that implement a memory
** allocation subsystem for use by SQLite.  
**
** $Id: mem2.c,v 1.14 2007/10/03 08:46:45 danielk1977 Exp $
*/

/*
** This version of the memory allocator is used only if the
** SQLITE_MEMDEBUG macro is defined and SQLITE_OMIT_MEMORY_ALLOCATION
** is not defined.
*/
................................................................................

/*
** Guard words
*/
#define FOREGUARD 0x80F5E153
#define REARGUARD 0xE4676B53






/*
** All of the static variables used by this module are collected
** into a single structure named "mem".  This is to keep the
** static variables organized and to reduce namespace pollution
** when this module is combined with other in the amalgamation.
*/
static struct {
................................................................................

  /* 
  ** sqlite3MallocDisallow() increments the following counter.
  ** sqlite3MallocAllow() decrements it.
  */
  int disallow; /* Do not allow memory allocation */
  







  
} mem;


/*
** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
*/
................................................................................
  if( nByte>0 ){
    enterMem();
    assert( mem.disallow==0 );
    if( mem.alarmCallback!=0 && mem.nowUsed+nByte>=mem.alarmThreshold ){
      sqlite3MemsysAlarm(nByte);
    }
    nByte = (nByte+3)&~3;





    totalSize = nByte + sizeof(*pHdr) + sizeof(int) +
                 mem.nBacktrace*sizeof(void*) + mem.nTitle;
    if( mem.iFail>0 ){
      if( mem.iFail==1 ){
        p = 0;
        mem.iFail = mem.iReset;
        if( mem.iFailCnt==0 ){
................................................................................
** Open the file indicated and write a log of all unfreed memory 
** allocations into that log.
*/
void sqlite3_memdebug_dump(const char *zFilename){
  FILE *out;
  struct MemBlockHdr *pHdr;
  void **pBt;

  out = fopen(zFilename, "w");
  if( out==0 ){
    fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
                    zFilename);
    return;
  }
  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
................................................................................
      fflush(out);
      pBt = (void**)pHdr;
      pBt -= pHdr->nBacktraceSlots;
      backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
      fprintf(out, "\n");
    }
  }









  fclose(out);
}

/*
** This routine is used to simulate malloc failures.
**
** After calling this routine, there will be iFail successful







|







 







>
>
>
>
>







 







>
>
>
>
>
>
>







 







>
>
>
>
>







 







>







 







>
>
>
>
>
>
>
>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
...
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
...
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
...
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains the C functions that implement a memory
** allocation subsystem for use by SQLite.  
**
** $Id: mem2.c,v 1.15 2007/10/15 19:34:32 drh Exp $
*/

/*
** This version of the memory allocator is used only if the
** SQLITE_MEMDEBUG macro is defined and SQLITE_OMIT_MEMORY_ALLOCATION
** is not defined.
*/
................................................................................

/*
** Guard words
*/
#define FOREGUARD 0x80F5E153
#define REARGUARD 0xE4676B53

/*
** Number of malloc size increments to track.
*/
#define NCSIZE  500

/*
** All of the static variables used by this module are collected
** into a single structure named "mem".  This is to keep the
** static variables organized and to reduce namespace pollution
** when this module is combined with other in the amalgamation.
*/
static struct {
................................................................................

  /* 
  ** sqlite3MallocDisallow() increments the following counter.
  ** sqlite3MallocAllow() decrements it.
  */
  int disallow; /* Do not allow memory allocation */

  /*
  ** Gather statistics on the sizes of memory allocations.
  ** sizeCnt[i] is the number of allocation attempts of i*4
  ** bytes.  i==NCSIZE is the number of allocation attempts for
  ** sizes more than NCSIZE*4 bytes.
  */
  int sizeCnt[NCSIZE];

} mem;


/*
** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
*/
................................................................................
  if( nByte>0 ){
    enterMem();
    assert( mem.disallow==0 );
    if( mem.alarmCallback!=0 && mem.nowUsed+nByte>=mem.alarmThreshold ){
      sqlite3MemsysAlarm(nByte);
    }
    nByte = (nByte+3)&~3;
    if( nByte/8>NCSIZE-1 ){
      mem.sizeCnt[NCSIZE-1]++;
    }else{
      mem.sizeCnt[nByte/8]++;
    }
    totalSize = nByte + sizeof(*pHdr) + sizeof(int) +
                 mem.nBacktrace*sizeof(void*) + mem.nTitle;
    if( mem.iFail>0 ){
      if( mem.iFail==1 ){
        p = 0;
        mem.iFail = mem.iReset;
        if( mem.iFailCnt==0 ){
................................................................................
** Open the file indicated and write a log of all unfreed memory 
** allocations into that log.
*/
void sqlite3_memdebug_dump(const char *zFilename){
  FILE *out;
  struct MemBlockHdr *pHdr;
  void **pBt;
  int i;
  out = fopen(zFilename, "w");
  if( out==0 ){
    fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
                    zFilename);
    return;
  }
  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
................................................................................
      fflush(out);
      pBt = (void**)pHdr;
      pBt -= pHdr->nBacktraceSlots;
      backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
      fprintf(out, "\n");
    }
  }
  fprintf(out, "COUNTS:\n");
  for(i=0; i<NCSIZE-1; i++){
    if( mem.sizeCnt[i] ){
      fprintf(out, "   %3d: %d\n", i*8+8, mem.sizeCnt[i]);
    }
  }
  if( mem.sizeCnt[NCSIZE-1] ){
    fprintf(out, "  >%3d: %d\n", NCSIZE*8, mem.sizeCnt[NCSIZE-1]);
  }
  fclose(out);
}

/*
** This routine is used to simulate malloc failures.
**
** After calling this routine, there will be iFail successful

Changes to test/tester.tcl.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
199
200
201
202
203
204
205



206
207
208
209
210
211
212
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements some common TCL routines used for regression
# testing the SQLite library
#
# $Id: tester.tcl,v 1.91 2007/09/01 09:02:54 danielk1977 Exp $


set tcl_precision 15
set sqlite_pending_byte 0x0010000

# 
# Check the command-line arguments for a default soft-heap-limit.
................................................................................
    incr nErr
    ifcapable memdebug {
      puts "Writing unfreed memory log to \"./memleak.txt\""
      sqlite3_memdebug_dump ./memleak.txt
    }
  } else {
    puts "All memory allocations freed - no leaks"



  }
  puts "Maximum memory usage: [sqlite3_memory_highwater] bytes"
  foreach f [glob -nocomplain test.db-*-journal] {
    file delete -force $f
  }
  foreach f [glob -nocomplain test.db-mj*] {
    file delete -force $f







|







 







>
>
>







7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements some common TCL routines used for regression
# testing the SQLite library
#
# $Id: tester.tcl,v 1.92 2007/10/15 19:34:32 drh Exp $


set tcl_precision 15
set sqlite_pending_byte 0x0010000

# 
# Check the command-line arguments for a default soft-heap-limit.
................................................................................
    incr nErr
    ifcapable memdebug {
      puts "Writing unfreed memory log to \"./memleak.txt\""
      sqlite3_memdebug_dump ./memleak.txt
    }
  } else {
    puts "All memory allocations freed - no leaks"
    ifcapable memdebug {
      sqlite3_memdebug_dump ./memusage.txt
    }
  }
  puts "Maximum memory usage: [sqlite3_memory_highwater] bytes"
  foreach f [glob -nocomplain test.db-*-journal] {
    file delete -force $f
  }
  foreach f [glob -nocomplain test.db-mj*] {
    file delete -force $f