From 468690ef60ed5960a7e523327cd6c8acf001184d Mon Sep 17 00:00:00 2001 From: mistachkin Date: Wed, 24 Aug 2011 17:42:22 +0000 Subject: [PATCH] Add error logging to native Win32 heap support. FossilOrigin-Name: 7fca5a284cded6d7531060da6e99a57aed50cf8f --- Makefile.msc | 1 + manifest | 17 +++++------- manifest.uuid | 2 +- src/os_win.c | 77 ++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 63 insertions(+), 34 deletions(-) diff --git a/Makefile.msc b/Makefile.msc index 9617281cb8..6664b68b1f 100644 --- a/Makefile.msc +++ b/Makefile.msc @@ -52,6 +52,7 @@ TCC = $(TCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS # Use native Win32 heap. # TCC = $(TCC) -DSQLITE_WIN32_MALLOC=1 +# TCC = $(TCC) -DSQLITE_WIN32_MALLOC_VALIDATE=1 # The locations of the Tcl header and library files. Also, the library that # non-stubs enabled programs using Tcl must link against. These variables diff --git a/manifest b/manifest index 1cc1391048..dc8a476ccd 100644 --- a/manifest +++ b/manifest @@ -1,9 +1,9 @@ -C Experimental\swork\sto\sallow\sSQLite\sto\suse\sthe\snative\sWin32\sheap\sAPI. -D 2011-08-24T16:13:57.329 +C Add\serror\slogging\sto\snative\sWin32\sheap\ssupport. +D 2011-08-24T17:42:22.399 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 8c930e7b493d59099ea1304bd0f2aed152eb3315 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 -F Makefile.msc 8ebd89ed4493641a9e2316a941d776e2bcc5ad39 +F Makefile.msc baf6b7037a8e0838387f8bfc9baacfcc57dc5902 F Makefile.vxworks c85ec1d8597fe2f7bc225af12ac1666e21379151 F README cd04a36fbc7ea56932a4052d7d0b7f09f27c33d6 F VERSION f724de7326e87b7f3b0a55f16ef4b4d993680d54 @@ -166,7 +166,7 @@ F src/os.h 9dbed8c2b9c1f2f2ebabc09e49829d4777c26bf9 F src/os_common.h 65a897143b64667d23ed329a7984b9b405accb58 F src/os_os2.c 4a75888ba3dfc820ad5e8177025972d74d7f2440 F src/os_unix.c 81341980c52a44106b10c1e28a0d5c5247476452 -F src/os_win.c a9950bf0f32753418ab6f74417aebd432bde2383 +F src/os_win.c f9958ef76430472cd3b43ec45643949b2876daa4 F src/pager.c 120550e7ef01dafaa2cbb4a0528c0d87c8f12b41 F src/pager.h 3f8c783de1d4706b40b1ac15b64f5f896bcc78d1 F src/parse.y 12b7ebd61ea54f0e1b1083ff69cc2c8ce9353d58 @@ -961,10 +961,7 @@ F tool/symbols.sh caaf6ccc7300fd43353318b44524853e222557d5 F tool/tostr.awk 11760e1b94a5d3dcd42378f3cc18544c06cfa576 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings.sh b7fdb2cc525f5ef4fa43c80e771636dd3690f9d2 -P 988998fe7b0a21ed113b67f812e51f357045bef4 -R 0396c522791d26a46580a50de4d03044 -T *branch * winNativeHeap -T *sym-winNativeHeap * -T -sym-trunk * +P bf3d0ab53829350637283442f75071fe6d925245 +R 0f7433104155ca1c8873be3fedc86cf8 U mistachkin -Z 6f33f4bfa3c296caae6e295d0a3da4b8 +Z 3b4d0724e0585445008e0568091ed65d diff --git a/manifest.uuid b/manifest.uuid index e1ad382845..eaeeb0699e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -bf3d0ab53829350637283442f75071fe6d925245 \ No newline at end of file +7fca5a284cded6d7531060da6e99a57aed50cf8f \ No newline at end of file diff --git a/src/os_win.c b/src/os_win.c index 73db98ad2d..e2e606ee18 100644 --- a/src/os_win.c +++ b/src/os_win.c @@ -154,6 +154,9 @@ struct winMemData { static struct winMemData win_mem_data = { WINMEM_MAGIC, NULL, FALSE }; +#define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC ) +#define winMemGetHeap() win_mem_data.hHeap + static void *winMemMalloc(int nBytes); static void winMemFree(void *pPrior); static void *winMemRealloc(void *pPrior, int nBytes); @@ -222,16 +225,22 @@ static int sqlite3_os_type = 0; */ static void *winMemMalloc(int nBytes){ HANDLE hHeap; + void *p; - assert( win_mem_data.magic==WINMEM_MAGIC ); - hHeap = win_mem_data.hHeap; + winMemAssertMagic(); + hHeap = winMemGetHeap(); assert( hHeap!=0 ); assert( hHeap!=INVALID_HANDLE_VALUE ); #ifdef SQLITE_WIN32_MALLOC_VALIDATE assert ( HeapValidate(hHeap, 0, NULL) ); #endif assert( nBytes>=0 ); - return HeapAlloc(hHeap, 0, (SIZE_T)nBytes); + p = HeapAlloc(hHeap, 0, (SIZE_T)nBytes); + if( !p ){ + sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p", + nBytes, GetLastError(), (void*)hHeap); + } + return p; } /* @@ -240,15 +249,18 @@ static void *winMemMalloc(int nBytes){ static void winMemFree(void *pPrior){ HANDLE hHeap; - assert( win_mem_data.magic==WINMEM_MAGIC ); - hHeap = win_mem_data.hHeap; + winMemAssertMagic(); + hHeap = winMemGetHeap(); assert( hHeap!=0 ); assert( hHeap!=INVALID_HANDLE_VALUE ); #ifdef SQLITE_WIN32_MALLOC_VALIDATE assert ( HeapValidate(hHeap, 0, pPrior) ); #endif - if (!pPrior) return; /* Passing NULL to HeapFree is undefined. */ - HeapFree(hHeap, 0, pPrior); + if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */ + if( !HeapFree(hHeap, 0, pPrior) ){ + sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p", + pPrior, GetLastError(), (void*)hHeap); + } } /* @@ -256,17 +268,27 @@ static void winMemFree(void *pPrior){ */ static void *winMemRealloc(void *pPrior, int nBytes){ HANDLE hHeap; + void *p; - assert( win_mem_data.magic==WINMEM_MAGIC ); - hHeap = win_mem_data.hHeap; + winMemAssertMagic(); + hHeap = winMemGetHeap(); assert( hHeap!=0 ); assert( hHeap!=INVALID_HANDLE_VALUE ); #ifdef SQLITE_WIN32_MALLOC_VALIDATE assert ( HeapValidate(hHeap, 0, pPrior) ); #endif assert( nBytes>=0 ); - if (!pPrior) return HeapAlloc(hHeap, 0, (SIZE_T)nBytes); - return HeapReAlloc(hHeap, 0, pPrior, (SIZE_T)nBytes); + if( !pPrior ){ + p = HeapAlloc(hHeap, 0, (SIZE_T)nBytes); + }else{ + p = HeapReAlloc(hHeap, 0, pPrior, (SIZE_T)nBytes); + } + if( !p ){ + sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p", + pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, GetLastError(), + (void*)hHeap); + } + return p; } /* @@ -276,16 +298,20 @@ static int winMemSize(void *p){ HANDLE hHeap; SIZE_T n; - assert( win_mem_data.magic==WINMEM_MAGIC ); - hHeap = win_mem_data.hHeap; + winMemAssertMagic(); + hHeap = winMemGetHeap(); assert( hHeap!=0 ); assert( hHeap!=INVALID_HANDLE_VALUE ); #ifdef SQLITE_WIN32_MALLOC_VALIDATE assert ( HeapValidate(hHeap, 0, NULL) ); #endif - if (!p) return 0; + if( !p ) return 0; n = HeapSize(hHeap, 0, p); - assert( n<=INT_MAX ); + if( n==(SIZE_T)-1 ){ + sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p", + p, GetLastError(), (void*)hHeap); + return 0; + } return (int)n; } @@ -302,12 +328,16 @@ static int winMemRoundup(int n){ static int winMemInit(void *pAppData){ winMemData *pWinMemData = (winMemData *)pAppData; - if (!pWinMemData) return SQLITE_ERROR; + if( !pWinMemData ) return SQLITE_ERROR; assert( pWinMemData->magic==WINMEM_MAGIC ); - if (!pWinMemData->hHeap){ + if( !pWinMemData->hHeap ){ pWinMemData->hHeap = HeapCreate(0, SQLITE_WIN32_HEAP_INIT_SIZE, SQLITE_WIN32_HEAP_MAX_SIZE); - if (!pWinMemData->hHeap){ + if( !pWinMemData->hHeap ){ + sqlite3_log(SQLITE_NOMEM, + "failed to HeapCreate (%d), initSize=%u, maxSize=%u", + GetLastError(), SQLITE_WIN32_HEAP_INIT_SIZE, + SQLITE_WIN32_HEAP_MAX_SIZE); return SQLITE_NOMEM; } pWinMemData->bOwned = TRUE; @@ -326,15 +356,16 @@ static int winMemInit(void *pAppData){ static void winMemShutdown(void *pAppData){ winMemData *pWinMemData = (winMemData *)pAppData; - if (!pWinMemData) return; - if (pWinMemData->hHeap){ + if( !pWinMemData ) return; + if( pWinMemData->hHeap ){ assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); #ifdef SQLITE_WIN32_MALLOC_VALIDATE assert( HeapValidate(pWinMemData->hHeap, 0, NULL) ); #endif - if (pWinMemData->bOwned){ - if (!HeapDestroy(pWinMemData->hHeap)){ - /* TODO: Log this? */ + if( pWinMemData->bOwned ){ + if( !HeapDestroy(pWinMemData->hHeap) ){ + sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p", + GetLastError(), (void*)pWinMemData->hHeap); } pWinMemData->bOwned = FALSE; }