SQLite

Check-in [9b97ce60c6]
Login

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

Overview
Comment:Add a mutex to mem6.c to make it threadsafe. (CVS 5468)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9b97ce60c63c8b8afa43496a97917afd5fc26c71
User & Date: danielk1977 2008-07-24 10:11:28.000
Context
2008-07-24
10:32
Do not run capi3.test or capi3c.test when testing memsys6. (CVS 5469) (check-in: e0a101117c user: danielk1977 tags: trunk)
10:11
Add a mutex to mem6.c to make it threadsafe. (CVS 5468) (check-in: 9b97ce60c6 user: danielk1977 tags: trunk)
08:20
Add mem6.c, a new allocator. More to come. (CVS 5467) (check-in: 192bc19218 user: danielk1977 tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/mem6.c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
** 2008 July 24
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** $Id: mem6.c,v 1.1 2008/07/24 08:20:40 danielk1977 Exp $
*/

#ifdef SQLITE_ENABLE_MEMSYS6

/*
** Maximum size of any allocation is ((1<<LOGMAX)*Mem6Chunk.nAtom). Since
** Mem6Chunk.nAtom is always at least 8, this is not really a practical












|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
** 2008 July 24
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** $Id: mem6.c,v 1.2 2008/07/24 10:11:28 danielk1977 Exp $
*/

#ifdef SQLITE_ENABLE_MEMSYS6

/*
** Maximum size of any allocation is ((1<<LOGMAX)*Mem6Chunk.nAtom). Since
** Mem6Chunk.nAtom is always at least 8, this is not really a practical
276
277
278
279
280
281
282


283
284
285
286
287









288
289
290
291
292
293
294
  return pChunk;
}

struct Mem6Global {
  sqlite3_mem_methods parent;     /* Used to allocate chunks */
  int nChunkSize;                 /* Size of each chunk, in bytes. */
  int nMinAlloc;                  /* Minimum allowed allocation size */



  /* This data structure will be fixed... */
  Mem6Chunk *pChunk;              /* Singly linked list of all memory chunks */
} mem6;










/*
** The argument is a pointer that may or may not have been allocated from
** one of the Mem6Chunk objects managed within mem6. If it is, return
** a pointer to the owner chunk. If not, return 0.
*/
static Mem6Chunk *findChunk(u8 *p){
  Mem6Chunk *pChunk;







>
>





>
>
>
>
>
>
>
>
>







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
  return pChunk;
}

struct Mem6Global {
  sqlite3_mem_methods parent;     /* Used to allocate chunks */
  int nChunkSize;                 /* Size of each chunk, in bytes. */
  int nMinAlloc;                  /* Minimum allowed allocation size */

  sqlite3_mutex *mutex;

  /* This data structure will be fixed... */
  Mem6Chunk *pChunk;              /* Singly linked list of all memory chunks */
} mem6;


static void mem6Enter(void){
  sqlite3_mutex_enter(mem6.mutex);
}

static void mem6Leave(void){
  sqlite3_mutex_leave(mem6.mutex);
}

/*
** The argument is a pointer that may or may not have been allocated from
** one of the Mem6Chunk objects managed within mem6. If it is, return
** a pointer to the owner chunk. If not, return 0.
*/
static Mem6Chunk *findChunk(u8 *p){
  Mem6Chunk *pChunk;
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
  for( pp=&mem6.pChunk; *pp!=pChunk; pp = &(*pp)->pNext );
  *pp = (*pp)->pNext;
  mem6.parent.xFree(pChunk);
}

static void *memsys6Malloc(int nByte){
  Mem6Chunk *pChunk;
  void *p;


  if( nByte>=mem6.nChunkSize/3 ){
    return mem6.parent.xMalloc(nByte);
  }
  for(pChunk=mem6.pChunk; pChunk; pChunk=pChunk->pNext){
    p = chunkMalloc(pChunk, nByte);
    if( p ){
      return p;
    }
  }

  p = mem6.parent.xMalloc(mem6.nChunkSize);
  if( p ){
    pChunk = chunkInit((u8 *)p, mem6.nChunkSize, mem6.nMinAlloc);
    pChunk->pNext = mem6.pChunk;
    mem6.pChunk = pChunk;
    p = chunkMalloc(pChunk, nByte);
    assert(p);
  }




  return p;
}

static int memsys6Size(void *p){
  Mem6Chunk *pChunk = findChunk(p);



  return (pChunk ? chunkSize(pChunk, p) : mem6.parent.xSize(p));


}

static void memsys6Free(void *p){
  Mem6Chunk *pChunk = findChunk(p);



  if( pChunk ){
    chunkFree(pChunk, p);
    if( chunkIsEmpty(pChunk) ){
      freeChunk(pChunk);
    }
  }else{
    mem6.parent.xFree(p);
  }

}

static void *memsys6Realloc(void *p, int nByte){
  Mem6Chunk *pChunk = findChunk(p);
  void *p2;

  if( !pChunk ){
    return mem6.parent.xRealloc(p, nByte);
  }


  p2 = memsys6Malloc(nByte);
  if( p2 ){
    assert( memsys6Size(p)<nByte );
    memcpy(p2, p, memsys6Size(p));
    memsys6Free(p);
  }


  return p2;
}


static int memsys6Roundup(int n){
  int iFullSz;
  for(iFullSz=mem6.nMinAlloc; iFullSz<n; iFullSz *= 2);
  return iFullSz;
}

