blob: 09a84b673193c08e3bec21a9603a5e4576304201 [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**
danielk1977f3d3c272008-11-19 16:52:44 +000026** $Id: mem3.c,v 1.25 2008/11/19 16:52:44 danielk1977 Exp $
drh9c7a60d2007-10-19 17:47:24 +000027*/
drh0d180202008-02-14 23:26:56 +000028#include "sqliteInt.h"
drh9c7a60d2007-10-19 17:47:24 +000029
30/*
danielk197757e5ea92008-06-24 19:02:55 +000031** This version of the memory allocator is only built into the library
danielk197732155ef2008-06-25 10:34:34 +000032** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
danielk197757e5ea92008-06-24 19:02:55 +000033** mean that the library will use a memory-pool by default, just that
34** it is available. The mempool allocator is activated by calling
35** sqlite3_config().
drh9c7a60d2007-10-19 17:47:24 +000036*/
danielk197732155ef2008-06-25 10:34:34 +000037#ifdef SQLITE_ENABLE_MEMSYS3
drhace03d12007-11-29 18:36:49 +000038
drh9c7a60d2007-10-19 17:47:24 +000039/*
40** Maximum size (in Mem3Blocks) of a "small" chunk.
41*/
42#define MX_SMALL 10
43
44
45/*
46** Number of freelist hash slots
47*/
48#define N_HASH 61
49
50/*
51** A memory allocation (also called a "chunk") consists of two or
52** more blocks where each block is 8 bytes. The first 8 bytes are
53** a header that is not returned to the user.
54**
55** A chunk is two or more blocks that is either checked out or
drh71f971b2007-12-29 13:18:22 +000056** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
drh9c7a60d2007-10-19 17:47:24 +000057** size of the allocation in blocks if the allocation is free.
drh71f971b2007-12-29 13:18:22 +000058** The u.hdr.size4x&1 bit is true if the chunk is checked out and
59** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
60** is true if the previous chunk is checked out and false if the
61** previous chunk is free. The u.hdr.prevSize field is the size of
62** the previous chunk in blocks if the previous chunk is on the
63** freelist. If the previous chunk is checked out, then
64** u.hdr.prevSize can be part of the data for that chunk and should
65** not be read or written.
drh9c7a60d2007-10-19 17:47:24 +000066**
danielk197757e5ea92008-06-24 19:02:55 +000067** We often identify a chunk by its index in mem3.aPool[]. When
drh9c7a60d2007-10-19 17:47:24 +000068** this is done, the chunk index refers to the second block of
69** the chunk. In this way, the first chunk has an index of 1.
70** A chunk index of 0 means "no such chunk" and is the equivalent
71** of a NULL pointer.
72**
73** The second block of free chunks is of the form u.list. The
74** two fields form a double-linked list of chunks of related sizes.
danielk197757e5ea92008-06-24 19:02:55 +000075** Pointers to the head of the list are stored in mem3.aiSmall[]
76** for smaller chunks and mem3.aiHash[] for larger chunks.
drh9c7a60d2007-10-19 17:47:24 +000077**
78** The second block of a chunk is user data if the chunk is checked
drh71f971b2007-12-29 13:18:22 +000079** out. If a chunk is checked out, the user data may extend into
80** the u.hdr.prevSize value of the following chunk.
drh9c7a60d2007-10-19 17:47:24 +000081*/
82typedef struct Mem3Block Mem3Block;
83struct Mem3Block {
84 union {
85 struct {
drh71f971b2007-12-29 13:18:22 +000086 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
87 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
drh9c7a60d2007-10-19 17:47:24 +000088 } hdr;
89 struct {
danielk197757e5ea92008-06-24 19:02:55 +000090 u32 next; /* Index in mem3.aPool[] of next free chunk */
91 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
drh9c7a60d2007-10-19 17:47:24 +000092 } list;
93 } u;
94};
95
96/*
97** All of the static variables used by this module are collected
danielk197757e5ea92008-06-24 19:02:55 +000098** into a single structure named "mem3". This is to keep the
drh9c7a60d2007-10-19 17:47:24 +000099** static variables organized and to reduce namespace pollution
100** when this module is combined with other in the amalgamation.
101*/
danielk19775c8f8582008-09-02 10:22:00 +0000102static SQLITE_WSD struct Mem3Global {
drh9c7a60d2007-10-19 17:47:24 +0000103 /*
danielk197723bf0f42008-09-02 17:52:51 +0000104 ** Memory available for allocation. nPool is the size of the array
105 ** (in Mem3Blocks) pointed to by aPool less 2.
106 */
107 u32 nPool;
108 Mem3Block *aPool;
109
110 /*
drha4e5d582007-10-20 15:41:57 +0000111 ** True if we are evaluating an out-of-memory callback.
drh9c7a60d2007-10-19 17:47:24 +0000112 */
drh9c7a60d2007-10-19 17:47:24 +0000113 int alarmBusy;
114
115 /*
116 ** Mutex to control access to the memory allocation subsystem.
117 */
118 sqlite3_mutex *mutex;
119
120 /*
drha4e5d582007-10-20 15:41:57 +0000121 ** The minimum amount of free space that we have seen.
drh9c7a60d2007-10-19 17:47:24 +0000122 */
drh71f971b2007-12-29 13:18:22 +0000123 u32 mnMaster;
drh9c7a60d2007-10-19 17:47:24 +0000124
125 /*
126 ** iMaster is the index of the master chunk. Most new allocations
127 ** occur off of this chunk. szMaster is the size (in Mem3Blocks)
128 ** of the current master. iMaster is 0 if there is not master chunk.
129 ** The master chunk is not in either the aiHash[] or aiSmall[].
130 */
drh71f971b2007-12-29 13:18:22 +0000131 u32 iMaster;
132 u32 szMaster;
drh9c7a60d2007-10-19 17:47:24 +0000133
134 /*
135 ** Array of lists of free blocks according to the block size
136 ** for smaller chunks, or a hash on the block size for larger
137 ** chunks.
138 */
drh71f971b2007-12-29 13:18:22 +0000139 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
140 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
danielk197723bf0f42008-09-02 17:52:51 +0000141} mem3 = { 97535575 };
danielk19775c8f8582008-09-02 10:22:00 +0000142
143#define mem3 GLOBAL(struct Mem3Global, mem3)
drh9c7a60d2007-10-19 17:47:24 +0000144
145/*
danielk197757e5ea92008-06-24 19:02:55 +0000146** Unlink the chunk at mem3.aPool[i] from list it is currently
drh9c7a60d2007-10-19 17:47:24 +0000147** on. *pRoot is the list that i is a member of.
148*/
drh71f971b2007-12-29 13:18:22 +0000149static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
danielk197757e5ea92008-06-24 19:02:55 +0000150 u32 next = mem3.aPool[i].u.list.next;
151 u32 prev = mem3.aPool[i].u.list.prev;
152 assert( sqlite3_mutex_held(mem3.mutex) );
drh9c7a60d2007-10-19 17:47:24 +0000153 if( prev==0 ){
154 *pRoot = next;
155 }else{
danielk197757e5ea92008-06-24 19:02:55 +0000156 mem3.aPool[prev].u.list.next = next;
drh9c7a60d2007-10-19 17:47:24 +0000157 }
158 if( next ){
danielk197757e5ea92008-06-24 19:02:55 +0000159 mem3.aPool[next].u.list.prev = prev;
drh9c7a60d2007-10-19 17:47:24 +0000160 }
danielk197757e5ea92008-06-24 19:02:55 +0000161 mem3.aPool[i].u.list.next = 0;
162 mem3.aPool[i].u.list.prev = 0;
drh9c7a60d2007-10-19 17:47:24 +0000163}
164
165/*
166** Unlink the chunk at index i from
167** whatever list is currently a member of.
168*/
drh71f971b2007-12-29 13:18:22 +0000169static void memsys3Unlink(u32 i){
170 u32 size, hash;
danielk197757e5ea92008-06-24 19:02:55 +0000171 assert( sqlite3_mutex_held(mem3.mutex) );
172 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
drh71f971b2007-12-29 13:18:22 +0000173 assert( i>=1 );
danielk197757e5ea92008-06-24 19:02:55 +0000174 size = mem3.aPool[i-1].u.hdr.size4x/4;
175 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
drh9c7a60d2007-10-19 17:47:24 +0000176 assert( size>=2 );
177 if( size <= MX_SMALL ){
danielk197757e5ea92008-06-24 19:02:55 +0000178 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
drh9c7a60d2007-10-19 17:47:24 +0000179 }else{
180 hash = size % N_HASH;
danielk197757e5ea92008-06-24 19:02:55 +0000181 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
drh9c7a60d2007-10-19 17:47:24 +0000182 }
183}
184
185/*
danielk197757e5ea92008-06-24 19:02:55 +0000186** Link the chunk at mem3.aPool[i] so that is on the list rooted
drh9c7a60d2007-10-19 17:47:24 +0000187** at *pRoot.
188*/
drh71f971b2007-12-29 13:18:22 +0000189static void memsys3LinkIntoList(u32 i, u32 *pRoot){
danielk197757e5ea92008-06-24 19:02:55 +0000190 assert( sqlite3_mutex_held(mem3.mutex) );
191 mem3.aPool[i].u.list.next = *pRoot;
192 mem3.aPool[i].u.list.prev = 0;
drh9c7a60d2007-10-19 17:47:24 +0000193 if( *pRoot ){
danielk197757e5ea92008-06-24 19:02:55 +0000194 mem3.aPool[*pRoot].u.list.prev = i;
drh9c7a60d2007-10-19 17:47:24 +0000195 }
196 *pRoot = i;
197}
198
199/*
200** Link the chunk at index i into either the appropriate
201** small chunk list, or into the large chunk hash table.
202*/
drh71f971b2007-12-29 13:18:22 +0000203static void memsys3Link(u32 i){
204 u32 size, hash;
danielk197757e5ea92008-06-24 19:02:55 +0000205 assert( sqlite3_mutex_held(mem3.mutex) );
drh71f971b2007-12-29 13:18:22 +0000206 assert( i>=1 );
danielk197757e5ea92008-06-24 19:02:55 +0000207 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
208 size = mem3.aPool[i-1].u.hdr.size4x/4;
209 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
drh9c7a60d2007-10-19 17:47:24 +0000210 assert( size>=2 );
211 if( size <= MX_SMALL ){
danielk197757e5ea92008-06-24 19:02:55 +0000212 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
drh9c7a60d2007-10-19 17:47:24 +0000213 }else{
214 hash = size % N_HASH;
danielk197757e5ea92008-06-24 19:02:55 +0000215 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
drh9c7a60d2007-10-19 17:47:24 +0000216 }
217}
218
219/*
danielk19776b39c2e2008-06-25 14:57:53 +0000220** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
221** will already be held (obtained by code in malloc.c) if
danielk1977075c23a2008-09-01 18:34:20 +0000222** sqlite3GlobalConfig.bMemStat is true.
drh9c7a60d2007-10-19 17:47:24 +0000223*/
drha4e5d582007-10-20 15:41:57 +0000224static void memsys3Enter(void){
danielk1977075c23a2008-09-01 18:34:20 +0000225 if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
danielk197757e5ea92008-06-24 19:02:55 +0000226 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drh9c7a60d2007-10-19 17:47:24 +0000227 }
danielk197757e5ea92008-06-24 19:02:55 +0000228 sqlite3_mutex_enter(mem3.mutex);
drh9c7a60d2007-10-19 17:47:24 +0000229}
danielk197757e5ea92008-06-24 19:02:55 +0000230static void memsys3Leave(void){
danielk19776b39c2e2008-06-25 14:57:53 +0000231 sqlite3_mutex_leave(mem3.mutex);
drh9c7a60d2007-10-19 17:47:24 +0000232}
233
234/*
drha4e5d582007-10-20 15:41:57 +0000235** Called when we are unable to satisfy an allocation of nBytes.
drh9c7a60d2007-10-19 17:47:24 +0000236*/
drha4e5d582007-10-20 15:41:57 +0000237static void memsys3OutOfMemory(int nByte){
danielk197757e5ea92008-06-24 19:02:55 +0000238 if( !mem3.alarmBusy ){
239 mem3.alarmBusy = 1;
240 assert( sqlite3_mutex_held(mem3.mutex) );
241 sqlite3_mutex_leave(mem3.mutex);
drha4e5d582007-10-20 15:41:57 +0000242 sqlite3_release_memory(nByte);
danielk197757e5ea92008-06-24 19:02:55 +0000243 sqlite3_mutex_enter(mem3.mutex);
244 mem3.alarmBusy = 0;
drha4e5d582007-10-20 15:41:57 +0000245 }
drh9c7a60d2007-10-19 17:47:24 +0000246}
247
drh40257ff2008-06-13 18:24:27 +0000248
249/*
drh9c7a60d2007-10-19 17:47:24 +0000250** Chunk i is a free chunk that has been unlinked. Adjust its
251** size parameters for check-out and return a pointer to the
252** user portion of the chunk.
253*/
danielk1977a03396a2008-11-19 14:35:46 +0000254static void *memsys3Checkout(u32 i, u32 nBlock){
drh71f971b2007-12-29 13:18:22 +0000255 u32 x;
danielk197757e5ea92008-06-24 19:02:55 +0000256 assert( sqlite3_mutex_held(mem3.mutex) );
drh71f971b2007-12-29 13:18:22 +0000257 assert( i>=1 );
danielk197757e5ea92008-06-24 19:02:55 +0000258 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
259 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
260 x = mem3.aPool[i-1].u.hdr.size4x;
261 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
262 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
263 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
264 return &mem3.aPool[i];
drh9c7a60d2007-10-19 17:47:24 +0000265}
266
267/*
danielk197757e5ea92008-06-24 19:02:55 +0000268** Carve a piece off of the end of the mem3.iMaster free chunk.
drh9c7a60d2007-10-19 17:47:24 +0000269** Return a pointer to the new allocation. Or, if the master chunk
270** is not large enough, return 0.
271*/
danielk1977a03396a2008-11-19 14:35:46 +0000272static void *memsys3FromMaster(u32 nBlock){
danielk197757e5ea92008-06-24 19:02:55 +0000273 assert( sqlite3_mutex_held(mem3.mutex) );
274 assert( mem3.szMaster>=nBlock );
275 if( nBlock>=mem3.szMaster-1 ){
drh9c7a60d2007-10-19 17:47:24 +0000276 /* Use the entire master */
danielk197757e5ea92008-06-24 19:02:55 +0000277 void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
278 mem3.iMaster = 0;
279 mem3.szMaster = 0;
280 mem3.mnMaster = 0;
drh9c7a60d2007-10-19 17:47:24 +0000281 return p;
282 }else{
283 /* Split the master block. Return the tail. */
drh71f971b2007-12-29 13:18:22 +0000284 u32 newi, x;
danielk197757e5ea92008-06-24 19:02:55 +0000285 newi = mem3.iMaster + mem3.szMaster - nBlock;
286 assert( newi > mem3.iMaster+1 );
287 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
288 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
289 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
290 mem3.szMaster -= nBlock;
291 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
292 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
293 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
294 if( mem3.szMaster < mem3.mnMaster ){
295 mem3.mnMaster = mem3.szMaster;
drha4e5d582007-10-20 15:41:57 +0000296 }
danielk197757e5ea92008-06-24 19:02:55 +0000297 return (void*)&mem3.aPool[newi];
drh9c7a60d2007-10-19 17:47:24 +0000298 }
299}
300
301/*
302** *pRoot is the head of a list of free chunks of the same size
303** or same size hash. In other words, *pRoot is an entry in either
danielk197757e5ea92008-06-24 19:02:55 +0000304** mem3.aiSmall[] or mem3.aiHash[].
drh9c7a60d2007-10-19 17:47:24 +0000305**
306** This routine examines all entries on the given list and tries
307** to coalesce each entries with adjacent free chunks.
308**
danielk197757e5ea92008-06-24 19:02:55 +0000309** If it sees a chunk that is larger than mem3.iMaster, it replaces
310** the current mem3.iMaster with the new larger chunk. In order for
311** this mem3.iMaster replacement to work, the master chunk must be
drh9c7a60d2007-10-19 17:47:24 +0000312** linked into the hash tables. That is not the normal state of
313** affairs, of course. The calling routine must link the master
314** chunk before invoking this routine, then must unlink the (possibly
315** changed) master chunk once this routine has finished.
316*/
drh71f971b2007-12-29 13:18:22 +0000317static void memsys3Merge(u32 *pRoot){
318 u32 iNext, prev, size, i, x;
drh9c7a60d2007-10-19 17:47:24 +0000319
danielk197757e5ea92008-06-24 19:02:55 +0000320 assert( sqlite3_mutex_held(mem3.mutex) );
drh9c7a60d2007-10-19 17:47:24 +0000321 for(i=*pRoot; i>0; i=iNext){
danielk197757e5ea92008-06-24 19:02:55 +0000322 iNext = mem3.aPool[i].u.list.next;
323 size = mem3.aPool[i-1].u.hdr.size4x;
drh71f971b2007-12-29 13:18:22 +0000324 assert( (size&1)==0 );
325 if( (size&2)==0 ){
drha4e5d582007-10-20 15:41:57 +0000326 memsys3UnlinkFromList(i, pRoot);
danielk197757e5ea92008-06-24 19:02:55 +0000327 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
328 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
drh9c7a60d2007-10-19 17:47:24 +0000329 if( prev==iNext ){
danielk197757e5ea92008-06-24 19:02:55 +0000330 iNext = mem3.aPool[prev].u.list.next;
drh9c7a60d2007-10-19 17:47:24 +0000331 }
drha4e5d582007-10-20 15:41:57 +0000332 memsys3Unlink(prev);
drh71f971b2007-12-29 13:18:22 +0000333 size = i + size/4 - prev;
danielk197757e5ea92008-06-24 19:02:55 +0000334 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
335 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
336 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
drha4e5d582007-10-20 15:41:57 +0000337 memsys3Link(prev);
drh9c7a60d2007-10-19 17:47:24 +0000338 i = prev;
drh71f971b2007-12-29 13:18:22 +0000339 }else{
340 size /= 4;
drh9c7a60d2007-10-19 17:47:24 +0000341 }
danielk197757e5ea92008-06-24 19:02:55 +0000342 if( size>mem3.szMaster ){
343 mem3.iMaster = i;
344 mem3.szMaster = size;
drh9c7a60d2007-10-19 17:47:24 +0000345 }
346 }
347}
348
349/*
350** Return a block of memory of at least nBytes in size.
351** Return NULL if unable.
danielk197732155ef2008-06-25 10:34:34 +0000352**
353** This function assumes that the necessary mutexes, if any, are
354** already held by the caller. Hence "Unsafe".
drh9c7a60d2007-10-19 17:47:24 +0000355*/
danielk197732155ef2008-06-25 10:34:34 +0000356static void *memsys3MallocUnsafe(int nByte){
drh71f971b2007-12-29 13:18:22 +0000357 u32 i;
danielk1977a03396a2008-11-19 14:35:46 +0000358 u32 nBlock;
359 u32 toFree;
drh9c7a60d2007-10-19 17:47:24 +0000360
danielk197757e5ea92008-06-24 19:02:55 +0000361 assert( sqlite3_mutex_held(mem3.mutex) );
drh9c7a60d2007-10-19 17:47:24 +0000362 assert( sizeof(Mem3Block)==8 );
drh71f971b2007-12-29 13:18:22 +0000363 if( nByte<=12 ){
drh9c7a60d2007-10-19 17:47:24 +0000364 nBlock = 2;
365 }else{
drh71f971b2007-12-29 13:18:22 +0000366 nBlock = (nByte + 11)/8;
drh9c7a60d2007-10-19 17:47:24 +0000367 }
danielk197757e5ea92008-06-24 19:02:55 +0000368 assert( nBlock>=2 );
drh9c7a60d2007-10-19 17:47:24 +0000369
370 /* STEP 1:
371 ** Look for an entry of the correct size in either the small
372 ** chunk table or in the large chunk hash table. This is
373 ** successful most of the time (about 9 times out of 10).
374 */
375 if( nBlock <= MX_SMALL ){
danielk197757e5ea92008-06-24 19:02:55 +0000376 i = mem3.aiSmall[nBlock-2];
drh9c7a60d2007-10-19 17:47:24 +0000377 if( i>0 ){
danielk197757e5ea92008-06-24 19:02:55 +0000378 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
drha4e5d582007-10-20 15:41:57 +0000379 return memsys3Checkout(i, nBlock);
drh9c7a60d2007-10-19 17:47:24 +0000380 }
381 }else{
382 int hash = nBlock % N_HASH;
danielk197757e5ea92008-06-24 19:02:55 +0000383 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
384 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
385 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
drha4e5d582007-10-20 15:41:57 +0000386 return memsys3Checkout(i, nBlock);
drh9c7a60d2007-10-19 17:47:24 +0000387 }
388 }
389 }
390
391 /* STEP 2:
392 ** Try to satisfy the allocation by carving a piece off of the end
393 ** of the master chunk. This step usually works if step 1 fails.
394 */
danielk197757e5ea92008-06-24 19:02:55 +0000395 if( mem3.szMaster>=nBlock ){
drha4e5d582007-10-20 15:41:57 +0000396 return memsys3FromMaster(nBlock);
drh9c7a60d2007-10-19 17:47:24 +0000397 }
398
399
400 /* STEP 3:
401 ** Loop through the entire memory pool. Coalesce adjacent free
402 ** chunks. Recompute the master chunk as the largest free chunk.
403 ** Then try again to satisfy the allocation by carving a piece off
404 ** of the end of the master chunk. This step happens very
405 ** rarely (we hope!)
406 */
danielk197757e5ea92008-06-24 19:02:55 +0000407 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
drh979aeaa2007-10-20 16:36:31 +0000408 memsys3OutOfMemory(toFree);
danielk197757e5ea92008-06-24 19:02:55 +0000409 if( mem3.iMaster ){
410 memsys3Link(mem3.iMaster);
411 mem3.iMaster = 0;
412 mem3.szMaster = 0;
drh979aeaa2007-10-20 16:36:31 +0000413 }
414 for(i=0; i<N_HASH; i++){
danielk197757e5ea92008-06-24 19:02:55 +0000415 memsys3Merge(&mem3.aiHash[i]);
drh979aeaa2007-10-20 16:36:31 +0000416 }
417 for(i=0; i<MX_SMALL-1; i++){
danielk197757e5ea92008-06-24 19:02:55 +0000418 memsys3Merge(&mem3.aiSmall[i]);
drh979aeaa2007-10-20 16:36:31 +0000419 }
danielk197757e5ea92008-06-24 19:02:55 +0000420 if( mem3.szMaster ){
421 memsys3Unlink(mem3.iMaster);
422 if( mem3.szMaster>=nBlock ){
drh979aeaa2007-10-20 16:36:31 +0000423 return memsys3FromMaster(nBlock);
424 }
drh9c7a60d2007-10-19 17:47:24 +0000425 }
426 }
427
428 /* If none of the above worked, then we fail. */
429 return 0;
430}
431
432/*
433** Free an outstanding memory allocation.
danielk197732155ef2008-06-25 10:34:34 +0000434**
435** This function assumes that the necessary mutexes, if any, are
436** already held by the caller. Hence "Unsafe".
drh9c7a60d2007-10-19 17:47:24 +0000437*/
danielk197732155ef2008-06-25 10:34:34 +0000438void memsys3FreeUnsafe(void *pOld){
drh9c7a60d2007-10-19 17:47:24 +0000439 Mem3Block *p = (Mem3Block*)pOld;
440 int i;
drh71f971b2007-12-29 13:18:22 +0000441 u32 size, x;
danielk197757e5ea92008-06-24 19:02:55 +0000442 assert( sqlite3_mutex_held(mem3.mutex) );
443 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
444 i = p - mem3.aPool;
445 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
446 size = mem3.aPool[i-1].u.hdr.size4x/4;
447 assert( i+size<=mem3.nPool+1 );
448 mem3.aPool[i-1].u.hdr.size4x &= ~1;
449 mem3.aPool[i+size-1].u.hdr.prevSize = size;
450 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
drha4e5d582007-10-20 15:41:57 +0000451 memsys3Link(i);
drh9c7a60d2007-10-19 17:47:24 +0000452
453 /* Try to expand the master using the newly freed chunk */
danielk197757e5ea92008-06-24 19:02:55 +0000454 if( mem3.iMaster ){
455 while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
456 size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
457 mem3.iMaster -= size;
458 mem3.szMaster += size;
459 memsys3Unlink(mem3.iMaster);
460 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
461 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
462 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
drh9c7a60d2007-10-19 17:47:24 +0000463 }
danielk197757e5ea92008-06-24 19:02:55 +0000464 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
465 while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
466 memsys3Unlink(mem3.iMaster+mem3.szMaster);
467 mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
468 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
469 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
drh9c7a60d2007-10-19 17:47:24 +0000470 }
471 }
472}
473
474/*
drhc702c7c2008-07-18 18:56:16 +0000475** Return the size of an outstanding allocation, in bytes. The
476** size returned omits the 8-byte header overhead. This only
477** works for chunks that are currently checked out.
478*/
479static int memsys3Size(void *p){
480 Mem3Block *pBlock;
481 if( p==0 ) return 0;
482 pBlock = (Mem3Block*)p;
483 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
484 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
485}
486
487/*
488** Round up a request size to the next valid allocation size.
489*/
490static int memsys3Roundup(int n){
491 if( n<=12 ){
492 return 12;
493 }else{
494 return ((n+11)&~7) - 4;
495 }
496}
497
498/*
danielk197757e5ea92008-06-24 19:02:55 +0000499** Allocate nBytes of memory.
drh9c7a60d2007-10-19 17:47:24 +0000500*/
danielk197732155ef2008-06-25 10:34:34 +0000501static void *memsys3Malloc(int nBytes){
danielk197757e5ea92008-06-24 19:02:55 +0000502 sqlite3_int64 *p;
503 assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
504 memsys3Enter();
danielk197732155ef2008-06-25 10:34:34 +0000505 p = memsys3MallocUnsafe(nBytes);
danielk197757e5ea92008-06-24 19:02:55 +0000506 memsys3Leave();
drh9c7a60d2007-10-19 17:47:24 +0000507 return (void*)p;
508}
509
510/*
511** Free memory.
512*/
danielk197732155ef2008-06-25 10:34:34 +0000513void memsys3Free(void *pPrior){
danielk197757e5ea92008-06-24 19:02:55 +0000514 assert( pPrior );
515 memsys3Enter();
danielk197732155ef2008-06-25 10:34:34 +0000516 memsys3FreeUnsafe(pPrior);
danielk197757e5ea92008-06-24 19:02:55 +0000517 memsys3Leave();
518}
519
520/*
drh9c7a60d2007-10-19 17:47:24 +0000521** Change the size of an existing memory allocation
522*/
danielk197732155ef2008-06-25 10:34:34 +0000523void *memsys3Realloc(void *pPrior, int nBytes){
drh9c7a60d2007-10-19 17:47:24 +0000524 int nOld;
525 void *p;
526 if( pPrior==0 ){
527 return sqlite3_malloc(nBytes);
528 }
529 if( nBytes<=0 ){
530 sqlite3_free(pPrior);
531 return 0;
532 }
danielk197732155ef2008-06-25 10:34:34 +0000533 nOld = memsys3Size(pPrior);
drha4e5d582007-10-20 15:41:57 +0000534 if( nBytes<=nOld && nBytes>=nOld-128 ){
535 return pPrior;
536 }
danielk197757e5ea92008-06-24 19:02:55 +0000537 memsys3Enter();
danielk197732155ef2008-06-25 10:34:34 +0000538 p = memsys3MallocUnsafe(nBytes);
drha4e5d582007-10-20 15:41:57 +0000539 if( p ){
540 if( nOld<nBytes ){
541 memcpy(p, pPrior, nOld);
542 }else{
543 memcpy(p, pPrior, nBytes);
drh9c7a60d2007-10-19 17:47:24 +0000544 }
danielk197732155ef2008-06-25 10:34:34 +0000545 memsys3FreeUnsafe(pPrior);
drh9c7a60d2007-10-19 17:47:24 +0000546 }
danielk197757e5ea92008-06-24 19:02:55 +0000547 memsys3Leave();
drh9c7a60d2007-10-19 17:47:24 +0000548 return p;
549}
550
551/*
danielk197757e5ea92008-06-24 19:02:55 +0000552** Initialize this module.
553*/
danielk197732155ef2008-06-25 10:34:34 +0000554static int memsys3Init(void *NotUsed){
danielk1977a03396a2008-11-19 14:35:46 +0000555 UNUSED_PARAMETER(NotUsed);
danielk1977075c23a2008-09-01 18:34:20 +0000556 if( !sqlite3GlobalConfig.pHeap ){
danielk19770d84e5b2008-06-27 14:05:24 +0000557 return SQLITE_ERROR;
558 }
559
560 /* Store a pointer to the memory block in global structure mem3. */
561 assert( sizeof(Mem3Block)==8 );
danielk1977075c23a2008-09-01 18:34:20 +0000562 mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
563 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
danielk19770d84e5b2008-06-27 14:05:24 +0000564
565 /* Initialize the master block. */
566 mem3.szMaster = mem3.nPool;
567 mem3.mnMaster = mem3.szMaster;
568 mem3.iMaster = 1;
569 mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
570 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
571 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
572
danielk197757e5ea92008-06-24 19:02:55 +0000573 return SQLITE_OK;
574}
575
576/*
577** Deinitialize this module.
578*/
danielk197732155ef2008-06-25 10:34:34 +0000579static void memsys3Shutdown(void *NotUsed){
danielk1977a03396a2008-11-19 14:35:46 +0000580 UNUSED_PARAMETER(NotUsed);
danielk197757e5ea92008-06-24 19:02:55 +0000581 return;
582}
583
584
585
586/*
drh9c7a60d2007-10-19 17:47:24 +0000587** Open the file indicated and write a log of all unfreed memory
588** allocations into that log.
589*/
danielk197732155ef2008-06-25 10:34:34 +0000590void sqlite3Memsys3Dump(const char *zFilename){
danielk19775c8f8582008-09-02 10:22:00 +0000591#ifdef SQLITE_DEBUG
drh9c7a60d2007-10-19 17:47:24 +0000592 FILE *out;
danielk1977a03396a2008-11-19 14:35:46 +0000593 u32 i, j;
drh71f971b2007-12-29 13:18:22 +0000594 u32 size;
drh9c7a60d2007-10-19 17:47:24 +0000595 if( zFilename==0 || zFilename[0]==0 ){
596 out = stdout;
597 }else{
598 out = fopen(zFilename, "w");
599 if( out==0 ){
600 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
601 zFilename);
602 return;
603 }
604 }
drha4e5d582007-10-20 15:41:57 +0000605 memsys3Enter();
drh9c7a60d2007-10-19 17:47:24 +0000606 fprintf(out, "CHUNKS:\n");
danielk197732155ef2008-06-25 10:34:34 +0000607 for(i=1; i<=mem3.nPool; i+=size/4){
danielk197757e5ea92008-06-24 19:02:55 +0000608 size = mem3.aPool[i-1].u.hdr.size4x;
drh71f971b2007-12-29 13:18:22 +0000609 if( size/4<=1 ){
danielk197757e5ea92008-06-24 19:02:55 +0000610 fprintf(out, "%p size error\n", &mem3.aPool[i]);
drh9c7a60d2007-10-19 17:47:24 +0000611 assert( 0 );
612 break;
613 }
danielk197757e5ea92008-06-24 19:02:55 +0000614 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
615 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
drh9c7a60d2007-10-19 17:47:24 +0000616 assert( 0 );
617 break;
618 }
danielk197757e5ea92008-06-24 19:02:55 +0000619 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
620 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
drh71f971b2007-12-29 13:18:22 +0000621 assert( 0 );
622 break;
623 }
624 if( size&1 ){
danielk197757e5ea92008-06-24 19:02:55 +0000625 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
drh9c7a60d2007-10-19 17:47:24 +0000626 }else{
danielk197757e5ea92008-06-24 19:02:55 +0000627 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
628 i==mem3.iMaster ? " **master**" : "");
drh9c7a60d2007-10-19 17:47:24 +0000629 }
630 }
631 for(i=0; i<MX_SMALL-1; i++){
danielk197757e5ea92008-06-24 19:02:55 +0000632 if( mem3.aiSmall[i]==0 ) continue;
drh9c7a60d2007-10-19 17:47:24 +0000633 fprintf(out, "small(%2d):", i);
danielk197757e5ea92008-06-24 19:02:55 +0000634 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
635 fprintf(out, " %p(%d)", &mem3.aPool[j],
636 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
drh9c7a60d2007-10-19 17:47:24 +0000637 }
638 fprintf(out, "\n");
639 }
640 for(i=0; i<N_HASH; i++){
danielk197757e5ea92008-06-24 19:02:55 +0000641 if( mem3.aiHash[i]==0 ) continue;
drh9c7a60d2007-10-19 17:47:24 +0000642 fprintf(out, "hash(%2d):", i);
danielk197757e5ea92008-06-24 19:02:55 +0000643 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
644 fprintf(out, " %p(%d)", &mem3.aPool[j],
645 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
drh9c7a60d2007-10-19 17:47:24 +0000646 }
647 fprintf(out, "\n");
648 }
danielk197757e5ea92008-06-24 19:02:55 +0000649 fprintf(out, "master=%d\n", mem3.iMaster);
danielk197732155ef2008-06-25 10:34:34 +0000650 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
651 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
danielk197757e5ea92008-06-24 19:02:55 +0000652 sqlite3_mutex_leave(mem3.mutex);
drh9c7a60d2007-10-19 17:47:24 +0000653 if( out==stdout ){
654 fflush(stdout);
655 }else{
656 fclose(out);
657 }
danielk1977f3d3c272008-11-19 16:52:44 +0000658#else
659 UNUSED_PARAMETER(zFilename);
danielk197757e5ea92008-06-24 19:02:55 +0000660#endif
danielk19775c8f8582008-09-02 10:22:00 +0000661}
drh9c7a60d2007-10-19 17:47:24 +0000662
danielk197757e5ea92008-06-24 19:02:55 +0000663/*
664** This routine is the only routine in this file with external
665** linkage.
666**
667** Populate the low-level memory allocation function pointers in
danielk1977075c23a2008-09-01 18:34:20 +0000668** sqlite3GlobalConfig.m with pointers to the routines in this file. The
danielk197757e5ea92008-06-24 19:02:55 +0000669** arguments specify the block of memory to manage.
670**
671** This routine is only called by sqlite3_config(), and therefore
672** is not required to be threadsafe (it is not).
673*/
danielk19770d84e5b2008-06-27 14:05:24 +0000674const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
danielk197757e5ea92008-06-24 19:02:55 +0000675 static const sqlite3_mem_methods mempoolMethods = {
danielk197732155ef2008-06-25 10:34:34 +0000676 memsys3Malloc,
677 memsys3Free,
678 memsys3Realloc,
679 memsys3Size,
680 memsys3Roundup,
681 memsys3Init,
682 memsys3Shutdown,
danielk197757e5ea92008-06-24 19:02:55 +0000683 0
684 };
danielk19770d84e5b2008-06-27 14:05:24 +0000685 return &mempoolMethods;
danielk197757e5ea92008-06-24 19:02:55 +0000686}
687
danielk197732155ef2008-06-25 10:34:34 +0000688#endif /* SQLITE_ENABLE_MEMSYS3 */