/ Hex Artifact Content
Login

Artifact f228cb7101ae403327824d327a1f5651d83ef0f2:


0000: 23 20 32 30 30 35 20 4e 6f 76 65 6d 62 65 72 20  # 2005 November 
0010: 33 30 0a 23 0a 23 20 54 68 65 20 61 75 74 68 6f  30.#.# The autho
0020: 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
0030: 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
0040: 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
0050: 6c 61 63 65 20 6f 66 0a 23 20 61 20 6c 65 67 61  lace of.# a lega
0060: 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
0070: 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 23 0a  s a blessing:.#.
0080: 23 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  #    May you do 
0090: 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
00a0: 6c 2e 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20  l..#    May you 
00b0: 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
00c0: 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
00d0: 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
00e0: 2e 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20 73  ..#    May you s
00f0: 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
0100: 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
0110: 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 23 0a  han you give..#.
0120: 23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  #***************
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 0a 23 0a 23 20 54 68 69  ********.#.# Thi
0170: 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
0180: 74 65 73 74 20 63 61 73 65 73 20 66 6f 63 75 73  test cases focus
0190: 65 64 20 6f 6e 20 74 68 65 20 74 77 6f 20 6d 65  ed on the two me
01a0: 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e 74 20  mory-management 
01b0: 41 50 49 73 2c 20 0a 23 20 73 71 6c 69 74 65 33  APIs, .# sqlite3
01c0: 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
01d0: 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72  () and sqlite3_r
01e0: 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 2e  elease_memory().
01f0: 0a 23 0a 23 20 24 49 64 3a 20 6d 61 6c 6c 6f 63  .#.# $Id: malloc
0200: 35 2e 74 65 73 74 2c 76 20 31 2e 31 30 20 32 30  5.test,v 1.10 20
0210: 30 37 2f 30 34 2f 30 37 20 31 37 3a 34 34 3a 32  07/04/07 17:44:2
0220: 38 20 64 72 68 20 45 78 70 20 24 0a 0a 23 2d 2d  8 drh Exp $..#--
0230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0240: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0250: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0260: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0270: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 4e 4f 54 45  ---------.# NOTE
0280: 53 20 4f 4e 20 45 58 50 45 43 54 45 44 20 42 45  S ON EXPECTED BE
0290: 48 41 56 49 4f 55 52 0a 23 0a 23 2d 2d 2d 2d 2d  HAVIOUR.#.#-----
02a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02e0: 2d 2d 2d 2d 2d 2d 0a 0a 0a 73 65 74 20 74 65 73  ------...set tes
02f0: 74 64 69 72 20 5b 66 69 6c 65 20 64 69 72 6e 61  tdir [file dirna
0300: 6d 65 20 24 61 72 67 76 30 5d 0a 73 6f 75 72 63  me $argv0].sourc
0310: 65 20 24 74 65 73 74 64 69 72 2f 74 65 73 74 65  e $testdir/teste
0320: 72 2e 74 63 6c 0a 64 62 20 63 6c 6f 73 65 0a 0a  r.tcl.db close..
0330: 23 20 4f 6e 6c 79 20 72 75 6e 20 74 68 65 73 65  # Only run these
0340: 20 74 65 73 74 73 20 69 66 20 6d 65 6d 6f 72 79   tests if memory
0350: 20 64 65 62 75 67 67 69 6e 67 20 69 73 20 74 75   debugging is tu
0360: 72 6e 65 64 20 6f 6e 2e 0a 69 66 20 7b 5b 69 6e  rned on..if {[in
0370: 66 6f 20 63 6f 6d 6d 61 6e 64 20 73 71 6c 69 74  fo command sqlit
0380: 65 5f 6d 61 6c 6c 6f 63 5f 73 74 61 74 5d 3d 3d  e_malloc_stat]==
0390: 22 22 7d 20 7b 0a 20 20 20 70 75 74 73 20 22 53  ""} {.   puts "S
03a0: 6b 69 70 70 69 6e 67 20 6d 61 6c 6c 6f 63 20 74  kipping malloc t
03b0: 65 73 74 73 3a 20 6e 6f 74 20 63 6f 6d 70 69 6c  ests: not compil
03c0: 65 64 20 77 69 74 68 20 2d 44 53 51 4c 49 54 45  ed with -DSQLITE
03d0: 5f 4d 45 4d 44 45 42 55 47 2e 2e 2e 22 0a 20 20  _MEMDEBUG...".  
03e0: 20 66 69 6e 69 73 68 5f 74 65 73 74 0a 20 20 20   finish_test.   
03f0: 72 65 74 75 72 6e 0a 7d 0a 0a 23 20 53 6b 69 70  return.}..# Skip
0400: 20 74 68 65 73 65 20 74 65 73 74 73 20 69 66 20   these tests if 
0410: 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  OMIT_MEMORY_MANA
0420: 47 45 4d 45 4e 54 20 77 61 73 20 64 65 66 69 6e  GEMENT was defin
0430: 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69  ed at compile ti
0440: 6d 65 2e 0a 69 66 63 61 70 61 62 6c 65 20 21 6d  me..ifcapable !m
0450: 65 6d 6f 72 79 6d 61 6e 61 67 65 20 7b 0a 20 20  emorymanage {.  
0460: 20 66 69 6e 69 73 68 5f 74 65 73 74 0a 20 20 20   finish_test.   
0470: 72 65 74 75 72 6e 0a 7d 0a 0a 73 71 6c 69 74 65  return.}..sqlite
0480: 33 20 64 62 20 74 65 73 74 2e 64 62 0a 0a 64 6f  3 db test.db..do
0490: 5f 74 65 73 74 20 6d 61 6c 6c 6f 63 35 2d 31 2e  _test malloc5-1.
04a0: 31 20 7b 0a 20 20 23 20 53 69 6d 70 6c 65 73 74  1 {.  # Simplest
04b0: 20 70 6f 73 73 69 62 6c 65 20 74 65 73 74 2e 20   possible test. 
04c0: 43 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 6c  Call sqlite3_rel
04d0: 65 61 73 65 5f 6d 65 6d 6f 72 79 20 77 68 65 6e  ease_memory when
04e0: 20 74 68 65 72 65 20 69 73 20 65 78 61 63 74 6c   there is exactl
04f0: 79 0a 20 20 23 20 6f 6e 65 20 75 6e 75 73 65 64  y.  # one unused
0500: 20 70 61 67 65 20 69 6e 20 61 20 73 69 6e 67 6c   page in a singl
0510: 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 54  e pager cache. T
0520: 68 69 73 20 74 65 73 74 20 63 61 73 65 20 73 65  his test case se
0530: 74 27 73 20 74 68 65 20 0a 20 20 23 20 76 61 6c  t's the .  # val
0540: 75 65 20 6f 66 20 74 68 65 20 3a 3a 70 67 61 6c  ue of the ::pgal
0550: 6c 6f 63 20 76 61 72 69 61 62 6c 65 2c 20 77 68  loc variable, wh
0560: 69 63 68 20 69 73 20 75 73 65 64 20 69 6e 20 73  ich is used in s
0570: 75 62 73 65 71 75 65 6e 74 20 74 65 73 74 73 2e  ubsequent tests.
0580: 0a 20 20 23 0a 20 20 23 20 4e 6f 74 65 3a 20 45  .  #.  # Note: E
0590: 76 65 6e 20 74 68 6f 75 67 68 20 65 78 65 63 75  ven though execu
05a0: 74 69 6e 67 20 74 68 69 73 20 73 74 61 74 65 6d  ting this statem
05b0: 65 6e 74 20 6f 6e 20 61 6e 20 65 6d 70 74 79 20  ent on an empty 
05c0: 64 61 74 61 62 61 73 65 20 0a 20 20 23 20 6d 6f  database .  # mo
05d0: 64 69 66 69 65 73 20 32 20 70 61 67 65 73 20 28  difies 2 pages (
05e0: 74 68 65 20 72 6f 6f 74 20 6f 66 20 73 71 6c 69  the root of sqli
05f0: 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 20 74 68  te_master and th
0600: 65 20 6e 65 77 20 72 6f 6f 74 20 70 61 67 65 29  e new root page)
0610: 2c 20 0a 20 20 23 20 74 68 65 20 73 71 6c 69 74  , .  # the sqlit
0620: 65 5f 6d 61 73 74 65 72 20 72 6f 6f 74 20 28 70  e_master root (p
0630: 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 20  age 1) is never 
0640: 66 72 65 65 64 20 62 65 63 61 75 73 65 20 74 68  freed because th
0650: 65 20 62 74 72 65 65 20 6c 61 79 65 72 0a 20 20  e btree layer.  
0660: 23 20 72 65 74 61 69 6e 73 20 61 20 72 65 66 65  # retains a refe
0670: 72 65 6e 63 65 20 74 6f 20 69 74 20 66 6f 72 20  rence to it for 
0680: 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73  the entire trans
0690: 61 63 74 69 6f 6e 2e 20 0a 20 20 65 78 65 63 73  action. .  execs
06a0: 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41 20  ql {.    PRAGMA 
06b0: 61 75 74 6f 5f 76 61 63 75 75 6d 3d 4f 46 46 3b  auto_vacuum=OFF;
06c0: 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20  .    BEGIN;.    
06d0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62 63  CREATE TABLE abc
06e0: 28 61 2c 20 62 2c 20 63 29 3b 0a 20 20 7d 0a 20  (a, b, c);.  }. 
06f0: 20 73 65 74 20 3a 3a 70 67 61 6c 6c 6f 63 20 5b   set ::pgalloc [
0700: 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
0710: 6d 65 6d 6f 72 79 5d 0a 20 20 65 78 70 72 20 24  memory].  expr $
0720: 3a 3a 70 67 61 6c 6c 6f 63 20 3e 20 30 0a 7d 20  ::pgalloc > 0.} 
0730: 7b 31 7d 0a 64 6f 5f 74 65 73 74 20 6d 61 6c 6c  {1}.do_test mall
0740: 6f 63 35 2d 31 2e 32 20 7b 0a 20 20 23 20 54 65  oc5-1.2 {.  # Te
0750: 73 74 20 74 68 61 74 20 74 68 65 20 74 72 61 6e  st that the tran
0760: 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 20  saction started 
0770: 69 6e 20 74 68 65 20 61 62 6f 76 65 20 74 65 73  in the above tes
0780: 74 20 69 73 20 73 74 69 6c 6c 20 61 63 74 69 76  t is still activ
0790: 65 2e 0a 20 20 23 20 42 65 63 61 75 73 65 20 74  e..  # Because t
07a0: 68 65 20 70 61 67 65 20 66 72 65 65 64 20 68 61  he page freed ha
07b0: 64 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  d been written t
07c0: 6f 2c 20 66 72 65 65 69 6e 67 20 69 74 20 72 65  o, freeing it re
07d0: 71 75 69 72 65 64 20 61 0a 20 20 23 20 6a 6f 75  quired a.  # jou
07e0: 72 6e 61 6c 20 73 79 6e 63 20 61 6e 64 20 65 78  rnal sync and ex
07f0: 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20  clusive lock on 
0800: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
0810: 65 2e 20 54 65 73 74 20 74 68 65 20 66 69 6c 65  e. Test the file
0820: 0a 20 20 23 20 61 70 70 65 61 72 73 20 74 6f 20  .  # appears to 
0830: 62 65 20 6c 6f 63 6b 65 64 2e 0a 20 20 73 71 6c  be locked..  sql
0840: 69 74 65 33 20 64 62 32 20 74 65 73 74 2e 64 62  ite3 db2 test.db
0850: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 0a 20 20  .  catchsql {.  
0860: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
0870: 61 62 63 3b 0a 20 20 7d 20 64 62 32 0a 7d 20 7b  abc;.  } db2.} {
0880: 31 20 7b 64 61 74 61 62 61 73 65 20 69 73 20 6c  1 {database is l
0890: 6f 63 6b 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20  ocked}}.do_test 
08a0: 6d 61 6c 6c 6f 63 35 2d 31 2e 33 20 7b 0a 20 20  malloc5-1.3 {.  
08b0: 23 20 41 67 61 69 6e 20 63 61 6c 6c 20 5b 73 71  # Again call [sq
08c0: 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
08d0: 6d 6f 72 79 5d 20 77 68 65 6e 20 74 68 65 72 65  mory] when there
08e0: 20 69 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20   is exactly one 
08f0: 75 6e 75 73 65 64 20 70 61 67 65 20 0a 20 20 23  unused page .  #
0900: 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 20 54   in the cache. T
0910: 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f  he same amount o
0920: 66 20 6d 65 6d 6f 72 79 20 69 73 20 72 65 71 75  f memory is requ
0930: 69 72 65 64 2c 20 62 75 74 20 6e 6f 20 6a 6f 75  ired, but no jou
0940: 72 6e 61 6c 2d 73 79 6e 63 0a 20 20 23 20 6f 72  rnal-sync.  # or
0950: 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
0960: 73 68 6f 75 6c 64 20 62 65 20 65 73 74 61 62 6c  should be establ
0970: 69 73 68 65 64 2e 0a 20 20 65 78 65 63 73 71 6c  ished..  execsql
0980: 20 7b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20   {.    COMMIT;. 
0990: 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 53 45     BEGIN;.    SE
09a0: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61 62 63 3b  LECT * FROM abc;
09b0: 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72  .  }.  sqlite3_r
09c0: 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 0a 7d 20  elease_memory.} 
09d0: 24 3a 3a 70 67 61 6c 6c 6f 63 0a 64 6f 5f 74 65  $::pgalloc.do_te
09e0: 73 74 20 6d 61 6c 6c 6f 63 35 2d 31 2e 34 20 7b  st malloc5-1.4 {
09f0: 0a 20 20 23 20 44 61 74 61 62 61 73 65 20 73 68  .  # Database sh
0a00: 6f 75 6c 64 20 6e 6f 74 20 62 65 20 6c 6f 63 6b  ould not be lock
0a10: 65 64 20 74 68 69 73 20 74 69 6d 65 2e 0a 20 20  ed this time..  
0a20: 63 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 53  catchsql {.    S
0a30: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61 62 63  ELECT * FROM abc
0a40: 3b 0a 20 20 7d 20 64 62 32 0a 7d 20 7b 30 20 7b  ;.  } db2.} {0 {
0a50: 7d 7d 0a 64 6f 5f 74 65 73 74 20 6d 61 6c 6c 6f  }}.do_test mallo
0a60: 63 35 2d 31 2e 35 20 7b 0a 20 20 23 20 4d 61 6e  c5-1.5 {.  # Man
0a70: 69 70 75 6c 61 74 65 20 74 68 65 20 63 61 63 68  ipulate the cach
0a80: 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 6f 6e  e so that it con
0a90: 74 61 69 6e 73 20 74 77 6f 20 75 6e 75 73 65 64  tains two unused
0aa0: 20 70 61 67 65 73 2e 20 4f 6e 65 20 72 65 71 75   pages. One requ
0ab0: 69 72 65 73 20 0a 20 20 23 20 61 20 6a 6f 75 72  ires .  # a jour
0ac0: 6e 61 6c 2d 73 79 6e 63 20 74 6f 20 66 72 65 65  nal-sync to free
0ad0: 2c 20 74 68 65 20 6f 74 68 65 72 20 64 6f 65 73  , the other does
0ae0: 20 6e 6f 74 2e 0a 20 20 64 62 32 20 63 6c 6f 73   not..  db2 clos
0af0: 65 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  e.  execsql {.  
0b00: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
0b10: 61 62 63 3b 0a 20 20 20 20 43 52 45 41 54 45 20  abc;.    CREATE 
0b20: 54 41 42 4c 45 20 64 65 66 28 64 2c 20 65 2c 20  TABLE def(d, e, 
0b30: 66 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  f);.  }.  sqlite
0b40: 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
0b50: 20 35 30 30 0a 7d 20 24 3a 3a 70 67 61 6c 6c 6f   500.} $::pgallo
0b60: 63 0a 64 6f 5f 74 65 73 74 20 6d 61 6c 6c 6f 63  c.do_test malloc
0b70: 35 2d 31 2e 36 20 7b 0a 20 20 23 20 44 61 74 61  5-1.6 {.  # Data
0b80: 62 61 73 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  base should not 
0b90: 62 65 20 6c 6f 63 6b 65 64 20 74 68 69 73 20 74  be locked this t
0ba0: 69 6d 65 2e 20 54 68 65 20 61 62 6f 76 65 20 74  ime. The above t
0bb0: 65 73 74 20 63 61 73 65 20 6f 6e 6c 79 0a 20 20  est case only.  
0bc0: 23 20 72 65 71 75 65 73 74 65 64 20 35 30 30 20  # requested 500 
0bd0: 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2c  bytes of memory,
0be0: 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 6f 62   which can be ob
0bf0: 74 61 69 6e 65 64 20 62 79 20 66 72 65 65 69 6e  tained by freein
0c00: 67 20 74 68 65 20 70 61 67 65 0a 20 20 23 20 74  g the page.  # t
0c10: 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 71  hat does not req
0c20: 75 69 72 65 20 61 6e 20 66 73 79 6e 63 28 29 2e  uire an fsync().
0c30: 0a 20 20 73 71 6c 69 74 65 33 20 64 62 32 20 74  .  sqlite3 db2 t
0c40: 65 73 74 2e 64 62 0a 20 20 63 61 74 63 68 73 71  est.db.  catchsq
0c50: 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a  l {.    SELECT *
0c60: 20 46 52 4f 4d 20 61 62 63 3b 0a 20 20 7d 20 64   FROM abc;.  } d
0c70: 62 32 0a 7d 20 7b 30 20 7b 7d 7d 0a 64 6f 5f 74  b2.} {0 {}}.do_t
0c80: 65 73 74 20 6d 61 6c 6c 6f 63 35 2d 31 2e 37 20  est malloc5-1.7 
0c90: 7b 0a 20 20 23 20 52 65 6c 65 61 73 65 20 61 6e  {.  # Release an
0ca0: 6f 74 68 65 72 20 35 30 30 20 62 79 74 65 73 20  other 500 bytes 
0cb0: 6f 66 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20  of memory. This 
0cc0: 74 69 6d 65 20 77 65 20 72 65 71 75 69 72 65 20  time we require 
0cd0: 61 20 73 79 6e 63 28 29 2c 20 0a 20 20 23 20 73  a sync(), .  # s
0ce0: 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
0cf0: 69 6c 65 20 77 69 6c 6c 20 62 65 20 6c 6f 63 6b  ile will be lock
0d00: 65 64 20 61 66 74 65 72 77 61 72 64 73 2e 0a 20  ed afterwards.. 
0d10: 20 64 62 32 20 63 6c 6f 73 65 0a 20 20 73 71 6c   db2 close.  sql
0d20: 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
0d30: 6f 72 79 20 35 30 30 0a 7d 20 24 3a 3a 70 67 61  ory 500.} $::pga
0d40: 6c 6c 6f 63 0a 64 6f 5f 74 65 73 74 20 6d 61 6c  lloc.do_test mal
0d50: 6c 6f 63 35 2d 31 2e 38 20 7b 0a 20 20 73 71 6c  loc5-1.8 {.  sql
0d60: 69 74 65 33 20 64 62 32 20 74 65 73 74 2e 64 62  ite3 db2 test.db
0d70: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 0a 20 20  .  catchsql {.  
0d80: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
0d90: 61 62 63 3b 0a 20 20 7d 20 64 62 32 0a 7d 20 7b  abc;.  } db2.} {
0da0: 31 20 7b 64 61 74 61 62 61 73 65 20 69 73 20 6c  1 {database is l
0db0: 6f 63 6b 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20  ocked}}.do_test 
0dc0: 6d 61 6c 6c 6f 63 35 2d 31 2e 39 20 7b 0a 20 20  malloc5-1.9 {.  
0dd0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 4f  execsql {.    CO
0de0: 4d 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a  MMIT;.  }.} {}..
0df0: 64 6f 5f 74 65 73 74 20 6d 61 6c 6c 6f 63 35 2d  do_test malloc5-
0e00: 32 2e 31 20 7b 0a 20 20 23 20 50 75 74 20 73 6f  2.1 {.  # Put so
0e10: 6d 65 20 64 61 74 61 20 69 6e 20 74 61 62 6c 65  me data in table
0e20: 73 20 61 62 63 20 61 6e 64 20 64 65 66 2e 20 42  s abc and def. B
0e30: 6f 74 68 20 74 61 62 6c 65 73 20 61 72 65 20 73  oth tables are s
0e40: 74 69 6c 6c 20 77 68 6f 6c 6c 79 20 0a 20 20 23  till wholly .  #
0e50: 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
0e60: 6e 20 74 68 65 69 72 20 72 6f 6f 74 20 70 61 67  n their root pag
0e70: 65 73 2e 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  es..  execsql {.
0e80: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
0e90: 61 62 63 20 56 41 4c 55 45 53 28 31 2c 20 32 2c  abc VALUES(1, 2,
0ea0: 20 33 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20   3);.    INSERT 
0eb0: 49 4e 54 4f 20 61 62 63 20 56 41 4c 55 45 53 28  INTO abc VALUES(
0ec0: 34 2c 20 35 2c 20 36 29 3b 0a 20 20 20 20 49 4e  4, 5, 6);.    IN
0ed0: 53 45 52 54 20 49 4e 54 4f 20 64 65 66 20 56 41  SERT INTO def VA
0ee0: 4c 55 45 53 28 37 2c 20 38 2c 20 39 29 3b 0a 20  LUES(7, 8, 9);. 
0ef0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 64     INSERT INTO d
0f00: 65 66 20 56 41 4c 55 45 53 28 31 30 2c 31 31 2c  ef VALUES(10,11,
0f10: 31 32 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  12);.  }.} {}.do
0f20: 5f 74 65 73 74 20 6d 61 6c 6c 6f 63 35 2d 32 2e  _test malloc5-2.
0f30: 32 20 7b 0a 20 20 23 20 4c 6f 61 64 20 74 68 65  2 {.  # Load the
0f40: 20 72 6f 6f 74 2d 70 61 67 65 20 66 6f 72 20 74   root-page for t
0f50: 61 62 6c 65 20 64 65 66 20 69 6e 74 6f 20 74 68  able def into th
0f60: 65 20 63 61 63 68 65 2e 20 54 68 65 6e 20 71 75  e cache. Then qu
0f70: 65 72 79 20 74 61 62 6c 65 20 61 62 63 2e 20 0a  ery table abc. .
0f80: 20 20 23 20 48 61 6c 66 77 61 79 20 74 68 72 6f    # Halfway thro
0f90: 75 67 68 20 74 68 65 20 71 75 65 72 79 20 63 61  ugh the query ca
0fa0: 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  ll sqlite3_relea
0fb0: 73 65 5f 6d 65 6d 6f 72 79 28 29 2e 20 54 68 65  se_memory(). The
0fc0: 20 67 6f 61 6c 20 6f 66 20 74 68 69 73 0a 20 20   goal of this.  
0fd0: 23 20 74 65 73 74 20 69 73 20 74 6f 20 6d 61 6b  # test is to mak
0fe0: 65 20 73 75 72 65 20 77 65 20 64 6f 6e 27 74 20  e sure we don't 
0ff0: 66 72 65 65 20 70 61 67 65 73 20 74 68 61 74 20  free pages that 
1000: 61 72 65 20 69 6e 20 75 73 65 20 28 73 70 65 63  are in use (spec
1010: 69 66 69 63 61 6c 6c 79 2c 20 0a 20 20 23 20 74  ifically, .  # t
1020: 68 65 20 72 6f 6f 74 20 6f 66 20 74 61 62 6c 65  he root of table
1030: 20 61 62 63 29 2e 0a 20 20 73 65 74 20 6e 52 65   abc)..  set nRe
1040: 6c 65 61 73 65 20 30 0a 20 20 65 78 65 63 73 71  lease 0.  execsq
1050: 6c 20 7b 20 0a 20 20 20 20 42 45 47 49 4e 3b 0a  l { .    BEGIN;.
1060: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
1070: 4d 20 64 65 66 3b 0a 20 20 7d 0a 20 20 73 65 74  M def;.  }.  set
1080: 20 64 61 74 61 20 5b 6c 69 73 74 5d 0a 20 20 64   data [list].  d
1090: 62 20 65 76 61 6c 20 7b 53 45 4c 45 43 54 20 2a  b eval {SELECT *
10a0: 20 46 52 4f 4d 20 61 62 63 7d 20 7b 0a 20 20 20   FROM abc} {.   
10b0: 20 69 6e 63 72 20 6e 52 65 6c 65 61 73 65 20 5b   incr nRelease [
10c0: 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
10d0: 6d 65 6d 6f 72 79 5d 0a 20 20 20 20 6c 61 70 70  memory].    lapp
10e0: 65 6e 64 20 64 61 74 61 20 24 61 20 24 62 20 24  end data $a $b $
10f0: 63 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c 20  c.  }.  execsql 
1100: 7b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20  {.    COMMIT;.  
1110: 7d 0a 20 20 6c 69 73 74 20 24 6e 52 65 6c 65 61  }.  list $nRelea
1120: 73 65 20 24 64 61 74 61 0a 7d 20 5b 6c 69 73 74  se $data.} [list
1130: 20 24 70 67 61 6c 6c 6f 63 20 5b 6c 69 73 74 20   $pgalloc [list 
1140: 31 20 32 20 33 20 34 20 35 20 36 5d 5d 0a 0a 64  1 2 3 4 5 6]]..d
1150: 6f 5f 74 65 73 74 20 6d 61 6c 6c 6f 63 35 2d 33  o_test malloc5-3
1160: 2e 31 20 7b 0a 20 20 23 20 53 69 6d 70 6c 65 20  .1 {.  # Simple 
1170: 74 65 73 74 20 74 6f 20 73 68 6f 77 20 74 68 61  test to show tha
1180: 74 20 69 66 20 74 77 6f 20 70 61 67 65 72 73 20  t if two pagers 
1190: 61 72 65 20 6f 70 65 6e 65 64 20 66 72 6f 6d 20  are opened from 
11a0: 77 69 74 68 69 6e 20 74 68 69 73 0a 20 20 23 20  within this.  # 
11b0: 74 68 72 65 61 64 2c 20 6d 65 6d 6f 72 79 20 69  thread, memory i
11c0: 73 20 66 72 65 65 64 20 66 72 6f 6d 20 62 6f 74  s freed from bot
11d0: 68 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 72  h when sqlite3_r
11e0: 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
11f0: 69 73 0a 20 20 23 20 63 61 6c 6c 65 64 2e 0a 20  is.  # called.. 
1200: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42   execsql {.    B
1210: 45 47 49 4e 3b 0a 20 20 20 20 53 45 4c 45 43 54  EGIN;.    SELECT
1220: 20 2a 20 46 52 4f 4d 20 61 62 63 3b 0a 20 20 7d   * FROM abc;.  }
1230: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
1240: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 73   SELECT * FROM s
1250: 71 6c 69 74 65 5f 6d 61 73 74 65 72 3b 0a 20 20  qlite_master;.  
1260: 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 53 45 4c    BEGIN;.    SEL
1270: 45 43 54 20 2a 20 46 52 4f 4d 20 64 65 66 3b 0a  ECT * FROM def;.
1280: 20 20 7d 20 64 62 32 0a 20 20 73 71 6c 69 74 65    } db2.  sqlite
1290: 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
12a0: 0a 7d 20 5b 65 78 70 72 20 24 3a 3a 70 67 61 6c  .} [expr $::pgal
12b0: 6c 6f 63 20 2a 20 32 5d 0a 64 6f 5f 74 65 73 74  loc * 2].do_test
12c0: 20 6d 61 6c 6c 6f 63 35 2d 33 2e 32 20 7b 0a 20   malloc5-3.2 {. 
12d0: 20 63 6f 6e 63 61 74 20 5c 0a 20 20 20 20 5b 65   concat \.    [e
12e0: 78 65 63 73 71 6c 20 7b 53 45 4c 45 43 54 20 2a  xecsql {SELECT *
12f0: 20 46 52 4f 4d 20 61 62 63 3b 20 43 4f 4d 4d 49   FROM abc; COMMI
1300: 54 7d 5d 20 5c 0a 20 20 20 20 5b 65 78 65 63 73  T}] \.    [execs
1310: 71 6c 20 7b 53 45 4c 45 43 54 20 2a 20 46 52 4f  ql {SELECT * FRO
1320: 4d 20 64 65 66 3b 20 43 4f 4d 4d 49 54 7d 20 64  M def; COMMIT} d
1330: 62 32 5d 0a 7d 20 7b 31 20 32 20 33 20 34 20 35  b2].} {1 2 3 4 5
1340: 20 36 20 37 20 38 20 39 20 31 30 20 31 31 20 31   6 7 8 9 10 11 1
1350: 32 7d 0a 0a 64 62 32 20 63 6c 6f 73 65 0a 73 71  2}..db2 close.sq
1360: 6c 69 74 65 5f 6d 61 6c 6c 6f 63 5f 6f 75 74 73  lite_malloc_outs
1370: 74 61 6e 64 69 6e 67 20 2d 63 6c 65 61 72 6d 61  tanding -clearma
1380: 78 62 79 74 65 73 0a 0a 23 20 54 68 65 20 66 6f  xbytes..# The fo
1390: 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 74 65 73 74  llowing two test
13a0: 20 63 61 73 65 73 20 65 61 63 68 20 65 78 65 63   cases each exec
13b0: 75 74 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ute a transactio
13c0: 6e 20 69 6e 20 77 68 69 63 68 20 0a 23 20 31 30  n in which .# 10
13d0: 30 30 30 20 72 6f 77 73 20 61 72 65 20 69 6e 73  000 rows are ins
13e0: 65 72 74 65 64 20 69 6e 74 6f 20 74 61 62 6c 65  erted into table
13f0: 20 61 62 63 2e 20 54 68 65 20 66 69 72 73 74 20   abc. The first 
1400: 74 65 73 74 20 63 61 73 65 20 69 73 20 75 73 65  test case is use
1410: 64 0a 23 20 74 6f 20 65 6e 73 75 72 65 20 74 68  d.# to ensure th
1420: 61 74 20 6d 6f 72 65 20 74 68 61 6e 20 31 4d 42  at more than 1MB
1430: 20 6f 66 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f   of dynamic memo
1440: 72 79 20 69 73 20 75 73 65 64 20 74 6f 20 70 65  ry is used to pe
1450: 72 66 6f 72 6d 0a 23 20 74 68 65 20 74 72 61 6e  rform.# the tran
1460: 73 61 63 74 69 6f 6e 2e 20 0a 23 0a 23 20 54 68  saction. .#.# Th
1470: 65 20 73 65 63 6f 6e 64 20 74 65 73 74 20 63 61  e second test ca
1480: 73 65 20 73 65 74 73 20 74 68 65 20 22 73 6f 66  se sets the "sof
1490: 74 2d 68 65 61 70 2d 6c 69 6d 69 74 22 20 74 6f  t-heap-limit" to
14a0: 20 31 30 30 2c 30 30 30 20 62 79 74 65 73 20 28   100,000 bytes (
14b0: 30 2e 31 20 4d 42 29 0a 23 20 61 6e 64 20 74 65  0.1 MB).# and te
14c0: 73 74 73 20 74 6f 20 73 65 65 20 74 68 61 74 20  sts to see that 
14d0: 74 68 69 73 20 6c 69 6d 69 74 20 69 73 20 6e 6f  this limit is no
14e0: 74 20 65 78 63 65 65 64 65 64 20 61 74 20 61 6e  t exceeded at an
14f0: 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 0a  y point during .
1500: 23 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 78  # transaction ex
1510: 65 63 75 74 69 6f 6e 2e 0a 23 0a 23 20 42 65 66  ecution..#.# Bef
1520: 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 6d 61  ore executing ma
1530: 6c 6c 6f 63 35 2d 34 2e 2a 20 77 65 20 73 61 76  lloc5-4.* we sav
1540: 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
1550: 68 65 20 63 75 72 72 65 6e 74 20 73 6f 66 74 20  he current soft 
1560: 68 65 61 70 20 0a 23 20 6c 69 6d 69 74 20 69 6e  heap .# limit in
1570: 20 76 61 72 69 61 62 6c 65 20 3a 3a 73 6f 66 74   variable ::soft
1580: 5f 6c 69 6d 69 74 2e 20 54 68 65 20 6f 72 69 67  _limit. The orig
1590: 69 6e 61 6c 20 76 61 6c 75 65 20 69 73 20 72 65  inal value is re
15a0: 73 74 6f 72 65 64 20 61 66 74 65 72 20 0a 23 20  stored after .# 
15b0: 72 75 6e 6e 69 6e 67 20 74 68 65 20 74 65 73 74  running the test
15c0: 73 2e 0a 23 0a 73 65 74 20 3a 3a 73 6f 66 74 5f  s..#.set ::soft_
15d0: 6c 69 6d 69 74 20 5b 73 71 6c 69 74 65 33 5f 73  limit [sqlite3_s
15e0: 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 20 2d  oft_heap_limit -
15f0: 31 5d 0a 64 6f 5f 74 65 73 74 20 6d 61 6c 6c 6f  1].do_test mallo
1600: 63 35 2d 34 2e 31 20 7b 0a 20 20 65 78 65 63 73  c5-4.1 {.  execs
1610: 71 6c 20 7b 42 45 47 49 4e 3b 7d 0a 20 20 65 78  ql {BEGIN;}.  ex
1620: 65 63 73 71 6c 20 7b 44 45 4c 45 54 45 20 46 52  ecsql {DELETE FR
1630: 4f 4d 20 61 62 63 3b 7d 0a 20 20 66 6f 72 20 7b  OM abc;}.  for {
1640: 73 65 74 20 69 20 30 7d 20 7b 24 69 20 3c 20 31  set i 0} {$i < 1
1650: 30 30 30 30 7d 20 7b 69 6e 63 72 20 69 7d 20 7b  0000} {incr i} {
1660: 0a 20 20 20 20 65 78 65 63 73 71 6c 20 22 49 4e  .    execsql "IN
1670: 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20 56 41  SERT INTO abc VA
1680: 4c 55 45 53 28 24 69 2c 20 24 69 2c 20 27 5b 73  LUES($i, $i, '[s
1690: 74 72 69 6e 67 20 72 65 70 65 61 74 20 58 20 31  tring repeat X 1
16a0: 30 30 5d 27 29 3b 22 0a 20 20 7d 0a 20 20 65 78  00]');".  }.  ex
16b0: 65 63 73 71 6c 20 7b 43 4f 4d 4d 49 54 3b 7d 0a  ecsql {COMMIT;}.
16c0: 20 20 73 65 74 20 3a 3a 6e 4d 61 78 42 79 74 65    set ::nMaxByte
16d0: 73 20 5b 73 71 6c 69 74 65 5f 6d 61 6c 6c 6f 63  s [sqlite_malloc
16e0: 5f 6f 75 74 73 74 61 6e 64 69 6e 67 20 2d 6d 61  _outstanding -ma
16f0: 78 62 79 74 65 73 5d 0a 20 20 69 66 20 7b 24 3a  xbytes].  if {$:
1700: 3a 6e 4d 61 78 42 79 74 65 73 3d 3d 22 22 7d 20  :nMaxBytes==""} 
1710: 7b 73 65 74 20 3a 3a 6e 4d 61 78 42 79 74 65 73  {set ::nMaxBytes
1720: 20 31 30 30 30 30 30 31 7d 0a 20 20 65 78 70 72   1000001}.  expr
1730: 20 24 3a 3a 6e 4d 61 78 42 79 74 65 73 20 3e 20   $::nMaxBytes > 
1740: 31 30 30 30 30 30 30 0a 7d 20 7b 31 7d 0a 64 6f  1000000.} {1}.do
1750: 5f 74 65 73 74 20 6d 61 6c 6c 6f 63 35 2d 34 2e  _test malloc5-4.
1760: 32 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  2 {.  sqlite3_re
1770: 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 0a 20 20 73  lease_memory.  s
1780: 71 6c 69 74 65 5f 6d 61 6c 6c 6f 63 5f 6f 75 74  qlite_malloc_out
1790: 73 74 61 6e 64 69 6e 67 20 2d 63 6c 65 61 72 6d  standing -clearm
17a0: 61 78 62 79 74 65 73 0a 20 20 73 71 6c 69 74 65  axbytes.  sqlite
17b0: 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
17c0: 74 20 31 30 30 30 30 30 0a 20 20 65 78 65 63 73  t 100000.  execs
17d0: 71 6c 20 7b 42 45 47 49 4e 3b 7d 0a 20 20 66 6f  ql {BEGIN;}.  fo
17e0: 72 20 7b 73 65 74 20 69 20 30 7d 20 7b 24 69 20  r {set i 0} {$i 
17f0: 3c 20 31 30 30 30 30 7d 20 7b 69 6e 63 72 20 69  < 10000} {incr i
1800: 7d 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20  } {.    execsql 
1810: 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63  "INSERT INTO abc
1820: 20 56 41 4c 55 45 53 28 24 69 2c 20 24 69 2c 20   VALUES($i, $i, 
1830: 27 5b 73 74 72 69 6e 67 20 72 65 70 65 61 74 20  '[string repeat 
1840: 58 20 31 30 30 5d 27 29 3b 22 0a 20 20 7d 0a 20  X 100]');".  }. 
1850: 20 65 78 65 63 73 71 6c 20 7b 43 4f 4d 4d 49 54   execsql {COMMIT
1860: 3b 7d 0a 20 20 73 65 74 20 3a 3a 6e 4d 61 78 42  ;}.  set ::nMaxB
1870: 79 74 65 73 20 5b 73 71 6c 69 74 65 5f 6d 61 6c  ytes [sqlite_mal
1880: 6c 6f 63 5f 6f 75 74 73 74 61 6e 64 69 6e 67 20  loc_outstanding 
1890: 2d 6d 61 78 62 79 74 65 73 5d 0a 20 20 69 66 20  -maxbytes].  if 
18a0: 7b 24 3a 3a 6e 4d 61 78 42 79 74 65 73 3d 3d 22  {$::nMaxBytes=="
18b0: 22 7d 20 7b 73 65 74 20 3a 3a 6e 4d 61 78 42 79  "} {set ::nMaxBy
18c0: 74 65 73 20 30 7d 0a 20 20 65 78 70 72 20 24 3a  tes 0}.  expr $:
18d0: 3a 6e 4d 61 78 42 79 74 65 73 20 3c 3d 20 31 30  :nMaxBytes <= 10
18e0: 30 30 30 30 0a 7d 20 7b 31 7d 0a 64 6f 5f 74 65  0000.} {1}.do_te
18f0: 73 74 20 6d 61 6c 6c 6f 63 35 2d 34 2e 33 20 7b  st malloc5-4.3 {
1900: 0a 20 20 23 20 43 68 65 63 6b 20 74 68 61 74 20  .  # Check that 
1910: 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
1920: 61 62 6c 65 20 61 62 63 20 69 73 20 61 74 20 6c  able abc is at l
1930: 65 61 73 74 20 72 6f 75 67 68 6c 79 20 61 73 20  east roughly as 
1940: 65 78 70 65 63 74 65 64 2e 0a 20 20 65 78 65 63  expected..  exec
1950: 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54  sql {.    SELECT
1960: 20 63 6f 75 6e 74 28 2a 29 2c 20 73 75 6d 28 61   count(*), sum(a
1970: 29 2c 20 73 75 6d 28 62 29 20 46 52 4f 4d 20 61  ), sum(b) FROM a
1980: 62 63 3b 0a 20 20 7d 0a 7d 20 5b 6c 69 73 74 20  bc;.  }.} [list 
1990: 32 30 30 30 30 20 5b 65 78 70 72 20 69 6e 74 28  20000 [expr int(
19a0: 32 30 30 30 30 2e 30 20 2a 20 34 39 39 39 2e 35  20000.0 * 4999.5
19b0: 29 5d 20 5b 65 78 70 72 20 69 6e 74 28 32 30 30  )] [expr int(200
19c0: 30 30 2e 30 20 2a 20 34 39 39 39 2e 35 29 5d 5d  00.0 * 4999.5)]]
19d0: 0a 0a 23 20 52 65 73 74 6f 72 65 20 74 68 65 20  ..# Restore the 
19e0: 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 2e  soft heap limit.
19f0: 0a 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65  .sqlite3_soft_he
1a00: 61 70 5f 6c 69 6d 69 74 20 24 3a 3a 73 6f 66 74  ap_limit $::soft
1a10: 5f 6c 69 6d 69 74 0a 0a 23 20 54 65 73 74 20 74  _limit..# Test t
1a20: 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hat there are no
1a30: 20 70 72 6f 62 6c 65 6d 73 20 63 61 6c 6c 69 6e   problems callin
1a40: 67 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73  g sqlite3_releas
1a50: 65 5f 6d 65 6d 6f 72 79 20 77 68 65 6e 0a 23 20  e_memory when.# 
1a60: 74 68 65 72 65 20 61 72 65 20 6f 70 65 6e 20 69  there are open i
1a70: 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
1a80: 65 73 2e 0a 23 0a 23 20 41 74 20 6f 6e 65 20 70  es..#.# At one p
1a90: 6f 69 6e 74 20 74 68 65 73 65 20 74 65 73 74 73  oint these tests
1aa0: 20 77 6f 75 6c 64 20 63 61 75 73 65 20 61 20 73   would cause a s
1ab0: 65 67 2d 66 61 75 6c 74 2e 0a 23 0a 64 6f 5f 74  eg-fault..#.do_t
1ac0: 65 73 74 20 6d 61 6c 6c 6f 63 35 2d 35 2e 31 20  est malloc5-5.1 
1ad0: 7b 0a 20 20 64 62 20 63 6c 6f 73 65 0a 20 20 73  {.  db close.  s
1ae0: 71 6c 69 74 65 33 20 64 62 20 3a 6d 65 6d 6f 72  qlite3 db :memor
1af0: 79 3a 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20  y:.  execsql {. 
1b00: 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 43 52     BEGIN;.    CR
1b10: 45 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61  EATE TABLE abc(a
1b20: 2c 20 62 2c 20 63 29 3b 0a 20 20 20 20 49 4e 53  , b, c);.    INS
1b30: 45 52 54 20 49 4e 54 4f 20 61 62 63 20 56 41 4c  ERT INTO abc VAL
1b40: 55 45 53 28 27 61 62 63 64 65 66 67 68 69 27 2c  UES('abcdefghi',
1b50: 20 31 32 33 34 35 36 37 38 39 30 2c 20 4e 55 4c   1234567890, NUL
1b60: 4c 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  L);.    INSERT I
1b70: 4e 54 4f 20 61 62 63 20 53 45 4c 45 43 54 20 2a  NTO abc SELECT *
1b80: 20 46 52 4f 4d 20 61 62 63 3b 0a 20 20 20 20 49   FROM abc;.    I
1b90: 4e 53 45 52 54 20 49 4e 54 4f 20 61 62 63 20 53  NSERT INTO abc S
1ba0: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61 62 63  ELECT * FROM abc
1bb0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
1bc0: 4f 20 61 62 63 20 53 45 4c 45 43 54 20 2a 20 46  O abc SELECT * F
1bd0: 52 4f 4d 20 61 62 63 3b 0a 20 20 20 20 49 4e 53  ROM abc;.    INS
1be0: 45 52 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c  ERT INTO abc SEL
1bf0: 45 43 54 20 2a 20 46 52 4f 4d 20 61 62 63 3b 0a  ECT * FROM abc;.
1c00: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
1c10: 61 62 63 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  abc SELECT * FRO
1c20: 4d 20 61 62 63 3b 0a 20 20 20 20 49 4e 53 45 52  M abc;.    INSER
1c30: 54 20 49 4e 54 4f 20 61 62 63 20 53 45 4c 45 43  T INTO abc SELEC
1c40: 54 20 2a 20 46 52 4f 4d 20 61 62 63 3b 0a 20 20  T * FROM abc;.  
1c50: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 62    INSERT INTO ab
1c60: 63 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  c SELECT * FROM 
1c70: 61 62 63 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  abc;.  }.  sqlit
1c80: 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
1c90: 79 0a 7d 20 30 0a 64 6f 5f 74 65 73 74 20 6d 61  y.} 0.do_test ma
1ca0: 6c 6c 6f 63 35 2d 35 2e 31 20 7b 0a 20 20 73 71  lloc5-5.1 {.  sq
1cb0: 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f  lite3_soft_heap_
1cc0: 6c 69 6d 69 74 20 35 30 30 30 0a 20 20 65 78 65  limit 5000.  exe
1cd0: 63 73 71 6c 20 7b 0a 20 20 20 20 43 4f 4d 4d 49  csql {.    COMMI
1ce0: 54 3b 0a 20 20 20 20 50 52 41 47 4d 41 20 74 65  T;.    PRAGMA te
1cf0: 6d 70 5f 73 74 6f 72 65 20 3d 20 6d 65 6d 6f 72  mp_store = memor
1d00: 79 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  y;.    SELECT * 
1d10: 46 52 4f 4d 20 61 62 63 20 4f 52 44 45 52 20 42  FROM abc ORDER B
1d20: 59 20 61 3b 0a 20 20 7d 0a 20 20 65 78 70 72 20  Y a;.  }.  expr 
1d30: 31 0a 7d 20 7b 31 7d 0a 0a 73 71 6c 69 74 65 33  1.} {1}..sqlite3
1d40: 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
1d50: 20 24 3a 3a 73 6f 66 74 5f 6c 69 6d 69 74 0a 66   $::soft_limit.f
1d60: 69 6e 69 73 68 5f 74 65 73 74 0a 63 61 74 63 68  inish_test.catch
1d70: 20 7b 64 62 20 63 6c 6f 73 65 7d 0a               {db close}.