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

Overview
Comment:Use the default sqlite4_mm object for malloc() and free().
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e3280f458535891a72bd5f5fa4ef170b423b2d4f
User & Date: dan 2013-05-18 19:42:42.907
Context
2013-05-21
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
2013-05-10
19:19
Change sqlite4_prepare() to return the number of bytes read from the input SQL script via an output variable (instead of returning a pointer to the next SQL statement within the buffer). check-in: 79197d57b4 user: dan tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/env.c.
41
42
43
44
45
46
47

48

49
50
51
52
53
54
55
   SQLITE4_DEFAULT_MEMSTATUS, /* bMemstat */
   1,                         /* bCoreMutex */
   SQLITE4_THREADSAFE==1,     /* bFullMutex */
   0x7ffffffe,                /* mxStrlen */
   128,                       /* szLookaside */
   500,                       /* nLookaside */
   &sqlite4MMSystem,          /* pMM */

   {0,0,0,0,0,0,0,0,0},       /* m */

   {0,0,0,0,0,0,0,0,0,0},     /* mutex */
   (void*)0,                  /* pHeap */
   0,                         /* nHeap */
   0, 0,                      /* mnHeap, mxHeap */
   0,                         /* mxParserStack */
   &sqlite4BuiltinFactory,    /* pFactory */
   sqlite4OsRandomness,       /* xRandomness */







>

>







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
   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 */
   0, 0,                      /* mnHeap, mxHeap */
   0,                         /* mxParserStack */
   &sqlite4BuiltinFactory,    /* pFactory */
   sqlite4OsRandomness,       /* xRandomness */
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

    /*
    ** 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: {

      /* Specify an alternative malloc implementation */
      if( pEnv->isInit ) return SQLITE4_MISUSE;
      pEnv->m = *va_arg(ap, sqlite4_mem_methods*);

      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: {

      /* Retrieve the current malloc() implementation */
      if( pEnv->m.xMalloc==0 ) sqlite4MemSetDefault(pEnv);
      *va_arg(ap, sqlite4_mem_methods*) = pEnv->m;

      break;
    }

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







>



>










>



>







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

    /*
    ** 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.  
Changes to src/fault.c.
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef SQLITE4_OMIT_BUILTIN_TEST
/*
** This (sqlite4EndBenignMalloc()) is called by SQLite code to indicate that
** subsequent malloc failures are benign. A call to sqlite4EndBenignMalloc()
** indicates that subsequent malloc failures are non-benign.
*/
void sqlite4BeginBenignMalloc(sqlite4_env *pEnv){
  if( pEnv->m.xBeginBenign ) pEnv->m.xBeginBenign(pEnv->m.pMemEnv);
}
void sqlite4EndBenignMalloc(sqlite4_env *pEnv){
  if( pEnv->m.xEndBenign ) pEnv->m.xEndBenign(pEnv->m.pMemEnv);
}
#endif   /* #ifndef SQLITE4_OMIT_BUILTIN_TEST */







|


|


29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef SQLITE4_OMIT_BUILTIN_TEST
/*
** This (sqlite4EndBenignMalloc()) is called by SQLite code to indicate that
** subsequent malloc failures are benign. A call to sqlite4EndBenignMalloc()
** indicates that subsequent malloc failures are non-benign.
*/
void sqlite4BeginBenignMalloc(sqlite4_env *pEnv){
  sqlite4_mm_benign_failures(pEnv->pMM, 1);
}
void sqlite4EndBenignMalloc(sqlite4_env *pEnv){
  sqlite4_mm_benign_failures(pEnv->pMM, 0);
}
#endif   /* #ifndef SQLITE4_OMIT_BUILTIN_TEST */
Changes to src/malloc.c.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
#include "sqliteInt.h"
#include <stdarg.h>

/*
** Initialize the memory allocation subsystem.
*/
int sqlite4MallocInit(sqlite4_env *pEnv){
  if( pEnv->m.xMalloc==0 ){
    sqlite4MemSetDefault(pEnv);
  }
  return pEnv->m.xInit(pEnv->m.pMemEnv);
}

/*
** Deinitialize the memory allocation subsystem.
*/
void sqlite4MallocEnd(sqlite4_env *pEnv){
  if( pEnv->m.xShutdown ){
    pEnv->m.xShutdown(pEnv->m.pMemEnv);
  }
}

