/* ** This is the test layer's wrapper around sqlite3OsFree(). The argument is a ** pointer to the space allocated for the application to use. */ static void OSFREE(void *pFree){ u32 *p = (u32 *)getOsPointer(pFree); /* p points to Os level allocation */ checkGuards(p); unlinkAlloc(p); memset(pFree, 0x55, OSSIZEOF(pFree)); sqlite3OsFree(p); sqlite3_nFree++; }
/* ** This is the test layer's wrapper around sqlite3OsFree(). The argument is a ** pointer to the space allocated for the application to use. */ static void OSFREE(void *pFree){ u32 *p; /* Pointer to the OS-layer allocation */ sqlite3OsEnterMutex(); p = (u32 *)getOsPointer(pFree); checkGuards(p); unlinkAlloc(p); memset(pFree, 0x55, OSSIZEOF(pFree)); sqlite3OsFree(p); sqlite3_nFree++; sqlite3OsLeaveMutex(); }
/* ** This is the test layer's wrapper around sqlite3OsRealloc(). */ static void * OSREALLOC(void *pRealloc, int n){ #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT sqlite3_nMaxAlloc = MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc); #endif assert( !sqlite3_mallocDisallowed ); if( !sqlite3TestMallocFail() ){ u32 *p = (u32 *)getOsPointer(pRealloc); checkGuards(p); p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD); applyGuards(p); relinkAlloc(p); return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); } return 0; }
/* ** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). The ** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as ** guard-posts. */ static void applyGuards(u32 *p) { int i; char *z; char *zAlloc = (char *)p; /* First set of guard words */ z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)]; for(i=0; i<TESTALLOC_NGUARD; i++){ ((u32 *)z)[i] = 0xdead1122; } /* Second set of guard words */ z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)]; for(i=0; i<TESTALLOC_NGUARD; i++){ static const int guard = 0xdead3344; memcpy(&z[i*sizeof(u32)], &guard, sizeof(u32)); } /* Line number */ z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)]; /* Guard words */ z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)]; memcpy(z, &sqlite3_iLine, sizeof(u32)); /* File name */ z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)]; strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE); z[TESTALLOC_FILESIZE - 1] = '\0'; /* User string */ z = &zAlloc[TESTALLOC_OFFSET_USER(p)]; z[0] = 0; if( sqlite3_malloc_id ){ strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE); z[TESTALLOC_USERSIZE-1] = 0; } /* backtrace() stack */ z = &zAlloc[TESTALLOC_OFFSET_STACK(p)]; backtrace((void **)z, TESTALLOC_STACKFRAMES); /* Sanity check to make sure checkGuards() is working */ checkGuards(p); }