Source code of Windows XP (NT5)
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.

206 lines
4.5 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. res.c
  5. Abstract:
  6. This module implements Win32 Resource Manager APIs
  7. Author:
  8. Rob Earhart (Earhart) 04-Apr-2001
  9. Revision History:
  10. --*/
  11. #include "basedll.h"
  12. //
  13. // N.B. This is a stub implementation intended to provide basic
  14. // resource management interfaces for applications which care
  15. // about their memory usage. It is NOT the final version of the
  16. // usermode side of the resource manager.
  17. //
  18. //
  19. // Globals used by the routines in this module
  20. //
  21. const WCHAR BasepMmLowMemoryConditionEventName[] = L"\\KernelObjects\\LowMemoryCondition";
  22. const WCHAR BasepMmHighMemoryConditionEventName[] = L"\\KernelObjects\\HighMemoryCondition";
  23. HANDLE
  24. APIENTRY
  25. CreateMemoryResourceNotification(
  26. IN MEMORY_RESOURCE_NOTIFICATION_TYPE NotificationType
  27. )
  28. /*++
  29. Routine Description:
  30. Creates a memory resource notification handle. Memory resource
  31. notification handles monitor memory for changes, and are used
  32. to query information about memory.
  33. Arguments:
  34. NotificationType -- the type of notification requested
  35. Return Value:
  36. Non-NULL - a handle to the new subscription object.
  37. NULL - The operation failed. Extended error status is available
  38. using GetLastError.
  39. --*/
  40. {
  41. LPCWSTR EventName;
  42. HANDLE Handle;
  43. OBJECT_ATTRIBUTES Obja;
  44. UNICODE_STRING ObjectName;
  45. NTSTATUS Status;
  46. //
  47. // Determine the event in which the caller's interested.
  48. //
  49. switch (NotificationType) {
  50. case LowMemoryResourceNotification:
  51. //
  52. // It's the low memory condition event
  53. //
  54. EventName = BasepMmLowMemoryConditionEventName;
  55. break;
  56. case HighMemoryResourceNotification:
  57. //
  58. // It's the high memory condition event
  59. //
  60. EventName = BasepMmHighMemoryConditionEventName;
  61. break;
  62. default:
  63. //
  64. // Not one of our known event-of-interest types; all we can do
  65. // is indicate an invalid parameter, and return a failure
  66. // condition.
  67. //
  68. SetLastError(ERROR_INVALID_PARAMETER);
  69. return NULL;
  70. }
  71. //
  72. // Attempt the actual event open
  73. //
  74. RtlInitUnicodeString(&ObjectName, EventName);
  75. InitializeObjectAttributes(&Obja,
  76. &ObjectName,
  77. 0,
  78. NULL,
  79. NULL);
  80. Status = NtOpenEvent(&Handle,
  81. SYNCHRONIZE | EVENT_QUERY_STATE,
  82. &Obja);
  83. if (! NT_SUCCESS(Status)) {
  84. //
  85. // We failed to open the event for some reason.
  86. //
  87. BaseSetLastNTError(Status);
  88. return NULL;
  89. }
  90. //
  91. // Otherwise, we have the handle, so we're all set; just return it.
  92. //
  93. return Handle;
  94. }
  95. BOOL
  96. APIENTRY
  97. QueryMemoryResourceNotification(
  98. IN HANDLE ResourceNotificationHandle,
  99. IN PBOOL ResourceState
  100. )
  101. /*++
  102. Routine Description:
  103. Query a memory resource notification handle for information about
  104. the associated memory resources.
  105. Arguments:
  106. ResourceNotificationHandle - a handle to the memory resource
  107. notification to query.
  108. ResourceState - location to put the information about the memory
  109. resource
  110. Return Value:
  111. TRUE - The query succeeded.
  112. FALSE - The query failed. Extended error status is available
  113. using GetLastError.
  114. --*/
  115. {
  116. EVENT_BASIC_INFORMATION EventInfo;
  117. NTSTATUS Status;
  118. //
  119. // Check parameter validity
  120. //
  121. if (! ResourceNotificationHandle
  122. || ResourceNotificationHandle == INVALID_HANDLE_VALUE
  123. || ! ResourceState) {
  124. SetLastError(ERROR_INVALID_PARAMETER);
  125. return FALSE;
  126. }
  127. //
  128. // Get the event's current state
  129. //
  130. Status = NtQueryEvent(ResourceNotificationHandle,
  131. EventBasicInformation,
  132. &EventInfo,
  133. sizeof(EventInfo),
  134. NULL);
  135. if (! NT_SUCCESS(Status)) {
  136. //
  137. // On failure, set the last NT error and indicate the failure
  138. // condition to our caller.
  139. //
  140. BaseSetLastNTError(Status);
  141. return FALSE;
  142. }
  143. //
  144. // Fill in the state
  145. //
  146. *ResourceState = (EventInfo.EventState == 1);
  147. //
  148. // And we're done -- return success to our caller.
  149. //
  150. return TRUE;
  151. }