blob: ec9a4e3a61d18e11a5fa247e543ac6b182d2f59e [file] [log] [blame]
drh90f6a5b2007-08-15 13:04:54 +00001/*
2** 2007 August 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*************************************************************************
drh90f6a5b2007-08-15 13:04:54 +000012**
drhfec00ea2008-06-14 16:56:21 +000013** This file contains low-level memory allocation drivers for when
14** SQLite will use the standard C-library malloc/realloc/free interface
15** to obtain the memory it needs.
16**
17** This file contains implementations of the low-level memory allocation
drhc710ccb2012-01-18 12:46:47 +000018** routines specified in the sqlite3_mem_methods object. The content of
19** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
20** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
21** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
22** default configuration is to use memory allocation routines in this
23** file.
24**
25** C-preprocessor macro summary:
26**
27** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
28** the malloc_usable_size() interface exists
29** on the target platform. Or, this symbol
30** can be set manually, if desired.
31** If an equivalent interface exists by
32** a different name, using a separate -D
mistachkina844f512012-02-11 21:56:59 +000033** option to rename it.
34**
drhc710ccb2012-01-18 12:46:47 +000035** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
36** memory allocator. Set this symbol to enable
37** building on older macs.
38**
39** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
40** _msize() on windows systems. This might
41** be necessary when compiling for Delphi,
42** for example.
drh90f6a5b2007-08-15 13:04:54 +000043*/
drh0d180202008-02-14 23:26:56 +000044#include "sqliteInt.h"
drh90f6a5b2007-08-15 13:04:54 +000045
46/*
drh4c3645c2007-08-15 17:07:57 +000047** This version of the memory allocator is the default. It is
48** used when no other memory allocator is specified using compile-time
49** macros.
50*/
drh0d180202008-02-14 23:26:56 +000051#ifdef SQLITE_SYSTEM_MALLOC
drhc710ccb2012-01-18 12:46:47 +000052#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
drhf1c57262011-12-08 20:41:33 +000053
54/*
drhc710ccb2012-01-18 12:46:47 +000055** Use the zone allocator available on apple products unless the
56** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
drhf1c57262011-12-08 20:41:33 +000057*/
58#include <sys/sysctl.h>
59#include <malloc/malloc.h>
60#include <libkern/OSAtomic.h>
61static malloc_zone_t* _sqliteZone_;
62#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
63#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
64#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
65#define SQLITE_MALLOCSIZE(x) \
66 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
67
68#else /* if not __APPLE__ */
69
70/*
drhc710ccb2012-01-18 12:46:47 +000071** Use standard C library malloc and free on non-Apple systems.
72** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
drhf1c57262011-12-08 20:41:33 +000073*/
mistachkin015a3042013-11-12 21:37:04 +000074#define SQLITE_MALLOC(x) malloc(x)
75#define SQLITE_FREE(x) free(x)
76#define SQLITE_REALLOC(x,y) realloc((x),(y))
drhf1c57262011-12-08 20:41:33 +000077
mistachkin015a3042013-11-12 21:37:04 +000078/*
79** The malloc.h header file is needed for malloc_usable_size() function
80** on some systems (e.g. Linux).
81*/
drh0ede9eb2015-01-10 16:49:23 +000082#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
83# define SQLITE_USE_MALLOC_H 1
84# define SQLITE_USE_MALLOC_USABLE_SIZE 1
mistachkin015a3042013-11-12 21:37:04 +000085/*
86** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
87** use of _msize() is automatic, but can be disabled by compiling with
88** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
89** the malloc.h header file.
90*/
91#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
92# define SQLITE_USE_MALLOC_H
93# define SQLITE_USE_MSIZE
drh86c5a932012-02-08 12:13:28 +000094#endif
mistachkin015a3042013-11-12 21:37:04 +000095
96/*
97** Include the malloc.h header file, if necessary. Also set define macro
98** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
99** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
100** The memory size function can always be overridden manually by defining
101** the macro SQLITE_MALLOCSIZE to the desired function name.
102*/
103#if defined(SQLITE_USE_MALLOC_H)
104# include <malloc.h>
105# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
106# if !defined(SQLITE_MALLOCSIZE)
107# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
108# endif
109# elif defined(SQLITE_USE_MSIZE)
110# if !defined(SQLITE_MALLOCSIZE)
111# define SQLITE_MALLOCSIZE _msize
112# endif
113# endif
114#endif /* defined(SQLITE_USE_MALLOC_H) */
drh6a8ab6d2011-11-09 01:53:25 +0000115
drhf1c57262011-12-08 20:41:33 +0000116#endif /* __APPLE__ or not __APPLE__ */
117
drh90f6a5b2007-08-15 13:04:54 +0000118/*
drhfec00ea2008-06-14 16:56:21 +0000119** Like malloc(), but remember the size of the allocation
120** so that we can find it later using sqlite3MemSize().
121**
122** For this low-level routine, we are guaranteed that nByte>0 because
123** cases of nByte<=0 will be intercepted and dealt with by higher level
124** routines.
drh90f6a5b2007-08-15 13:04:54 +0000125*/
drhfec00ea2008-06-14 16:56:21 +0000126static void *sqlite3MemMalloc(int nByte){
drhf1c57262011-12-08 20:41:33 +0000127#ifdef SQLITE_MALLOCSIZE
128 void *p = SQLITE_MALLOC( nByte );
drh6a8ab6d2011-11-09 01:53:25 +0000129 if( p==0 ){
130 testcase( sqlite3GlobalConfig.xLog!=0 );
131 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
132 }
133 return p;
134#else
drh153c62c2007-08-24 03:51:33 +0000135 sqlite3_int64 *p;
drhfec00ea2008-06-14 16:56:21 +0000136 assert( nByte>0 );
danielk1977bc739712009-03-23 04:33:32 +0000137 nByte = ROUND8(nByte);
drhf1c57262011-12-08 20:41:33 +0000138 p = SQLITE_MALLOC( nByte+8 );
danielk1977950292f2008-06-23 15:10:24 +0000139 if( p ){
140 p[0] = nByte;
141 p++;
drh413c3d32010-02-23 20:11:56 +0000142 }else{
drhaf46dc12010-02-24 21:44:07 +0000143 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000144 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
danielk1977950292f2008-06-23 15:10:24 +0000145 }
146 return (void *)p;
drh6a8ab6d2011-11-09 01:53:25 +0000147#endif
drhfec00ea2008-06-14 16:56:21 +0000148}
149
150/*
151** Like free() but works for allocations obtained from sqlite3MemMalloc()
152** or sqlite3MemRealloc().
153**
154** For this low-level routine, we already know that pPrior!=0 since
155** cases where pPrior==0 will have been intecepted and dealt with
156** by higher-level routines.
157*/
158static void sqlite3MemFree(void *pPrior){
drhf1c57262011-12-08 20:41:33 +0000159#ifdef SQLITE_MALLOCSIZE
160 SQLITE_FREE(pPrior);
drh6a8ab6d2011-11-09 01:53:25 +0000161#else
drhfec00ea2008-06-14 16:56:21 +0000162 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
danielk1977834a5aa2008-06-23 14:40:18 +0000163 assert( pPrior!=0 );
drh90f6a5b2007-08-15 13:04:54 +0000164 p--;
drhf1c57262011-12-08 20:41:33 +0000165 SQLITE_FREE(p);
drh6a8ab6d2011-11-09 01:53:25 +0000166#endif
drh90f6a5b2007-08-15 13:04:54 +0000167}
168
169/*
drh413c3d32010-02-23 20:11:56 +0000170** Report the allocated size of a prior return from xMalloc()
171** or xRealloc().
172*/
173static int sqlite3MemSize(void *pPrior){
drhf1c57262011-12-08 20:41:33 +0000174#ifdef SQLITE_MALLOCSIZE
175 return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
drh6a8ab6d2011-11-09 01:53:25 +0000176#else
drh413c3d32010-02-23 20:11:56 +0000177 sqlite3_int64 *p;
178 if( pPrior==0 ) return 0;
179 p = (sqlite3_int64*)pPrior;
180 p--;
181 return (int)p[0];
drh6a8ab6d2011-11-09 01:53:25 +0000182#endif
drh413c3d32010-02-23 20:11:56 +0000183}
184
185/*
drhfec00ea2008-06-14 16:56:21 +0000186** Like realloc(). Resize an allocation previously obtained from
187** sqlite3MemMalloc().
188**
189** For this low-level interface, we know that pPrior!=0. Cases where
190** pPrior==0 while have been intercepted by higher-level routine and
peter.d.reid60ec9142014-09-06 16:39:46 +0000191** redirected to xMalloc. Similarly, we know that nByte>0 because
drhfec00ea2008-06-14 16:56:21 +0000192** cases where nByte<=0 will have been intercepted by higher-level
193** routines and redirected to xFree.
danielk1977a7a8e142008-02-13 18:25:27 +0000194*/
drhfec00ea2008-06-14 16:56:21 +0000195static void *sqlite3MemRealloc(void *pPrior, int nByte){
drhf1c57262011-12-08 20:41:33 +0000196#ifdef SQLITE_MALLOCSIZE
197 void *p = SQLITE_REALLOC(pPrior, nByte);
drh6a8ab6d2011-11-09 01:53:25 +0000198 if( p==0 ){
199 testcase( sqlite3GlobalConfig.xLog!=0 );
200 sqlite3_log(SQLITE_NOMEM,
201 "failed memory resize %u to %u bytes",
drhf1c57262011-12-08 20:41:33 +0000202 SQLITE_MALLOCSIZE(pPrior), nByte);
drh6a8ab6d2011-11-09 01:53:25 +0000203 }
204 return p;
205#else
drhfec00ea2008-06-14 16:56:21 +0000206 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
207 assert( pPrior!=0 && nByte>0 );
drh9f129f42010-08-31 15:27:32 +0000208 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
drhfec00ea2008-06-14 16:56:21 +0000209 p--;
drhf1c57262011-12-08 20:41:33 +0000210 p = SQLITE_REALLOC(p, nByte+8 );
drhfec00ea2008-06-14 16:56:21 +0000211 if( p ){
212 p[0] = nByte;
213 p++;
drh413c3d32010-02-23 20:11:56 +0000214 }else{
drhaf46dc12010-02-24 21:44:07 +0000215 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000216 sqlite3_log(SQLITE_NOMEM,
217 "failed memory resize %u to %u bytes",
218 sqlite3MemSize(pPrior), nByte);
drhfec00ea2008-06-14 16:56:21 +0000219 }
220 return (void*)p;
drh6a8ab6d2011-11-09 01:53:25 +0000221#endif
danielk1977a7a8e142008-02-13 18:25:27 +0000222}
223
224/*
drhfec00ea2008-06-14 16:56:21 +0000225** Round up a request size to the next valid allocation size.
226*/
227static int sqlite3MemRoundup(int n){
danielk1977bc739712009-03-23 04:33:32 +0000228 return ROUND8(n);
drhfec00ea2008-06-14 16:56:21 +0000229}
230
231/*
232** Initialize this module.
233*/
234static int sqlite3MemInit(void *NotUsed){
drhc710ccb2012-01-18 12:46:47 +0000235#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
drhf1c57262011-12-08 20:41:33 +0000236 int cpuCount;
237 size_t len;
238 if( _sqliteZone_ ){
239 return SQLITE_OK;
240 }
241 len = sizeof(cpuCount);
242 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
243 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
244 if( cpuCount>1 ){
245 /* defer MT decisions to system malloc */
246 _sqliteZone_ = malloc_default_zone();
247 }else{
248 /* only 1 core, use our own zone to contention over global locks,
249 ** e.g. we have our own dedicated locks */
drhf2f105d2012-08-20 15:53:54 +0000250 bool success;
drhf1c57262011-12-08 20:41:33 +0000251 malloc_zone_t* newzone = malloc_create_zone(4096, 0);
252 malloc_set_zone_name(newzone, "Sqlite_Heap");
253 do{
254 success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
255 (void * volatile *)&_sqliteZone_);
256 }while(!_sqliteZone_);
drhf2f105d2012-08-20 15:53:54 +0000257 if( !success ){
drhf1c57262011-12-08 20:41:33 +0000258 /* somebody registered a zone first */
259 malloc_destroy_zone(newzone);
260 }
261 }
262#endif
danielk197762c14b32008-11-19 09:05:26 +0000263 UNUSED_PARAMETER(NotUsed);
drh40257ff2008-06-13 18:24:27 +0000264 return SQLITE_OK;
265}
266
267/*
drhfec00ea2008-06-14 16:56:21 +0000268** Deinitialize this module.
drh90f6a5b2007-08-15 13:04:54 +0000269*/
drhfec00ea2008-06-14 16:56:21 +0000270static void sqlite3MemShutdown(void *NotUsed){
danielk197762c14b32008-11-19 09:05:26 +0000271 UNUSED_PARAMETER(NotUsed);
drhfec00ea2008-06-14 16:56:21 +0000272 return;
273}
274
drhd1370b62008-10-28 18:58:20 +0000275/*
276** This routine is the only routine in this file with external linkage.
277**
278** Populate the low-level memory allocation function pointers in
279** sqlite3GlobalConfig.m with pointers to the routines in this file.
280*/
281void sqlite3MemSetDefault(void){
drhfec00ea2008-06-14 16:56:21 +0000282 static const sqlite3_mem_methods defaultMethods = {
283 sqlite3MemMalloc,
284 sqlite3MemFree,
285 sqlite3MemRealloc,
286 sqlite3MemSize,
287 sqlite3MemRoundup,
288 sqlite3MemInit,
289 sqlite3MemShutdown,
290 0
291 };
drhd1370b62008-10-28 18:58:20 +0000292 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
drh90f6a5b2007-08-15 13:04:54 +0000293}
drh4c3645c2007-08-15 17:07:57 +0000294
drh0d180202008-02-14 23:26:56 +0000295#endif /* SQLITE_SYSTEM_MALLOC */