/*
** Return the amount of memory currently checked out.
*/
sqlite4_uint64 sqlite4_memory_used(sqlite4_env *pEnv){
  sqlite4_uint64 n, mx;
  if( pEnv==0 ) pEnv = sqlite4_env_default();
  sqlite4_env_status(pEnv, SQLITE4_ENVSTATUS_MEMORY_USED, &n, &mx, 0);
  return n;
}

/*
** Return the maximum amount of memory that has ever been
** checked out since either the beginning of this process
** or since the most recent reset.
*/
sqlite4_uint64 sqlite4_memory_highwater(sqlite4_env *pEnv, int resetFlag){
  sqlite4_uint64 n, mx;
  if( pEnv==0 ) pEnv = sqlite4_env_default();
  sqlite4_env_status(pEnv, SQLITE4_ENVSTATUS_MEMORY_USED, &n, &mx, resetFlag);
  return mx;
}

/*
** Allocate memory.  This routine is like sqlite4_malloc() except that it
** assumes the memory subsystem has already been initialized.
*/
void *sqlite4Malloc(sqlite4_env *pEnv, int n){
  void *p;
  if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;

  if( n<=0               /* IMP: R-65312-04917 */ 
   || n>=0x7fffff00
  ){
    /* A memory allocation of a number of bytes which is near the maximum
    ** signed integer value might cause an integer overflow inside of the
    ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
    ** 255 bytes of overhead.  SQLite itself will never use anything near
    ** this amount.  The only way to reach the limit is with sqlite4_malloc() */
    p = 0;
  }else if( pEnv->bMemstat ){
    int nFull = (n + 7)&~7;
    sqlite4_mutex_enter(pEnv->pMemMutex);
    p = pEnv->m.xMalloc(pEnv->m.pMemEnv, nFull);
    if( p ){
      nFull = sqlite4MallocSize(pEnv, p);
      sqlite4StatusAdd(pEnv, SQLITE4_ENVSTATUS_MEMORY_USED, nFull);
      sqlite4StatusAdd(pEnv, SQLITE4_ENVSTATUS_MALLOC_COUNT, 1);
    }
    sqlite4StatusSet(pEnv, SQLITE4_ENVSTATUS_MALLOC_SIZE, n);
    sqlite4_mutex_leave(pEnv->pMemMutex);
  }else{
    p = pEnv->m.xMalloc(pEnv->m.pMemEnv, n);
  }
  assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
  return p;
}

/*
** This version of the memory allocation is for use by the application.
** First make sure the memory subsystem is initialized, then do the
** allocation.
*/
void *sqlite4_malloc(sqlite4_env *pEnv, sqlite4_size_t n){
#ifndef SQLITE4_OMIT_AUTOINIT
  if( sqlite4_initialize(pEnv) ) return 0;
#endif
  if( n>0x7fff0000 ) return 0;
  return sqlite4Malloc(pEnv, (int)n);
}


/*
** TRUE if p is a lookaside memory allocation from db
*/







<
|
<
|






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







|











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













<







15
16
17
18
19
20
21

22

23
24
25
26
27
28
29




30









31










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
58
59
60
61
62
63
64
65
66
67

68
69
70
71
72
73
74
#include "sqliteInt.h"
#include <stdarg.h>

/*
** Initialize the memory allocation subsystem.
*/
int sqlite4MallocInit(sqlite4_env *pEnv){

  /* TODO: SQLite3 has sqlite3_mem_methods.xInit. But src4 does not? */

  return SQLITE4_OK;
}

/*
** Deinitialize the memory allocation subsystem.
*/
void sqlite4MallocEnd(sqlite4_env *pEnv){




  /* TODO: Should sqlite4_mm_methods.xFinal() be called here? Or some 









  ** reference count decremented? */










}

/*
** Allocate memory.  This routine is like sqlite4_malloc() except that it
** assumes the memory subsystem has already been initialized.
*/
void *sqlite4Malloc(sqlite4_env *pEnv, int n){
  void *p;                        /* Return value */
  if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;

  if( n<=0               /* IMP: R-65312-04917 */ 
   || n>=0x7fffff00
  ){
    /* A memory allocation of a number of bytes which is near the maximum
    ** signed integer value might cause an integer overflow inside of the
    ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
    ** 255 bytes of overhead.  SQLite itself will never use anything near
    ** this amount.  The only way to reach the limit is with sqlite4_malloc() */
    p = 0;
  }else{


    p = sqlite4_mm_malloc(pEnv->pMM, n);




  }





  assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
  return p;
}

