1 /* 2 ** 2007 August 14 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** This file contains low-level memory allocation drivers for when 14 ** SQLite will use the standard C-library malloc/realloc/free interface 15 ** to obtain the memory it needs. 16 ** 17 ** This file contains implementations of the low-level memory allocation 18 ** routines specified in the sqlite3_mem_methods object. 19 */ 20 #include "sqliteInt.h" 21 22 /* 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 */ 27 #ifdef SQLITE_SYSTEM_MALLOC 28 29 /* 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. 36 */ 37 static void *sqlite3MemMalloc(int nByte){ 38 sqlite3_int64 *p; 39 assert( nByte>0 ); 40 nByte = ROUND8(nByte); 41 p = malloc( nByte+8 ); 42 if( p ){ 43 p[0] = nByte; 44 p++; 45 } 46 return (void *)p; 47 } 48 49 /* 50 ** Like free() but works for allocations obtained from sqlite3MemMalloc() 51 ** or sqlite3MemRealloc(). 52 ** 53 ** For this low-level routine, we already know that pPrior!=0 since 54 ** cases where pPrior==0 will have been intecepted and dealt with 55 ** by higher-level routines. 56 */ 57 static void sqlite3MemFree(void *pPrior){ 58 sqlite3_int64 *p = (sqlite3_int64*)pPrior; 59 assert( pPrior!=0 ); 60 p--; 61 free(p); 62 } 63 64 /* 65 ** Like realloc(). Resize an allocation previously obtained from 66 ** sqlite3MemMalloc(). 67 ** 68 ** For this low-level interface, we know that pPrior!=0. Cases where 69 ** pPrior==0 while have been intercepted by higher-level routine and 70 ** redirected to xMalloc. Similarly, we know that nByte>0 becauses 71 ** cases where nByte<=0 will have been intercepted by higher-level 72 ** routines and redirected to xFree. 73 */ 74 static void *sqlite3MemRealloc(void *pPrior, int nByte){ 75 sqlite3_int64 *p = (sqlite3_int64*)pPrior; 76 assert( pPrior!=0 && nByte>0 ); 77 nByte = ROUND8(nByte); 78 p--; 79 p = realloc(p, nByte+8 ); 80 if( p ){ 81 p[0] = nByte; 82 p++; 83 } 84 return (void*)p; 85 } 86 87 /* 88 ** Report the allocated size of a prior return from xMalloc() 89 ** or xRealloc(). 90 */ 91 static int sqlite3MemSize(void *pPrior){ 92 sqlite3_int64 *p; 93 if( pPrior==0 ) return 0; 94 p = (sqlite3_int64*)pPrior; 95 p--; 96 return (int)p[0]; 97 } 98 99 /* 100 ** Round up a request size to the next valid allocation size. 101 */ 102 static int sqlite3MemRoundup(int n){ 103 return ROUND8(n); 104 } 105 106 /* 107 ** Initialize this module. 108 */ 109 static int sqlite3MemInit(void *NotUsed){ 110 UNUSED_PARAMETER(NotUsed); 111 return SQLITE_OK; 112 } 113 114 /* 115 ** Deinitialize this module. 116 */ 117 static void sqlite3MemShutdown(void *NotUsed){ 118 UNUSED_PARAMETER(NotUsed); 119 return; 120 } 121 122 /* 123 ** This routine is the only routine in this file with external linkage. 124 ** 125 ** Populate the low-level memory allocation function pointers in 126 ** sqlite3GlobalConfig.m with pointers to the routines in this file. 127 */ 128 void sqlite3MemSetDefault(void){ 129 static const sqlite3_mem_methods defaultMethods = { 130 sqlite3MemMalloc, 131 sqlite3MemFree, 132 sqlite3MemRealloc, 133 sqlite3MemSize, 134 sqlite3MemRoundup, 135 sqlite3MemInit, 136 sqlite3MemShutdown, 137 0 138 }; 139 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); 140 } 141 142 #endif /* SQLITE_SYSTEM_MALLOC */ 143