xref: /sqlite-3.40.0/src/fault.c (revision 2d1d86fb)
1 /*
2 ** 2008 Jan 22
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 ** $Id: fault.c,v 1.9 2008/06/20 14:59:51 danielk1977 Exp $
14 */
15 
16 /*
17 ** This file contains code to support the concept of "benign"
18 ** malloc failures (when the xMalloc() or xRealloc() method of the
19 ** sqlite3_mem_methods structure fails to allocate a block of memory
20 ** and returns 0).
21 **
22 ** Most malloc failures are non-benign. After they occur, SQLite
23 ** abandons the current operation and returns an error code (usually
24 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
25 ** fatal. For example, if a malloc fails while resizing a hash table, this
26 ** is completely recoverable simply by not carrying out the resize. The
27 ** hash table will continue to function normally.  So a malloc failure
28 ** during a hash table resize is a benign fault.
29 */
30 
31 #include "sqliteInt.h"
32 
33 #ifndef SQLITE_OMIT_BUILTIN_TEST
34 
35 /*
36 ** Global variables.
37 */
38 static struct BenignMallocHooks {
39   void (*xBenignBegin)(void);
40   void (*xBenignEnd)(void);
41 } hooks;
42 
43 /*
44 ** Register hooks to call when sqlite3BeginBenignMalloc() and
45 ** sqlite3EndBenignMalloc() are called, respectively.
46 */
47 void sqlite3BenignMallocHooks(
48   void (*xBenignBegin)(void),
49   void (*xBenignEnd)(void)
50 ){
51   hooks.xBenignBegin = xBenignBegin;
52   hooks.xBenignEnd = xBenignEnd;
53 }
54 
55 /*
56 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
57 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
58 ** indicates that subsequent malloc failures are non-benign.
59 */
60 void sqlite3BeginBenignMalloc(void){
61   if( hooks.xBenignBegin ){
62     hooks.xBenignBegin();
63   }
64 }
65 void sqlite3EndBenignMalloc(void){
66   if( hooks.xBenignEnd ){
67     hooks.xBenignEnd();
68   }
69 }
70 
71 #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
72 
73