drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 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 | ************************************************************************* |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 12 | ** |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 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. |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 19 | */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 20 | #include "sqliteInt.h" |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 23 | ** This version of the memory allocator is the default. It is |
| 24 | ** used when no other memory allocator is specified using compile-time |
| 25 | ** macros. |
| 26 | */ |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 27 | #ifdef SQLITE_SYSTEM_MALLOC |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 28 | |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 29 | #ifdef HAVE_MALLOC_USABLE_SIZE |
| 30 | #include <malloc.h> |
| 31 | #endif |
| 32 | |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 33 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 34 | ** Like malloc(), but remember the size of the allocation |
| 35 | ** so that we can find it later using sqlite3MemSize(). |
| 36 | ** |
| 37 | ** For this low-level routine, we are guaranteed that nByte>0 because |
| 38 | ** cases of nByte<=0 will be intercepted and dealt with by higher level |
| 39 | ** routines. |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 40 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 41 | static void *sqlite3MemMalloc(int nByte){ |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 42 | #ifdef HAVE_MALLOC_USABLE_SIZE |
| 43 | void *p = malloc( nByte ); |
| 44 | if( p==0 ){ |
| 45 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 46 | sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); |
| 47 | } |
| 48 | return p; |
| 49 | #else |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 50 | sqlite3_int64 *p; |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 51 | assert( nByte>0 ); |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 52 | nByte = ROUND8(nByte); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 53 | p = malloc( nByte+8 ); |
danielk1977 | 950292f | 2008-06-23 15:10:24 +0000 | [diff] [blame] | 54 | if( p ){ |
| 55 | p[0] = nByte; |
| 56 | p++; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 57 | }else{ |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 58 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 59 | sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); |
danielk1977 | 950292f | 2008-06-23 15:10:24 +0000 | [diff] [blame] | 60 | } |
| 61 | return (void *)p; |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 62 | #endif |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* |
| 66 | ** Like free() but works for allocations obtained from sqlite3MemMalloc() |
| 67 | ** or sqlite3MemRealloc(). |
| 68 | ** |
| 69 | ** For this low-level routine, we already know that pPrior!=0 since |
| 70 | ** cases where pPrior==0 will have been intecepted and dealt with |
| 71 | ** by higher-level routines. |
| 72 | */ |
| 73 | static void sqlite3MemFree(void *pPrior){ |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 74 | #if HAVE_MALLOC_USABLE_SIZE |
| 75 | free(pPrior); |
| 76 | #else |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 77 | sqlite3_int64 *p = (sqlite3_int64*)pPrior; |
danielk1977 | 834a5aa | 2008-06-23 14:40:18 +0000 | [diff] [blame] | 78 | assert( pPrior!=0 ); |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 79 | p--; |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 80 | free(p); |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 81 | #endif |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 85 | ** Report the allocated size of a prior return from xMalloc() |
| 86 | ** or xRealloc(). |
| 87 | */ |
| 88 | static int sqlite3MemSize(void *pPrior){ |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 89 | #if HAVE_MALLOC_USABLE_SIZE |
| 90 | return pPrior ? (int)malloc_usable_size(pPrior) : 0; |
| 91 | #else |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 92 | sqlite3_int64 *p; |
| 93 | if( pPrior==0 ) return 0; |
| 94 | p = (sqlite3_int64*)pPrior; |
| 95 | p--; |
| 96 | return (int)p[0]; |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 97 | #endif |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 101 | ** Like realloc(). Resize an allocation previously obtained from |
| 102 | ** sqlite3MemMalloc(). |
| 103 | ** |
| 104 | ** For this low-level interface, we know that pPrior!=0. Cases where |
| 105 | ** pPrior==0 while have been intercepted by higher-level routine and |
| 106 | ** redirected to xMalloc. Similarly, we know that nByte>0 becauses |
| 107 | ** cases where nByte<=0 will have been intercepted by higher-level |
| 108 | ** routines and redirected to xFree. |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 109 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 110 | static void *sqlite3MemRealloc(void *pPrior, int nByte){ |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 111 | #if HAVE_MALLOC_USABLE_SIZE |
| 112 | void *p = realloc(pPrior, nByte); |
| 113 | if( p==0 ){ |
| 114 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 115 | sqlite3_log(SQLITE_NOMEM, |
| 116 | "failed memory resize %u to %u bytes", |
| 117 | malloc_usable_size(pPrior), nByte); |
| 118 | } |
| 119 | return p; |
| 120 | #else |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 121 | sqlite3_int64 *p = (sqlite3_int64*)pPrior; |
| 122 | assert( pPrior!=0 && nByte>0 ); |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 123 | assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 124 | p--; |
| 125 | p = realloc(p, nByte+8 ); |
| 126 | if( p ){ |
| 127 | p[0] = nByte; |
| 128 | p++; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 129 | }else{ |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 130 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 131 | sqlite3_log(SQLITE_NOMEM, |
| 132 | "failed memory resize %u to %u bytes", |
| 133 | sqlite3MemSize(pPrior), nByte); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 134 | } |
| 135 | return (void*)p; |
drh | 6a8ab6d | 2011-11-09 01:53:25 +0000 | [diff] [blame] | 136 | #endif |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 140 | ** Round up a request size to the next valid allocation size. |
| 141 | */ |
| 142 | static int sqlite3MemRoundup(int n){ |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 143 | return ROUND8(n); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | ** Initialize this module. |
| 148 | */ |
| 149 | static int sqlite3MemInit(void *NotUsed){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 150 | UNUSED_PARAMETER(NotUsed); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 151 | return SQLITE_OK; |
| 152 | } |
| 153 | |
| 154 | /* |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 155 | ** Deinitialize this module. |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 156 | */ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 157 | static void sqlite3MemShutdown(void *NotUsed){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 158 | UNUSED_PARAMETER(NotUsed); |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
drh | d1370b6 | 2008-10-28 18:58:20 +0000 | [diff] [blame] | 162 | /* |
| 163 | ** This routine is the only routine in this file with external linkage. |
| 164 | ** |
| 165 | ** Populate the low-level memory allocation function pointers in |
| 166 | ** sqlite3GlobalConfig.m with pointers to the routines in this file. |
| 167 | */ |
| 168 | void sqlite3MemSetDefault(void){ |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 169 | static const sqlite3_mem_methods defaultMethods = { |
| 170 | sqlite3MemMalloc, |
| 171 | sqlite3MemFree, |
| 172 | sqlite3MemRealloc, |
| 173 | sqlite3MemSize, |
| 174 | sqlite3MemRoundup, |
| 175 | sqlite3MemInit, |
| 176 | sqlite3MemShutdown, |
| 177 | 0 |
| 178 | }; |
drh | d1370b6 | 2008-10-28 18:58:20 +0000 | [diff] [blame] | 179 | sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); |
drh | 90f6a5b | 2007-08-15 13:04:54 +0000 | [diff] [blame] | 180 | } |
drh | 4c3645c | 2007-08-15 17:07:57 +0000 | [diff] [blame] | 181 | |
drh | 0d18020 | 2008-02-14 23:26:56 +0000 | [diff] [blame] | 182 | #endif /* SQLITE_SYSTEM_MALLOC */ |