SQLite4
Check-in [4424d6f7b3]
Not logged in

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

Overview
Comment:Enhancements to the sqlite4_mm implementation. Fix the amalgamation generator so that it works after the addition of sqlite4_mm.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4424d6f7b3d941a7375fe6dec6431b01a09990bb
User & Date: drh 2013-02-24 00:43:39
Context
2013-02-25
16:36
File Format Change Modify the key encoding so that final BLOBs are entered byte-for-byte with no terminator. check-in: 3b2515079a user: drh tags: trunk
2013-02-24
00:43
Enhancements to the sqlite4_mm implementation. Fix the amalgamation generator so that it works after the addition of sqlite4_mm. check-in: 4424d6f7b3 user: drh tags: trunk
2013-02-23
18:36
Fix a bug in the makefile that prevents the building of the amalgamation. check-in: 9e3d29f603 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/env.c.

10
11
12
13
14
15
16



























































17
18
19
20
21
22
23
...
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
**
*************************************************************************
** 
** This file contains code used to help implement the sqlite4_env object.
*/
#include "sqliteInt.h"




























































/*
** Initialize SQLite.  
**
** This routine must be called to initialize the run-time environment
** As long as you do not compile with SQLITE4_OMIT_AUTOINIT
** this routine will be called automatically by key routines such as
** sqlite4_open().  
................................................................................
      rc = SQLITE4_ERROR;
      break;
    }
  }
  va_end(ap);
  return rc;
}


/*
** Default factory objects
*/
static KVFactory memFactory = {
   0,
   "temp",
   sqlite4KVStoreOpenMem,
   1
};
KVFactory sqlite4BuiltinFactory = {
   &memFactory,
   "main",
   sqlite4KVStoreOpenLsm,
   1
};

/*
** The following singleton contains the global configuration for
** the SQLite library.
*/
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 */
   {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 */
   sqlite4OsCurrentTime,      /* xCurrentTime */
   /* All the rest should always be initialized to zero */
   0,                         /* isInit */
   0,                         /* pFactoryMutex */
   0,                         /* pPrngMutex */
   0, 0,                      /* prngX, prngY */
   0,                         /* xLog */
   0,                         /* pLogArg */
   0,                         /* bLocaltimeFault */
   0,                         /* pMemMutex */
   {0,0,0,0},                 /* nowValue[] */
   {0,0,0,0},                 /* mxValue[] */
   {0,}                       /* hashGlobalFunc */
};

/*
** Return the default environment
*/
sqlite4_env *sqlite4_env_default(void){ return &sqlite4DefaultEnv; }







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







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
10
11
12
13
14
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
...
436
437
438
439
440
441
442



























































**
*************************************************************************
** 
** This file contains code used to help implement the sqlite4_env object.
*/
#include "sqliteInt.h"


/*
** Default factory objects
*/
static KVFactory memFactory = {
   0,
   "temp",
   sqlite4KVStoreOpenMem,
   1
};
KVFactory sqlite4BuiltinFactory = {
   &memFactory,
   "main",
   sqlite4KVStoreOpenLsm,
   1
};

/*
** The following singleton contains the global configuration for
** the SQLite library.
*/
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 */
   {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 */
   sqlite4OsCurrentTime,      /* xCurrentTime */
   /* All the rest should always be initialized to zero */
   0,                         /* isInit */
   0,                         /* pFactoryMutex */
   0,                         /* pPrngMutex */
   0, 0,                      /* prngX, prngY */
   0,                         /* xLog */
   0,                         /* pLogArg */
   0,                         /* bLocaltimeFault */
   0,                         /* pMemMutex */
   {0,0,0,0},                 /* nowValue[] */
   {0,0,0,0},                 /* mxValue[] */
   {0,}                       /* hashGlobalFunc */
};

/*
** Return the default environment
*/
sqlite4_env *sqlite4_env_default(void){ return &sqlite4DefaultEnv; }

