fault.c

Go to the documentation of this file.
00001 /*
00002 ** 2008 Jan 22
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 ** $Id: fault.c,v 1.11 2008/09/02 00:52:52 drh Exp $
00014 */
00015 
00016 /*
00017 ** This file contains code to support the concept of "benign" 
00018 ** malloc failures (when the xMalloc() or xRealloc() method of the
00019 ** sqlite3_mem_methods structure fails to allocate a block of memory
00020 ** and returns 0). 
00021 **
00022 ** Most malloc failures are non-benign. After they occur, SQLite
00023 ** abandons the current operation and returns an error code (usually
00024 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
00025 ** fatal. For example, if a malloc fails while resizing a hash table, this 
00026 ** is completely recoverable simply by not carrying out the resize. The 
00027 ** hash table will continue to function normally.  So a malloc failure 
00028 ** during a hash table resize is a benign fault.
00029 */
00030 
00031 #include "sqliteInt.h"
00032 
00033 #ifndef SQLITE_OMIT_BUILTIN_TEST
00034 
00035 /*
00036 ** Global variables.
00037 */
00038 typedef struct BenignMallocHooks BenignMallocHooks;
00039 static SQLITE_WSD struct BenignMallocHooks {
00040   void (*xBenignBegin)(void);
00041   void (*xBenignEnd)(void);
00042 } sqlite3Hooks = { 0, 0 };
00043 
00044 /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
00045 ** structure.  If writable static data is unsupported on the target,
00046 ** we have to locate the state vector at run-time.  In the more common
00047 ** case where writable static data is supported, wsdHooks can refer directly
00048 ** to the "sqlite3Hooks" state vector declared above.
00049 */
00050 #ifdef SQLITE_OMIT_WSD
00051 # define wsdHooksInit \
00052   BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
00053 # define wsdHooks x[0]
00054 #else
00055 # define wsdHooksInit
00056 # define wsdHooks sqlite3Hooks
00057 #endif
00058 
00059 
00060 /*
00061 ** Register hooks to call when sqlite3BeginBenignMalloc() and
00062 ** sqlite3EndBenignMalloc() are called, respectively.
00063 */
00064 void sqlite3BenignMallocHooks(
00065   void (*xBenignBegin)(void),
00066   void (*xBenignEnd)(void)
00067 ){
00068   wsdHooksInit;
00069   wsdHooks.xBenignBegin = xBenignBegin;
00070   wsdHooks.xBenignEnd = xBenignEnd;
00071 }
00072 
00073 /*
00074 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
00075 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
00076 ** indicates that subsequent malloc failures are non-benign.
00077 */
00078 void sqlite3BeginBenignMalloc(void){
00079   wsdHooksInit;
00080   if( wsdHooks.xBenignBegin ){
00081     wsdHooks.xBenignBegin();
00082   }
00083 }
00084 void sqlite3EndBenignMalloc(void){
00085   wsdHooksInit;
00086   if( wsdHooks.xBenignEnd ){
00087     wsdHooks.xBenignEnd();
00088   }
00089 }
00090 
00091 #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:53 2011 by Doxygen 1.6.1