Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.5 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // memory management stuff
  4. //
  5. // 22-Nov-1999 sburns (refactored)
  6. #include "headers.hxx"
  7. //
  8. // Debug and Retail build
  9. //
  10. static const int RES_STRING_SIZE = 512;
  11. static TCHAR LOW_MEMORY_MESSAGE[RES_STRING_SIZE];
  12. static TCHAR LOW_MEMORY_TITLE[RES_STRING_SIZE];
  13. // we declare a static instance of bad_alloc so that the loader allocates
  14. // space for it. Otherwise, we'd have to allocate an instance during failure
  15. // of operator new, which we obviously can't do, since we're out-of-memory at
  16. // that point.
  17. static const std::bad_alloc nomem;
  18. // Called when OperatorNew cannot satisfy an allocation request.
  19. //
  20. // Brings up a dialog to inform the user to free up some memory then retry the
  21. // allocation, or cancel. The dialog can appear in low-memory conditions.
  22. //
  23. // Returns IDRETRY if the allocation should be re-attempted, IDCANCEL
  24. // otherwise. Returns IDCANCEL if the module resource handle has not been
  25. // set (see burnslib.hpp).
  26. int
  27. DoLowMemoryDialog()
  28. {
  29. static bool initialized = false;
  30. if (!initialized)
  31. {
  32. HINSTANCE hinstance = GetResourceModuleHandle();
  33. if (!hinstance)
  34. {
  35. // DLL/WinMain has not set the handle. So just throw.
  36. return IDCANCEL;
  37. }
  38. // This will work, even in low memory, as it just returns a pointer
  39. // to the string in the exe image.
  40. int result1 =
  41. // REVIEWED-2002/03/06-sburns correct character count passed
  42. ::LoadString(
  43. hinstance,
  44. IDS_LOW_MEMORY_MESSAGE,
  45. LOW_MEMORY_MESSAGE,
  46. RES_STRING_SIZE);
  47. int result2 =
  48. // REVIEWED-2002/03/06-sburns correct character count passed
  49. ::LoadString(
  50. hinstance,
  51. IDS_LOW_MEMORY_TITLE,
  52. LOW_MEMORY_TITLE,
  53. RES_STRING_SIZE);
  54. if (result1 && result2)
  55. {
  56. initialized = true;
  57. }
  58. }
  59. return
  60. ::MessageBox(
  61. ::GetDesktopWindow(),
  62. LOW_MEMORY_MESSAGE,
  63. LOW_MEMORY_TITLE,
  64. // ICONHAND + SYSTEMMODAL gets us the special low-memory
  65. // message box.
  66. MB_RETRYCANCEL | MB_ICONHAND | MB_SYSTEMMODAL);
  67. }
  68. // Include the code for the debug or retail variations of the replacement
  69. // operator new and delete
  70. #ifdef DBG
  71. // debug code
  72. #include "heapdbg.cpp"
  73. #else
  74. // retail code
  75. #include "heapretl.cpp"
  76. #endif