/*
** Initialize SQLite.  
**
** This routine must be called to initialize the run-time environment
** As long as you do not compile with SQLITE4_OMIT_AUTOINIT
** this routine will be called automatically by key routines such as
** sqlite4_open().  
................................................................................
      rc = SQLITE4_ERROR;
      break;
    }
  }
  va_end(ap);
  return rc;
}



























































Changes to src/mem.c.

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
155
156
157
158
159
160
161

162
163
164
165
166
167
168
...
189
190
191
192
193
194
195
196
197
198
199
200







201
202
203
204
205
206
207
...
255
256
257
258
259
260
261

262
263
264
265
266
267
268
...
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
...
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
...
398
399
400
401
402
403
404










405
406
407
408
409
410
411
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains the implementation of the "sqlite4_mm" memory
** allocator object.
*/
#include "sqlite4.h"
#include <stdlib.h>

/*************************************************************************
** The SQLITE4_MM_SYSTEM memory allocator.  This allocator uses the
** malloc/realloc/free from the system library.  It also tries to use
** the memory allocation sizer from the system library if such a routine
** exists.  If there is no msize in the system library, then each allocation
** is increased in size by 8 bytes and the size of the allocation is stored
................................................................................
  /* xMalloc  */    mmSysMalloc,
  /* xRealloc */    mmSysRealloc,
  /* xFree    */    mmSysFree,
  /* xMsize   */    mmSysMsize,
  /* xMember  */    0,
  /* xBenign  */    0,
  /* xStat    */    0,

  /* xFinal   */    0
};
sqlite4_mm sqlite4MMSystem =  {
  /* pMethods */    &mmSysMethods
};

