fault.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. ** This file contains code to support the concept of "benign"
  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).
  17. **
  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.
  25. */
  26. #include "sqliteInt.h"
  27. #ifndef SQLITE_OMIT_BUILTIN_TEST
  28. /*
  29. ** Global variables.
  30. */
  31. typedef struct BenignMallocHooks BenignMallocHooks;
  32. static SQLITE_WSD struct BenignMallocHooks {
  33. void (*xBenignBegin)(void);
  34. void (*xBenignEnd)(void);
  35. } sqlite3Hooks = { 0, 0 };
  36. /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
  37. ** structure. If writable static data is unsupported on the target,
  38. ** we have to locate the state vector at run-time. In the more common
  39. ** case where writable static data is supported, wsdHooks can refer directly
  40. ** to the "sqlite3Hooks" state vector declared above.
  41. */
  42. #ifdef SQLITE_OMIT_WSD
  43. # define wsdHooksInit \
  44. BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
  45. # define wsdHooks x[0]
  46. #else
  47. # define wsdHooksInit
  48. # define wsdHooks sqlite3Hooks
  49. #endif
  50. /*
  51. ** Register hooks to call when sqlite3BeginBenignMalloc() and
  52. ** sqlite3EndBenignMalloc() are called, respectively.
  53. */
  54. void sqlite3BenignMallocHooks(
  55. void (*xBenignBegin)(void),
  56. void (*xBenignEnd)(void)
  57. ){
  58. wsdHooksInit;
  59. wsdHooks.xBenignBegin = xBenignBegin;
  60. wsdHooks.xBenignEnd = xBenignEnd;
  61. }
  62. /*
  63. ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
  64. ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
  65. ** indicates that subsequent malloc failures are non-benign.
  66. */
  67. void sqlite3BeginBenignMalloc(void){
  68. wsdHooksInit;
  69. if( wsdHooks.xBenignBegin ){
  70. wsdHooks.xBenignBegin();
  71. }
  72. }
  73. void sqlite3EndBenignMalloc(void){
  74. wsdHooksInit;
  75. if( wsdHooks.xBenignEnd ){
  76. wsdHooks.xBenignEnd();
  77. }
  78. }
  79. #endif /* #ifndef SQLITE_OMIT_BUILTIN_TEST */