drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 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 | ************************************************************************* |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 12 | ** |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 13 | ** This file contains code to support the concept of "benign" |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 14 | ** malloc failures (when the xMalloc() or xRealloc() method of the |
| 15 | ** sqlite3_mem_methods structure fails to allocate a block of memory |
| 16 | ** and returns 0). |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 17 | ** |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 18 | ** Most malloc failures are non-benign. After they occur, SQLite |
| 19 | ** abandons the current operation and returns an error code (usually |
| 20 | ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily |
| 21 | ** fatal. For example, if a malloc fails while resizing a hash table, this |
| 22 | ** is completely recoverable simply by not carrying out the resize. The |
| 23 | ** hash table will continue to function normally. So a malloc failure |
| 24 | ** during a hash table resize is a benign fault. |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 25 | */ |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 26 | |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 27 | #include "sqliteInt.h" |
| 28 | |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 29 | #ifndef SQLITE_OMIT_BUILTIN_TEST |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 30 | |
| 31 | /* |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 32 | ** Global variables. |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 33 | */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 34 | typedef struct BenignMallocHooks BenignMallocHooks; |
| 35 | static SQLITE_WSD struct BenignMallocHooks { |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 36 | void (*xBenignBegin)(void); |
| 37 | void (*xBenignEnd)(void); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 38 | } sqlite3Hooks = { 0, 0 }; |
| 39 | |
| 40 | /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks |
| 41 | ** structure. If writable static data is unsupported on the target, |
| 42 | ** we have to locate the state vector at run-time. In the more common |
| 43 | ** case where writable static data is supported, wsdHooks can refer directly |
| 44 | ** to the "sqlite3Hooks" state vector declared above. |
| 45 | */ |
| 46 | #ifdef SQLITE_OMIT_WSD |
| 47 | # define wsdHooksInit \ |
| 48 | BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks) |
| 49 | # define wsdHooks x[0] |
| 50 | #else |
| 51 | # define wsdHooksInit |
| 52 | # define wsdHooks sqlite3Hooks |
| 53 | #endif |
| 54 | |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 55 | |
| 56 | /* |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 57 | ** Register hooks to call when sqlite3BeginBenignMalloc() and |
| 58 | ** sqlite3EndBenignMalloc() are called, respectively. |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 59 | */ |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 60 | void sqlite3BenignMallocHooks( |
| 61 | void (*xBenignBegin)(void), |
| 62 | void (*xBenignEnd)(void) |
| 63 | ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 64 | wsdHooksInit; |
| 65 | wsdHooks.xBenignBegin = xBenignBegin; |
| 66 | wsdHooks.xBenignEnd = xBenignEnd; |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 67 | } |
| 68 | |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 69 | /* |
| 70 | ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that |
| 71 | ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc() |
| 72 | ** indicates that subsequent malloc failures are non-benign. |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 73 | */ |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 74 | void sqlite3BeginBenignMalloc(void){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 75 | wsdHooksInit; |
| 76 | if( wsdHooks.xBenignBegin ){ |
| 77 | wsdHooks.xBenignBegin(); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 78 | } |
drh | 4873d5f | 2008-05-13 13:27:33 +0000 | [diff] [blame] | 79 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 80 | void sqlite3EndBenignMalloc(void){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 81 | wsdHooksInit; |
| 82 | if( wsdHooks.xBenignEnd ){ |
| 83 | wsdHooks.xBenignEnd(); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 84 | } |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 85 | } |
| 86 | |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 87 | #endif /* #ifndef SQLITE_OMIT_BUILTIN_TEST */ |