/* The system memory allocator is the default. */
................................................................................
  if( pRes==0 ){
    pRes = pOvfl->pB->pMethods->xMalloc(pOvfl->pB, iSz);
  }
  return pRes;
}
static void *mmOvflRealloc(sqlite4_mm *pMM, void *pOld, sqlite4_size_t iSz){
  const struct mmOvfl *pOvfl;
  void *pRes;
  if( pOld==0 ) return mmOvflMalloc(pMM, iSz);
  pOvfl = (const struct mmOvfl*)pMM;
  if( pOvfl->xMemberOfA(pOvfl->pA, pOld) ){
    pRes = pOvfl->pA->pMethods->xRealloc(pOvfl->pA, pOld, iSz);







  }else{
    pRes = pOvfl->pB->pMethods->xRealloc(pOvfl->pB, pOld, iSz);
  }
  return pRes;
}
static void mmOvflFree(sqlite4_mm *pMM, void *pOld){
  const struct mmOvfl *pOvfl;
................................................................................
  /* xMalloc  */    mmOvflMalloc,
  /* xRealloc */    mmOvflRealloc,
  /* xFree    */    mmOvflFree,
  /* xMsize   */    mmOvflMsize,
  /* xMember  */    mmOvflMember,
  /* xBenign  */    mmOvflBenign,
  /* xStat    */    0,

  /* xFinal   */    mmOvflFinal
};
static sqlite4_mm *mmOvflNew(sqlite4_mm *pA, sqlite4_mm *pB){
  struct mmOvfl *pOvfl;
  if( pA->pMethods->xMember==0 ) return 0;
  pOvfl = sqlite4_mm_malloc(pA, sizeof(*pOvfl));
  if( pOvfl==0 ){
................................................................................
*/
struct mmOnesz {
  sqlite4_mm base;            /* Base class.  Must be first. */
  const void *pSpace;         /* Space to allocate */
  const void *pLast;          /* Last possible allocation */
  struct mmOneszBlock *pFree; /* List of free blocks */
  int sz;                     /* Size of each allocation */






};

/* A free block in the buffer */
struct mmOneszBlock {
  struct mmOneszBlock *pNext;  /* Next on the freelist */
};

static void *mmOneszMalloc(sqlite4_mm *pMM, sqlite4_size_t iSz){
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  void *pRes;

  if( iSz>pOnesz->sz ) return 0;
  if( pOnesz->pFree==0 ) return 0;


  pRes = pOnesz->pFree;
  pOnesz->pFree = pOnesz->pFree->pNext;
  return pRes;
}
static void mmOneszFree(sqlite4_mm *pMM, void *pOld){
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  if( pOld ){
    struct mmOneszBlock *pBlock = (struct mmOneszBlock*)pOld;
    pBlock->pNext = pOnesz->pFree;
    pOnesz->pFree = pBlock;

  }
}
static void *mmOneszRealloc(sqlite4_mm *pMM, void *pOld, sqlite4_size_t iSz){
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  if( pOld==0 ) return mmOneszMalloc(pMM, iSz);
  if( iSz<=0 ){
    mmOneszFree(pMM, pOld);
................................................................................
static sqlite4_size_t mmOneszMsize(sqlite4_mm *pMM, void *pOld){
  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 const sqlite4_mm_methods mmOneszMethods = {
  /* iVersion */    1,
  /* xMalloc  */    mmOneszMalloc,
  /* xRealloc */    mmOneszRealloc,
  /* xFree    */    mmOneszFree,
  /* xMsize   */    mmOneszMsize,
  /* xMember  */    mmOneszMember,
  /* xBenign  */    0,
  /* xStat    */    0,

  /* xFinal   */    0
};
static sqlite4_mm *mmOneszNew(void *pSpace, int sz, int cnt){
  struct mmOnesz *pOnesz;
  unsigned char *pMem;
  if( sz<sizeof(*pOnesz) ) return 0;
  if( cnt<2 ) return 0;
  pMem = (unsigned char*)pSpace;
  pOnesz = (struct mmOnesz*)pMem;

  pMem += sz;



  pOnesz->base.pMethods = &mmOneszMethods;
  pOnesz->pSpace = (const void*)pMem;
  pOnesz->sz = sz;
  pOnesz->pLast = (const void*)(pMem + sz*(cnt-2));
  pOnesz->pFree = 0;
  while( cnt ){
    struct mmOneszBlock *pBlock = (struct mmOneszBlock*)pMem;
................................................................................
    pMM->pMethods->xBenign(pMM, bEnable);
  }
}
sqlite4_int64 sqlite4_mm_stat(sqlite4_mm *pMM, int eStatType, unsigned flags){
  if( pMM==0 ) return -1;
  if( pMM->pMethods->xStat==0 ) return -1;
  return pMM->pMethods->xStat(pMM, eStatType, flags);










}
void sqlite4_mm_destroy(sqlite4_mm *pMM){
  if( pMM && pMM->pMethods->xFinal ) pMM->pMethods->xFinal(pMM);
}

/*
** Create a new memory allocation object.  eType determines the type of







|
<







 







>







 







|




>
>
>
>
>
>
>







 







>







 







>
>
>
>
>
>










>
|
|
>
>










>







 







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









|
>





|
|


>
|
>
>
>







 







>
>
>
>
>
>
>
>
>
>







9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
...
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
...
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
...
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
...
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
...
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
...
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains the implementation of the "sqlite4_mm" memory
** allocator object.
*/
#include "sqliteInt.h"


/*************************************************************************
** The SQLITE4_MM_SYSTEM memory allocator.  This allocator uses the
** malloc/realloc/free from the system library.  It also tries to use
** the memory allocation sizer from the system library if such a routine
** exists.  If there is no msize in the system library, then each allocation
** is increased in size by 8 bytes and the size of the allocation is stored
................................................................................
  /* xMalloc  */    mmSysMalloc,
  /* xRealloc */    mmSysRealloc,
  /* xFree    */    mmSysFree,
  /* xMsize   */    mmSysMsize,
  /* xMember  */    0,
  /* xBenign  */    0,
  /* xStat    */    0,
  /* xCtrl    */    0,
  /* xFinal   */    0
};
sqlite4_mm sqlite4MMSystem =  {
  /* pMethods */    &mmSysMethods
};

/* The system memory allocator is the default. */
................................................................................
  if( pRes==0 ){
    pRes = pOvfl->pB->pMethods->xMalloc(pOvfl->pB, iSz);
  }
  return pRes;
}
static void *mmOvflRealloc(sqlite4_mm *pMM, void *pOld, sqlite4_size_t iSz){
  const struct mmOvfl *pOvfl;
  void *pRes, *pAlt;
  if( pOld==0 ) return mmOvflMalloc(pMM, iSz);
  pOvfl = (const struct mmOvfl*)pMM;
  if( pOvfl->xMemberOfA(pOvfl->pA, pOld) ){
    pRes = pOvfl->pA->pMethods->xRealloc(pOvfl->pA, pOld, iSz);
    if( pRes==0 && (pAlt = pOvfl->pB->pMethods->xMalloc(pOvfl->pB, iSz))!=0 ){
      sqlite4_size_t nOld = pOvfl->pA->pMethods->xMsize(pOvfl->pA, pOld);
      assert( nOld<iSz );
      memcpy(pAlt, pOld, (size_t)nOld);
      pOvfl->pA->pMethods->xFree(pOvfl->pA, pOld);
      pRes = pAlt;
    }
  }else{
    pRes = pOvfl->pB->pMethods->xRealloc(pOvfl->pB, pOld, iSz);
  }
  return pRes;
}
static void mmOvflFree(sqlite4_mm *pMM, void *pOld){
  const struct mmOvfl *pOvfl;
................................................................................
  /* xMalloc  */    mmOvflMalloc,
  /* xRealloc */    mmOvflRealloc,
  /* xFree    */    mmOvflFree,
  /* xMsize   */    mmOvflMsize,
  /* xMember  */    mmOvflMember,
  /* xBenign  */    mmOvflBenign,
  /* xStat    */    0,
  /* xCtrl    */    0,
  /* xFinal   */    mmOvflFinal
};
static sqlite4_mm *mmOvflNew(sqlite4_mm *pA, sqlite4_mm *pB){
  struct mmOvfl *pOvfl;
  if( pA->pMethods->xMember==0 ) return 0;
  pOvfl = sqlite4_mm_malloc(pA, sizeof(*pOvfl));
  if( pOvfl==0 ){
................................................................................
*/
struct mmOnesz {
  sqlite4_mm base;            /* Base class.  Must be first. */
  const void *pSpace;         /* Space to allocate */
  const void *pLast;          /* Last possible allocation */
  struct mmOneszBlock *pFree; /* List of free blocks */
  int sz;                     /* Size of each allocation */
  unsigned nFailSize;         /* Failures due to size */
  unsigned nFailMem;          /* Failures due to OOM */
  unsigned nSlot;             /* Number of available slots */
  unsigned nUsed;             /* Current number of slots in use */
  unsigned nUsedHw;           /* Highwater mark for slots in use */
  sqlite4_size_t mxSize;      /* Maximum request size */
};

/* A free block in the buffer */
struct mmOneszBlock {
  struct mmOneszBlock *pNext;  /* Next on the freelist */
};

static void *mmOneszMalloc(sqlite4_mm *pMM, sqlite4_size_t iSz){
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  void *pRes;
  if( iSz>pOnesz->mxSize ) pOnesz->mxSize = iSz;
  if( iSz>pOnesz->sz ){ pOnesz->nFailSize++; return 0; }
  if( pOnesz->pFree==0 ){ pOnesz->nFailMem++;  return 0; }
  pOnesz->nUsed++;
  if( pOnesz->nUsed>pOnesz->nUsedHw ) pOnesz->nUsedHw = pOnesz->nUsed;
  pRes = pOnesz->pFree;
  pOnesz->pFree = pOnesz->pFree->pNext;
  return pRes;
}
static void mmOneszFree(sqlite4_mm *pMM, void *pOld){
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  if( pOld ){
    struct mmOneszBlock *pBlock = (struct mmOneszBlock*)pOld;
    pBlock->pNext = pOnesz->pFree;
    pOnesz->pFree = pBlock;
    pOnesz->nUsed--;
  }
}
static void *mmOneszRealloc(sqlite4_mm *pMM, void *pOld, sqlite4_size_t iSz){
  struct mmOnesz *pOnesz = (struct mmOnesz*)pMM;
  if( pOld==0 ) return mmOneszMalloc(pMM, iSz);
  if( iSz<=0 ){
    mmOneszFree(pMM, pOld);
................................................................................
static sqlite4_size_t mmOneszMsize(sqlite4_mm *pMM, void *pOld){
  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;
    }
    case SQLITE4_MMSTAT_OUT_HW: {
      x = pOnesz->nUsedHw*pOnesz->sz;
      if( flgs & SQLITE4_MMSTAT_RESET ) pOnesz->nUsedHw = pOnesz->nUsed;
      break;
    }
    case SQLITE4_MMSTAT_UNITS: {
      x = pOnesz->nUsed;
      break;
    }
    case SQLITE4_MMSTAT_UNITS_HW: {
      x = pOnesz->nUsedHw;
      if( flgs & SQLITE4_MMSTAT_RESET ) pOnesz->nUsedHw = pOnesz->nUsed;
      break;
    }
    case SQLITE4_MMSTAT_SIZE: {
      x = pOnesz->mxSize;
      if( flgs & SQLITE4_MMSTAT_RESET ) pOnesz->mxSize = 0;
      break;
    }
    case SQLITE4_MMSTAT_SZFAULT: {
      x = pOnesz->nFailSize;
      if( flgs & SQLITE4_MMSTAT_RESET ) pOnesz->nFailSize = 0;
      break;
    }
    case SQLITE4_MMSTAT_MEMFAULT: {
      x = pOnesz->nFailMem;
      if( flgs & SQLITE4_MMSTAT_RESET ) pOnesz->nFailMem = 0;
      break;
    }
    case SQLITE4_MMSTAT_FAULT: {
      x = pOnesz->nFailSize + pOnesz->nFailMem;
      if( flgs & SQLITE4_MMSTAT_RESET ){
        pOnesz->nFailSize = 0;
        pOnesz->nFailMem = 0;
      }
      break;
    }
  }
  return x;
}
static const sqlite4_mm_methods mmOneszMethods = {
  /* iVersion */    1,
  /* xMalloc  */    mmOneszMalloc,
  /* xRealloc */    mmOneszRealloc,
  /* xFree    */    mmOneszFree,
  /* xMsize   */    mmOneszMsize,
  /* xMember  */    mmOneszMember,
  /* xBenign  */    0,
  /* xStat    */    mmOneszStat,
  /* xCtrl    */    0,
  /* xFinal   */    0
};
static sqlite4_mm *mmOneszNew(void *pSpace, int sz, int cnt){
  struct mmOnesz *pOnesz;
  unsigned char *pMem;
  int n;
  if( sz<sizeof(struct mmOneszBlock) ) return 0;
  pMem = (unsigned char*)pSpace;
  pOnesz = (struct mmOnesz*)pMem;
  n = (sizeof(*pOnesz) + sz - 1)/sz;
  pMem += sz*n;
  cnt -= n;
  if( cnt<2 ) return 0;
  memset(pOnesz, 0, sizeof(*pOnesz));
  pOnesz->base.pMethods = &mmOneszMethods;
  pOnesz->pSpace = (const void*)pMem;
  pOnesz->sz = sz;
  pOnesz->pLast = (const void*)(pMem + sz*(cnt-2));
  pOnesz->pFree = 0;
  while( cnt ){
    struct mmOneszBlock *pBlock = (struct mmOneszBlock*)pMem;
................................................................................
    pMM->pMethods->xBenign(pMM, bEnable);
  }
}
sqlite4_int64 sqlite4_mm_stat(sqlite4_mm *pMM, int eStatType, unsigned flags){
  if( pMM==0 ) return -1;
  if( pMM->pMethods->xStat==0 ) return -1;
  return pMM->pMethods->xStat(pMM, eStatType, flags);
}
int sqlite4_mm_control(sqlite4_mm *pMM, int eCtrlType, ...){
  int rc = SQLITE4_NOTFOUND;
  if( pMM && pMM->pMethods->xCtrl ){
    va_list ap;
    va_start(ap, eCtrlType);
    rc = pMM->pMethods->xCtrl(pMM, eCtrlType, ap);
    va_end(ap);
  }
  return rc;
}
void sqlite4_mm_destroy(sqlite4_mm *pMM){
  if( pMM && pMM->pMethods->xFinal ) pMM->pMethods->xFinal(pMM);
}

/*
** Create a new memory allocation object.  eType determines the type of

Changes to src/sqlite.h.in.

115
116
117
118
119
120
121

122
123
124
125
126
127
128
129
130






131
132
133
134
135
136
137
...
165
166
167
168
169
170
171

















172
173
174
175
176
177
178
179





180
181
182
183
184
185
186
  void *(*xMalloc)(sqlite4_mm*, sqlite4_size_t);
  void *(*xRealloc)(sqlite4_mm*, void*, sqlite4_size_t);
  void (*xFree)(sqlite4_mm*, void*);
  sqlite4_size_t (*xMsize)(sqlite4_mm*, void*);
  int (*xMember)(sqlite4_mm*, const void*);
  void (*xBenign)(sqlite4_mm*, int);
  sqlite4_int64 (*xStat)(sqlite4_mm*, unsigned eType, unsigned bFlags);

  void (*xFinal)(sqlite4_mm*);
};

/*
** 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, ...);

/*
................................................................................
** 1.  If not, return 0.  If we cannot determine an answer, return -1.
**
** 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);


















/*
** Return statistics or status information about a memory allocator.
** Not all memory allocators provide all stat values.  Some memory
** allocators provides no states at all.  If a particular stat for
** a memory allocator is unavailable, then -1 is returned.
*/
sqlite4_int64 sqlite4_mm_stat(sqlite4_mm *pMM, int eType, unsigned flags);






/*
** 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".
*/
void sqlite4_mm_benign_failures(sqlite4_mm*, int bEnable);







>









>
>
>
>
>
>







 







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








>
>
>
>
>







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
...
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
  void *(*xMalloc)(sqlite4_mm*, sqlite4_size_t);
  void *(*xRealloc)(sqlite4_mm*, void*, sqlite4_size_t);
  void (*xFree)(sqlite4_mm*, void*);
  sqlite4_size_t (*xMsize)(sqlite4_mm*, void*);
  int (*xMember)(sqlite4_mm*, const void*);
  void (*xBenign)(sqlite4_mm*, int);
  sqlite4_int64 (*xStat)(sqlite4_mm*, unsigned eType, unsigned bFlags);
  int (*xCtrl)(sqlite4_mm*, unsigned eType, va_list);
  void (*xFinal)(sqlite4_mm*);
};

/*
** 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, ...);

/*
................................................................................
** 1.  If not, return 0.  If we cannot determine an answer, return -1.
**
** 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
#define SQLITE4_MMSTAT_MEMFAULT   7
#define SQLITE4_MMSTAT_FAULT      8

/*
** Bits for the bit vector third parameter ("flags") to sqlite4_mm_type()
*/
#define SQLITE4_MMSTAT_RESET      0x01

/*
** Return statistics or status information about a memory allocator.
** Not all memory allocators provide all stat values.  Some memory
** allocators provides no states at all.  If a particular stat for
** 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".
*/
void sqlite4_mm_benign_failures(sqlite4_mm*, int bEnable);

Changes to tool/mksqlite4c.tcl.

211
212
213
214
215
216
217

218
219
220
221
222
223

224
225
226
227
228
229
230
# used subroutines first in order to help the compiler find
# inlining opportunities.
#
foreach file {
   sqliteInt.h

   global.c

   ctime.c
   status.c
   date.c

   os.c
   fault.c

   mem0.c
   mem1.c
   mem2.c
   mem3.c
   mem5.c
   mutex.c
   mutex_noop.c







>






>







211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# used subroutines first in order to help the compiler find
# inlining opportunities.
#
foreach file {
   sqliteInt.h

   global.c
   env.c
   ctime.c
   status.c
   date.c

   os.c
   fault.c
   mem.c
   mem0.c
   mem1.c
   mem2.c
   mem3.c
   mem5.c
   mutex.c
   mutex_noop.c