Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: third_party/sqlite/sqlite-src-3170000/src/mem1.c

Issue 2747283002: [sql] Import reference version of SQLite 3.17.. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
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 *************************************************************************
12 **
13 ** 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
18 ** 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
33 ** option to rename it.
34 **
35 ** 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.
43 */
44 #include "sqliteInt.h"
45
46 /*
47 ** 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 */
51 #ifdef SQLITE_SYSTEM_MALLOC
52 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
53
54 /*
55 ** Use the zone allocator available on apple products unless the
56 ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
57 */
58 #include <sys/sysctl.h>
59 #include <malloc/malloc.h>
60 #include <libkern/OSAtomic.h>
61 static 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 /*
71 ** Use standard C library malloc and free on non-Apple systems.
72 ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
73 */
74 #define SQLITE_MALLOC(x) malloc(x)
75 #define SQLITE_FREE(x) free(x)
76 #define SQLITE_REALLOC(x,y) realloc((x),(y))
77
78 /*
79 ** The malloc.h header file is needed for malloc_usable_size() function
80 ** on some systems (e.g. Linux).
81 */
82 #if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
83 # define SQLITE_USE_MALLOC_H 1
84 # define SQLITE_USE_MALLOC_USABLE_SIZE 1
85 /*
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
94 #endif
95
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) */
115
116 #endif /* __APPLE__ or not __APPLE__ */
117
118 /*
119 ** 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.
125 */
126 static void *sqlite3MemMalloc(int nByte){
127 #ifdef SQLITE_MALLOCSIZE
128 void *p;
129 testcase( ROUND8(nByte)==nByte );
130 p = SQLITE_MALLOC( nByte );
131 if( p==0 ){
132 testcase( sqlite3GlobalConfig.xLog!=0 );
133 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
134 }
135 return p;
136 #else
137 sqlite3_int64 *p;
138 assert( nByte>0 );
139 testcase( ROUND8(nByte)!=nByte );
140 p = SQLITE_MALLOC( nByte+8 );
141 if( p ){
142 p[0] = nByte;
143 p++;
144 }else{
145 testcase( sqlite3GlobalConfig.xLog!=0 );
146 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
147 }
148 return (void *)p;
149 #endif
150 }
151
152 /*
153 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
154 ** or sqlite3MemRealloc().
155 **
156 ** For this low-level routine, we already know that pPrior!=0 since
157 ** cases where pPrior==0 will have been intecepted and dealt with
158 ** by higher-level routines.
159 */
160 static void sqlite3MemFree(void *pPrior){
161 #ifdef SQLITE_MALLOCSIZE
162 SQLITE_FREE(pPrior);
163 #else
164 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
165 assert( pPrior!=0 );
166 p--;
167 SQLITE_FREE(p);
168 #endif
169 }
170
171 /*
172 ** Report the allocated size of a prior return from xMalloc()
173 ** or xRealloc().
174 */
175 static int sqlite3MemSize(void *pPrior){
176 #ifdef SQLITE_MALLOCSIZE
177 assert( pPrior!=0 );
178 return (int)SQLITE_MALLOCSIZE(pPrior);
179 #else
180 sqlite3_int64 *p;
181 assert( pPrior!=0 );
182 p = (sqlite3_int64*)pPrior;
183 p--;
184 return (int)p[0];
185 #endif
186 }
187
188 /*
189 ** Like realloc(). Resize an allocation previously obtained from
190 ** sqlite3MemMalloc().
191 **
192 ** For this low-level interface, we know that pPrior!=0. Cases where
193 ** pPrior==0 while have been intercepted by higher-level routine and
194 ** redirected to xMalloc. Similarly, we know that nByte>0 because
195 ** cases where nByte<=0 will have been intercepted by higher-level
196 ** routines and redirected to xFree.
197 */
198 static void *sqlite3MemRealloc(void *pPrior, int nByte){
199 #ifdef SQLITE_MALLOCSIZE
200 void *p = SQLITE_REALLOC(pPrior, nByte);
201 if( p==0 ){
202 testcase( sqlite3GlobalConfig.xLog!=0 );
203 sqlite3_log(SQLITE_NOMEM,
204 "failed memory resize %u to %u bytes",
205 SQLITE_MALLOCSIZE(pPrior), nByte);
206 }
207 return p;
208 #else
209 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
210 assert( pPrior!=0 && nByte>0 );
211 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
212 p--;
213 p = SQLITE_REALLOC(p, nByte+8 );
214 if( p ){
215 p[0] = nByte;
216 p++;
217 }else{
218 testcase( sqlite3GlobalConfig.xLog!=0 );
219 sqlite3_log(SQLITE_NOMEM,
220 "failed memory resize %u to %u bytes",
221 sqlite3MemSize(pPrior), nByte);
222 }
223 return (void*)p;
224 #endif
225 }
226
227 /*
228 ** Round up a request size to the next valid allocation size.
229 */
230 static int sqlite3MemRoundup(int n){
231 return ROUND8(n);
232 }
233
234 /*
235 ** Initialize this module.
236 */
237 static int sqlite3MemInit(void *NotUsed){
238 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
239 int cpuCount;
240 size_t len;
241 if( _sqliteZone_ ){
242 return SQLITE_OK;
243 }
244 len = sizeof(cpuCount);
245 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
246 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
247 if( cpuCount>1 ){
248 /* defer MT decisions to system malloc */
249 _sqliteZone_ = malloc_default_zone();
250 }else{
251 /* only 1 core, use our own zone to contention over global locks,
252 ** e.g. we have our own dedicated locks */
253 bool success;
254 malloc_zone_t* newzone = malloc_create_zone(4096, 0);
255 malloc_set_zone_name(newzone, "Sqlite_Heap");
256 do{
257 success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
258 (void * volatile *)&_sqliteZone_);
259 }while(!_sqliteZone_);
260 if( !success ){
261 /* somebody registered a zone first */
262 malloc_destroy_zone(newzone);
263 }
264 }
265 #endif
266 UNUSED_PARAMETER(NotUsed);
267 return SQLITE_OK;
268 }
269
270 /*
271 ** Deinitialize this module.
272 */
273 static void sqlite3MemShutdown(void *NotUsed){
274 UNUSED_PARAMETER(NotUsed);
275 return;
276 }
277
278 /*
279 ** This routine is the only routine in this file with external linkage.
280 **
281 ** Populate the low-level memory allocation function pointers in
282 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
283 */
284 void sqlite3MemSetDefault(void){
285 static const sqlite3_mem_methods defaultMethods = {
286 sqlite3MemMalloc,
287 sqlite3MemFree,
288 sqlite3MemRealloc,
289 sqlite3MemSize,
290 sqlite3MemRoundup,
291 sqlite3MemInit,
292 sqlite3MemShutdown,
293 0
294 };
295 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
296 }
297
298 #endif /* SQLITE_SYSTEM_MALLOC */
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3170000/src/mem0.c ('k') | third_party/sqlite/sqlite-src-3170000/src/mem2.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698