/ Check-in [0de54891]
Login

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

Overview
Comment:Speed up the xFree() method of the mem6.c allocator by storing the offset from the pointer to the start of its chunk in a header field. (CVS 5475)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0de54891d4901dec929459d27004efe87106ffa2
User & Date: danielk1977 2008-07-25 10:40:19
Context
2008-07-25
12:39
Fix a build problem on tclsqlite in Makefile.in. (CVS 5476) check-in: e7a60480 user: drh tags: trunk
10:40
Speed up the xFree() method of the mem6.c allocator by storing the offset from the pointer to the start of its chunk in a header field. (CVS 5475) check-in: 0de54891 user: danielk1977 tags: trunk
09:24
Change mem6.c to use the malloc() and free() functions directly, instead of going via another sqlite3_mem_methods structure. (CVS 5474) check-in: cfa65e23 user: danielk1977 tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/mem6.c.

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
...
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
** the same as that used by mem5.c. 
**
** This strategy is designed to prevent the default memory allocation
** system (usually the system malloc) from suffering from heap 
** fragmentation. On some systems, heap fragmentation can cause a 
** significant real-time slowdown.
**
** $Id: mem6.c,v 1.4 2008/07/25 09:24:13 danielk1977 Exp $
*/

#ifdef SQLITE_ENABLE_MEMSYS6

#include "sqliteInt.h"

/*
................................................................................
  free(pChunk);
}

static void *memsys6Malloc(int nByte){
  Mem6Chunk *pChunk;
  void *p = 0;
  int nTotal = nByte+8;


  mem6Enter();
  if( nTotal>mem6.nThreshold ){
    p = malloc(nTotal);
  }else{
    for(pChunk=mem6.pChunk; !p && pChunk; pChunk=pChunk->pNext){
      p = chunkMalloc(pChunk, nTotal);



    }
  
    if( !p ){
      int iSize = nextChunkSize();
      p = malloc(iSize);
      if( p ){
        pChunk = chunkInit((u8 *)p, iSize, mem6.nMinAlloc);
        pChunk->pNext = mem6.pChunk;
        mem6.pChunk = pChunk;
        p = chunkMalloc(pChunk, nTotal);
        assert(p);
      }
    }


  }
  mem6Leave();




  ((sqlite3_int64 *)p)[0] = nByte;

  return &((sqlite3_int64 *)p)[1];
}

static int memsys6Size(void *pPrior){
  sqlite3_int64 *p;
  if( pPrior==0 ) return 0;
  p = (sqlite3_int64*)pPrior;
  p--;
  return p[0];
}

static void memsys6Free(void *pPrior){
  Mem6Chunk *pChunk;
  void *p = &((sqlite3_int64 *)pPrior)[-1];

  mem6Enter();
  pChunk = findChunk(p);
  if( pChunk ){
    chunkFree(pChunk, p);
    if( chunkIsEmpty(pChunk) ){
      freeChunk(pChunk);
    }

  }else{
    free(p);
  }
  mem6Leave();
}

static void *memsys6Realloc(void *p, int nByte){
  void *p2;

  if( p && nByte<=memsys6Size(p) ){
    p2 = p;







|







 







>





|

>
>
>













>
>



>
>
>
|
>
|



<

<
<
|



|
|
|
|
|
|




>



<







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
...
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
** the same as that used by mem5.c. 
**
** This strategy is designed to prevent the default memory allocation
** system (usually the system malloc) from suffering from heap 
** fragmentation. On some systems, heap fragmentation can cause a 
** significant real-time slowdown.
**
** $Id: mem6.c,v 1.5 2008/07/25 10:40:19 danielk1977 Exp $
*/

#ifdef SQLITE_ENABLE_MEMSYS6

#include "sqliteInt.h"

/*
................................................................................
  free(pChunk);
}

static void *memsys6Malloc(int nByte){
  Mem6Chunk *pChunk;
  void *p = 0;
  int nTotal = nByte+8;
  int iOffset = 0;

  mem6Enter();
  if( nTotal>mem6.nThreshold ){
    p = malloc(nTotal);
  }else{
    for(pChunk=mem6.pChunk; pChunk; pChunk=pChunk->pNext){
      p = chunkMalloc(pChunk, nTotal);
      if( p ){
        break;
      }
    }
  
    if( !p ){
      int iSize = nextChunkSize();
      p = malloc(iSize);
      if( p ){
        pChunk = chunkInit((u8 *)p, iSize, mem6.nMinAlloc);
        pChunk->pNext = mem6.pChunk;
        mem6.pChunk = pChunk;
        p = chunkMalloc(pChunk, nTotal);
        assert(p);
      }
    }

    iOffset = ((u8*)p - (u8*)pChunk);
  }
  mem6Leave();

  if( !p ){
    return 0;
  }
  ((u32 *)p)[0] = iOffset;
  ((u32 *)p)[1] = nByte;
  return &((u32 *)p)[2];
}

static int memsys6Size(void *pPrior){

  if( pPrior==0 ) return 0;


  return ((u32*)pPrior)[-1];
}

static void memsys6Free(void *pPrior){
  int iSlot;
  void *p = &((u32 *)pPrior)[-2];
  iSlot = ((u32 *)p)[0];
  if( iSlot ){
    mem6Enter();
    Mem6Chunk *pChunk = (Mem6Chunk *)(&((u8 *)p)[-1 * iSlot]);
    chunkFree(pChunk, p);
    if( chunkIsEmpty(pChunk) ){
      freeChunk(pChunk);
    }
    mem6Leave();
  }else{
    free(p);
  }

}

static void *memsys6Realloc(void *p, int nByte){
  void *p2;

  if( p && nByte<=memsys6Size(p) ){
    p2 = p;