/*
** This version of the memory allocation is for use by the application.
** First make sure the memory subsystem is initialized, then do the
** allocation.
*/
void *sqlite4_malloc(sqlite4_env *pEnv, sqlite4_size_t n){
#ifndef SQLITE4_OMIT_AUTOINIT
  if( sqlite4_initialize(pEnv) ) return 0;
#endif

  return sqlite4Malloc(pEnv, (int)n);
}


/*
** TRUE if p is a lookaside memory allocation from db
*/
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
/*
** Return the size of a memory allocation previously obtained from
** sqlite4Malloc() or sqlite4_malloc().
*/
int sqlite4MallocSize(sqlite4_env *pEnv, void *p){
  assert( sqlite4MemdebugHasType(p, MEMTYPE_HEAP) );
  assert( sqlite4MemdebugNoType(p, MEMTYPE_DB) );

  if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;
  return pEnv->m.xSize(pEnv->m.pMemEnv, p);
}
int sqlite4DbMallocSize(sqlite4 *db, void *p){
  assert( db==0 || sqlite4_mutex_held(db->mutex) );
  if( db && isLookaside(db, p) ){
    return db->lookaside.sz;
  }else{
    sqlite4_env *pEnv = db->pEnv;
    assert( sqlite4MemdebugHasType(p, MEMTYPE_DB) );
    assert( sqlite4MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
    assert( db!=0 || sqlite4MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
    return pEnv->m.xSize(pEnv->m.pMemEnv, p);
  }
}

/*
** Free memory previously obtained from sqlite4Malloc().
*/
void sqlite4_free(sqlite4_env *pEnv, void *p){
  if( p==0 ) return;  /* IMP: R-49053-54554 */
  assert( sqlite4MemdebugNoType(p, MEMTYPE_DB) );
  assert( sqlite4MemdebugHasType(p, MEMTYPE_HEAP) );

  if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;
  if( pEnv->bMemstat ){
    sqlite4_mutex_enter(pEnv->pMemMutex);
    sqlite4StatusAdd(pEnv,SQLITE4_ENVSTATUS_MEMORY_USED,
                     -sqlite4MallocSize(pEnv, p));
    sqlite4StatusAdd(pEnv,SQLITE4_ENVSTATUS_MALLOC_COUNT, -1);
    pEnv->m.xFree(pEnv->m.pMemEnv, p);
    sqlite4_mutex_leave(pEnv->pMemMutex);
  }else{
    pEnv->m.xFree(pEnv->m.pMemEnv, p);
  }
}

/*
** Free memory that might be associated with a particular database
** connection.
*/
void sqlite4DbFree(sqlite4 *db, void *p){







>

|






|
<
<
<
<










>

<
<
<
<
<
|
<
<
<
<







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
/*
** Return the size of a memory allocation previously obtained from
** sqlite4Malloc() or sqlite4_malloc().
*/
int sqlite4MallocSize(sqlite4_env *pEnv, void *p){
  assert( sqlite4MemdebugHasType(p, MEMTYPE_HEAP) );
  assert( sqlite4MemdebugNoType(p, MEMTYPE_DB) );

  if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;
  return sqlite4_mm_msize(pEnv->pMM, p);
}
int sqlite4DbMallocSize(sqlite4 *db, void *p){
  assert( db==0 || sqlite4_mutex_held(db->mutex) );
  if( db && isLookaside(db, p) ){
    return db->lookaside.sz;
  }else{
    return sqlite4MallocSize(db->pEnv, p);




  }
}

/*
** Free memory previously obtained from sqlite4Malloc().
*/
void sqlite4_free(sqlite4_env *pEnv, void *p){
  if( p==0 ) return;  /* IMP: R-49053-54554 */
  assert( sqlite4MemdebugNoType(p, MEMTYPE_DB) );
  assert( sqlite4MemdebugHasType(p, MEMTYPE_HEAP) );

  if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;





  sqlite4_mm_free(pEnv->pMM, p);




}

/*
** Free memory that might be associated with a particular database
** connection.
*/
void sqlite4DbFree(sqlite4 *db, void *p){
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
  sqlite4_free(db==0 ? 0 : db->pEnv, p);
}

/*
** Change the size of an existing memory allocation
*/
void *sqlite4Realloc(sqlite4_env *pEnv, void *pOld, int nBytes){
  int nOld, nNew;
  void *pNew;
  if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;

  if( pOld==0 ){
    return sqlite4Malloc(pEnv, nBytes); /* IMP: R-28354-25769 */
  }
  if( nBytes<=0 ){
    sqlite4_free(pEnv, pOld); /* IMP: R-31593-10574 */
    return 0;
  }
  if( nBytes>=0x7fffff00 ){
    /* The 0x7ffff00 limit term is explained in comments on sqlite4Malloc() */
    return 0;
  }
  nOld = sqlite4MallocSize(pEnv, pOld);
  nNew = (nBytes + 7)&~7;
  if( pEnv->bMemstat ){
    sqlite4_mutex_enter(pEnv->pMemMutex);
    sqlite4StatusSet(pEnv, SQLITE4_ENVSTATUS_MALLOC_SIZE, nBytes);
    assert( sqlite4MemdebugHasType(pOld, MEMTYPE_HEAP) );
    assert( sqlite4MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
    pNew = pEnv->m.xRealloc(pEnv->m.pMemEnv, pOld, nNew);
    if( pNew ){
      nNew = sqlite4MallocSize(pEnv, pNew);
      sqlite4StatusAdd(pEnv, SQLITE4_ENVSTATUS_MEMORY_USED, nNew-nOld);
    }
    sqlite4_mutex_leave(pEnv->pMemMutex);
  }else{
    pNew = pEnv->m.xRealloc(pEnv->m.pMemEnv, pOld, nNew);
  }
  assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
  return pNew;
}

/*
** The public interface to sqlite4Realloc.  Make sure that the memory
** subsystem is initialized prior to invoking sqliteRealloc.
*/
void *sqlite4_realloc(sqlite4_env *pEnv, void *pOld, sqlite4_size_t n){
#ifndef SQLITE4_OMIT_AUTOINIT
  if( sqlite4_initialize(pEnv) ) return 0;
#endif
  if( n>0x7fff0000 ) return 0;
  return sqlite4Realloc(pEnv, pOld, (int)n);
}


/*
** Allocate and zero memory.
*/ 







<
<













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










<







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
  sqlite4_free(db==0 ? 0 : db->pEnv, p);
}

/*
** Change the size of an existing memory allocation
*/
void *sqlite4Realloc(sqlite4_env *pEnv, void *pOld, int nBytes){


  if( pEnv==0 ) pEnv = &sqlite4DefaultEnv;

  if( pOld==0 ){
    return sqlite4Malloc(pEnv, nBytes); /* IMP: R-28354-25769 */
  }
  if( nBytes<=0 ){
    sqlite4_free(pEnv, pOld); /* IMP: R-31593-10574 */
    return 0;
  }
  if( nBytes>=0x7fffff00 ){
    /* The 0x7ffff00 limit term is explained in comments on sqlite4Malloc() */
    return 0;
  }

















  return sqlite4_mm_realloc(pEnv->pMM, pOld, nBytes);
}

/*
** The public interface to sqlite4Realloc.  Make sure that the memory
** subsystem is initialized prior to invoking sqliteRealloc.
*/
void *sqlite4_realloc(sqlite4_env *pEnv, void *pOld, sqlite4_size_t n){
#ifndef SQLITE4_OMIT_AUTOINIT
  if( sqlite4_initialize(pEnv) ) return 0;
#endif

  return sqlite4Realloc(pEnv, pOld, (int)n);
}


/*
** Allocate and zero memory.
*/ 
Changes to src/mem1.c.
274
275
276
277
278
279
280

281
282

283
284
285
     sqlite4MemSize,
     sqlite4MemInit,
     sqlite4MemShutdown,
     0,
     0,
     0
  };

  pEnv->m = defaultMethods;
  pEnv->m.pMemEnv = (void*)pEnv;

}

#endif /* SQLITE4_SYSTEM_MALLOC */







>


>



274
275
276
277
278
279
280
281
282
283
284
285
286
287
     sqlite4MemSize,
     sqlite4MemInit,
     sqlite4MemShutdown,
     0,
     0,
     0
  };
