1
2
3
4
5
6
7
8
9
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
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
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
167
168
169
170
171
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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
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
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
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
469
470
471
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
|
/*
** 2001 September 15
**
** 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.
**
*************************************************************************
**
** Memory allocation functions used throughout sqlite.
**
** $Id: malloc.c,v 1.34 2008/08/05 17:53:23 drh Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>
/*
** This routine runs when the memory allocator sees that the
** total memory allocation is about to exceed the soft heap
** limit.
*/
static void softHeapLimitEnforcer(
void *NotUsed,
sqlite3_int64 inUse,
int allocSize
){
sqlite3_release_memory(allocSize);
}
/*
** Set the soft heap-size limit for the library. Passing a zero or
** negative value indicates no limit.
*/
void sqlite3_soft_heap_limit(int n){
sqlite3_uint64 iLimit;
int overage;
if( n<0 ){
iLimit = 0;
}else{
iLimit = n;
}
sqlite3_initialize();
if( iLimit>0 ){
sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
}else{
sqlite3_memory_alarm(0, 0, 0);
}
overage = sqlite3_memory_used() - n;
if( overage>0 ){
sqlite3_release_memory(overage);
}
}
/*
** Attempt to release up to n bytes of non-essential memory currently
** held by SQLite. An example of non-essential memory is memory used to
** cache database pages that are not currently in use.
*/
int sqlite3_release_memory(int n){
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
int nRet = sqlite3VdbeReleaseMemory(n);
nRet += sqlite3PagerReleaseMemory(n-nRet);
return nRet;
#else
return SQLITE_OK;
#endif
}
/*
** State information local to the memory allocation subsystem.
*/
static struct {
sqlite3_mutex *mutex; /* Mutex to serialize access */
/*
** The alarm callback and its arguments. The mem0.mutex lock will
** be held while the callback is running. Recursive calls into
** the memory subsystem are allowed, but no new callbacks will be
** issued. The alarmBusy variable is set to prevent recursive
** callbacks.
*/
sqlite3_int64 alarmThreshold;
void (*alarmCallback)(void*, sqlite3_int64,int);
void *alarmArg;
int alarmBusy;
/*
** Pointers to the end of sqlite3Config.pScratch and
** sqlite3Config.pPage to a block of memory that records
** which pages are available.
*/
u32 *aScratchFree;
u32 *aPageFree;
/* Number of free pages for scratch and page-cache memory */
u32 nScratchFree;
u32 nPageFree;
} mem0;
/*
** Initialize the memory allocation subsystem.
*/
int sqlite3MallocInit(void){
if( sqlite3Config.m.xMalloc==0 ){
sqlite3MemSetDefault();
}
memset(&mem0, 0, sizeof(mem0));
if( sqlite3Config.bCoreMutex ){
mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
}
if( sqlite3Config.pScratch && sqlite3Config.szScratch>=100
&& sqlite3Config.nScratch>=0 ){
int i;
sqlite3Config.szScratch -= 4;
mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
[sqlite3Config.szScratch*sqlite3Config.nScratch];
for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
mem0.nScratchFree = sqlite3Config.nScratch;
}else{
sqlite3Config.pScratch = 0;
sqlite3Config.szScratch = 0;
}
if( sqlite3Config.pPage && sqlite3Config.szPage>=512
&& sqlite3Config.nPage>=1 ){
int i;
int overhead;
int sz = sqlite3Config.szPage;
int n = sqlite3Config.nPage;
overhead = (4*n + sz - 1)/sz;
sqlite3Config.nPage -= overhead;
mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
[sqlite3Config.szPage*sqlite3Config.nPage];
for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
mem0.nPageFree = sqlite3Config.nPage;
}else{
sqlite3Config.pPage = 0;
sqlite3Config.szPage = 0;
}
return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
}
/*
** Deinitialize the memory allocation subsystem.
*/
void sqlite3MallocEnd(void){
sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
memset(&mem0, 0, sizeof(mem0));
}
/*
** Return the amount of memory currently checked out.
*/
sqlite3_int64 sqlite3_memory_used(void){
int n, mx;
sqlite3_int64 res;
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
return res;
}
/*
** 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.
*/
sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
int n, mx;
sqlite3_int64 res;
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
return res;
}
/*
** Change the alarm callback
*/
int sqlite3_memory_alarm(
void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
void *pArg,
sqlite3_int64 iThreshold
){
sqlite3_mutex_enter(mem0.mutex);
mem0.alarmCallback = xCallback;
mem0.alarmArg = pArg;
mem0.alarmThreshold = iThreshold;
sqlite3_mutex_leave(mem0.mutex);
return SQLITE_OK;
}
/*
** Trigger the alarm
*/
static void sqlite3MallocAlarm(int nByte){
void (*xCallback)(void*,sqlite3_int64,int);
sqlite3_int64 nowUsed;
void *pArg;
if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
mem0.alarmBusy = 1;
xCallback = mem0.alarmCallback;
nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
pArg = mem0.alarmArg;
sqlite3_mutex_leave(mem0.mutex);
xCallback(pArg, nowUsed, nByte);
sqlite3_mutex_enter(mem0.mutex);
mem0.alarmBusy = 0;
}
/*
** Do a memory allocation with statistics and alarms. Assume the
** lock is already held.
*/
static int mallocWithAlarm(int n, void **pp){
int nFull;
void *p;
assert( sqlite3_mutex_held(mem0.mutex) );
nFull = sqlite3Config.m.xRoundup(n);
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
if( mem0.alarmCallback!=0 ){
int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
if( nUsed+nFull >= mem0.alarmThreshold ){
sqlite3MallocAlarm(nFull);
}
}
p = sqlite3Config.m.xMalloc(nFull);
if( p==0 && mem0.alarmCallback ){
sqlite3MallocAlarm(nFull);
p = sqlite3Config.m.xMalloc(nFull);
}
if( p ){
nFull = sqlite3MallocSize(p);
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
}
*pp = p;
return nFull;
}
/*
** Allocate memory. This routine is like sqlite3_malloc() except that it
** assumes the memory subsystem has already been initialized.
*/
void *sqlite3Malloc(int n){
void *p;
if( n<=0 ){
p = 0;
}else if( sqlite3Config.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
mallocWithAlarm(n, &p);
sqlite3_mutex_leave(mem0.mutex);
}else{
p = sqlite3Config.m.xMalloc(n);
}
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 *sqlite3_malloc(int n){
#ifndef SQLITE_OMIT_AUTOINIT
if( sqlite3_initialize() ) return 0;
#endif
return sqlite3Malloc(n);
}
/*
** Each thread may only have a single outstanding allocation from
** xScratchMalloc(). We verify this constraint in the single-threaded
** case by setting scratchAllocOut to 1 when an allocation
** is outstanding clearing it when the allocation is freed.
*/
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
static int scratchAllocOut = 0;
#endif
/*
** Allocate memory that is to be used and released right away.
** This routine is similar to alloca() in that it is not intended
** for situations where the memory might be held long-term. This
** routine is intended to get memory to old large transient data
** structures that would not normally fit on the stack of an
** embedded processor.
*/
void *sqlite3ScratchMalloc(int n){
void *p;
assert( n>0 );
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
/* Verify that no more than one scratch allocation per thread
** is outstanding at one time. (This is only checked in the
** single-threaded case since checking in the multi-threaded case
** would be much more complicated.) */
assert( scratchAllocOut==0 );
#endif
if( sqlite3Config.szScratch<n ){
goto scratch_overflow;
}else{
sqlite3_mutex_enter(mem0.mutex);
if( mem0.nScratchFree==0 ){
sqlite3_mutex_leave(mem0.mutex);
goto scratch_overflow;
}else{
int i;
i = mem0.aScratchFree[--mem0.nScratchFree];
sqlite3_mutex_leave(mem0.mutex);
i *= sqlite3Config.szScratch;
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
p = (void*)&((char*)sqlite3Config.pScratch)[i];
}
}
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
scratchAllocOut = p!=0;
#endif
return p;
scratch_overflow:
if( sqlite3Config.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
n = mallocWithAlarm(n, &p);
if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
sqlite3_mutex_leave(mem0.mutex);
}else{
p = sqlite3Config.m.xMalloc(n);
}
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
scratchAllocOut = p!=0;
#endif
return p;
}
void sqlite3ScratchFree(void *p){
if( p ){
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
/* Verify that no more than one scratch allocation per thread
** is outstanding at one time. (This is only checked in the
** single-threaded case since checking in the multi-threaded case
** would be much more complicated.) */
assert( scratchAllocOut==1 );
scratchAllocOut = 0;
#endif
if( sqlite3Config.pScratch==0
|| p<sqlite3Config.pScratch
|| p>=(void*)mem0.aScratchFree ){
if( sqlite3Config.bMemstat ){
int iSize = sqlite3MallocSize(p);
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
sqlite3Config.m.xFree(p);
sqlite3_mutex_leave(mem0.mutex);
}else{
sqlite3Config.m.xFree(p);
}
}else{
int i;
i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
i /= sqlite3Config.szScratch;
assert( i>=0 && i<sqlite3Config.nScratch );
sqlite3_mutex_enter(mem0.mutex);
assert( mem0.nScratchFree<sqlite3Config.nScratch );
mem0.aScratchFree[mem0.nScratchFree++] = i;
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
sqlite3_mutex_leave(mem0.mutex);
}
}
}
/*
** Allocate memory to be used by the page cache. Make use of the
** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
** and that memory is of the right size and is not completely
** consumed. Otherwise, failover to sqlite3Malloc().
*/
void *sqlite3PageMalloc(int n){
void *p;
assert( n>0 );
assert( (n & (n-1))==0 );
assert( n>=512 && n<=32768 );
if( sqlite3Config.szPage<n ){
goto page_overflow;
}else{
sqlite3_mutex_enter(mem0.mutex);
if( mem0.nPageFree==0 ){
sqlite3_mutex_leave(mem0.mutex);
goto page_overflow;
}else{
int i;
i = mem0.aPageFree[--mem0.nPageFree];
sqlite3_mutex_leave(mem0.mutex);
i *= sqlite3Config.szPage;
sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
p = (void*)&((char*)sqlite3Config.pPage)[i];
}
}
return p;
page_overflow:
if( sqlite3Config.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
n = mallocWithAlarm(n, &p);
if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
sqlite3_mutex_leave(mem0.mutex);
}else{
p = sqlite3Config.m.xMalloc(n);
}
return p;
}
void sqlite3PageFree(void *p){
if( p ){
if( sqlite3Config.pPage==0
|| p<sqlite3Config.pPage
|| p>=(void*)mem0.aPageFree ){
/* In this case, the page allocation was obtained from a regular
** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
** "overflow"). Free the block with sqlite3_mem_methods.xFree().
*/
if( sqlite3Config.bMemstat ){
int iSize = sqlite3MallocSize(p);
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
sqlite3Config.m.xFree(p);
sqlite3_mutex_leave(mem0.mutex);
}else{
sqlite3Config.m.xFree(p);
}
}else{
/* The page allocation was allocated from the sqlite3Config.pPage
** buffer. In this case all that is add the index of the page in
** the sqlite3Config.pPage array to the set of free indexes stored
** in the mem0.aPageFree[] array.
*/
int i;
i = (u8 *)p - (u8 *)sqlite3Config.pPage;
i /= sqlite3Config.szPage;
assert( i>=0 && i<sqlite3Config.nPage );
sqlite3_mutex_enter(mem0.mutex);
assert( mem0.nPageFree<sqlite3Config.nPage );
mem0.aPageFree[mem0.nPageFree++] = i;
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
sqlite3_mutex_leave(mem0.mutex);
#if !defined(NDEBUG) && 0
/* Assert that a duplicate was not just inserted into aPageFree[]. */
for(i=0; i<mem0.nPageFree-1; i++){
assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
}
#endif
}
}
}
/*
** TRUE if p is a lookaside memory allocation from db
*/
static int isLookaside(sqlite3 *db, void *p){
return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
}
/*
** Return the size of a memory allocation previously obtained from
** sqlite3Malloc() or sqlite3_malloc().
*/
int sqlite3MallocSize(void *p){
return sqlite3Config.m.xSize(p);
}
int sqlite3DbMallocSize(sqlite3 *db, void *p){
if( isLookaside(db, p) ){
return db->lookaside.sz;
}else{
return sqlite3Config.m.xSize(p);
}
}
/*
** Free memory previously obtained from sqlite3Malloc().
*/
void sqlite3_free(void *p){
if( p==0 ) return;
if( sqlite3Config.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
sqlite3Config.m.xFree(p);
sqlite3_mutex_leave(mem0.mutex);
}else{
sqlite3Config.m.xFree(p);
}
}
/*
** Free memory that might be associated with a particular database
** connection.
*/
void sqlite3DbFree(sqlite3 *db, void *p){
if( isLookaside(db, p) ){
LookasideSlot *pBuf = (LookasideSlot*)p;
pBuf->pNext = db->lookaside.pFree;
db->lookaside.pFree = pBuf;
db->lookaside.nOut--;
}else{
sqlite3_free(p);
}
}
/*
** Change the size of an existing memory allocation
*/
void *sqlite3Realloc(void *pOld, int nBytes){
int nOld, nNew;
void *pNew;
if( pOld==0 ){
return sqlite3Malloc(nBytes);
}
if( nBytes<=0 ){
sqlite3_free(pOld);
return 0;
}
nOld = sqlite3MallocSize(pOld);
if( sqlite3Config.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
nNew = sqlite3Config.m.xRoundup(nBytes);
if( nOld==nNew ){
pNew = pOld;
}else{
if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
mem0.alarmThreshold ){
sqlite3MallocAlarm(nNew-nOld);
}
pNew = sqlite3Config.m.xRealloc(pOld, nNew);
if( pNew==0 && mem0.alarmCallback ){
sqlite3MallocAlarm(nBytes);
pNew = sqlite3Config.m.xRealloc(pOld, nNew);
}
if( pNew ){
nNew = sqlite3MallocSize(pNew);
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
}
}
sqlite3_mutex_leave(mem0.mutex);
}else{
pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
}
return pNew;
}
/*
** The public interface to sqlite3Realloc. Make sure that the memory
** subsystem is initialized prior to invoking sqliteRealloc.
*/
void *sqlite3_realloc(void *pOld, int n){
#ifndef SQLITE_OMIT_AUTOINIT
if( sqlite3_initialize() ) return 0;
#endif
return sqlite3Realloc(pOld, n);
}
/*
** Allocate and zero memory.
*/
void *sqlite3MallocZero(int n){
void *p = sqlite3Malloc(n);
if( p ){
memset(p, 0, n);
}
return p;
}
/*
** Allocate and zero memory. If the allocation fails, make
** the mallocFailed flag in the connection pointer.
*/
void *sqlite3DbMallocZero(sqlite3 *db, int n){
void *p = sqlite3DbMallocRaw(db, n);
if( p ){
memset(p, 0, n);
}
return p;
}
/*
** Allocate and zero memory. If the allocation fails, make
** the mallocFailed flag in the connection pointer.
*/
void *sqlite3DbMallocRaw(sqlite3 *db, int n){
void *p;
if( db ){
LookasideSlot *pBuf;
if( db->mallocFailed ){
return 0;
}
if( db->lookaside.bEnabled && n<=db->lookaside.sz
&& (pBuf = db->lookaside.pFree)!=0 ){
db->lookaside.pFree = pBuf->pNext;
db->lookaside.nOut++;
if( db->lookaside.nOut>db->lookaside.mxOut ){
db->lookaside.mxOut = db->lookaside.nOut;
}
return (void*)pBuf;
}
}
p = sqlite3Malloc(n);
if( !p && db ){
db->mallocFailed = 1;
}
return p;
}
/*
** Resize the block of memory pointed to by p to n bytes. If the
** resize fails, set the mallocFailed flag in the connection object.
*/
void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
void *pNew = 0;
if( db->mallocFailed==0 ){
if( p==0 ){
return sqlite3DbMallocRaw(db, n);
}
if( isLookaside(db, p) ){
if( n<=db->lookaside.sz ){
return p;
}
pNew = sqlite3DbMallocRaw(db, n);
if( pNew ){
memcpy(pNew, p, db->lookaside.sz);
sqlite3DbFree(db, p);
}
}else{
pNew = sqlite3_realloc(p, n);
if( !pNew ){
db->mallocFailed = 1;
}
}
}
return pNew;
}
/*
** Attempt to reallocate p. If the reallocation fails, then free p
** and set the mallocFailed flag in the database connection.
*/
void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
void *pNew;
pNew = sqlite3DbRealloc(db, p, n);
if( !pNew ){
sqlite3DbFree(db, p);
}
return pNew;
}
/*
** Make a copy of a string in memory obtained from sqliteMalloc(). These
** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
** is because when memory debugging is turned on, these two functions are
** called via macros that record the current file and line number in the
** ThreadData structure.
*/
char *sqlite3DbStrDup(sqlite3 *db, const char *z){
char *zNew;
size_t n;
if( z==0 ){
return 0;
}
n = strlen(z)+1;
assert( (n&0x7fffffff)==n );
zNew = sqlite3DbMallocRaw(db, (int)n);
if( zNew ){
memcpy(zNew, z, n);
}
return zNew;
}
char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
char *zNew;
if( z==0 ){
return 0;
}
assert( (n&0x7fffffff)==n );
zNew = sqlite3DbMallocRaw(db, n+1);
if( zNew ){
memcpy(zNew, z, n);
zNew[n] = 0;
}
return zNew;
}
/*
** Create a string from the zFromat argument and the va_list that follows.
** Store the string in memory obtained from sqliteMalloc() and make *pz
** point to that string.
*/
void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
va_list ap;
char *z;
va_start(ap, zFormat);
z = sqlite3VMPrintf(db, zFormat, ap);
va_end(ap);
sqlite3DbFree(db, *pz);
*pz = z;
}
/*
** This function must be called before exiting any API function (i.e.
** returning control to the user) that has called sqlite3_malloc or
** sqlite3_realloc.
**
** The returned value is normally a copy of the second argument to this
** function. However, if a malloc() failure has occured since the previous
** invocation SQLITE_NOMEM is returned instead.
**
** If the first argument, db, is not NULL and a malloc() error has occured,
** then the connection error-code (the value returned by sqlite3_errcode())
** is set to SQLITE_NOMEM.
*/
int sqlite3ApiExit(sqlite3* db, int rc){
/* If the db handle is not NULL, then we must hold the connection handle
** mutex here. Otherwise the read (and possible write) of db->mallocFailed
** is unsafe, as is the call to sqlite3Error().
*/
assert( !db || sqlite3_mutex_held(db->mutex) );
if( db && db->mallocFailed ){
sqlite3Error(db, SQLITE_NOMEM, 0);
db->mallocFailed = 0;
rc = SQLITE_NOMEM;
}
return rc & (db ? db->errMask : 0xff);
}
|