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