#if 0
  pEnv->m = defaultMethods;
  pEnv->m.pMemEnv = (void*)pEnv;
#endif
}

#endif /* SQLITE4_SYSTEM_MALLOC */
Changes to src/sqlite.h.in.
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

/*
** Return a pointer to the default memory allocator, which is basically
** a wrapper around system malloc()/realloc()/free().
*/
sqlite4_mm *sqlite4_mm_default(void);


/*
** Create a new memory allocator object.
*/
sqlite4_mm *sqlite4_mm_new(sqlite4_mm_type, ...);


/*
** Allocate a new memory manager.  Return NULL if unable.
*/
sqlite4_mm *sqlite4_mm_new(sqlite4_mm_type, ...);

/*
** Free the sqlite4_mm object.







<
<
<
<
<
<
<







125
126
127
128
129
130
131







132
133
134
135
136
137
138

/*
** Return a pointer to the default memory allocator, which is basically
** a wrapper around system malloc()/realloc()/free().
*/
sqlite4_mm *sqlite4_mm_default(void);








/*
** Allocate a new memory manager.  Return NULL if unable.
*/
sqlite4_mm *sqlite4_mm_new(sqlite4_mm_type, ...);

/*
** Free the sqlite4_mm object.
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#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, *factor */

/*
** CAPIREF: Compile-Time Library Version Numbers
**
** ^(The [SQLITE4_VERSION] C preprocessor macro in the sqlite4.h header
** evaluates to a string literal that is the SQLite version in the
** format "X.Y.Z" where X is the major version number (always 3 for







|







252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#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 */

