0000: 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
0010: 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 14.**.** The a
0020: 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
0030: 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
0040: 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
0050: 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
0060: 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
0070: 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
0080: 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
0090: 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
00a0: 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
00b0: 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
00c0: 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
00d0: 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
00e0: 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
00f0: 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
0100: 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
0110: 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
0120: 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
0180: 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 ile contains low
0190: 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
01a0: 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 location drivers
01b0: 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c for when.** SQL
01c0: 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 ite will use the
01d0: 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 standard C-libr
01e0: 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c ary malloc/reall
01f0: 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63 oc/free interfac
0200: 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 e.** to obtain t
0210: 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 he memory it nee
0220: 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ds..**.** This f
0230: 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 ile contains imp
0240: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 lementations of
0250: 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 the low-level me
0260: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a mory allocation.
0270: 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 63 ** routines spec
0280: 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 6c ified in the sql
0290: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
02a0: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 63 6f object. The co
02b0: 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 74 68 69 73 ntent of.** this
02c0: 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 file is only us
02d0: 65 64 20 69 66 20 53 51 4c 49 54 45 5f 53 59 53 ed if SQLITE_SYS
02e0: 54 45 4d 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65 TEM_MALLOC is de
02f0: 66 69 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 fined. The.** S
0300: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
0310: 4c 4f 43 20 6d 61 63 72 6f 20 69 73 20 64 65 66 LOC macro is def
0320: 69 6e 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c ined automatical
0330: 6c 79 20 69 66 20 6e 65 69 74 68 65 72 20 74 68 ly if neither th
0340: 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 e.** SQLITE_MEMD
0350: 45 42 55 47 20 6e 6f 72 20 74 68 65 20 53 51 4c EBUG nor the SQL
0360: 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43 ITE_WIN32_MALLOC
0370: 20 6d 61 63 72 6f 73 20 61 72 65 20 64 65 66 69 macros are defi
0380: 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 64 65 66 ned. The.** def
0390: 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 ault configurati
03a0: 6f 6e 20 69 73 20 74 6f 20 75 73 65 20 6d 65 6d on is to use mem
03b0: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
03c0: 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 0a outines in this.
03d0: 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 43 ** file..**.** C
03e0: 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 -preprocessor ma
03f0: 63 72 6f 20 73 75 6d 6d 61 72 79 3a 0a 2a 2a 0a cro summary:.**.
0400: 2a 2a 20 20 20 20 48 41 56 45 5f 4d 41 4c 4c 4f ** HAVE_MALLO
0410: 43 5f 55 53 41 42 4c 45 5f 53 49 5a 45 20 20 20 C_USABLE_SIZE
0420: 20 20 54 68 65 20 63 6f 6e 66 69 67 75 72 65 20 The configure
0430: 73 63 72 69 70 74 20 73 65 74 73 20 74 68 69 73 script sets this
0440: 20 73 79 6d 62 6f 6c 20 69 66 0a 2a 2a 20 20 20 symbol if.**
0450: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0460: 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
0470: 20 6d 61 6c 6c 6f 63 5f 75 73 61 62 6c 65 5f 73 malloc_usable_s
0480: 69 7a 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 ize() interface
0490: 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 20 20 exists.**
04a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
04b0: 20 20 20 20 20 20 20 20 20 6f 6e 20 74 68 65 20 on the
04c0: 74 61 72 67 65 74 20 70 6c 61 74 66 6f 72 6d 2e target platform.
04d0: 20 20 4f 72 2c 20 74 68 69 73 20 73 79 6d 62 6f Or, this symbo
04e0: 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 l.**
04f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0500: 20 20 20 20 63 61 6e 20 62 65 20 73 65 74 20 6d can be set m
0510: 61 6e 75 61 6c 6c 79 2c 20 69 66 20 64 65 73 69 anually, if desi
0520: 72 65 64 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 red..**
0530: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0540: 20 20 20 20 20 20 20 49 66 20 61 6e 20 65 71 75 If an equ
0550: 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 66 61 63 ivalent interfac
0560: 65 20 65 78 69 73 74 73 20 62 79 0a 2a 2a 20 20 e exists by.**
0570: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0580: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 20 a
0590: 64 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 2c 20 different name,
05a0: 75 73 69 6e 67 20 61 20 73 65 70 61 72 61 74 65 using a separate
05b0: 20 2d 44 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 -D.**
05c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
05d0: 20 20 20 20 20 20 6f 70 74 69 6f 6e 20 74 6f 20 option to
05e0: 72 65 6e 61 6d 65 20 69 74 2e 0a 2a 2a 0a 2a 2a rename it..**.**
05f0: 20 20 20 20 53 51 4c 49 54 45 5f 57 49 54 48 4f SQLITE_WITHO
0600: 55 54 5f 5a 4f 4e 45 4d 41 4c 4c 4f 43 20 20 20 UT_ZONEMALLOC
0610: 53 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 63 73 20 Some older macs
0620: 6c 61 63 6b 20 73 75 70 70 6f 72 74 20 66 6f 72 lack support for
0630: 20 74 68 65 20 7a 6f 6e 65 0a 2a 2a 20 20 20 20 the zone.**
0640: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0650: 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f memo
0660: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 53 ry allocator. S
0670: 65 74 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 74 et this symbol t
0680: 6f 20 65 6e 61 62 6c 65 0a 2a 2a 20 20 20 20 20 o enable.**
0690: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
06a0: 20 20 20 20 20 20 20 20 20 20 20 62 75 69 6c 64 build
06b0: 69 6e 67 20 6f 6e 20 6f 6c 64 65 72 20 6d 61 63 ing on older mac
06c0: 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 51 4c 49 s..**.** SQLI
06d0: 54 45 5f 57 49 54 48 4f 55 54 5f 4d 53 49 5a 45 TE_WITHOUT_MSIZE
06e0: 20 20 20 20 20 20 20 20 53 65 74 20 74 68 69 73 Set this
06f0: 20 73 79 6d 62 6f 6c 20 74 6f 20 64 69 73 61 62 symbol to disab
0700: 6c 65 20 74 68 65 20 75 73 65 20 6f 66 0a 2a 2a le the use of.**
0710: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0720: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0730: 5f 6d 73 69 7a 65 28 29 20 6f 6e 20 77 69 6e 64 _msize() on wind
0740: 6f 77 73 20 73 79 73 74 65 6d 73 2e 20 20 54 68 ows systems. Th
0750: 69 73 20 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20 is might.**
0760: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0770: 20 20 20 20 20 20 20 20 20 20 20 62 65 20 6e 65 be ne
0780: 63 65 73 73 61 72 79 20 77 68 65 6e 20 63 6f 6d cessary when com
0790: 70 69 6c 69 6e 67 20 66 6f 72 20 44 65 6c 70 68 piling for Delph
07a0: 69 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 i,.**
07b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
07c0: 20 20 20 20 20 66 6f 72 20 65 78 61 6d 70 6c 65 for example
07d0: 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 73 ..*/.#include "s
07e0: 71 6c 69 74 65 49 6e 74 2e 68 22 0a 0a 2f 2a 0a qliteInt.h"../*.
07f0: 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
0800: 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c of the memory al
0810: 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 locator is the d
0820: 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a efault. It is.*
0830: 2a 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f * used when no o
0840: 74 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ther memory allo
0850: 63 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 cator is specifi
0860: 65 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 ed using compile
0870: 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e -time.** macros.
0880: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
0890: 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a E_SYSTEM_MALLOC.
08a0: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 #if defined(__AP
08b0: 50 4c 45 5f 5f 29 20 26 26 20 21 64 65 66 69 6e PLE__) && !defin
08c0: 65 64 28 53 51 4c 49 54 45 5f 57 49 54 48 4f 55 ed(SQLITE_WITHOU
08d0: 54 5f 5a 4f 4e 45 4d 41 4c 4c 4f 43 29 0a 0a 2f T_ZONEMALLOC)../
08e0: 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 7a 6f 6e *.** Use the zon
08f0: 65 20 61 6c 6c 6f 63 61 74 6f 72 20 61 76 61 69 e allocator avai
0900: 6c 61 62 6c 65 20 6f 6e 20 61 70 70 6c 65 20 70 lable on apple p
0910: 72 6f 64 75 63 74 73 20 75 6e 6c 65 73 73 20 74 roducts unless t
0920: 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 57 49 54 he.** SQLITE_WIT
0930: 48 4f 55 54 5f 5a 4f 4e 45 4d 41 4c 4c 4f 43 20 HOUT_ZONEMALLOC
0940: 73 79 6d 62 6f 6c 20 69 73 20 64 65 66 69 6e 65 symbol is define
0950: 64 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c d..*/.#include <
0960: 73 79 73 2f 73 79 73 63 74 6c 2e 68 3e 0a 23 69 sys/sysctl.h>.#i
0970: 6e 63 6c 75 64 65 20 3c 6d 61 6c 6c 6f 63 2f 6d nclude <malloc/m
0980: 61 6c 6c 6f 63 2e 68 3e 0a 23 69 66 64 65 66 20 alloc.h>.#ifdef
0990: 53 51 4c 49 54 45 5f 4d 49 47 48 54 5f 42 45 5f SQLITE_MIGHT_BE_
09a0: 53 49 4e 47 4c 45 5f 43 4f 52 45 0a 23 69 6e 63 SINGLE_CORE.#inc
09b0: 6c 75 64 65 20 3c 6c 69 62 6b 65 72 6e 2f 4f 53 lude <libkern/OS
09c0: 41 74 6f 6d 69 63 2e 68 3e 0a 23 65 6e 64 69 66 Atomic.h>.#endif
09d0: 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 49 47 48 54 /* SQLITE_MIGHT
09e0: 5f 42 45 5f 53 49 4e 47 4c 45 5f 43 4f 52 45 20 _BE_SINGLE_CORE
09f0: 2a 2f 0a 73 74 61 74 69 63 20 6d 61 6c 6c 6f 63 */.static malloc
0a00: 5f 7a 6f 6e 65 5f 74 2a 20 5f 73 71 6c 69 74 65 _zone_t* _sqlite
0a10: 5a 6f 6e 65 5f 3b 0a 23 64 65 66 69 6e 65 20 53 Zone_;.#define S
0a20: 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 28 78 29 20 QLITE_MALLOC(x)
0a30: 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 6d 61 6c 6c malloc_zone_mall
0a40: 6f 63 28 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 2c oc(_sqliteZone_,
0a50: 20 28 78 29 29 0a 23 64 65 66 69 6e 65 20 53 51 (x)).#define SQ
0a60: 4c 49 54 45 5f 46 52 45 45 28 78 29 20 6d 61 6c LITE_FREE(x) mal
0a70: 6c 6f 63 5f 7a 6f 6e 65 5f 66 72 65 65 28 5f 73 loc_zone_free(_s
0a80: 71 6c 69 74 65 5a 6f 6e 65 5f 2c 20 28 78 29 29 qliteZone_, (x))
0a90: 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ;.#define SQLITE
0aa0: 5f 52 45 41 4c 4c 4f 43 28 78 2c 79 29 20 6d 61 _REALLOC(x,y) ma
0ab0: 6c 6c 6f 63 5f 7a 6f 6e 65 5f 72 65 61 6c 6c 6f lloc_zone_reallo
0ac0: 63 28 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 2c 20 c(_sqliteZone_,
0ad0: 28 78 29 2c 20 28 79 29 29 0a 23 64 65 66 69 6e (x), (y)).#defin
0ae0: 65 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 e SQLITE_MALLOCS
0af0: 49 5a 45 28 78 29 20 5c 0a 20 20 20 20 20 20 20 IZE(x) \.
0b00: 20 28 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 20 3f (_sqliteZone_ ?
0b10: 20 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 2d 3e 73 _sqliteZone_->s
0b20: 69 7a 65 28 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f ize(_sqliteZone_
0b30: 2c 78 29 20 3a 20 6d 61 6c 6c 6f 63 5f 73 69 7a ,x) : malloc_siz
0b40: 65 28 78 29 29 0a 0a 23 65 6c 73 65 20 2f 2a 20 e(x))..#else /*
0b50: 69 66 20 6e 6f 74 20 5f 5f 41 50 50 4c 45 5f 5f if not __APPLE__
0b60: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 20 73 */../*.** Use s
0b70: 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 tandard C librar
0b80: 79 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 66 72 65 y malloc and fre
0b90: 65 20 6f 6e 20 6e 6f 6e 2d 41 70 70 6c 65 20 73 e on non-Apple s
0ba0: 79 73 74 65 6d 73 2e 20 20 0a 2a 2a 20 41 6c 73 ystems. .** Als
0bb0: 6f 20 75 73 65 64 20 62 79 20 41 70 70 6c 65 20 o used by Apple
0bc0: 73 79 73 74 65 6d 73 20 69 66 20 53 51 4c 49 54 systems if SQLIT
0bd0: 45 5f 57 49 54 48 4f 55 54 5f 5a 4f 4e 45 4d 41 E_WITHOUT_ZONEMA
0be0: 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 64 2e LLOC is defined.
0bf0: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
0c00: 54 45 5f 4d 41 4c 4c 4f 43 28 78 29 20 20 20 20 TE_MALLOC(x)
0c10: 20 20 20 20 20 20 20 20 20 6d 61 6c 6c 6f 63 28 malloc(
0c20: 78 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 x).#define SQLIT
0c30: 45 5f 46 52 45 45 28 78 29 20 20 20 20 20 20 20 E_FREE(x)
0c40: 20 20 20 20 20 20 20 20 66 72 65 65 28 78 29 0a free(x).
0c50: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
0c60: 45 41 4c 4c 4f 43 28 78 2c 79 29 20 20 20 20 20 EALLOC(x,y)
0c70: 20 20 20 20 20 72 65 61 6c 6c 6f 63 28 28 78 29 realloc((x)
0c80: 2c 28 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ,(y))../*.** The
0c90: 20 6d 61 6c 6c 6f 63 2e 68 20 68 65 61 64 65 72 malloc.h header
0ca0: 20 66 69 6c 65 20 69 73 20 6e 65 65 64 65 64 20 file is needed
0cb0: 66 6f 72 20 6d 61 6c 6c 6f 63 5f 75 73 61 62 6c for malloc_usabl
0cc0: 65 5f 73 69 7a 65 28 29 20 66 75 6e 63 74 69 6f e_size() functio
0cd0: 6e 0a 2a 2a 20 6f 6e 20 73 6f 6d 65 20 73 79 73 n.** on some sys
0ce0: 74 65 6d 73 20 28 65 2e 67 2e 20 4c 69 6e 75 78 tems (e.g. Linux
0cf0: 29 2e 0a 2a 2f 0a 23 69 66 20 48 41 56 45 5f 4d )..*/.#if HAVE_M
0d00: 41 4c 4c 4f 43 5f 48 20 26 26 20 48 41 56 45 5f ALLOC_H && HAVE_
0d10: 4d 41 4c 4c 4f 43 5f 55 53 41 42 4c 45 5f 53 49 MALLOC_USABLE_SI
0d20: 5a 45 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c ZE.# define SQL
0d30: 49 54 45 5f 55 53 45 5f 4d 41 4c 4c 4f 43 5f 48 ITE_USE_MALLOC_H
0d40: 20 31 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 1.# define SQL
0d50: 49 54 45 5f 55 53 45 5f 4d 41 4c 4c 4f 43 5f 55 ITE_USE_MALLOC_U
0d60: 53 41 42 4c 45 5f 53 49 5a 45 20 31 0a 2f 2a 0a SABLE_SIZE 1./*.
0d70: 2a 2a 20 54 68 65 20 4d 53 56 43 52 54 20 68 61 ** The MSVCRT ha
0d80: 73 20 6d 61 6c 6c 6f 63 5f 75 73 61 62 6c 65 5f s malloc_usable_
0d90: 73 69 7a 65 28 29 2c 20 62 75 74 20 69 74 20 69 size(), but it i
0da0: 73 20 63 61 6c 6c 65 64 20 5f 6d 73 69 7a 65 28 s called _msize(
0db0: 29 2e 20 20 54 68 65 0a 2a 2a 20 75 73 65 20 6f ). The.** use o
0dc0: 66 20 5f 6d 73 69 7a 65 28 29 20 69 73 20 61 75 f _msize() is au
0dd0: 74 6f 6d 61 74 69 63 2c 20 62 75 74 20 63 61 6e tomatic, but can
0de0: 20 62 65 20 64 69 73 61 62 6c 65 64 20 62 79 20 be disabled by
0df0: 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 0a 2a compiling with.*
0e00: 2a 20 2d 44 53 51 4c 49 54 45 5f 57 49 54 48 4f * -DSQLITE_WITHO
0e10: 55 54 5f 4d 53 49 5a 45 2e 20 20 55 73 69 6e 67 UT_MSIZE. Using
0e20: 20 74 68 65 20 5f 6d 73 69 7a 65 28 29 20 66 75 the _msize() fu
0e30: 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 72 65 71 75 nction also requ
0e40: 69 72 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c ires.** the mall
0e50: 6f 63 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 oc.h header file
0e60: 2e 0a 2a 2f 0a 23 65 6c 69 66 20 64 65 66 69 6e ..*/.#elif defin
0e70: 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 26 26 20 ed(_MSC_VER) &&
0e80: 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
0e90: 57 49 54 48 4f 55 54 5f 4d 53 49 5a 45 29 0a 23 WITHOUT_MSIZE).#
0ea0: 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
0eb0: 55 53 45 5f 4d 41 4c 4c 4f 43 5f 48 0a 23 20 20 USE_MALLOC_H.#
0ec0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 53 define SQLITE_US
0ed0: 45 5f 4d 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a E_MSIZE.#endif..
0ee0: 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 /*.** Include th
0ef0: 65 20 6d 61 6c 6c 6f 63 2e 68 20 68 65 61 64 65 e malloc.h heade
0f00: 72 20 66 69 6c 65 2c 20 69 66 20 6e 65 63 65 73 r file, if neces
0f10: 73 61 72 79 2e 20 20 41 6c 73 6f 20 73 65 74 20 sary. Also set
0f20: 64 65 66 69 6e 65 20 6d 61 63 72 6f 0a 2a 2a 20 define macro.**
0f30: 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a SQLITE_MALLOCSIZ
0f40: 45 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 E to the appropr
0f50: 69 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 iate function na
0f60: 6d 65 2c 20 77 68 69 63 68 20 69 73 20 5f 6d 73 me, which is _ms
0f70: 69 7a 65 28 29 0a 2a 2a 20 66 6f 72 20 4d 53 56 ize().** for MSV
0f80: 43 20 61 6e 64 20 6d 61 6c 6c 6f 63 5f 75 73 61 C and malloc_usa
0f90: 62 6c 65 5f 73 69 7a 65 28 29 20 66 6f 72 20 6d ble_size() for m
0fa0: 6f 73 74 20 6f 74 68 65 72 20 73 79 73 74 65 6d ost other system
0fb0: 73 20 28 65 2e 67 2e 20 4c 69 6e 75 78 29 2e 0a s (e.g. Linux)..
0fc0: 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 73 69 ** The memory si
0fd0: 7a 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 ze function can
0fe0: 61 6c 77 61 79 73 20 62 65 20 6f 76 65 72 72 69 always be overri
0ff0: 64 64 65 6e 20 6d 61 6e 75 61 6c 6c 79 20 62 79 dden manually by
1000: 20 64 65 66 69 6e 69 6e 67 0a 2a 2a 20 74 68 65 defining.** the
1010: 20 6d 61 63 72 6f 20 53 51 4c 49 54 45 5f 4d 41 macro SQLITE_MA
1020: 4c 4c 4f 43 53 49 5a 45 20 74 6f 20 74 68 65 20 LLOCSIZE to the
1030: 64 65 73 69 72 65 64 20 66 75 6e 63 74 69 6f 6e desired function
1040: 20 6e 61 6d 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 name..*/.#if de
1050: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 55 53 45 fined(SQLITE_USE
1060: 5f 4d 41 4c 4c 4f 43 5f 48 29 0a 23 20 20 69 6e _MALLOC_H).# in
1070: 63 6c 75 64 65 20 3c 6d 61 6c 6c 6f 63 2e 68 3e clude <malloc.h>
1080: 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28 53 .# if defined(S
1090: 51 4c 49 54 45 5f 55 53 45 5f 4d 41 4c 4c 4f 43 QLITE_USE_MALLOC
10a0: 5f 55 53 41 42 4c 45 5f 53 49 5a 45 29 0a 23 20 _USABLE_SIZE).#
10b0: 20 20 20 69 66 20 21 64 65 66 69 6e 65 64 28 53 if !defined(S
10c0: 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 QLITE_MALLOCSIZE
10d0: 29 0a 23 20 20 20 20 20 20 64 65 66 69 6e 65 20 ).# define
10e0: 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a SQLITE_MALLOCSIZ
10f0: 45 28 78 29 20 20 20 6d 61 6c 6c 6f 63 5f 75 73 E(x) malloc_us
1100: 61 62 6c 65 5f 73 69 7a 65 28 78 29 0a 23 20 20 able_size(x).#
1110: 20 20 65 6e 64 69 66 0a 23 20 20 65 6c 69 66 20 endif.# elif
1120: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 55 defined(SQLITE_U
1130: 53 45 5f 4d 53 49 5a 45 29 0a 23 20 20 20 20 69 SE_MSIZE).# i
1140: 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
1150: 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 29 0a 23 20 E_MALLOCSIZE).#
1160: 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
1170: 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 20 20 20 TE_MALLOCSIZE
1180: 20 20 20 5f 6d 73 69 7a 65 0a 23 20 20 20 20 65 _msize.# e
1190: 6e 64 69 66 0a 23 20 20 65 6e 64 69 66 0a 23 65 ndif.# endif.#e
11a0: 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 ndif /* defined(
11b0: 53 51 4c 49 54 45 5f 55 53 45 5f 4d 41 4c 4c 4f SQLITE_USE_MALLO
11c0: 43 5f 48 29 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 C_H) */..#endif
11d0: 2f 2a 20 5f 5f 41 50 50 4c 45 5f 5f 20 6f 72 20 /* __APPLE__ or
11e0: 6e 6f 74 20 5f 5f 41 50 50 4c 45 5f 5f 20 2a 2f not __APPLE__ */
11f0: 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c ../*.** Like mal
1200: 6c 6f 63 28 29 2c 20 62 75 74 20 72 65 6d 65 6d loc(), but remem
1210: 62 65 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ber the size of
1220: 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a the allocation.*
1230: 2a 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e * so that we can
1240: 20 66 69 6e 64 20 69 74 20 6c 61 74 65 72 20 75 find it later u
1250: 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 sing sqlite3MemS
1260: 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 ize()..**.** For
1270: 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 this low-level
1280: 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 72 65 20 routine, we are
1290: 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 guaranteed that
12a0: 6e 42 79 74 65 3e 30 20 62 65 63 61 75 73 65 0a nByte>0 because.
12b0: 2a 2a 20 63 61 73 65 73 20 6f 66 20 6e 42 79 74 ** cases of nByt
12c0: 65 3c 3d 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 e<=0 will be int
12d0: 65 72 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 ercepted and dea
12e0: 6c 74 20 77 69 74 68 20 62 79 20 68 69 67 68 65 lt with by highe
12f0: 72 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 r level.** routi
1300: 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 nes..*/.static v
1310: 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d oid *sqlite3MemM
1320: 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 alloc(int nByte)
1330: 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
1340: 4d 41 4c 4c 4f 43 53 49 5a 45 0a 20 20 76 6f 69 MALLOCSIZE. voi
1350: 64 20 2a 70 3b 0a 20 20 74 65 73 74 63 61 73 65 d *p;. testcase
1360: 28 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3d ( ROUND8(nByte)=
1370: 3d 6e 42 79 74 65 20 29 3b 0a 20 20 70 20 3d 20 =nByte );. p =
1380: 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 28 20 6e SQLITE_MALLOC( n
1390: 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 70 3d Byte );. if( p=
13a0: 3d 30 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 =0 ){. testca
13b0: 73 65 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 se( sqlite3Globa
13c0: 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d 30 20 lConfig.xLog!=0
13d0: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c );. sqlite3_l
13e0: 6f 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c og(SQLITE_NOMEM,
13f0: 20 22 66 61 69 6c 65 64 20 74 6f 20 61 6c 6c 6f "failed to allo
1400: 63 61 74 65 20 25 75 20 62 79 74 65 73 20 6f 66 cate %u bytes of
1410: 20 6d 65 6d 6f 72 79 22 2c 20 6e 42 79 74 65 29 memory", nByte)
1420: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
1430: 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 ;.#else. sqlite
1440: 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 3_int64 *p;. as
1450: 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b sert( nByte>0 );
1460: 0a 20 20 74 65 73 74 63 61 73 65 28 20 52 4f 55 . testcase( ROU
1470: 4e 44 38 28 6e 42 79 74 65 29 21 3d 6e 42 79 74 ND8(nByte)!=nByt
1480: 65 20 29 3b 0a 20 20 70 20 3d 20 53 51 4c 49 54 e );. p = SQLIT
1490: 45 5f 4d 41 4c 4c 4f 43 28 20 6e 42 79 74 65 2b E_MALLOC( nByte+
14a0: 38 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 8 );. if( p ){.
14b0: 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 p[0] = nByte
14c0: 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 65 6c ;. p++;. }el
14d0: 73 65 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 se{. testcase
14e0: 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
14f0: 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29 3b onfig.xLog!=0 );
1500: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 . sqlite3_log
1510: 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 (SQLITE_NOMEM, "
1520: 66 61 69 6c 65 64 20 74 6f 20 61 6c 6c 6f 63 61 failed to alloca
1530: 74 65 20 25 75 20 62 79 74 65 73 20 6f 66 20 6d te %u bytes of m
1540: 65 6d 6f 72 79 22 2c 20 6e 42 79 74 65 29 3b 0a emory", nByte);.
1550: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f }. return (vo
1560: 69 64 20 2a 29 70 3b 0a 23 65 6e 64 69 66 0a 7d id *)p;.#endif.}
1570: 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 66 72 65 ../*.** Like fre
1580: 65 28 29 20 62 75 74 20 77 6f 72 6b 73 20 66 6f e() but works fo
1590: 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 62 r allocations ob
15a0: 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
15b0: 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 0a 2a te3MemMalloc().*
15c0: 2a 20 6f 72 20 73 71 6c 69 74 65 33 4d 65 6d 52 * or sqlite3MemR
15d0: 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 ealloc()..**.**
15e0: 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 For this low-lev
15f0: 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 el routine, we a
1600: 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 lready know that
1610: 20 70 50 72 69 6f 72 21 3d 30 20 73 69 6e 63 65 pPrior!=0 since
1620: 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 .** cases where
1630: 70 50 72 69 6f 72 3d 3d 30 20 77 69 6c 6c 20 68 pPrior==0 will h
1640: 61 76 65 20 62 65 65 6e 20 69 6e 74 65 63 65 70 ave been intecep
1650: 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69 ted and dealt wi
1660: 74 68 0a 2a 2a 20 62 79 20 68 69 67 68 65 72 2d th.** by higher-
1670: 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 2e 0a level routines..
1680: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
1690: 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f qlite3MemFree(vo
16a0: 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 23 69 66 id *pPrior){.#if
16b0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f def SQLITE_MALLO
16c0: 43 53 49 5a 45 0a 20 20 53 51 4c 49 54 45 5f 46 CSIZE. SQLITE_F
16d0: 52 45 45 28 70 50 72 69 6f 72 29 3b 0a 23 65 6c REE(pPrior);.#el
16e0: 73 65 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 se. sqlite3_int
16f0: 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 64 *p = (sqlite3
1700: 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a _int64*)pPrior;.
1710: 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 assert( pPrior
1720: 21 3d 30 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 20 !=0 );. p--;.
1730: 53 51 4c 49 54 45 5f 46 52 45 45 28 70 29 3b 0a SQLITE_FREE(p);.
1740: 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
1750: 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 Report the alloc
1760: 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 ated size of a p
1770: 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d rior return from
1780: 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 xMalloc().** or
1790: 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a xRealloc()..*/.
17a0: 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 static int sqlit
17b0: 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a e3MemSize(void *
17c0: 70 50 72 69 6f 72 29 7b 0a 23 69 66 64 65 66 20 pPrior){.#ifdef
17d0: 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a SQLITE_MALLOCSIZ
17e0: 45 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 E. assert( pPri
17f0: 6f 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 or!=0 );. retur
1800: 6e 20 28 69 6e 74 29 53 51 4c 49 54 45 5f 4d 41 n (int)SQLITE_MA
1810: 4c 4c 4f 43 53 49 5a 45 28 70 50 72 69 6f 72 29 LLOCSIZE(pPrior)
1820: 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 ;.#else. sqlite
1830: 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 3_int64 *p;. as
1840: 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 sert( pPrior!=0
1850: 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 );. p = (sqlite
1860: 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 3_int64*)pPrior;
1870: 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 74 75 72 6e . p--;. return
1880: 20 28 69 6e 74 29 70 5b 30 5d 3b 0a 23 65 6e 64 (int)p[0];.#end
1890: 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 if.}../*.** Like
18a0: 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20 52 65 73 realloc(). Res
18b0: 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f ize an allocatio
18c0: 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 n previously obt
18d0: 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 ained from.** sq
18e0: 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 lite3MemMalloc()
18f0: 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 ..**.** For this
1900: 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e 74 65 72 low-level inter
1910: 66 61 63 65 2c 20 77 65 20 6b 6e 6f 77 20 74 68 face, we know th
1920: 61 74 20 70 50 72 69 6f 72 21 3d 30 2e 20 20 43 at pPrior!=0. C
1930: 61 73 65 73 20 77 68 65 72 65 0a 2a 2a 20 70 50 ases where.** pP
1940: 72 69 6f 72 3d 3d 30 20 77 68 69 6c 65 20 68 61 rior==0 while ha
1950: 76 65 20 62 65 65 6e 20 69 6e 74 65 72 63 65 70 ve been intercep
1960: 74 65 64 20 62 79 20 68 69 67 68 65 72 2d 6c 65 ted by higher-le
1970: 76 65 6c 20 72 6f 75 74 69 6e 65 20 61 6e 64 0a vel routine and.
1980: 2a 2a 20 72 65 64 69 72 65 63 74 65 64 20 74 6f ** redirected to
1990: 20 78 4d 61 6c 6c 6f 63 2e 20 20 53 69 6d 69 6c xMalloc. Simil
19a0: 61 72 6c 79 2c 20 77 65 20 6b 6e 6f 77 20 74 68 arly, we know th
19b0: 61 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75 at nByte>0 becau
19c0: 73 65 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 se.** cases wher
19d0: 65 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 e nByte<=0 will
19e0: 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 63 have been interc
19f0: 65 70 74 65 64 20 62 79 20 68 69 67 68 65 72 2d epted by higher-
1a00: 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 level.** routine
1a10: 73 20 61 6e 64 20 72 65 64 69 72 65 63 74 65 64 s and redirected
1a20: 20 74 6f 20 78 46 72 65 65 2e 0a 2a 2f 0a 73 74 to xFree..*/.st
1a30: 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 atic void *sqlit
1a40: 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 e3MemRealloc(voi
1a50: 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e d *pPrior, int n
1a60: 42 79 74 65 29 7b 0a 23 69 66 64 65 66 20 53 51 Byte){.#ifdef SQ
1a70: 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 0a LITE_MALLOCSIZE.
1a80: 20 20 76 6f 69 64 20 2a 70 20 3d 20 53 51 4c 49 void *p = SQLI
1a90: 54 45 5f 52 45 41 4c 4c 4f 43 28 70 50 72 69 6f TE_REALLOC(pPrio
1aa0: 72 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 r, nByte);. if(
1ab0: 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 74 65 73 p==0 ){. tes
1ac0: 74 63 61 73 65 28 20 73 71 6c 69 74 65 33 47 6c tcase( sqlite3Gl
1ad0: 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 obalConfig.xLog!
1ae0: 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 =0 );. sqlite
1af0: 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 3_log(SQLITE_NOM
1b00: 45 4d 2c 0a 20 20 20 20 20 20 22 66 61 69 6c 65 EM,. "faile
1b10: 64 20 6d 65 6d 6f 72 79 20 72 65 73 69 7a 65 20 d memory resize
1b20: 25 75 20 74 6f 20 25 75 20 62 79 74 65 73 22 2c %u to %u bytes",
1b30: 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 41 . SQLITE_MA
1b40: 4c 4c 4f 43 53 49 5a 45 28 70 50 72 69 6f 72 29 LLOCSIZE(pPrior)
1b50: 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20 20 , nByte);. }.
1b60: 72 65 74 75 72 6e 20 70 3b 0a 23 65 6c 73 65 0a return p;.#else.
1b70: 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
1b80: 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e *p = (sqlite3_in
1b90: 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 t64*)pPrior;. a
1ba0: 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 ssert( pPrior!=0
1bb0: 20 26 26 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 && nByte>0 );.
1bc0: 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3d 3d assert( nByte==
1bd0: 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 20 29 3b ROUND8(nByte) );
1be0: 20 2f 2a 20 45 56 3a 20 52 2d 34 36 31 39 39 2d /* EV: R-46199-
1bf0: 33 30 32 34 39 20 2a 2f 0a 20 20 70 2d 2d 3b 0a 30249 */. p--;.
1c00: 20 20 70 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 p = SQLITE_REA
1c10: 4c 4c 4f 43 28 70 2c 20 6e 42 79 74 65 2b 38 20 LLOC(p, nByte+8
1c20: 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 );. if( p ){.
1c30: 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a p[0] = nByte;.
1c40: 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 65 6c 73 65 p++;. }else
1c50: 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 {. testcase(
1c60: 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
1c70: 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a 20 fig.xLog!=0 );.
1c80: 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 sqlite3_log(S
1c90: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 0a 20 20 20 QLITE_NOMEM,.
1ca0: 20 20 20 22 66 61 69 6c 65 64 20 6d 65 6d 6f 72 "failed memor
1cb0: 79 20 72 65 73 69 7a 65 20 25 75 20 74 6f 20 25 y resize %u to %
1cc0: 75 20 62 79 74 65 73 22 2c 0a 20 20 20 20 20 20 u bytes",.
1cd0: 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 70 sqlite3MemSize(p
1ce0: 50 72 69 6f 72 29 2c 20 6e 42 79 74 65 29 3b 0a Prior), nByte);.
1cf0: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f }. return (vo
1d00: 69 64 2a 29 70 3b 0a 23 65 6e 64 69 66 0a 7d 0a id*)p;.#endif.}.
1d10: 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 ./*.** Round up
1d20: 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 a request size t
1d30: 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 o the next valid
1d40: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
1d50: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1d60: 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 sqlite3MemRoundu
1d70: 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 p(int n){. retu
1d80: 72 6e 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a rn ROUND8(n);.}.
1d90: 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
1da0: 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a e this module..*
1db0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c /.static int sql
1dc0: 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 ite3MemInit(void
1dd0: 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 23 69 66 20 *NotUsed){.#if
1de0: 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
1df0: 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 _) && !defined(S
1e00: 51 4c 49 54 45 5f 57 49 54 48 4f 55 54 5f 5a 4f QLITE_WITHOUT_ZO
1e10: 4e 45 4d 41 4c 4c 4f 43 29 0a 20 20 69 6e 74 20 NEMALLOC). int
1e20: 63 70 75 43 6f 75 6e 74 3b 0a 20 20 73 69 7a 65 cpuCount;. size
1e30: 5f 74 20 6c 65 6e 3b 0a 20 20 69 66 28 20 5f 73 _t len;. if( _s
1e40: 71 6c 69 74 65 5a 6f 6e 65 5f 20 29 7b 0a 20 20 qliteZone_ ){.
1e50: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1e60: 4f 4b 3b 0a 20 20 7d 0a 20 20 6c 65 6e 20 3d 20 OK;. }. len =
1e70: 73 69 7a 65 6f 66 28 63 70 75 43 6f 75 6e 74 29 sizeof(cpuCount)
1e80: 3b 0a 20 20 2f 2a 20 4f 6e 65 20 75 73 75 61 6c ;. /* One usual
1e90: 6c 79 20 77 61 6e 74 73 20 74 6f 20 75 73 65 20 ly wants to use
1ea0: 68 77 2e 61 63 63 74 69 76 65 63 70 75 20 66 6f hw.acctivecpu fo
1eb0: 72 20 4d 54 20 64 65 63 69 73 69 6f 6e 73 2c 20 r MT decisions,
1ec0: 62 75 74 20 6e 6f 74 20 68 65 72 65 20 2a 2f 0a but not here */.
1ed0: 20 20 73 79 73 63 74 6c 62 79 6e 61 6d 65 28 22 sysctlbyname("
1ee0: 68 77 2e 6e 63 70 75 22 2c 20 26 63 70 75 43 6f hw.ncpu", &cpuCo
1ef0: 75 6e 74 2c 20 26 6c 65 6e 2c 20 4e 55 4c 4c 2c unt, &len, NULL,
1f00: 20 30 29 3b 0a 20 20 69 66 28 20 63 70 75 43 6f 0);. if( cpuCo
1f10: 75 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 unt>1 ){. /*
1f20: 64 65 66 65 72 20 4d 54 20 64 65 63 69 73 69 6f defer MT decisio
1f30: 6e 73 20 74 6f 20 73 79 73 74 65 6d 20 6d 61 6c ns to system mal
1f40: 6c 6f 63 20 2a 2f 0a 20 20 20 20 5f 73 71 6c 69 loc */. _sqli
1f50: 74 65 5a 6f 6e 65 5f 20 3d 20 6d 61 6c 6c 6f 63 teZone_ = malloc
1f60: 5f 64 65 66 61 75 6c 74 5f 7a 6f 6e 65 28 29 3b _default_zone();
1f70: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1f80: 20 6f 6e 6c 79 20 31 20 63 6f 72 65 2c 20 75 73 only 1 core, us
1f90: 65 20 6f 75 72 20 6f 77 6e 20 7a 6f 6e 65 20 74 e our own zone t
1fa0: 6f 20 63 6f 6e 74 65 6e 74 69 6f 6e 20 6f 76 65 o contention ove
1fb0: 72 20 67 6c 6f 62 61 6c 20 6c 6f 63 6b 73 2c 20 r global locks,
1fc0: 0a 20 20 20 20 2a 2a 20 65 2e 67 2e 20 77 65 20 . ** e.g. we
1fd0: 68 61 76 65 20 6f 75 72 20 6f 77 6e 20 64 65 64 have our own ded
1fe0: 69 63 61 74 65 64 20 6c 6f 63 6b 73 20 2a 2f 0a icated locks */.
1ff0: 20 20 20 20 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f _sqliteZone_
2000: 20 3d 20 6d 61 6c 6c 6f 63 5f 63 72 65 61 74 65 = malloc_create
2010: 5f 7a 6f 6e 65 28 34 30 39 36 2c 20 30 29 3b 0a _zone(4096, 0);.
2020: 20 20 20 20 6d 61 6c 6c 6f 63 5f 73 65 74 5f 7a malloc_set_z
2030: 6f 6e 65 5f 6e 61 6d 65 28 5f 73 71 6c 69 74 65 one_name(_sqlite
2040: 5a 6f 6e 65 5f 2c 20 22 53 71 6c 69 74 65 5f 48 Zone_, "Sqlite_H
2050: 65 61 70 22 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 eap");. }.#endi
2060: 66 20 2f 2a 20 20 64 65 66 69 6e 65 64 28 5f 5f f /* defined(__
2070: 41 50 50 4c 45 5f 5f 29 20 26 26 20 21 64 65 66 APPLE__) && !def
2080: 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 54 48 ined(SQLITE_WITH
2090: 4f 55 54 5f 5a 4f 4e 45 4d 41 4c 4c 4f 43 29 20 OUT_ZONEMALLOC)
20a0: 2a 2f 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 */. UNUSED_PARA
20b0: 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
20c0: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
20d0: 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 OK;.}../*.** Dei
20e0: 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d nitialize this m
20f0: 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 odule..*/.static
2100: 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
2110: 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e Shutdown(void *N
2120: 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 otUsed){. UNUSE
2130: 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
2140: 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a sed);. return;.
2150: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
2160: 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c utine is the onl
2170: 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 y routine in thi
2180: 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 s file with exte
2190: 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a rnal linkage..**
21a0: 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 .** Populate the
21b0: 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 low-level memor
21c0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e y allocation fun
21d0: 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 ction pointers i
21e0: 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 n.** sqlite3Glob
21f0: 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 alConfig.m with
2200: 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 pointers to the
2210: 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 routines in this
2220: 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 73 file..*/.void s
2230: 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 qlite3MemSetDefa
2240: 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 ult(void){. sta
2250: 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
2260: 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 3_mem_methods de
2270: 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b faultMethods = {
2280: 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
2290: 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c Malloc,. sql
22a0: 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 ite3MemFree,.
22b0: 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c sqlite3MemReal
22c0: 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 loc,. sqlite
22d0: 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 3MemSize,. s
22e0: 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 qlite3MemRoundup
22f0: 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
2300: 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 mInit,. sqli
2310: 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a te3MemShutdown,.
2320: 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 0. };. sq
2330: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c lite3_config(SQL
2340: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f ITE_CONFIG_MALLO
2350: 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f C, &defaultMetho
2360: 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f ds);.}..#endif /
2370: 2a 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f * SQLITE_SYSTEM_
2380: 4d 41 4c 4c 4f 43 20 2a 2f 0a MALLOC */.