Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 5b41b603639f3d4032ee7fce53793d926f9f7936 [file] [log] [blame]
drh643167f2008-01-22 21:30:531/*
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*************************************************************************
drh643167f2008-01-22 21:30:5312**
danielk1977ef05f2d2008-06-20 11:05:3713** This file contains code to support the concept of "benign"
danielk19772d1d86f2008-06-20 14:59:5114** 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).
drh643167f2008-01-22 21:30:5317**
danielk19772d1d86f2008-06-20 14:59:5118** 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.
drh643167f2008-01-22 21:30:5325*/
danielk1977ef05f2d2008-06-20 11:05:3726
drh643167f2008-01-22 21:30:5327#include "sqliteInt.h"
28
drhd12602a2016-12-07 15:49:0229#ifndef SQLITE_UNTESTABLE
danielk1977d09414c2008-06-19 18:17:4930
31/*
danielk19772d1d86f2008-06-20 14:59:5132** Global variables.
danielk1977d09414c2008-06-19 18:17:4933*/
drh78f82d12008-09-02 00:52:5234typedef struct BenignMallocHooks BenignMallocHooks;
35static SQLITE_WSD struct BenignMallocHooks {
danielk19772d1d86f2008-06-20 14:59:5136 void (*xBenignBegin)(void);
37 void (*xBenignEnd)(void);
drh78f82d12008-09-02 00:52:5238} 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
danielk1977d09414c2008-06-19 18:17:4955
56/*
danielk19772d1d86f2008-06-20 14:59:5157** Register hooks to call when sqlite3BeginBenignMalloc() and
58** sqlite3EndBenignMalloc() are called, respectively.
danielk1977d09414c2008-06-19 18:17:4959*/
danielk19772d1d86f2008-06-20 14:59:5160void sqlite3BenignMallocHooks(
61 void (*xBenignBegin)(void),
62 void (*xBenignEnd)(void)
63){
drh78f82d12008-09-02 00:52:5264 wsdHooksInit;
65 wsdHooks.xBenignBegin = xBenignBegin;
66 wsdHooks.xBenignEnd = xBenignEnd;
danielk1977d09414c2008-06-19 18:17:4967}
68
danielk19772d1d86f2008-06-20 14:59:5169/*
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.
drh643167f2008-01-22 21:30:5373*/
danielk19772d1d86f2008-06-20 14:59:5174void sqlite3BeginBenignMalloc(void){
drh78f82d12008-09-02 00:52:5275 wsdHooksInit;
76 if( wsdHooks.xBenignBegin ){
77 wsdHooks.xBenignBegin();
danielk19772d1d86f2008-06-20 14:59:5178 }
drh4873d5f2008-05-13 13:27:3379}
danielk19772d1d86f2008-06-20 14:59:5180void sqlite3EndBenignMalloc(void){
drh78f82d12008-09-02 00:52:5281 wsdHooksInit;
82 if( wsdHooks.xBenignEnd ){
83 wsdHooks.xBenignEnd();
danielk19772d1d86f2008-06-20 14:59:5184 }
drh643167f2008-01-22 21:30:5385}
86
drhd12602a2016-12-07 15:49:0287#endif /* #ifndef SQLITE_UNTESTABLE */