/*
** CAPIREF: Compile-Time Library Version Numbers
**
** ^(The [SQLITE4_VERSION] C preprocessor macro in the sqlite4.h header
** evaluates to a string literal that is the SQLite version in the
** format "X.Y.Z" where X is the major version number (always 3 for
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
** a block of memory after it has been released using
** [sqlite4_free()] or [sqlite4_realloc()].
*/
void *sqlite4_malloc(sqlite4_env*, sqlite4_size_t);
void *sqlite4_realloc(sqlite4_env*, void*, sqlite4_size_t);
void sqlite4_free(sqlite4_env*, void*);

/*
** CAPIREF: Memory Allocator Statistics
**
** SQLite provides these two interfaces for reporting on the status
** of the [sqlite4_malloc()], [sqlite4_free()], and [sqlite4_realloc()]
** routines, which form the built-in memory allocation subsystem.
**
** ^The [sqlite4_memory_used(E)] routine returns the number of bytes
** of memory currently outstanding (malloced but not freed) for 
** sqlite4_env environment E.
** ^The [sqlite4_memory_highwater(E)] routine returns the maximum
** value of [sqlite4_memory_used(E)] since the high-water mark
** was last reset.  ^The values returned by [sqlite4_memory_used()] and
** [sqlite4_memory_highwater()] include any overhead
** added by SQLite in its implementation of [sqlite4_malloc()],
** but not overhead added by the any underlying system library
** routines that [sqlite4_malloc()] may call.
**
** ^The memory high-water mark is reset to the current value of
** [sqlite4_memory_used(E)] if and only if the R parameter to
** [sqlite4_memory_highwater(E,R)] is true.  ^The value returned
** by [sqlite4_memory_highwater(E,1)] is the high-water mark
** prior to the reset.
*/
sqlite4_uint64 sqlite4_memory_used(sqlite4_env*);
sqlite4_uint64 sqlite4_memory_highwater(sqlite4_env*, int resetFlag);

/*
** CAPIREF: Pseudo-Random Number Generator
**
** ^A call to this routine stores N bytes of pseudo-randomness into buffer P.
*/
void sqlite4_randomness(sqlite4_env*, int N, void *P);








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1186
1187
1188
1189
1190
1191
1192



























1193
1194
1195
1196
1197
1198
1199
** a block of memory after it has been released using
** [sqlite4_free()] or [sqlite4_realloc()].
*/
void *sqlite4_malloc(sqlite4_env*, sqlite4_size_t);
void *sqlite4_realloc(sqlite4_env*, void*, sqlite4_size_t);
void sqlite4_free(sqlite4_env*, void*);




























