blob: b34a04e8b63047f97fbf581665dc58a2684d98ff [file] [log] [blame]
drh0d180202008-02-14 23:26:56 +00001/*
2** 2007 October 14
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains the C functions that implement a memory
13** allocation subsystem for use by SQLite.
14**
15** This version of the memory allocation subsystem omits all
drh4c5514d2009-08-18 01:54:19 +000016** use of malloc(). The application gives SQLite a block of memory
danielk1977c66c0e12008-06-25 14:26:07 +000017** before calling sqlite3_initialize() from which allocations
18** are made and returned by the xMalloc() and xRealloc()
19** implementations. Once sqlite3_initialize() has been called,
20** the amount of memory available to SQLite is fixed and cannot
21** be changed.
drh0d180202008-02-14 23:26:56 +000022**
danielk1977c66c0e12008-06-25 14:26:07 +000023** This version of the memory allocation subsystem is included
24** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
drh0d180202008-02-14 23:26:56 +000025**
drh4c5514d2009-08-18 01:54:19 +000026** This memory allocator uses the following algorithm:
27**
28** 1. All memory allocations sizes are rounded up to a power of 2.
29**
drh7c6791c2009-08-18 14:48:53 +000030** 2. If two adjacent free blocks are the halves of a larger block,
peter.d.reid60ec9142014-09-06 16:39:46 +000031** then the two blocks are coalesced into the single larger block.
drh4c5514d2009-08-18 01:54:19 +000032**
33** 3. New memory is allocated from the first available free block.
34**
35** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
36** Concerning Dynamic Storage Allocation". Journal of the Association for
37** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
38**
39** Let n be the size of the largest allocation divided by the minimum
40** allocation size (after rounding all sizes up to a power of 2.) Let M
41** be the maximum amount of memory ever outstanding at one time. Let
42** N be the total amount of memory available for allocation. Robson
43** proved that this memory allocator will never breakdown due to
44** fragmentation as long as the following constraint holds:
45**
46** N >= M*(1 + log2(n)/2) - n + 1
47**
48** The sqlite3_status() logic tracks the maximum values of n and M so
49** that an application can, at any time, verify this constraint.
drh0d180202008-02-14 23:26:56 +000050*/
51#include "sqliteInt.h"
52
53/*
54** This version of the memory allocator is used only when
drhd1370b62008-10-28 18:58:20 +000055** SQLITE_ENABLE_MEMSYS5 is defined.
drh0d180202008-02-14 23:26:56 +000056*/
danielk1977c66c0e12008-06-25 14:26:07 +000057#ifdef SQLITE_ENABLE_MEMSYS5
drh0d180202008-02-14 23:26:56 +000058
59/*
drh2d7636e2008-02-16 16:21:45 +000060** A minimum allocation is an instance of the following structure.
61** Larger allocations are an array of these structures where the
62** size of the array is a power of 2.
drh4c5514d2009-08-18 01:54:19 +000063**
64** The size of this object must be a power of two. That fact is
65** verified in memsys5Init().
drh2d7636e2008-02-16 16:21:45 +000066*/
danielk19775099be52008-06-27 13:27:03 +000067typedef struct Mem5Link Mem5Link;
68struct Mem5Link {
69 int next; /* Index of next free chunk */
70 int prev; /* Index of previous free chunk */
drh0d180202008-02-14 23:26:56 +000071};
72
73/*
drh7c6791c2009-08-18 14:48:53 +000074** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
75** mem5.szAtom is always at least 8 and 32-bit integers are used,
drh4c5514d2009-08-18 01:54:19 +000076** it is not actually possible to reach this limit.
drh2d7636e2008-02-16 16:21:45 +000077*/
danielk19775099be52008-06-27 13:27:03 +000078#define LOGMAX 30
drh2d7636e2008-02-16 16:21:45 +000079
80/*
danielk1977c66c0e12008-06-25 14:26:07 +000081** Masks used for mem5.aCtrl[] elements.
drh2d7636e2008-02-16 16:21:45 +000082*/
drh7c6791c2009-08-18 14:48:53 +000083#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
drh2d7636e2008-02-16 16:21:45 +000084#define CTRL_FREE 0x20 /* True if not checked out */
85
86/*
drh0d180202008-02-14 23:26:56 +000087** All of the static variables used by this module are collected
danielk1977c66c0e12008-06-25 14:26:07 +000088** into a single structure named "mem5". This is to keep the
drh0d180202008-02-14 23:26:56 +000089** static variables organized and to reduce namespace pollution
90** when this module is combined with other in the amalgamation.
91*/
danielk19775c8f8582008-09-02 10:22:00 +000092static SQLITE_WSD struct Mem5Global {
drh0d180202008-02-14 23:26:56 +000093 /*
danielk197723bf0f42008-09-02 17:52:51 +000094 ** Memory available for allocation
drh0d180202008-02-14 23:26:56 +000095 */
drh7c6791c2009-08-18 14:48:53 +000096 int szAtom; /* Smallest possible allocation in bytes */
97 int nBlock; /* Number of szAtom sized blocks in zPool */
drh4c5514d2009-08-18 01:54:19 +000098 u8 *zPool; /* Memory available to be allocated */
drh0d180202008-02-14 23:26:56 +000099
100 /*
101 ** Mutex to control access to the memory allocation subsystem.
102 */
103 sqlite3_mutex *mutex;
drh2d7636e2008-02-16 16:21:45 +0000104
105 /*
106 ** Performance statistics
107 */
108 u64 nAlloc; /* Total number of calls to malloc */
109 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
110 u64 totalExcess; /* Total internal fragmentation */
111 u32 currentOut; /* Current checkout, including internal fragmentation */
112 u32 currentCount; /* Current number of distinct checkouts */
113 u32 maxOut; /* Maximum instantaneous currentOut */
114 u32 maxCount; /* Maximum instantaneous currentCount */
115 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
drh0d180202008-02-14 23:26:56 +0000116
117 /*
drh7c6791c2009-08-18 14:48:53 +0000118 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
119 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
120 ** and so forth.
drh0d180202008-02-14 23:26:56 +0000121 */
danielk19775099be52008-06-27 13:27:03 +0000122 int aiFreelist[LOGMAX+1];
drh0d180202008-02-14 23:26:56 +0000123
124 /*
drh2d7636e2008-02-16 16:21:45 +0000125 ** Space for tracking which blocks are checked out and the size
126 ** of each block. One byte per block.
drh0d180202008-02-14 23:26:56 +0000127 */
danielk1977c66c0e12008-06-25 14:26:07 +0000128 u8 *aCtrl;
drh0d180202008-02-14 23:26:56 +0000129
drhfcd71b62011-04-05 22:08:24 +0000130} mem5;
danielk19775c8f8582008-09-02 10:22:00 +0000131
drh4c5514d2009-08-18 01:54:19 +0000132/*
mistachkin3dfaf672013-08-14 00:20:23 +0000133** Access the static variable through a macro for SQLITE_OMIT_WSD.
drh4c5514d2009-08-18 01:54:19 +0000134*/
danielk19775c8f8582008-09-02 10:22:00 +0000135#define mem5 GLOBAL(struct Mem5Global, mem5)
drh0d180202008-02-14 23:26:56 +0000136
drh4c5514d2009-08-18 01:54:19 +0000137/*
138** Assuming mem5.zPool is divided up into an array of Mem5Link
mistachkin3dfaf672013-08-14 00:20:23 +0000139** structures, return a pointer to the idx-th such link.
drh4c5514d2009-08-18 01:54:19 +0000140*/
drh7c6791c2009-08-18 14:48:53 +0000141#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
danielk19775099be52008-06-27 13:27:03 +0000142
drh0d180202008-02-14 23:26:56 +0000143/*
danielk1977c66c0e12008-06-25 14:26:07 +0000144** Unlink the chunk at mem5.aPool[i] from list it is currently
145** on. It should be found on mem5.aiFreelist[iLogsize].
drh0d180202008-02-14 23:26:56 +0000146*/
drh2d7636e2008-02-16 16:21:45 +0000147static void memsys5Unlink(int i, int iLogsize){
148 int next, prev;
danielk1977c66c0e12008-06-25 14:26:07 +0000149 assert( i>=0 && i<mem5.nBlock );
danielk19775099be52008-06-27 13:27:03 +0000150 assert( iLogsize>=0 && iLogsize<=LOGMAX );
danielk1977c66c0e12008-06-25 14:26:07 +0000151 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
drh2d7636e2008-02-16 16:21:45 +0000152
danielk19775099be52008-06-27 13:27:03 +0000153 next = MEM5LINK(i)->next;
154 prev = MEM5LINK(i)->prev;
drh2d7636e2008-02-16 16:21:45 +0000155 if( prev<0 ){
danielk1977c66c0e12008-06-25 14:26:07 +0000156 mem5.aiFreelist[iLogsize] = next;
drh0d180202008-02-14 23:26:56 +0000157 }else{
danielk19775099be52008-06-27 13:27:03 +0000158 MEM5LINK(prev)->next = next;
drh0d180202008-02-14 23:26:56 +0000159 }
drh2d7636e2008-02-16 16:21:45 +0000160 if( next>=0 ){
danielk19775099be52008-06-27 13:27:03 +0000161 MEM5LINK(next)->prev = prev;
drh0d180202008-02-14 23:26:56 +0000162 }
drh0d180202008-02-14 23:26:56 +0000163}
164
165/*
danielk1977c66c0e12008-06-25 14:26:07 +0000166** Link the chunk at mem5.aPool[i] so that is on the iLogsize
drh2d7636e2008-02-16 16:21:45 +0000167** free list.
drh0d180202008-02-14 23:26:56 +0000168*/
drh2d7636e2008-02-16 16:21:45 +0000169static void memsys5Link(int i, int iLogsize){
170 int x;
danielk1977c66c0e12008-06-25 14:26:07 +0000171 assert( sqlite3_mutex_held(mem5.mutex) );
172 assert( i>=0 && i<mem5.nBlock );
danielk19775099be52008-06-27 13:27:03 +0000173 assert( iLogsize>=0 && iLogsize<=LOGMAX );
danielk1977c66c0e12008-06-25 14:26:07 +0000174 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
drh0d180202008-02-14 23:26:56 +0000175
danielk19775099be52008-06-27 13:27:03 +0000176 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
177 MEM5LINK(i)->prev = -1;
drh2d7636e2008-02-16 16:21:45 +0000178 if( x>=0 ){
danielk1977c66c0e12008-06-25 14:26:07 +0000179 assert( x<mem5.nBlock );
danielk19775099be52008-06-27 13:27:03 +0000180 MEM5LINK(x)->prev = i;
drh0d180202008-02-14 23:26:56 +0000181 }
danielk1977c66c0e12008-06-25 14:26:07 +0000182 mem5.aiFreelist[iLogsize] = i;
drh0d180202008-02-14 23:26:56 +0000183}
184
185/*
danielk19776b39c2e2008-06-25 14:57:53 +0000186** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
187** will already be held (obtained by code in malloc.c) if
danielk1977075c23a2008-09-01 18:34:20 +0000188** sqlite3GlobalConfig.bMemStat is true.
drh0d180202008-02-14 23:26:56 +0000189*/
drh2d7636e2008-02-16 16:21:45 +0000190static void memsys5Enter(void){
danielk19776b39c2e2008-06-25 14:57:53 +0000191 sqlite3_mutex_enter(mem5.mutex);
drh0d180202008-02-14 23:26:56 +0000192}
danielk1977c66c0e12008-06-25 14:26:07 +0000193static void memsys5Leave(void){
danielk19776b39c2e2008-06-25 14:57:53 +0000194 sqlite3_mutex_leave(mem5.mutex);
drh0d180202008-02-14 23:26:56 +0000195}
196
197/*
drh0d180202008-02-14 23:26:56 +0000198** Return the size of an outstanding allocation, in bytes. The
199** size returned omits the 8-byte header overhead. This only
200** works for chunks that are currently checked out.
201*/
danielk1977c66c0e12008-06-25 14:26:07 +0000202static int memsys5Size(void *p){
drh039ca6a2015-10-15 16:20:57 +0000203 int iSize, i;
204 assert( p!=0 );
205 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
206 assert( i>=0 && i<mem5.nBlock );
207 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
drh0d180202008-02-14 23:26:56 +0000208 return iSize;
209}
210
211/*
drh0d180202008-02-14 23:26:56 +0000212** Return a block of memory of at least nBytes in size.
drh4c5514d2009-08-18 01:54:19 +0000213** Return NULL if unable. Return NULL if nBytes==0.
214**
mistachkin3dfaf672013-08-14 00:20:23 +0000215** The caller guarantees that nByte is positive.
drh4c5514d2009-08-18 01:54:19 +0000216**
217** The caller has obtained a mutex prior to invoking this
218** routine so there is never any chance that two or more
219** threads can be in this routine at the same time.
drh0d180202008-02-14 23:26:56 +0000220*/
danielk1977c66c0e12008-06-25 14:26:07 +0000221static void *memsys5MallocUnsafe(int nByte){
222 int i; /* Index of a mem5.aPool[] slot */
223 int iBin; /* Index into mem5.aiFreelist[] */
drh2d7636e2008-02-16 16:21:45 +0000224 int iFullSz; /* Size of allocation rounded up to power of 2 */
225 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
drh0d180202008-02-14 23:26:56 +0000226
drh4c5514d2009-08-18 01:54:19 +0000227 /* nByte must be a positive */
228 assert( nByte>0 );
229
drheee4c8c2008-02-18 22:24:57 +0000230 /* Keep track of the maximum allocation request. Even unfulfilled
231 ** requests are counted */
danielk197700e13612008-11-17 19:18:54 +0000232 if( (u32)nByte>mem5.maxRequest ){
danielk1977c66c0e12008-06-25 14:26:07 +0000233 mem5.maxRequest = nByte;
drheee4c8c2008-02-18 22:24:57 +0000234 }
235
drh7c6791c2009-08-18 14:48:53 +0000236 /* Abort if the requested allocation size is larger than the largest
237 ** power of two that we can represent using 32-bit signed integers.
238 */
drh4c5514d2009-08-18 01:54:19 +0000239 if( nByte > 0x40000000 ){
240 return 0;
241 }
242
drheee4c8c2008-02-18 22:24:57 +0000243 /* Round nByte up to the next valid power of two */
drh62aaa6c2015-11-21 17:27:42 +0000244 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
drh0d180202008-02-14 23:26:56 +0000245
danielk1977c66c0e12008-06-25 14:26:07 +0000246 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
drheee4c8c2008-02-18 22:24:57 +0000247 ** block. If not, then split a block of the next larger power of
248 ** two in order to create a new free block of size iLogsize.
249 */
drhb6635872014-03-31 13:42:42 +0000250 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
drh413c3d32010-02-23 20:11:56 +0000251 if( iBin>LOGMAX ){
drhaf46dc12010-02-24 21:44:07 +0000252 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000253 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
254 return 0;
255 }
drh0c2df172013-11-24 00:46:00 +0000256 i = mem5.aiFreelist[iBin];
257 memsys5Unlink(i, iBin);
drh2d7636e2008-02-16 16:21:45 +0000258 while( iBin>iLogsize ){
259 int newSize;
260
261 iBin--;
262 newSize = 1 << iBin;
danielk1977c66c0e12008-06-25 14:26:07 +0000263 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
drh2d7636e2008-02-16 16:21:45 +0000264 memsys5Link(i+newSize, iBin);
drh0d180202008-02-14 23:26:56 +0000265 }
danielk1977c66c0e12008-06-25 14:26:07 +0000266 mem5.aCtrl[i] = iLogsize;
drh0d180202008-02-14 23:26:56 +0000267
drheee4c8c2008-02-18 22:24:57 +0000268 /* Update allocator performance statistics. */
danielk1977c66c0e12008-06-25 14:26:07 +0000269 mem5.nAlloc++;
270 mem5.totalAlloc += iFullSz;
271 mem5.totalExcess += iFullSz - nByte;
272 mem5.currentCount++;
273 mem5.currentOut += iFullSz;
274 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
275 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
drh0d180202008-02-14 23:26:56 +0000276
drh9d41bc12014-02-24 19:07:51 +0000277#ifdef SQLITE_DEBUG
278 /* Make sure the allocated memory does not assume that it is set to zero
279 ** or retains a value from a previous allocation */
280 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
281#endif
282
drheee4c8c2008-02-18 22:24:57 +0000283 /* Return a pointer to the allocated memory. */
drh7c6791c2009-08-18 14:48:53 +0000284 return (void*)&mem5.zPool[i*mem5.szAtom];
drh0d180202008-02-14 23:26:56 +0000285}
286
287/*
288** Free an outstanding memory allocation.
289*/
danielk1977c66c0e12008-06-25 14:26:07 +0000290static void memsys5FreeUnsafe(void *pOld){
drh2d7636e2008-02-16 16:21:45 +0000291 u32 size, iLogsize;
drh4c5514d2009-08-18 01:54:19 +0000292 int iBlock;
drh0d180202008-02-14 23:26:56 +0000293
danielk19775099be52008-06-27 13:27:03 +0000294 /* Set iBlock to the index of the block pointed to by pOld in
drh7c6791c2009-08-18 14:48:53 +0000295 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
danielk19775099be52008-06-27 13:27:03 +0000296 */
mistachkin3f9cd2a2013-12-03 22:33:35 +0000297 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
danielk19775099be52008-06-27 13:27:03 +0000298
299 /* Check that the pointer pOld points to a valid, non-free block. */
300 assert( iBlock>=0 && iBlock<mem5.nBlock );
drh7c6791c2009-08-18 14:48:53 +0000301 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
danielk19775099be52008-06-27 13:27:03 +0000302 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
303
304 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
drh2d7636e2008-02-16 16:21:45 +0000305 size = 1<<iLogsize;
danielk197700e13612008-11-17 19:18:54 +0000306 assert( iBlock+size-1<(u32)mem5.nBlock );
danielk19775099be52008-06-27 13:27:03 +0000307
308 mem5.aCtrl[iBlock] |= CTRL_FREE;
309 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
danielk1977c66c0e12008-06-25 14:26:07 +0000310 assert( mem5.currentCount>0 );
drh7c6791c2009-08-18 14:48:53 +0000311 assert( mem5.currentOut>=(size*mem5.szAtom) );
danielk1977c66c0e12008-06-25 14:26:07 +0000312 mem5.currentCount--;
drh7c6791c2009-08-18 14:48:53 +0000313 mem5.currentOut -= size*mem5.szAtom;
danielk1977c66c0e12008-06-25 14:26:07 +0000314 assert( mem5.currentOut>0 || mem5.currentCount==0 );
315 assert( mem5.currentCount>0 || mem5.currentOut==0 );
drh2d7636e2008-02-16 16:21:45 +0000316
danielk19775099be52008-06-27 13:27:03 +0000317 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
drh7c6791c2009-08-18 14:48:53 +0000318 while( ALWAYS(iLogsize<LOGMAX) ){
drh2d7636e2008-02-16 16:21:45 +0000319 int iBuddy;
danielk19775099be52008-06-27 13:27:03 +0000320 if( (iBlock>>iLogsize) & 1 ){
321 iBuddy = iBlock - size;
drh2d7636e2008-02-16 16:21:45 +0000322 }else{
danielk19775099be52008-06-27 13:27:03 +0000323 iBuddy = iBlock + size;
drh0d180202008-02-14 23:26:56 +0000324 }
danielk19775099be52008-06-27 13:27:03 +0000325 assert( iBuddy>=0 );
326 if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
danielk1977c66c0e12008-06-25 14:26:07 +0000327 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
drh2d7636e2008-02-16 16:21:45 +0000328 memsys5Unlink(iBuddy, iLogsize);
329 iLogsize++;
danielk19775099be52008-06-27 13:27:03 +0000330 if( iBuddy<iBlock ){
danielk1977c66c0e12008-06-25 14:26:07 +0000331 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
danielk19775099be52008-06-27 13:27:03 +0000332 mem5.aCtrl[iBlock] = 0;
333 iBlock = iBuddy;
drh2d7636e2008-02-16 16:21:45 +0000334 }else{
danielk19775099be52008-06-27 13:27:03 +0000335 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
danielk1977c66c0e12008-06-25 14:26:07 +0000336 mem5.aCtrl[iBuddy] = 0;
drh0d180202008-02-14 23:26:56 +0000337 }
drh2d7636e2008-02-16 16:21:45 +0000338 size *= 2;
drh0d180202008-02-14 23:26:56 +0000339 }
drh9d41bc12014-02-24 19:07:51 +0000340
341#ifdef SQLITE_DEBUG
342 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
343 ** not used after being freed */
344 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
345#endif
346
danielk19775099be52008-06-27 13:27:03 +0000347 memsys5Link(iBlock, iLogsize);
drh0d180202008-02-14 23:26:56 +0000348}
349
350/*
mistachkin3dfaf672013-08-14 00:20:23 +0000351** Allocate nBytes of memory.
drh0d180202008-02-14 23:26:56 +0000352*/
danielk1977c66c0e12008-06-25 14:26:07 +0000353static void *memsys5Malloc(int nBytes){
drh0d180202008-02-14 23:26:56 +0000354 sqlite3_int64 *p = 0;
355 if( nBytes>0 ){
drh2d7636e2008-02-16 16:21:45 +0000356 memsys5Enter();
danielk1977c66c0e12008-06-25 14:26:07 +0000357 p = memsys5MallocUnsafe(nBytes);
358 memsys5Leave();
drh0d180202008-02-14 23:26:56 +0000359 }
360 return (void*)p;
361}
362
363/*
364** Free memory.
drh4c5514d2009-08-18 01:54:19 +0000365**
366** The outer layer memory allocator prevents this routine from
367** being called with pPrior==0.
drh0d180202008-02-14 23:26:56 +0000368*/
danielk1977c66c0e12008-06-25 14:26:07 +0000369static void memsys5Free(void *pPrior){
drh4c5514d2009-08-18 01:54:19 +0000370 assert( pPrior!=0 );
danielk1977c66c0e12008-06-25 14:26:07 +0000371 memsys5Enter();
372 memsys5FreeUnsafe(pPrior);
373 memsys5Leave();
drh0d180202008-02-14 23:26:56 +0000374}
375
376/*
drh4c5514d2009-08-18 01:54:19 +0000377** Change the size of an existing memory allocation.
378**
379** The outer layer memory allocator prevents this routine from
drh7c6791c2009-08-18 14:48:53 +0000380** being called with pPrior==0.
381**
382** nBytes is always a value obtained from a prior call to
383** memsys5Round(). Hence nBytes is always a non-negative power
384** of two. If nBytes==0 that means that an oversize allocation
385** (an allocation larger than 0x40000000) was requested and this
386** routine should return 0 without freeing pPrior.
drh0d180202008-02-14 23:26:56 +0000387*/
danielk1977c66c0e12008-06-25 14:26:07 +0000388static void *memsys5Realloc(void *pPrior, int nBytes){
drh0d180202008-02-14 23:26:56 +0000389 int nOld;
390 void *p;
drh4c5514d2009-08-18 01:54:19 +0000391 assert( pPrior!=0 );
drh9f129f42010-08-31 15:27:32 +0000392 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
drh7c6791c2009-08-18 14:48:53 +0000393 assert( nBytes>=0 );
394 if( nBytes==0 ){
drh0d180202008-02-14 23:26:56 +0000395 return 0;
396 }
danielk1977c66c0e12008-06-25 14:26:07 +0000397 nOld = memsys5Size(pPrior);
drh2d7636e2008-02-16 16:21:45 +0000398 if( nBytes<=nOld ){
drh0d180202008-02-14 23:26:56 +0000399 return pPrior;
400 }
danielk1977c66c0e12008-06-25 14:26:07 +0000401 memsys5Enter();
402 p = memsys5MallocUnsafe(nBytes);
drh0d180202008-02-14 23:26:56 +0000403 if( p ){
drh2d7636e2008-02-16 16:21:45 +0000404 memcpy(p, pPrior, nOld);
danielk1977c66c0e12008-06-25 14:26:07 +0000405 memsys5FreeUnsafe(pPrior);
drh0d180202008-02-14 23:26:56 +0000406 }
danielk1977c66c0e12008-06-25 14:26:07 +0000407 memsys5Leave();
drh0d180202008-02-14 23:26:56 +0000408 return p;
409}
410
411/*
drh4c5514d2009-08-18 01:54:19 +0000412** Round up a request size to the next valid allocation size. If
413** the allocation is too large to be handled by this allocation system,
414** return 0.
drh7c6791c2009-08-18 14:48:53 +0000415**
416** All allocations must be a power of two and must be expressed by a
417** 32-bit signed integer. Hence the largest allocation is 0x40000000
418** or 1073741824 bytes.
danielk1977c66c0e12008-06-25 14:26:07 +0000419*/
420static int memsys5Roundup(int n){
421 int iFullSz;
drh4c5514d2009-08-18 01:54:19 +0000422 if( n > 0x40000000 ) return 0;
drh7c6791c2009-08-18 14:48:53 +0000423 for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
danielk1977c66c0e12008-06-25 14:26:07 +0000424 return iFullSz;
425}
426
drh4c5514d2009-08-18 01:54:19 +0000427/*
drh7c6791c2009-08-18 14:48:53 +0000428** Return the ceiling of the logarithm base 2 of iValue.
429**
430** Examples: memsys5Log(1) -> 0
431** memsys5Log(2) -> 1
432** memsys5Log(4) -> 2
433** memsys5Log(5) -> 3
434** memsys5Log(8) -> 3
435** memsys5Log(9) -> 4
drh4c5514d2009-08-18 01:54:19 +0000436*/
danielk19775099be52008-06-27 13:27:03 +0000437static int memsys5Log(int iValue){
438 int iLog;
drhfcd71b62011-04-05 22:08:24 +0000439 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
danielk19775099be52008-06-27 13:27:03 +0000440 return iLog;
441}
442
danielk1977c66c0e12008-06-25 14:26:07 +0000443/*
drh7c6791c2009-08-18 14:48:53 +0000444** Initialize the memory allocator.
drh1b257532009-08-18 15:33:44 +0000445**
446** This routine is not threadsafe. The caller must be holding a mutex
447** to prevent multiple threads from entering at the same time.
danielk1977c66c0e12008-06-25 14:26:07 +0000448*/
449static int memsys5Init(void *NotUsed){
drh7c6791c2009-08-18 14:48:53 +0000450 int ii; /* Loop counter */
451 int nByte; /* Number of bytes of memory available to this allocator */
452 u8 *zByte; /* Memory usable by this allocator */
453 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
454 int iOffset; /* An offset into mem5.aCtrl[] */
danielk19775099be52008-06-27 13:27:03 +0000455
danielk1977a03396a2008-11-19 14:35:46 +0000456 UNUSED_PARAMETER(NotUsed);
457
drh1b257532009-08-18 15:33:44 +0000458 /* For the purposes of this routine, disable the mutex */
459 mem5.mutex = 0;
460
drh7c6791c2009-08-18 14:48:53 +0000461 /* The size of a Mem5Link object must be a power of two. Verify that
462 ** this is case.
463 */
464 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
465
466 nByte = sqlite3GlobalConfig.nHeap;
467 zByte = (u8*)sqlite3GlobalConfig.pHeap;
468 assert( zByte!=0 ); /* sqlite3_config() does not allow otherwise */
danielk19770d84e5b2008-06-27 14:05:24 +0000469
shaneha6ec8922011-03-09 21:36:17 +0000470 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
shaneh93247942011-03-10 03:54:55 +0000471 nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
drh7c6791c2009-08-18 14:48:53 +0000472 mem5.szAtom = (1<<nMinLog);
473 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
474 mem5.szAtom = mem5.szAtom << 1;
danielk19775099be52008-06-27 13:27:03 +0000475 }
476
drh7c6791c2009-08-18 14:48:53 +0000477 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
danielk19775099be52008-06-27 13:27:03 +0000478 mem5.zPool = zByte;
drh7c6791c2009-08-18 14:48:53 +0000479 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
danielk19775099be52008-06-27 13:27:03 +0000480
481 for(ii=0; ii<=LOGMAX; ii++){
482 mem5.aiFreelist[ii] = -1;
483 }
484
485 iOffset = 0;
486 for(ii=LOGMAX; ii>=0; ii--){
487 int nAlloc = (1<<ii);
488 if( (iOffset+nAlloc)<=mem5.nBlock ){
489 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
490 memsys5Link(iOffset, ii);
491 iOffset += nAlloc;
492 }
493 assert((iOffset+nAlloc)>mem5.nBlock);
494 }
495
drh1b257532009-08-18 15:33:44 +0000496 /* If a mutex is required for normal operation, allocate one */
drhdaf4a9f2009-08-20 20:05:55 +0000497 if( sqlite3GlobalConfig.bMemstat==0 ){
drh1b257532009-08-18 15:33:44 +0000498 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
499 }
500
danielk1977c66c0e12008-06-25 14:26:07 +0000501 return SQLITE_OK;
502}
503
504/*
505** Deinitialize this module.
506*/
507static void memsys5Shutdown(void *NotUsed){
danielk1977a03396a2008-11-19 14:35:46 +0000508 UNUSED_PARAMETER(NotUsed);
drh15385ad2009-08-18 12:16:03 +0000509 mem5.mutex = 0;
danielk1977c66c0e12008-06-25 14:26:07 +0000510 return;
511}
512
drh7c6791c2009-08-18 14:48:53 +0000513#ifdef SQLITE_TEST
danielk1977c66c0e12008-06-25 14:26:07 +0000514/*
drh0d180202008-02-14 23:26:56 +0000515** Open the file indicated and write a log of all unfreed memory
516** allocations into that log.
517*/
danielk1977c66c0e12008-06-25 14:26:07 +0000518void sqlite3Memsys5Dump(const char *zFilename){
drh0d180202008-02-14 23:26:56 +0000519 FILE *out;
drh2d7636e2008-02-16 16:21:45 +0000520 int i, j, n;
danielk19775099be52008-06-27 13:27:03 +0000521 int nMinLog;
drh2d7636e2008-02-16 16:21:45 +0000522
drh0d180202008-02-14 23:26:56 +0000523 if( zFilename==0 || zFilename[0]==0 ){
524 out = stdout;
525 }else{
526 out = fopen(zFilename, "w");
527 if( out==0 ){
528 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
529 zFilename);
530 return;
531 }
532 }
drh2d7636e2008-02-16 16:21:45 +0000533 memsys5Enter();
drh7c6791c2009-08-18 14:48:53 +0000534 nMinLog = memsys5Log(mem5.szAtom);
danielk19775099be52008-06-27 13:27:03 +0000535 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
536 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
drh7c6791c2009-08-18 14:48:53 +0000537 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
drh0d180202008-02-14 23:26:56 +0000538 }
danielk1977c66c0e12008-06-25 14:26:07 +0000539 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
540 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
541 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
542 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
543 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
544 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
545 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
546 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
547 memsys5Leave();
drh0d180202008-02-14 23:26:56 +0000548 if( out==stdout ){
549 fflush(stdout);
550 }else{
551 fclose(out);
552 }
drh0d180202008-02-14 23:26:56 +0000553}
drh7c6791c2009-08-18 14:48:53 +0000554#endif
drh0d180202008-02-14 23:26:56 +0000555
danielk1977c66c0e12008-06-25 14:26:07 +0000556/*
557** This routine is the only routine in this file with external
danielk19775099be52008-06-27 13:27:03 +0000558** linkage. It returns a pointer to a static sqlite3_mem_methods
559** struct populated with the memsys5 methods.
danielk1977c66c0e12008-06-25 14:26:07 +0000560*/
danielk19775099be52008-06-27 13:27:03 +0000561const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
danielk1977c66c0e12008-06-25 14:26:07 +0000562 static const sqlite3_mem_methods memsys5Methods = {
563 memsys5Malloc,
564 memsys5Free,
565 memsys5Realloc,
566 memsys5Size,
567 memsys5Roundup,
568 memsys5Init,
569 memsys5Shutdown,
570 0
571 };
danielk19775099be52008-06-27 13:27:03 +0000572 return &memsys5Methods;
danielk1977c66c0e12008-06-25 14:26:07 +0000573}
574
575#endif /* SQLITE_ENABLE_MEMSYS5 */