190f6a5beSdrh /*
290f6a5beSdrh ** 2007 August 14
390f6a5beSdrh **
490f6a5beSdrh ** The author disclaims copyright to this source code. In place of
590f6a5beSdrh ** a legal notice, here is a blessing:
690f6a5beSdrh **
790f6a5beSdrh ** May you do good and not evil.
890f6a5beSdrh ** May you find forgiveness for yourself and forgive others.
990f6a5beSdrh ** May you share freely, never taking more than you give.
1090f6a5beSdrh **
1190f6a5beSdrh *************************************************************************
1290f6a5beSdrh **
13fec00eabSdrh ** This file contains low-level memory allocation drivers for when
14fec00eabSdrh ** SQLite will use the standard C-library malloc/realloc/free interface
15fec00eabSdrh ** to obtain the memory it needs.
16fec00eabSdrh **
17fec00eabSdrh ** This file contains implementations of the low-level memory allocation
18c710ccb0Sdrh ** routines specified in the sqlite3_mem_methods object. The content of
19c710ccb0Sdrh ** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
20c710ccb0Sdrh ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
21c710ccb0Sdrh ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
22c710ccb0Sdrh ** default configuration is to use memory allocation routines in this
23c710ccb0Sdrh ** file.
24c710ccb0Sdrh **
25c710ccb0Sdrh ** C-preprocessor macro summary:
26c710ccb0Sdrh **
27c710ccb0Sdrh ** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
28c710ccb0Sdrh ** the malloc_usable_size() interface exists
29c710ccb0Sdrh ** on the target platform. Or, this symbol
30c710ccb0Sdrh ** can be set manually, if desired.
31c710ccb0Sdrh ** If an equivalent interface exists by
32c710ccb0Sdrh ** a different name, using a separate -D
33a844f513Smistachkin ** option to rename it.
34c710ccb0Sdrh **
35c710ccb0Sdrh ** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
36c710ccb0Sdrh ** memory allocator. Set this symbol to enable
37c710ccb0Sdrh ** building on older macs.
38c710ccb0Sdrh **
39c710ccb0Sdrh ** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
40c710ccb0Sdrh ** _msize() on windows systems. This might
41c710ccb0Sdrh ** be necessary when compiling for Delphi,
42c710ccb0Sdrh ** for example.
4390f6a5beSdrh */
440d18020bSdrh #include "sqliteInt.h"
4590f6a5beSdrh
4690f6a5beSdrh /*
474c3645c6Sdrh ** This version of the memory allocator is the default. It is
484c3645c6Sdrh ** used when no other memory allocator is specified using compile-time
494c3645c6Sdrh ** macros.
504c3645c6Sdrh */
510d18020bSdrh #ifdef SQLITE_SYSTEM_MALLOC
52c710ccb0Sdrh #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
53f1c5726eSdrh
54f1c5726eSdrh /*
55c710ccb0Sdrh ** Use the zone allocator available on apple products unless the
56c710ccb0Sdrh ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
57f1c5726eSdrh */
58f1c5726eSdrh #include <sys/sysctl.h>
59f1c5726eSdrh #include <malloc/malloc.h>
60a80e1606Sdrh #ifdef SQLITE_MIGHT_BE_SINGLE_CORE
61f1c5726eSdrh #include <libkern/OSAtomic.h>
62a80e1606Sdrh #endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
63f1c5726eSdrh static malloc_zone_t* _sqliteZone_;
64f1c5726eSdrh #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
65f1c5726eSdrh #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
66f1c5726eSdrh #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
67f1c5726eSdrh #define SQLITE_MALLOCSIZE(x) \
68f1c5726eSdrh (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
69f1c5726eSdrh
70f1c5726eSdrh #else /* if not __APPLE__ */
71f1c5726eSdrh
72f1c5726eSdrh /*
73f1c5726eSdrh ** Use standard C library malloc and free on non-Apple systems.
74c710ccb0Sdrh ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
75f1c5726eSdrh */
76f1c5726eSdrh #define SQLITE_MALLOC(x) malloc(x)
77f1c5726eSdrh #define SQLITE_FREE(x) free(x)
78f1c5726eSdrh #define SQLITE_REALLOC(x,y) realloc((x),(y))
79f1c5726eSdrh
80015a304fSmistachkin /*
81015a304fSmistachkin ** The malloc.h header file is needed for malloc_usable_size() function
82015a304fSmistachkin ** on some systems (e.g. Linux).
83015a304fSmistachkin */
840ede9ebeSdrh #if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
850ede9ebeSdrh # define SQLITE_USE_MALLOC_H 1
860ede9ebeSdrh # define SQLITE_USE_MALLOC_USABLE_SIZE 1
87015a304fSmistachkin /*
88015a304fSmistachkin ** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
89015a304fSmistachkin ** use of _msize() is automatic, but can be disabled by compiling with
90015a304fSmistachkin ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
91015a304fSmistachkin ** the malloc.h header file.
92015a304fSmistachkin */
93015a304fSmistachkin #elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
94015a304fSmistachkin # define SQLITE_USE_MALLOC_H
95015a304fSmistachkin # define SQLITE_USE_MSIZE
9686c5a930Sdrh #endif
97015a304fSmistachkin
98015a304fSmistachkin /*
99015a304fSmistachkin ** Include the malloc.h header file, if necessary. Also set define macro
100015a304fSmistachkin ** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
101015a304fSmistachkin ** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
102015a304fSmistachkin ** The memory size function can always be overridden manually by defining
103015a304fSmistachkin ** the macro SQLITE_MALLOCSIZE to the desired function name.
104015a304fSmistachkin */
105015a304fSmistachkin #if defined(SQLITE_USE_MALLOC_H)
106015a304fSmistachkin # include <malloc.h>
107015a304fSmistachkin # if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
108015a304fSmistachkin # if !defined(SQLITE_MALLOCSIZE)
109f1c5726eSdrh # define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
1100541b3d4Sdrh # endif
111015a304fSmistachkin # elif defined(SQLITE_USE_MSIZE)
112015a304fSmistachkin # if !defined(SQLITE_MALLOCSIZE)
113015a304fSmistachkin # define SQLITE_MALLOCSIZE _msize
1146a8ab6d9Sdrh # endif
115015a304fSmistachkin # endif
116015a304fSmistachkin #endif /* defined(SQLITE_USE_MALLOC_H) */
1176a8ab6d9Sdrh
118f1c5726eSdrh #endif /* __APPLE__ or not __APPLE__ */
119f1c5726eSdrh
12090f6a5beSdrh /*
121fec00eabSdrh ** Like malloc(), but remember the size of the allocation
122fec00eabSdrh ** so that we can find it later using sqlite3MemSize().
123fec00eabSdrh **
124fec00eabSdrh ** For this low-level routine, we are guaranteed that nByte>0 because
125fec00eabSdrh ** cases of nByte<=0 will be intercepted and dealt with by higher level
126fec00eabSdrh ** routines.
12790f6a5beSdrh */
sqlite3MemMalloc(int nByte)128fec00eabSdrh static void *sqlite3MemMalloc(int nByte){
129f1c5726eSdrh #ifdef SQLITE_MALLOCSIZE
130087a29c7Sdrh void *p;
131087a29c7Sdrh testcase( ROUND8(nByte)==nByte );
132087a29c7Sdrh p = SQLITE_MALLOC( nByte );
1336a8ab6d9Sdrh if( p==0 ){
1346a8ab6d9Sdrh testcase( sqlite3GlobalConfig.xLog!=0 );
1356a8ab6d9Sdrh sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
1366a8ab6d9Sdrh }
1376a8ab6d9Sdrh return p;
1386a8ab6d9Sdrh #else
139fec00eabSdrh sqlite3_int64 *p;
140fec00eabSdrh assert( nByte>0 );
141087a29c7Sdrh testcase( ROUND8(nByte)!=nByte );
142f1c5726eSdrh p = SQLITE_MALLOC( nByte+8 );
143950292fbSdanielk1977 if( p ){
144fec00eabSdrh p[0] = nByte;
145950292fbSdanielk1977 p++;
146413c3d36Sdrh }else{
147af46dc12Sdrh testcase( sqlite3GlobalConfig.xLog!=0 );
148413c3d36Sdrh sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
149950292fbSdanielk1977 }
150950292fbSdanielk1977 return (void *)p;
1516a8ab6d9Sdrh #endif
152ca0c8971Sdanielk1977 }
15390f6a5beSdrh
15490f6a5beSdrh /*
155fec00eabSdrh ** Like free() but works for allocations obtained from sqlite3MemMalloc()
156fec00eabSdrh ** or sqlite3MemRealloc().
157fec00eabSdrh **
158fec00eabSdrh ** For this low-level routine, we already know that pPrior!=0 since
159fec00eabSdrh ** cases where pPrior==0 will have been intecepted and dealt with
160fec00eabSdrh ** by higher-level routines.
16190f6a5beSdrh */
sqlite3MemFree(void * pPrior)162fec00eabSdrh static void sqlite3MemFree(void *pPrior){
163f1c5726eSdrh #ifdef SQLITE_MALLOCSIZE
164f1c5726eSdrh SQLITE_FREE(pPrior);
1656a8ab6d9Sdrh #else
166fec00eabSdrh sqlite3_int64 *p = (sqlite3_int64*)pPrior;
167834a5aabSdanielk1977 assert( pPrior!=0 );
168fec00eabSdrh p--;
169f1c5726eSdrh SQLITE_FREE(p);
1706a8ab6d9Sdrh #endif
17190f6a5beSdrh }
17290f6a5beSdrh
17390f6a5beSdrh /*
174413c3d36Sdrh ** Report the allocated size of a prior return from xMalloc()
175413c3d36Sdrh ** or xRealloc().
176413c3d36Sdrh */
sqlite3MemSize(void * pPrior)177413c3d36Sdrh static int sqlite3MemSize(void *pPrior){
178f1c5726eSdrh #ifdef SQLITE_MALLOCSIZE
1794dd83a22Sdrh assert( pPrior!=0 );
180039ca6abSdrh return (int)SQLITE_MALLOCSIZE(pPrior);
1816a8ab6d9Sdrh #else
182413c3d36Sdrh sqlite3_int64 *p;
1834dd83a22Sdrh assert( pPrior!=0 );
184413c3d36Sdrh p = (sqlite3_int64*)pPrior;
185413c3d36Sdrh p--;
186413c3d36Sdrh return (int)p[0];
1876a8ab6d9Sdrh #endif
188413c3d36Sdrh }
189413c3d36Sdrh
190413c3d36Sdrh /*
191fec00eabSdrh ** Like realloc(). Resize an allocation previously obtained from
192fec00eabSdrh ** sqlite3MemMalloc().
193fec00eabSdrh **
194fec00eabSdrh ** For this low-level interface, we know that pPrior!=0. Cases where
195fec00eabSdrh ** pPrior==0 while have been intercepted by higher-level routine and
19660ec914cSpeter.d.reid ** redirected to xMalloc. Similarly, we know that nByte>0 because
197fec00eabSdrh ** cases where nByte<=0 will have been intercepted by higher-level
198fec00eabSdrh ** routines and redirected to xFree.
19990f6a5beSdrh */
sqlite3MemRealloc(void * pPrior,int nByte)200fec00eabSdrh static void *sqlite3MemRealloc(void *pPrior, int nByte){
201f1c5726eSdrh #ifdef SQLITE_MALLOCSIZE
202f1c5726eSdrh void *p = SQLITE_REALLOC(pPrior, nByte);
2036a8ab6d9Sdrh if( p==0 ){
2046a8ab6d9Sdrh testcase( sqlite3GlobalConfig.xLog!=0 );
2056a8ab6d9Sdrh sqlite3_log(SQLITE_NOMEM,
2066a8ab6d9Sdrh "failed memory resize %u to %u bytes",
207f1c5726eSdrh SQLITE_MALLOCSIZE(pPrior), nByte);
2086a8ab6d9Sdrh }
2096a8ab6d9Sdrh return p;
2106a8ab6d9Sdrh #else
211fec00eabSdrh sqlite3_int64 *p = (sqlite3_int64*)pPrior;
212fec00eabSdrh assert( pPrior!=0 && nByte>0 );
2139f129f46Sdrh assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
214fec00eabSdrh p--;
215f1c5726eSdrh p = SQLITE_REALLOC(p, nByte+8 );
21690f6a5beSdrh if( p ){
217fec00eabSdrh p[0] = nByte;
21890f6a5beSdrh p++;
219413c3d36Sdrh }else{
220af46dc12Sdrh testcase( sqlite3GlobalConfig.xLog!=0 );
221413c3d36Sdrh sqlite3_log(SQLITE_NOMEM,
222413c3d36Sdrh "failed memory resize %u to %u bytes",
223413c3d36Sdrh sqlite3MemSize(pPrior), nByte);
224ca0c8971Sdanielk1977 }
22590f6a5beSdrh return (void*)p;
2266a8ab6d9Sdrh #endif
22790f6a5beSdrh }
22890f6a5beSdrh
22990f6a5beSdrh /*
230fec00eabSdrh ** Round up a request size to the next valid allocation size.
231fec00eabSdrh */
sqlite3MemRoundup(int n)232fec00eabSdrh static int sqlite3MemRoundup(int n){
233bc73971dSdanielk1977 return ROUND8(n);
234fec00eabSdrh }
235fec00eabSdrh
236fec00eabSdrh /*
237fec00eabSdrh ** Initialize this module.
238fec00eabSdrh */
sqlite3MemInit(void * NotUsed)239fec00eabSdrh static int sqlite3MemInit(void *NotUsed){
240c710ccb0Sdrh #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
241*918938f9Sdrh int cpuCount;
242*918938f9Sdrh size_t len;
243f1c5726eSdrh if( _sqliteZone_ ){
244f1c5726eSdrh return SQLITE_OK;
245f1c5726eSdrh }
246f1c5726eSdrh len = sizeof(cpuCount);
247f1c5726eSdrh /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
248f1c5726eSdrh sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
249f1c5726eSdrh if( cpuCount>1 ){
250f1c5726eSdrh /* defer MT decisions to system malloc */
251f1c5726eSdrh _sqliteZone_ = malloc_default_zone();
252f1c5726eSdrh }else{
253f1c5726eSdrh /* only 1 core, use our own zone to contention over global locks,
254f1c5726eSdrh ** e.g. we have our own dedicated locks */
255*918938f9Sdrh _sqliteZone_ = malloc_create_zone(4096, 0);
256*918938f9Sdrh malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
257f1c5726eSdrh }
258a80e1606Sdrh #endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
25962c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed);
260fec00eabSdrh return SQLITE_OK;
261fec00eabSdrh }
262fec00eabSdrh
263fec00eabSdrh /*
264fec00eabSdrh ** Deinitialize this module.
265fec00eabSdrh */
sqlite3MemShutdown(void * NotUsed)266fec00eabSdrh static void sqlite3MemShutdown(void *NotUsed){
26762c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed);
26890f6a5beSdrh return;
26990f6a5beSdrh }
27090f6a5beSdrh
271d1370b6dSdrh /*
272d1370b6dSdrh ** This routine is the only routine in this file with external linkage.
273d1370b6dSdrh **
274d1370b6dSdrh ** Populate the low-level memory allocation function pointers in
275d1370b6dSdrh ** sqlite3GlobalConfig.m with pointers to the routines in this file.
276d1370b6dSdrh */
sqlite3MemSetDefault(void)277d1370b6dSdrh void sqlite3MemSetDefault(void){
278fec00eabSdrh static const sqlite3_mem_methods defaultMethods = {
279fec00eabSdrh sqlite3MemMalloc,
280fec00eabSdrh sqlite3MemFree,
281fec00eabSdrh sqlite3MemRealloc,
282fec00eabSdrh sqlite3MemSize,
283fec00eabSdrh sqlite3MemRoundup,
284fec00eabSdrh sqlite3MemInit,
285fec00eabSdrh sqlite3MemShutdown,
286fec00eabSdrh 0
287fec00eabSdrh };
288d1370b6dSdrh sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
28990f6a5beSdrh }
2904c3645c6Sdrh
2910d18020bSdrh #endif /* SQLITE_SYSTEM_MALLOC */
292