/*
** CAPIREF: Pseudo-Random Number Generator
**
** ^A call to this routine stores N bytes of pseudo-randomness into buffer P.
*/
void sqlite4_randomness(sqlite4_env*, int N, void *P);

2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
void *sqlite4_aggregate_context(sqlite4_context*, int nBytes);

/*
** CAPIREF: User Data For Functions
**
** ^The sqlite4_user_data() interface returns a copy of
** the pointer that was the pUserData parameter (the 5th parameter)
** of the [sqlite4_create_function()]
** and [sqlite4_create_function16()] routines that originally
** registered the application defined function.
**
** This routine must be called from the same thread in which
** the application-defined function is running.
*/
void *sqlite4_user_data(sqlite4_context*);

/*
** CAPIREF: Database Connection For Functions
**
** ^The sqlite4_context_db_handle() interface returns a copy of







|
<
|

|
|







2535
2536
2537
2538
2539
2540
2541
2542

2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
void *sqlite4_aggregate_context(sqlite4_context*, int nBytes);

/*
** CAPIREF: User Data For Functions
**
** ^The sqlite4_user_data() interface returns a copy of
** the pointer that was the pUserData parameter (the 5th parameter)
** passed to the [sqlite4_create_function()] or [sqlite4_create_function16()] 

** call that registered the application defined function.
**
** This routine must be called from the same thread in which the 
** application-defined function is running.
*/
void *sqlite4_user_data(sqlite4_context*);

/*
** CAPIREF: Database Connection For Functions
**
** ^The sqlite4_context_db_handle() interface returns a copy of
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
void sqlite4_result_text16le(sqlite4_context*, const void*, int,
                             void(*)(void*,void*),void*);
void sqlite4_result_text16be(sqlite4_context*, const void*, int,
                             void(*)(void*,void*),void*);
void sqlite4_result_value(sqlite4_context*, sqlite4_value*);

/*
** CAPIREF: Define New Collating Sequences
**
** ^This function adds, removes, or modifies a [collation] associated
** with the [database connection] specified as the first argument.
**
** ^The name of the collation is a UTF-8 string.
** ^Collation names that compare equal according to [sqlite4_strnicmp()] are
** considered to be the same name.







|







2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
void sqlite4_result_text16le(sqlite4_context*, const void*, int,
                             void(*)(void*,void*),void*);
void sqlite4_result_text16be(sqlite4_context*, const void*, int,
                             void(*)(void*,void*),void*);
void sqlite4_result_value(sqlite4_context*, sqlite4_value*);

/*
** CAPIREF: Define New Collation Sequences
**
** ^This function adds, removes, or modifies a [collation] associated
** with the [database connection] specified as the first argument.
**
** ^The name of the collation is a UTF-8 string.
** ^Collation names that compare equal according to [sqlite4_strnicmp()] are
** considered to be the same name.
Changes to src/sqliteInt.h.
2406
2407
2408
2409
2410
2411
2412

2413

2414
2415
2416
2417
2418
2419
2420
  int bMemstat;                     /* True to enable memory status */
  int bCoreMutex;                   /* True to enable core mutexing */
  int bFullMutex;                   /* True to enable full mutexing */
  int mxStrlen;                     /* Maximum string length */
  int szLookaside;                  /* Default lookaside buffer size */
  int nLookaside;                   /* Default lookaside buffer count */
  sqlite4_mm *pMM;                  /* Memory allocator for this environment */

  sqlite4_mem_methods m;            /* Low-level memory allocation interface */

  sqlite4_mutex_methods mutex;      /* Low-level mutex interface */
  void *pHeap;                      /* Heap storage space */
  int nHeap;                        /* Size of pHeap[] */
  int mnReq, mxReq;                 /* Min and max heap requests sizes */
  int mxParserStack;                /* maximum depth of the parser stack */
  KVFactory *pFactory;              /* List of factories */
  int (*xRandomness)(sqlite4_env*, int, unsigned char*);







>

>







2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
  int bMemstat;                     /* True to enable memory status */
  int bCoreMutex;                   /* True to enable core mutexing */
  int bFullMutex;                   /* True to enable full mutexing */
  int mxStrlen;                     /* Maximum string length */
  int szLookaside;                  /* Default lookaside buffer size */
  int nLookaside;                   /* Default lookaside buffer count */
  sqlite4_mm *pMM;                  /* Memory allocator for this environment */