static int memsys6Init(void *pCtx){

  mem6.parent = *sqlite3MemGetDefault();
  mem6.nChunkSize = (1<<16);
  mem6.nMinAlloc = 16;
  mem6.pChunk = 0;





  /* Initialize the parent allocator. */



  mem6.parent.xInit(mem6.parent.pAppData);




  return SQLITE_OK;
}

static void memsys6Shutdown(void *pCtx){
}








|
>
>

|
|
|
|
<
<

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




|
>
>
>
|
>
>



|
>
>
>








>



<


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











>





>
>
>
>

>
>
>

>
>
>







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
  for( pp=&mem6.pChunk; *pp!=pChunk; pp = &(*pp)->pNext );
  *pp = (*pp)->pNext;
  mem6.parent.xFree(pChunk);
}

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

  mem6Enter();
  if( nByte>=mem6.nChunkSize/3 ){
    p = mem6.parent.xMalloc(nByte);
  }else{
    for(pChunk=mem6.pChunk; !p && pChunk; pChunk=pChunk->pNext){
      p = chunkMalloc(pChunk, nByte);


    }
  
    if( !p ){
      p = mem6.parent.xMalloc(mem6.nChunkSize);
      if( p ){
        pChunk = chunkInit((u8 *)p, mem6.nChunkSize, mem6.nMinAlloc);
        pChunk->pNext = mem6.pChunk;
        mem6.pChunk = pChunk;
        p = chunkMalloc(pChunk, nByte);
        assert(p);
      }
    }
  }
  mem6Leave();

  return p;
}

static int memsys6Size(void *p){
  Mem6Chunk *pChunk;
  int iSize;
  mem6Enter();
  pChunk = findChunk(p);
  iSize = (pChunk ? chunkSize(pChunk, p) : mem6.parent.xSize(p));
  mem6Leave();
  return iSize;
}

static void memsys6Free(void *p){
  Mem6Chunk *pChunk;

  mem6Enter();
  pChunk = findChunk(p);
  if( pChunk ){
    chunkFree(pChunk, p);
    if( chunkIsEmpty(pChunk) ){
      freeChunk(pChunk);
    }
  }else{
    mem6.parent.xFree(p);
  }
  mem6Leave();
}

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

  void *p2;

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


    p2 = p;
  }else{
    p2 = memsys6Malloc(nByte);
    if( p && p2 ){

      memcpy(p2, p, memsys6Size(p));
      memsys6Free(p);
    }
  }

  return p2;
}


static int memsys6Roundup(int n){
  int iFullSz;
  for(iFullSz=mem6.nMinAlloc; iFullSz<n; iFullSz *= 2);
  return iFullSz;
}

static int memsys6Init(void *pCtx){
  u8 bMemstat = sqlite3Config.bMemstat;
  mem6.parent = *sqlite3MemGetDefault();
  mem6.nChunkSize = (1<<16);
  mem6.nMinAlloc = 16;
  mem6.pChunk = 0;

  if( !bMemstat ){
    mem6.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
  }

  /* Initialize the parent allocator. */
#ifdef SQLITE_MEMDEBUG
  sqlite3Config.bMemstat = 1;
#endif
  mem6.parent.xInit(mem6.parent.pAppData);
#ifdef SQLITE_MEMDEBUG
  sqlite3Config.bMemstat = bMemstat;
#endif

  return SQLITE_OK;
}

static void memsys6Shutdown(void *pCtx){
}

Changes to test/permutations.test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 2008 June 21
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# $Id: permutations.test,v 1.14 2008/07/24 08:20:40 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Argument processing.
#
set ::testmode [lindex $argv 0]











|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 2008 June 21
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# $Id: permutations.test,v 1.15 2008/07/24 10:11:29 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Argument processing.
#
set ::testmode [lindex $argv 0]
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
  } -shutdown {
    catch {db close}
    sqlite3_shutdown
    install_mutex_counters 0
  }
}

  run_tests "memsys6" -description {
    Run tests using the allocator in mem6.c.
  } -include {
    select1.test 
  } -initialize {
    catch {db close}
    sqlite3_reset_auto_extension
    sqlite3_shutdown
    sqlite3_config_chunkalloc
    install_malloc_faultsim 1 
    sqlite3_initialize
    autoinstall_test_functions
  } -shutdown {
    catch {db close}
    sqlite3_reset_auto_extension
    sqlite3_shutdown
    sqlite3_config_heap 0 0
    install_malloc_faultsim 1 
    sqlite3_initialize
  }

# run_tests "crash_safe_append" -description {
#   Run crash.test with persistent journals on a SAFE_APPEND file-system.
# } -initialize {
#   rename crashsql sa_crashsql
#   proc crashsql {args} {
#     set options [lrange $args 0 [expr {[llength $args]-2}]]







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







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
  } -shutdown {
    catch {db close}
    sqlite3_shutdown
    install_mutex_counters 0
  }
}

run_tests "memsys6" -description {
  Run tests using the allocator in mem6.c.


} -initialize {
  catch {db close}
  sqlite3_reset_auto_extension
  sqlite3_shutdown
  sqlite3_config_chunkalloc
  install_malloc_faultsim 1 
  sqlite3_initialize
  autoinstall_test_functions
} -shutdown {
  catch {db close}
  sqlite3_reset_auto_extension
  sqlite3_shutdown
  sqlite3_config_heap 0 0
  install_malloc_faultsim 1 
  sqlite3_initialize
}

# run_tests "crash_safe_append" -description {
#   Run crash.test with persistent journals on a SAFE_APPEND file-system.
# } -initialize {
#   rename crashsql sa_crashsql
#   proc crashsql {args} {
#     set options [lrange $args 0 [expr {[llength $args]-2}]]