blob: 2de028daa936eb6defe843f34fdec75105b12b2e [file] [log] [blame]
drh9c7a60d2007-10-19 17:47:24 +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
danielk197732155ef2008-06-25 10:34:34 +000016** use of malloc(). The SQLite user supplies a block of memory
17** 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.
drh9c7a60d2007-10-19 17:47:24 +000022**
danielk197732155ef2008-06-25 10:34:34 +000023** This version of the memory allocation subsystem is included
24** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
drh9c7a60d2007-10-19 17:47:24 +000025*/
drh0d180202008-02-14 23:26:56 +000026#include "sqliteInt.h"
drh9c7a60d2007-10-19 17:47:24 +000027
28/*
danielk197757e5ea92008-06-24 19:02:55 +000029** This version of the memory allocator is only built into the library
danielk197732155ef2008-06-25 10:34:34 +000030** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
danielk197757e5ea92008-06-24 19:02:55 +000031** mean that the library will use a memory-pool by default, just that
32** it is available. The mempool allocator is activated by calling
33** sqlite3_config().
drh9c7a60d2007-10-19 17:47:24 +000034*/
danielk197732155ef2008-06-25 10:34:34 +000035#ifdef SQLITE_ENABLE_MEMSYS3
drhace03d12007-11-29 18:36:49 +000036
drh9c7a60d2007-10-19 17:47:24 +000037/*
38** Maximum size (in Mem3Blocks) of a "small" chunk.
39*/
40#define MX_SMALL 10
41
42
43/*
44** Number of freelist hash slots
45*/
46#define N_HASH 61
47
48/*
49** A memory allocation (also called a "chunk") consists of two or
50** more blocks where each block is 8 bytes. The first 8 bytes are
51** a header that is not returned to the user.
52**
53** A chunk is two or more blocks that is either checked out or
drh71f971b2007-12-29 13:18:22 +000054** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
drh9c7a60d2007-10-19 17:47:24 +000055** size of the allocation in blocks if the allocation is free.
drh71f971b2007-12-29 13:18:22 +000056** The u.hdr.size4x&1 bit is true if the chunk is checked out and
57** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
58** is true if the previous chunk is checked out and false if the
59** previous chunk is free. The u.hdr.prevSize field is the size of
60** the previous chunk in blocks if the previous chunk is on the
61** freelist. If the previous chunk is checked out, then
62** u.hdr.prevSize can be part of the data for that chunk and should
63** not be read or written.
drh9c7a60d2007-10-19 17:47:24 +000064**
danielk197757e5ea92008-06-24 19:02:55 +000065** We often identify a chunk by its index in mem3.aPool[]. When
drh9c7a60d2007-10-19 17:47:24 +000066** this is done, the chunk index refers to the second block of
67** the chunk. In this way, the first chunk has an index of 1.
68** A chunk index of 0 means "no such chunk" and is the equivalent
69** of a NULL pointer.
70**
71** The second block of free chunks is of the form u.list. The
72** two fields form a double-linked list of chunks of related sizes.
danielk197757e5ea92008-06-24 19:02:55 +000073** Pointers to the head of the list are stored in mem3.aiSmall[]
74** for smaller chunks and mem3.aiHash[] for larger chunks.
drh9c7a60d2007-10-19 17:47:24 +000075**
76** The second block of a chunk is user data if the chunk is checked
drh71f971b2007-12-29 13:18:22 +000077** out. If a chunk is checked out, the user data may extend into
78** the u.hdr.prevSize value of the following chunk.
drh9c7a60d2007-10-19 17:47:24 +000079*/
80typedef struct Mem3Block Mem3Block;
81struct Mem3Block {
82 union {
83 struct {
drh71f971b2007-12-29 13:18:22 +000084 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
85 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
drh9c7a60d2007-10-19 17:47:24 +000086 } hdr;
87 struct {
danielk197757e5ea92008-06-24 19:02:55 +000088 u32 next; /* Index in mem3.aPool[] of next free chunk */
89 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
drh9c7a60d2007-10-19 17:47:24 +000090 } list;
91 } u;
92};
93
94/*
95** All of the static variables used by this module are collected
danielk197757e5ea92008-06-24 19:02:55 +000096** into a single structure named "mem3". This is to keep the
drh9c7a60d2007-10-19 17:47:24 +000097** static variables organized and to reduce namespace pollution
98** when this module is combined with other in the amalgamation.
99*/
danielk19775c8f8582008-09-02 10:22:00 +0000100static SQLITE_WSD struct Mem3Global {
drh9c7a60d2007-10-19 17:47:24 +0000101 /*
danielk197723bf0f42008-09-02 17:52:51 +0000102 ** Memory available for allocation. nPool is the size of the array
103 ** (in Mem3Blocks) pointed to by aPool less 2.
104 */
105 u32 nPool;
106 Mem3Block *aPool;
107
108 /*
drha4e5d582007-10-20 15:41:57 +0000109 ** True if we are evaluating an out-of-memory callback.
drh9c7a60d2007-10-19 17:47:24 +0000110 */
drh9c7a60d2007-10-19 17:47:24 +0000111 int alarmBusy;
112
113 /*
114 ** Mutex to control access to the memory allocation subsystem.
115 */
116 sqlite3_mutex *mutex;
117
118 /*
drha4e5d582007-10-20 15:41:57 +0000119 ** The minimum amount of free space that we have seen.
drh9c7a60d2007-10-19 17:47:24 +0000120 */
drh71f971b2007-12-29 13:18:22 +0000121 u32 mnMaster;
drh9c7a60d2007-10-19 17:47:24 +0000122
123 /*
124 ** iMaster is the index of the master chunk. Most new allocations
125 ** occur off of this chunk. szMaster is the size (in Mem3Blocks)
126 ** of the current master. iMaster is 0 if there is not master chunk.
127 ** The master chunk is not in either the aiHash[] or aiSmall[].
128 */
drh71f971b2007-12-29 13:18:22 +0000129 u32 iMaster;
130 u32 szMaster;
drh9c7a60d2007-10-19 17:47:24 +0000131
132 /*
133 ** Array of lists of free blocks according to the block size
134 ** for smaller chunks, or a hash on the block size for larger
135 ** chunks.
136 */
drh71f971b2007-12-29 13:18:22 +0000137 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
138 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
danielk197723bf0f42008-09-02 17:52:51 +0000139} mem3 = { 97535575 };
danielk19775c8f8582008-09-02 10:22:00 +0000140
141#define mem3 GLOBAL(struct Mem3Global, mem3)
drh9c7a60d2007-10-19 17:47:24 +0000142
143/*
danielk197757e5ea92008-06-24 19:02:55 +0000144** Unlink the chunk at mem3.aPool[i] from list it is currently
drh9c7a60d2007-10-19 17:47:24 +0000145** on. *pRoot is the list that i is a member of.
146*/
drh71f971b2007-12-29 13:18:22 +0000147static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
danielk197757e5ea92008-06-24 19:02:55 +0000148 u32 next = mem3.aPool[i].u.list.next;
149 u32 prev = mem3.aPool[i].u.list.prev;
150 assert( sqlite3_mutex_held(mem3.mutex) );
drh9c7a60d2007-10-19 17:47:24 +0000151 if( prev==0 ){
152 *pRoot = next;
153 }else{
danielk197757e5ea92008-06-24 19:02:55 +0000154 mem3.aPool[prev].u.list.next = next;
drh9c7a60d2007-10-19 17:47:24 +0000155 }
156 if( next ){
danielk197757e5ea92008-06-24 19:02:55 +0000157 mem3.aPool[next].u.list.prev = prev;
drh9c7a60d2007-10-19 17:47:24 +0000158 }
danielk197757e5ea92008-06-24 19:02:55 +0000159 mem3.aPool[i].u.list.next = 0;
160 mem3.aPool[i].u.list.prev = 0;
drh9c7a60d2007-10-19 17:47:24 +0000161}
162
163/*
164** Unlink the chunk at index i from
165** whatever list is currently a member of.
166*/
drh71f971b2007-12-29 13:18:22 +0000167static void memsys3Unlink(u32 i){
168 u32 size, hash;
danielk197757e5ea92008-06-24 19:02:55 +0000169 assert( sqlite3_mutex_held(mem3.mutex) );
170 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
drh71f971b2007-12-29 13:18:22 +0000171 assert( i>=1 );
danielk197757e5ea92008-06-24 19:02:55 +0000172 size = mem3.aPool[i-1].u.hdr.size4x/4;
173 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
drh9c7a60d2007-10-19 17:47:24 +0000174 assert( size>=2 );
175 if( size <= MX_SMALL ){
danielk197757e5ea92008-06-24 19:02:55 +0000176 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
drh9c7a60d2007-10-19 17:47:24 +0000177 }else{
178 hash = size % N_HASH;
danielk197757e5ea92008-06-24 19:02:55 +0000179 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
drh9c7a60d2007-10-19 17:47:24 +0000180 }
181}
182
183/*
danielk197757e5ea92008-06-24 19:02:55 +0000184** Link the chunk at mem3.aPool[i] so that is on the list rooted
drh9c7a60d2007-10-19 17:47:24 +0000185** at *pRoot.
186*/
drh71f971b2007-12-29 13:18:22 +0000187static void memsys3LinkIntoList(u32 i, u32 *pRoot){
danielk197757e5ea92008-06-24 19:02:55 +0000188 assert( sqlite3_mutex_held(mem3.mutex) );
189 mem3.aPool[i].u.list.next = *pRoot;
190 mem3.aPool[i].u.list.prev = 0;
drh9c7a60d2007-10-19 17:47:24 +0000191 if( *pRoot ){
danielk197757e5ea92008-06-24 19:02:55 +0000192 mem3.aPool[*pRoot].u.list.prev = i;
drh9c7a60d2007-10-19 17:47:24 +0000193 }
194 *pRoot = i;
195}
196
197/*
198** Link the chunk at index i into either the appropriate
199** small chunk list, or into the large chunk hash table.
200*/
drh71f971b2007-12-29 13:18:22 +0000201static void memsys3Link(u32 i){
202 u32 size, hash;
danielk197757e5ea92008-06-24 19:02:55 +0000203 assert( sqlite3_mutex_held(mem3.mutex) );
drh71f971b2007-12-29 13:18:22 +0000204 assert( i>=1 );
danielk197757e5ea92008-06-24 19:02:55 +0000205 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
206 size = mem3.aPool[i-1].u.hdr.size4x/4;
207 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
drh9c7a60d2007-10-19 17:47:24 +0000208 assert( size>=2 );
209 if( size <= MX_SMALL ){
danielk197757e5ea92008-06-24 19:02:55 +0000210 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
drh9c7a60d2007-10-19 17:47:24 +0000211 }else{
212 hash = size % N_HASH;
danielk197757e5ea92008-06-24 19:02:55 +0000213 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
drh9c7a60d2007-10-19 17:47:24 +0000214 }
215}
216
217/*
danielk19776b39c2e2008-06-25 14:57:53 +0000218** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
219** will already be held (obtained by code in malloc.c) if
danielk1977075c23a2008-09-01 18:34:20 +0000220** sqlite3GlobalConfig.bMemStat is true.
drh9c7a60d2007-10-19 17:47:24 +0000221*/
drha4e5d582007-10-20 15:41:57 +0000222static void memsys3Enter(void){
danielk1977075c23a2008-09-01 18:34:20 +0000223 if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
danielk197757e5ea92008-06-24 19:02:55 +0000224 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drh9c7a60d2007-10-19 17:47:24 +0000225 }
danielk197757e5ea92008-06-24 19:02:55 +0000226 sqlite3_mutex_enter(mem3.mutex);
drh9c7a60d2007-10-19 17:47:24 +0000227}
danielk197757e5ea92008-06-24 19:02:55 +0000228static void memsys3Leave(void){
danielk19776b39c2e2008-06-25 14:57:53 +0000229 sqlite3_mutex_leave(mem3.mutex);
drh9c7a60d2007-10-19 17:47:24 +0000230}
231
232/*
drha4e5d582007-10-20 15:41:57 +0000233** Called when we are unable to satisfy an allocation of nBytes.
drh9c7a60d2007-10-19 17:47:24 +0000234*/
drha4e5d582007-10-20 15:41:57 +0000235static void memsys3OutOfMemory(int nByte){
danielk197757e5ea92008-06-24 19:02:55 +0000236 if( !mem3.alarmBusy ){
237 mem3.alarmBusy = 1;
238 assert( sqlite3_mutex_held(mem3.mutex) );
239 sqlite3_mutex_leave(mem3.mutex);
drha4e5d582007-10-20 15:41:57 +0000240 sqlite3_release_memory(nByte);
danielk197757e5ea92008-06-24 19:02:55 +0000241 sqlite3_mutex_enter(mem3.mutex);
242 mem3.alarmBusy = 0;
drha4e5d582007-10-20 15:41:57 +0000243 }
drh9c7a60d2007-10-19 17:47:24 +0000244}
245
drh40257ff2008-06-13 18:24:27 +0000246
247/*
drh9c7a60d2007-10-19 17:47:24 +0000248** Chunk i is a free chunk that has been unlinked. Adjust its
249** size parameters for check-out and return a pointer to the
250** user portion of the chunk.
251*/
danielk1977a03396a2008-11-19 14:35:46 +0000252static void *memsys3Checkout(u32 i, u32 nBlock){
drh71f971b2007-12-29 13:18:22 +0000253 u32 x;
danielk197757e5ea92008-06-24 19:02:55 +0000254 assert( sqlite3_mutex_held(mem3.mutex) );
drh71f971b2007-12-29 13:18:22 +0000255 assert( i>=1 );
danielk197757e5ea92008-06-24 19:02:55 +0000256 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
257 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
258 x = mem3.aPool[i-1].u.hdr.size4x;
259 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
260 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
261 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
262 return &mem3.aPool[i];
drh9c7a60d2007-10-19 17:47:24 +0000263}
264
265/*
danielk197757e5ea92008-06-24 19:02:55 +0000266** Carve a piece off of the end of the mem3.iMaster free chunk.
drh9c7a60d2007-10-19 17:47:24 +0000267** Return a pointer to the new allocation. Or, if the master chunk
268** is not large enough, return 0.
269*/
danielk1977a03396a2008-11-19 14:35:46 +0000270static void *memsys3FromMaster(u32 nBlock){
danielk197757e5ea92008-06-24 19:02:55 +0000271 assert( sqlite3_mutex_held(mem3.mutex) );
272 assert( mem3.szMaster>=nBlock );
273 if( nBlock>=mem3.szMaster-1 ){
drh9c7a60d2007-10-19 17:47:24 +0000274 /* Use the entire master */
danielk197757e5ea92008-06-24 19:02:55 +0000275 void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
276 mem3.iMaster = 0;
277 mem3.szMaster = 0;
278 mem3.mnMaster = 0;
drh9c7a60d2007-10-19 17:47:24 +0000279 return p;
280 }else{
281 /* Split the master block. Return the tail. */
drh71f971b2007-12-29 13:18:22 +0000282 u32 newi, x;
danielk197757e5ea92008-06-24 19:02:55 +0000283 newi = mem3.iMaster + mem3.szMaster - nBlock;
284 assert( newi > mem3.iMaster+1 );
285 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
286 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
287 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
288 mem3.szMaster -= nBlock;
289 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
290 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
291 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
292 if( mem3.szMaster < mem3.mnMaster ){
293 mem3.mnMaster = mem3.szMaster;
drha4e5d582007-10-20 15:41:57 +0000294 }
danielk197757e5ea92008-06-24 19:02:55 +0000295 return (void*)&mem3.aPool[newi];
drh9c7a60d2007-10-19 17:47:24 +0000296 }
297}
298
299/*
300** *pRoot is the head of a list of free chunks of the same size
301** or same size hash. In other words, *pRoot is an entry in either
danielk197757e5ea92008-06-24 19:02:55 +0000302** mem3.aiSmall[] or mem3.aiHash[].
drh9c7a60d2007-10-19 17:47:24 +0000303**
304** This routine examines all entries on the given list and tries
305** to coalesce each entries with adjacent free chunks.
306**
danielk197757e5ea92008-06-24 19:02:55 +0000307** If it sees a chunk that is larger than mem3.iMaster, it replaces
308** the current mem3.iMaster with the new larger chunk. In order for
309** this mem3.iMaster replacement to work, the master chunk must be
drh9c7a60d2007-10-19 17:47:24 +0000310** linked into the hash tables. That is not the normal state of
311** affairs, of course. The calling routine must link the master
312** chunk before invoking this routine, then must unlink the (possibly
313** changed) master chunk once this routine has finished.
314*/
drh71f971b2007-12-29 13:18:22 +0000315static void memsys3Merge(u32 *pRoot){
316 u32 iNext, prev, size, i, x;
drh9c7a60d2007-10-19 17:47:24 +0000317
danielk197757e5ea92008-06-24 19:02:55 +0000318 assert( sqlite3_mutex_held(mem3.mutex) );
drh9c7a60d2007-10-19 17:47:24 +0000319 for(i=*pRoot; i>0; i=iNext){
danielk197757e5ea92008-06-24 19:02:55 +0000320 iNext = mem3.aPool[i].u.list.next;
321 size = mem3.aPool[i-1].u.hdr.size4x;
drh71f971b2007-12-29 13:18:22 +0000322 assert( (size&1)==0 );
323 if( (size&2)==0 ){
drha4e5d582007-10-20 15:41:57 +0000324 memsys3UnlinkFromList(i, pRoot);
danielk197757e5ea92008-06-24 19:02:55 +0000325 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
326 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
drh9c7a60d2007-10-19 17:47:24 +0000327 if( prev==iNext ){
danielk197757e5ea92008-06-24 19:02:55 +0000328 iNext = mem3.aPool[prev].u.list.next;
drh9c7a60d2007-10-19 17:47:24 +0000329 }
drha4e5d582007-10-20 15:41:57 +0000330 memsys3Unlink(prev);
drh71f971b2007-12-29 13:18:22 +0000331 size = i + size/4 - prev;
danielk197757e5ea92008-06-24 19:02:55 +0000332 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
333 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
334 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
drha4e5d582007-10-20 15:41:57 +0000335 memsys3Link(prev);
drh9c7a60d2007-10-19 17:47:24 +0000336 i = prev;
drh71f971b2007-12-29 13:18:22 +0000337 }else{
338 size /= 4;
drh9c7a60d2007-10-19 17:47:24 +0000339 }
danielk197757e5ea92008-06-24 19:02:55 +0000340 if( size>mem3.szMaster ){
341 mem3.iMaster = i;
342 mem3.szMaster = size;
drh9c7a60d2007-10-19 17:47:24 +0000343 }
344 }
345}
346
347/*
348** Return a block of memory of at least nBytes in size.
349** Return NULL if unable.
danielk197732155ef2008-06-25 10:34:34 +0000350**
351** This function assumes that the necessary mutexes, if any, are
352** already held by the caller. Hence "Unsafe".
drh9c7a60d2007-10-19 17:47:24 +0000353*/
danielk197732155ef2008-06-25 10:34:34 +0000354static void *memsys3MallocUnsafe(int nByte){
drh71f971b2007-12-29 13:18:22 +0000355 u32 i;
danielk1977a03396a2008-11-19 14:35:46 +0000356 u32 nBlock;
357 u32 toFree;
drh9c7a60d2007-10-19 17:47:24 +0000358
danielk197757e5ea92008-06-24 19:02:55 +0000359 assert( sqlite3_mutex_held(mem3.mutex) );
drh9c7a60d2007-10-19 17:47:24 +0000360 assert( sizeof(Mem3Block)==8 );
drh71f971b2007-12-29 13:18:22 +0000361 if( nByte<=12 ){
drh9c7a60d2007-10-19 17:47:24 +0000362 nBlock = 2;
363 }else{
drh71f971b2007-12-29 13:18:22 +0000364 nBlock = (nByte + 11)/8;
drh9c7a60d2007-10-19 17:47:24 +0000365 }
danielk197757e5ea92008-06-24 19:02:55 +0000366 assert( nBlock>=2 );
drh9c7a60d2007-10-19 17:47:24 +0000367
368 /* STEP 1:
369 ** Look for an entry of the correct size in either the small
370 ** chunk table or in the large chunk hash table. This is
371 ** successful most of the time (about 9 times out of 10).
372 */
373 if( nBlock <= MX_SMALL ){
danielk197757e5ea92008-06-24 19:02:55 +0000374 i = mem3.aiSmall[nBlock-2];
drh9c7a60d2007-10-19 17:47:24 +0000375 if( i>0 ){
danielk197757e5ea92008-06-24 19:02:55 +0000376 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
drha4e5d582007-10-20 15:41:57 +0000377 return memsys3Checkout(i, nBlock);
drh9c7a60d2007-10-19 17:47:24 +0000378 }
379 }else{
380 int hash = nBlock % N_HASH;
danielk197757e5ea92008-06-24 19:02:55 +0000381 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
382 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
383 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
drha4e5d582007-10-20 15:41:57 +0000384 return memsys3Checkout(i, nBlock);
drh9c7a60d2007-10-19 17:47:24 +0000385 }
386 }
387 }
388
389 /* STEP 2:
390 ** Try to satisfy the allocation by carving a piece off of the end
391 ** of the master chunk. This step usually works if step 1 fails.
392 */
danielk197757e5ea92008-06-24 19:02:55 +0000393 if( mem3.szMaster>=nBlock ){
drha4e5d582007-10-20 15:41:57 +0000394 return memsys3FromMaster(nBlock);
drh9c7a60d2007-10-19 17:47:24 +0000395 }
396
397
398 /* STEP 3:
399 ** Loop through the entire memory pool. Coalesce adjacent free
400 ** chunks. Recompute the master chunk as the largest free chunk.
401 ** Then try again to satisfy the allocation by carving a piece off
402 ** of the end of the master chunk. This step happens very
403 ** rarely (we hope!)
404 */
danielk197757e5ea92008-06-24 19:02:55 +0000405 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
drh979aeaa2007-10-20 16:36:31 +0000406 memsys3OutOfMemory(toFree);
danielk197757e5ea92008-06-24 19:02:55 +0000407 if( mem3.iMaster ){
408 memsys3Link(mem3.iMaster);
409 mem3.iMaster = 0;
410 mem3.szMaster = 0;
drh979aeaa2007-10-20 16:36:31 +0000411 }
412 for(i=0; i<N_HASH; i++){
danielk197757e5ea92008-06-24 19:02:55 +0000413 memsys3Merge(&mem3.aiHash[i]);
drh979aeaa2007-10-20 16:36:31 +0000414 }
415 for(i=0; i<MX_SMALL-1; i++){
danielk197757e5ea92008-06-24 19:02:55 +0000416 memsys3Merge(&mem3.aiSmall[i]);
drh979aeaa2007-10-20 16:36:31 +0000417 }
danielk197757e5ea92008-06-24 19:02:55 +0000418 if( mem3.szMaster ){
419 memsys3Unlink(mem3.iMaster);
420 if( mem3.szMaster>=nBlock ){
drh979aeaa2007-10-20 16:36:31 +0000421 return memsys3FromMaster(nBlock);
422 }
drh9c7a60d2007-10-19 17:47:24 +0000423 }
424 }
425
426 /* If none of the above worked, then we fail. */
427 return 0;
428}
429
430/*
431** Free an outstanding memory allocation.
danielk197732155ef2008-06-25 10:34:34 +0000432**
433** This function assumes that the necessary mutexes, if any, are
434** already held by the caller. Hence "Unsafe".
drh9c7a60d2007-10-19 17:47:24 +0000435*/
dane7152dc2011-07-07 08:19:16 +0000436static void memsys3FreeUnsafe(void *pOld){
drh9c7a60d2007-10-19 17:47:24 +0000437 Mem3Block *p = (Mem3Block*)pOld;
438 int i;
drh71f971b2007-12-29 13:18:22 +0000439 u32 size, x;
danielk197757e5ea92008-06-24 19:02:55 +0000440 assert( sqlite3_mutex_held(mem3.mutex) );
441 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
442 i = p - mem3.aPool;
443 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
444 size = mem3.aPool[i-1].u.hdr.size4x/4;
445 assert( i+size<=mem3.nPool+1 );
446 mem3.aPool[i-1].u.hdr.size4x &= ~1;
447 mem3.aPool[i+size-1].u.hdr.prevSize = size;
448 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
drha4e5d582007-10-20 15:41:57 +0000449 memsys3Link(i);
drh9c7a60d2007-10-19 17:47:24 +0000450
451 /* Try to expand the master using the newly freed chunk */
danielk197757e5ea92008-06-24 19:02:55 +0000452 if( mem3.iMaster ){
453 while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
454 size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
455 mem3.iMaster -= size;
456 mem3.szMaster += size;
457 memsys3Unlink(mem3.iMaster);
458 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
459 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
460 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
drh9c7a60d2007-10-19 17:47:24 +0000461 }
danielk197757e5ea92008-06-24 19:02:55 +0000462 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
463 while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
464 memsys3Unlink(mem3.iMaster+mem3.szMaster);
465 mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
466 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
467 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
drh9c7a60d2007-10-19 17:47:24 +0000468 }
469 }
470}
471
472/*
drhc702c7c2008-07-18 18:56:16 +0000473** Return the size of an outstanding allocation, in bytes. The
474** size returned omits the 8-byte header overhead. This only
475** works for chunks that are currently checked out.
476*/
477static int memsys3Size(void *p){
478 Mem3Block *pBlock;
drh039ca6a2015-10-15 16:20:57 +0000479 assert( p!=0 );
drhc702c7c2008-07-18 18:56:16 +0000480 pBlock = (Mem3Block*)p;
481 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
482 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
483}
484
485/*
486** Round up a request size to the next valid allocation size.
487*/
488static int memsys3Roundup(int n){
489 if( n<=12 ){
490 return 12;
491 }else{
492 return ((n+11)&~7) - 4;
493 }
494}
495
496/*
danielk197757e5ea92008-06-24 19:02:55 +0000497** Allocate nBytes of memory.
drh9c7a60d2007-10-19 17:47:24 +0000498*/
danielk197732155ef2008-06-25 10:34:34 +0000499static void *memsys3Malloc(int nBytes){
danielk197757e5ea92008-06-24 19:02:55 +0000500 sqlite3_int64 *p;
501 assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
502 memsys3Enter();
danielk197732155ef2008-06-25 10:34:34 +0000503 p = memsys3MallocUnsafe(nBytes);
danielk197757e5ea92008-06-24 19:02:55 +0000504 memsys3Leave();
drh9c7a60d2007-10-19 17:47:24 +0000505 return (void*)p;
506}
507
508/*
509** Free memory.
510*/
dane7152dc2011-07-07 08:19:16 +0000511static void memsys3Free(void *pPrior){
danielk197757e5ea92008-06-24 19:02:55 +0000512 assert( pPrior );
513 memsys3Enter();
danielk197732155ef2008-06-25 10:34:34 +0000514 memsys3FreeUnsafe(pPrior);
danielk197757e5ea92008-06-24 19:02:55 +0000515 memsys3Leave();
516}
517
518/*
drh9c7a60d2007-10-19 17:47:24 +0000519** Change the size of an existing memory allocation
520*/
dane7152dc2011-07-07 08:19:16 +0000521static void *memsys3Realloc(void *pPrior, int nBytes){
drh9c7a60d2007-10-19 17:47:24 +0000522 int nOld;
523 void *p;
524 if( pPrior==0 ){
525 return sqlite3_malloc(nBytes);
526 }
527 if( nBytes<=0 ){
528 sqlite3_free(pPrior);
529 return 0;
530 }
danielk197732155ef2008-06-25 10:34:34 +0000531 nOld = memsys3Size(pPrior);
drha4e5d582007-10-20 15:41:57 +0000532 if( nBytes<=nOld && nBytes>=nOld-128 ){
533 return pPrior;
534 }
danielk197757e5ea92008-06-24 19:02:55 +0000535 memsys3Enter();
danielk197732155ef2008-06-25 10:34:34 +0000536 p = memsys3MallocUnsafe(nBytes);
drha4e5d582007-10-20 15:41:57 +0000537 if( p ){
538 if( nOld<nBytes ){
539 memcpy(p, pPrior, nOld);
540 }else{
541 memcpy(p, pPrior, nBytes);
drh9c7a60d2007-10-19 17:47:24 +0000542 }
danielk197732155ef2008-06-25 10:34:34 +0000543 memsys3FreeUnsafe(pPrior);
drh9c7a60d2007-10-19 17:47:24 +0000544 }
danielk197757e5ea92008-06-24 19:02:55 +0000545 memsys3Leave();
drh9c7a60d2007-10-19 17:47:24 +0000546 return p;
547}
548
549/*
danielk197757e5ea92008-06-24 19:02:55 +0000550** Initialize this module.
551*/
danielk197732155ef2008-06-25 10:34:34 +0000552static int memsys3Init(void *NotUsed){
danielk1977a03396a2008-11-19 14:35:46 +0000553 UNUSED_PARAMETER(NotUsed);
danielk1977075c23a2008-09-01 18:34:20 +0000554 if( !sqlite3GlobalConfig.pHeap ){
danielk19770d84e5b2008-06-27 14:05:24 +0000555 return SQLITE_ERROR;
556 }
557
558 /* Store a pointer to the memory block in global structure mem3. */
559 assert( sizeof(Mem3Block)==8 );
danielk1977075c23a2008-09-01 18:34:20 +0000560 mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
561 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
danielk19770d84e5b2008-06-27 14:05:24 +0000562
563 /* Initialize the master block. */
564 mem3.szMaster = mem3.nPool;
565 mem3.mnMaster = mem3.szMaster;
566 mem3.iMaster = 1;
567 mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
568 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
569 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
570
danielk197757e5ea92008-06-24 19:02:55 +0000571 return SQLITE_OK;
572}
573
574/*
575** Deinitialize this module.
576*/
danielk197732155ef2008-06-25 10:34:34 +0000577static void memsys3Shutdown(void *NotUsed){
danielk1977a03396a2008-11-19 14:35:46 +0000578 UNUSED_PARAMETER(NotUsed);
drh4591c7b2009-10-13 12:48:43 +0000579 mem3.mutex = 0;
danielk197757e5ea92008-06-24 19:02:55 +0000580 return;
581}
582
583
584
585/*
drh9c7a60d2007-10-19 17:47:24 +0000586** Open the file indicated and write a log of all unfreed memory
587** allocations into that log.
588*/
danielk197732155ef2008-06-25 10:34:34 +0000589void sqlite3Memsys3Dump(const char *zFilename){
danielk19775c8f8582008-09-02 10:22:00 +0000590#ifdef SQLITE_DEBUG
drh9c7a60d2007-10-19 17:47:24 +0000591 FILE *out;
danielk1977a03396a2008-11-19 14:35:46 +0000592 u32 i, j;
drh71f971b2007-12-29 13:18:22 +0000593 u32 size;
drh9c7a60d2007-10-19 17:47:24 +0000594 if( zFilename==0 || zFilename[0]==0 ){
595 out = stdout;
596 }else{
597 out = fopen(zFilename, "w");
598 if( out==0 ){
599 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
600 zFilename);
601 return;
602 }
603 }
drha4e5d582007-10-20 15:41:57 +0000604 memsys3Enter();
drh9c7a60d2007-10-19 17:47:24 +0000605 fprintf(out, "CHUNKS:\n");
danielk197732155ef2008-06-25 10:34:34 +0000606 for(i=1; i<=mem3.nPool; i+=size/4){
danielk197757e5ea92008-06-24 19:02:55 +0000607 size = mem3.aPool[i-1].u.hdr.size4x;
drh71f971b2007-12-29 13:18:22 +0000608 if( size/4<=1 ){
danielk197757e5ea92008-06-24 19:02:55 +0000609 fprintf(out, "%p size error\n", &mem3.aPool[i]);
drh9c7a60d2007-10-19 17:47:24 +0000610 assert( 0 );
611 break;
612 }
danielk197757e5ea92008-06-24 19:02:55 +0000613 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
614 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
drh9c7a60d2007-10-19 17:47:24 +0000615 assert( 0 );
616 break;
617 }
danielk197757e5ea92008-06-24 19:02:55 +0000618 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
619 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
drh71f971b2007-12-29 13:18:22 +0000620 assert( 0 );
621 break;
622 }
623 if( size&1 ){
danielk197757e5ea92008-06-24 19:02:55 +0000624 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
drh9c7a60d2007-10-19 17:47:24 +0000625 }else{
danielk197757e5ea92008-06-24 19:02:55 +0000626 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
627 i==mem3.iMaster ? " **master**" : "");
drh9c7a60d2007-10-19 17:47:24 +0000628 }
629 }
630 for(i=0; i<MX_SMALL-1; i++){
danielk197757e5ea92008-06-24 19:02:55 +0000631 if( mem3.aiSmall[i]==0 ) continue;
drh9c7a60d2007-10-19 17:47:24 +0000632 fprintf(out, "small(%2d):", i);
danielk197757e5ea92008-06-24 19:02:55 +0000633 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
634 fprintf(out, " %p(%d)", &mem3.aPool[j],
635 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
drh9c7a60d2007-10-19 17:47:24 +0000636 }
637 fprintf(out, "\n");
638 }
639 for(i=0; i<N_HASH; i++){
danielk197757e5ea92008-06-24 19:02:55 +0000640 if( mem3.aiHash[i]==0 ) continue;
drh9c7a60d2007-10-19 17:47:24 +0000641 fprintf(out, "hash(%2d):", i);
danielk197757e5ea92008-06-24 19:02:55 +0000642 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
643 fprintf(out, " %p(%d)", &mem3.aPool[j],
644 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
drh9c7a60d2007-10-19 17:47:24 +0000645 }
646 fprintf(out, "\n");
647 }
danielk197757e5ea92008-06-24 19:02:55 +0000648 fprintf(out, "master=%d\n", mem3.iMaster);
danielk197732155ef2008-06-25 10:34:34 +0000649 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
650 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
danielk197757e5ea92008-06-24 19:02:55 +0000651 sqlite3_mutex_leave(mem3.mutex);
drh9c7a60d2007-10-19 17:47:24 +0000652 if( out==stdout ){
653 fflush(stdout);
654 }else{
655 fclose(out);
656 }
danielk1977f3d3c272008-11-19 16:52:44 +0000657#else
658 UNUSED_PARAMETER(zFilename);
danielk197757e5ea92008-06-24 19:02:55 +0000659#endif
danielk19775c8f8582008-09-02 10:22:00 +0000660}
drh9c7a60d2007-10-19 17:47:24 +0000661
danielk197757e5ea92008-06-24 19:02:55 +0000662/*
663** This routine is the only routine in this file with external
664** linkage.
665**
666** Populate the low-level memory allocation function pointers in
danielk1977075c23a2008-09-01 18:34:20 +0000667** sqlite3GlobalConfig.m with pointers to the routines in this file. The
danielk197757e5ea92008-06-24 19:02:55 +0000668** arguments specify the block of memory to manage.
669**
670** This routine is only called by sqlite3_config(), and therefore
671** is not required to be threadsafe (it is not).
672*/
danielk19770d84e5b2008-06-27 14:05:24 +0000673const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
danielk197757e5ea92008-06-24 19:02:55 +0000674 static const sqlite3_mem_methods mempoolMethods = {
danielk197732155ef2008-06-25 10:34:34 +0000675 memsys3Malloc,
676 memsys3Free,
677 memsys3Realloc,
678 memsys3Size,
679 memsys3Roundup,
680 memsys3Init,
681 memsys3Shutdown,
danielk197757e5ea92008-06-24 19:02:55 +0000682 0
683 };
danielk19770d84e5b2008-06-27 14:05:24 +0000684 return &mempoolMethods;
danielk197757e5ea92008-06-24 19:02:55 +0000685}
686
danielk197732155ef2008-06-25 10:34:34 +0000687#endif /* SQLITE_ENABLE_MEMSYS3 */