#if 0
  sqlite4_mem_methods m;            /* Low-level memory allocation interface */
#endif
  sqlite4_mutex_methods mutex;      /* Low-level mutex interface */
  void *pHeap;                      /* Heap storage space */
  int nHeap;                        /* Size of pHeap[] */
  int mnReq, mxReq;                 /* Min and max heap requests sizes */
  int mxParserStack;                /* maximum depth of the parser stack */
  KVFactory *pFactory;              /* List of factories */
  int (*xRandomness)(sqlite4_env*, int, unsigned char*);
Changes to test/test_malloc.c.
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
    size -= n;
    sqlite4TestBinToHex(zHex, n);
    Tcl_AppendResult(interp, zHex, (char*)0);
  }
  return TCL_OK;
}

/*
** Usage:    sqlite4_memory_used
**
** Raw test interface for sqlite4_memory_used().
*/
static int test_memory_used(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite4_memory_used(0)));
  return TCL_OK;
}

/*
** Usage:    sqlite4_memory_highwater ?RESETFLAG?
**
** Raw test interface for sqlite4_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(sqlite4_memory_highwater(0, resetFlag)));
  return TCL_OK;
}

/*
** Usage:    sqlite4_memdebug_backtrace DEPTH
**
** Set the depth of backtracing.  If SQLITE4_MEMDEBUG is not defined
** then this routine is a no-op.
*/
static int test_memdebug_backtrace(







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







472
473
474
475
476
477
478







































479
480
481
482
483
484
485
    size -= n;
    sqlite4TestBinToHex(zHex, n);
    Tcl_AppendResult(interp, zHex, (char*)0);
  }
  return TCL_OK;
}








































/*
** Usage:    sqlite4_memdebug_backtrace DEPTH
**
** Set the depth of backtracing.  If SQLITE4_MEMDEBUG is not defined
** then this routine is a no-op.
*/
static int test_memdebug_backtrace(
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
     int clientData;
  } aObjCmd[] = {
     { "sqlite4_malloc",                 test_malloc                   ,0 },
     { "sqlite4_realloc",                test_realloc                  ,0 },
     { "sqlite4_free",                   test_free                     ,0 },
     { "memset",                         test_memset                   ,0 },
     { "memget",                         test_memget                   ,0 },
     { "sqlite4_memory_used",            test_memory_used              ,0 },
     { "sqlite4_memory_highwater",       test_memory_highwater         ,0 },
     { "sqlite4_memdebug_backtrace",     test_memdebug_backtrace       ,0 },
     { "sqlite4_memdebug_dump",          test_memdebug_dump            ,0 },
     { "sqlite4_memdebug_fail",          test_memdebug_fail            ,0 },
     { "sqlite4_memdebug_pending",       test_memdebug_pending         ,0 },
     { "sqlite4_memdebug_settitle",      test_memdebug_settitle        ,0 },
     { "sqlite4_memdebug_malloc_count",  test_memdebug_malloc_count ,0 },
     { "sqlite4_memdebug_log",           test_memdebug_log             ,0 },







<
<







1131
1132
1133
1134
1135
1136
1137


1138
1139
1140
1141
1142
1143
1144
     int clientData;
  } aObjCmd[] = {
     { "sqlite4_malloc",                 test_malloc                   ,0 },
     { "sqlite4_realloc",                test_realloc                  ,0 },
     { "sqlite4_free",                   test_free                     ,0 },
     { "memset",                         test_memset                   ,0 },
     { "memget",                         test_memget                   ,0 },


     { "sqlite4_memdebug_backtrace",     test_memdebug_backtrace       ,0 },
     { "sqlite4_memdebug_dump",          test_memdebug_dump            ,0 },
     { "sqlite4_memdebug_fail",          test_memdebug_fail            ,0 },
     { "sqlite4_memdebug_pending",       test_memdebug_pending         ,0 },
     { "sqlite4_memdebug_settitle",      test_memdebug_settitle        ,0 },
     { "sqlite4_memdebug_malloc_count",  test_memdebug_malloc_count ,0 },
     { "sqlite4_memdebug_log",           test_memdebug_log             ,0 },
Changes to test/tester.tcl.
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
    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
  }







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

|
|







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
    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
  }