00001 /* 00002 ** 2007 August 14 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** 00013 ** This file contains low-level memory allocation drivers for when 00014 ** SQLite will use the standard C-library malloc/realloc/free interface 00015 ** to obtain the memory it needs. 00016 ** 00017 ** This file contains implementations of the low-level memory allocation 00018 ** routines specified in the sqlite3_mem_methods object. 00019 ** 00020 ** $Id: mem1.c,v 1.27 2008/10/28 18:58:20 drh Exp $ 00021 */ 00022 #include "sqliteInt.h" 00023 00024 /* 00025 ** This version of the memory allocator is the default. It is 00026 ** used when no other memory allocator is specified using compile-time 00027 ** macros. 00028 */ 00029 #ifdef SQLITE_SYSTEM_MALLOC 00030 00031 /* 00032 ** Like malloc(), but remember the size of the allocation 00033 ** so that we can find it later using sqlite3MemSize(). 00034 ** 00035 ** For this low-level routine, we are guaranteed that nByte>0 because 00036 ** cases of nByte<=0 will be intercepted and dealt with by higher level 00037 ** routines. 00038 */ 00039 static void *sqlite3MemMalloc(int nByte){ 00040 sqlite3_int64 *p; 00041 assert( nByte>0 ); 00042 nByte = (nByte+7)&~7; 00043 p = malloc( nByte+8 ); 00044 if( p ){ 00045 p[0] = nByte; 00046 p++; 00047 } 00048 return (void *)p; 00049 } 00050 00051 /* 00052 ** Like free() but works for allocations obtained from sqlite3MemMalloc() 00053 ** or sqlite3MemRealloc(). 00054 ** 00055 ** For this low-level routine, we already know that pPrior!=0 since 00056 ** cases where pPrior==0 will have been intecepted and dealt with 00057 ** by higher-level routines. 00058 */ 00059 static void sqlite3MemFree(void *pPrior){ 00060 sqlite3_int64 *p = (sqlite3_int64*)pPrior; 00061 assert( pPrior!=0 ); 00062 p--; 00063 free(p); 00064 } 00065 00066 /* 00067 ** Like realloc(). Resize an allocation previously obtained from 00068 ** sqlite3MemMalloc(). 00069 ** 00070 ** For this low-level interface, we know that pPrior!=0. Cases where 00071 ** pPrior==0 while have been intercepted by higher-level routine and 00072 ** redirected to xMalloc. Similarly, we know that nByte>0 becauses 00073 ** cases where nByte<=0 will have been intercepted by higher-level 00074 ** routines and redirected to xFree. 00075 */ 00076 static void *sqlite3MemRealloc(void *pPrior, int nByte){ 00077 sqlite3_int64 *p = (sqlite3_int64*)pPrior; 00078 assert( pPrior!=0 && nByte>0 ); 00079 nByte = (nByte+7)&~7; 00080 p = (sqlite3_int64*)pPrior; 00081 p--; 00082 p = realloc(p, nByte+8 ); 00083 if( p ){ 00084 p[0] = nByte; 00085 p++; 00086 } 00087 return (void*)p; 00088 } 00089 00090 /* 00091 ** Report the allocated size of a prior return from xMalloc() 00092 ** or xRealloc(). 00093 */ 00094 static int sqlite3MemSize(void *pPrior){ 00095 sqlite3_int64 *p; 00096 if( pPrior==0 ) return 0; 00097 p = (sqlite3_int64*)pPrior; 00098 p--; 00099 return p[0]; 00100 } 00101 00102 /* 00103 ** Round up a request size to the next valid allocation size. 00104 */ 00105 static int sqlite3MemRoundup(int n){ 00106 return (n+7) & ~7; 00107 } 00108 00109 /* 00110 ** Initialize this module. 00111 */ 00112 static int sqlite3MemInit(void *NotUsed){ 00113 return SQLITE_OK; 00114 } 00115 00116 /* 00117 ** Deinitialize this module. 00118 */ 00119 static void sqlite3MemShutdown(void *NotUsed){ 00120 return; 00121 } 00122 00123 /* 00124 ** This routine is the only routine in this file with external linkage. 00125 ** 00126 ** Populate the low-level memory allocation function pointers in 00127 ** sqlite3GlobalConfig.m with pointers to the routines in this file. 00128 */ 00129 void sqlite3MemSetDefault(void){ 00130 static const sqlite3_mem_methods defaultMethods = { 00131 sqlite3MemMalloc, 00132 sqlite3MemFree, 00133 sqlite3MemRealloc, 00134 sqlite3MemSize, 00135 sqlite3MemRoundup, 00136 sqlite3MemInit, 00137 sqlite3MemShutdown, 00138 0 00139 }; 00140 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); 00141 } 00142 00143 #endif /* SQLITE_SYSTEM_MALLOC */
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:55 2011 by Doxygen 1.6.1