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.

219 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. srvgmem.c
  5. Abstract:
  6. This file contains the Global Memory manager API routines
  7. Author:
  8. Steve Wood (stevewo) 29-Oct-1990
  9. Revision History:
  10. --*/
  11. #include "basesrv.h"
  12. #ifdef ENABLE_SHARED_MEMORY
  13. NTSTATUS
  14. BaseSrvInitializeGlobalHeap( VOID )
  15. {
  16. NTSTATUS Status;
  17. LARGE_INTEGER SectionSize;
  18. ULONG ViewSize;
  19. BaseSrvGlobalMemorySize = 4 * 1024; // 4kb
  20. SectionSize.LowPart = BaseSrvGlobalMemorySize;
  21. SectionSize.HighPart = 0;
  22. Status = NtCreateSection( &BaseSrvGlobalSection,
  23. SECTION_ALL_ACCESS,
  24. (POBJECT_ATTRIBUTES) NULL,
  25. &SectionSize,
  26. PAGE_EXECUTE_READWRITE,
  27. SEC_BASED | SEC_RESERVE,
  28. (HANDLE) NULL
  29. );
  30. if (!NT_SUCCESS( Status )) {
  31. return( Status );
  32. }
  33. ViewSize = 0;
  34. BaseSrvGlobalMemoryBase = NULL;
  35. Status = NtMapViewOfSection( BaseSrvGlobalSection,
  36. NtCurrentProcess(),
  37. &BaseSrvGlobalMemoryBase,
  38. 0, // Zerobits?
  39. 0,
  40. NULL,
  41. &ViewSize,
  42. ViewUnmap,
  43. MEM_TOP_DOWN,
  44. PAGE_EXECUTE_READWRITE
  45. );
  46. if (!NT_SUCCESS( Status )) {
  47. NtClose( BaseSrvGlobalSection );
  48. return( Status );
  49. }
  50. BaseSrvGlobalHeap = RtlCreateHeap( HEAP_ZERO_MEMORY,
  51. BaseSrvGlobalMemoryBase,
  52. ViewSize,
  53. 4*1024,
  54. 0,
  55. 0
  56. );
  57. if (BaseSrvGlobalHeap == NULL) {
  58. NtUnmapViewOfSection( NtCurrentProcess(),
  59. BaseSrvGlobalMemoryBase
  60. );
  61. NtClose( BaseSrvGlobalSection );
  62. return( STATUS_UNSUCCESSFUL );
  63. }
  64. #if 0
  65. DbgPrint( " Shared Memory Region: [%lX .. %lX)\n",
  66. BaseSrvGlobalMemoryBase,
  67. (ULONG)BaseSrvGlobalMemoryBase + BaseSrvGlobalMemorySize
  68. );
  69. #endif
  70. return( STATUS_SUCCESS );
  71. }
  72. NTSTATUS
  73. BaseSrvAttachGlobalHeap(
  74. IN HANDLE Process
  75. )
  76. {
  77. NTSTATUS Status;
  78. ULONG ViewSize;
  79. ViewSize = 0;
  80. Status = NtMapViewOfSection( BaseSrvGlobalSection,
  81. Process,
  82. &BaseSrvGlobalMemoryBase,
  83. 0, // Zerobits?
  84. 0,
  85. NULL,
  86. &ViewSize,
  87. ViewUnmap,
  88. 0,
  89. PAGE_EXECUTE_READWRITE
  90. );
  91. return( Status );
  92. }
  93. ULONG
  94. BaseSrvGlobalAlloc(
  95. IN OUT PCSR_API_MSG m,
  96. IN OUT PCSR_REPLY_STATUS ReplyStatus
  97. )
  98. {
  99. PBASE_GLOBALALLOC_MSG a = (PBASE_GLOBALALLOC_MSG)&m->u.ApiMessageData;
  100. PVOID Memory;
  101. PULONG p;
  102. ULONG Flags;
  103. Flags = HEAP_NO_EXCEPTIONS | HEAP_SERIALIZE;
  104. if (a->uFlags & GMEM_ZEROINIT) {
  105. Flags |= HEAP_ZERO_MEMORY;
  106. }
  107. Memory = RtlExAllocateHeap( BaseSrvGlobalHeap, Flags, a->dwBytes );
  108. if (Memory != NULL) {
  109. if (a->uFlags & GMEM_ZEROINIT) {
  110. RtlZeroMemory( Memory, a->dwBytes );
  111. }
  112. #if DBG
  113. else {
  114. p = (PULONG)Memory;
  115. *p = 0xABCDEF01;
  116. }
  117. #endif
  118. }
  119. return( (ULONG)Memory );
  120. ReplyStatus; // get rid of unreferenced parameter warning message
  121. }
  122. ULONG
  123. BaseSrvGlobalReAlloc(
  124. IN OUT PCSR_API_MSG m,
  125. IN OUT PCSR_REPLY_STATUS ReplyStatus
  126. )
  127. {
  128. PBASE_GLOBALREALLOC_MSG a = (PBASE_GLOBALREALLOC_MSG)&m->u.ApiMessageData;
  129. PVOID Memory;
  130. ULONG OldSize;
  131. ULONG Flags;
  132. Flags = HEAP_NO_EXCEPTIONS | HEAP_SERIALIZE;
  133. if (a->uFlags & GMEM_ZEROINIT) {
  134. Flags |= HEAP_ZERO_MEMORY;
  135. }
  136. if (a->uFlags & GMEM_MOVEABLE) {
  137. Flags |= HEAP_GROWTH_ALLOWED;
  138. }
  139. if (a->uFlags & GMEM_MODIFY) {
  140. Memory = a->hMem;
  141. }
  142. else {
  143. Memory = RtlExReAllocateHeap( BaseSrvGlobalHeap, Flags, a->hMem, a->dwBytes );
  144. }
  145. return( (ULONG)Memory );
  146. ReplyStatus; // get rid of unreferenced parameter warning message
  147. }
  148. ULONG
  149. BaseSrvGlobalSize(
  150. IN OUT PCSR_API_MSG m,
  151. IN OUT PCSR_REPLY_STATUS ReplyStatus
  152. )
  153. {
  154. PBASE_GLOBALSIZE_MSG a = (PBASE_GLOBALSIZE_MSG)&m->u.ApiMessageData;
  155. ULONG Flags;
  156. Flags = HEAP_NO_EXCEPTIONS | HEAP_SERIALIZE;
  157. return( RtlExSizeHeap( BaseSrvGlobalHeap, Flags, a->hMem ) );
  158. ReplyStatus; // get rid of unreferenced parameter warning message
  159. }
  160. ULONG
  161. BaseSrvGlobalFlags(
  162. IN OUT PCSR_API_MSG m,
  163. IN OUT PCSR_REPLY_STATUS ReplyStatus
  164. )
  165. {
  166. PBASE_GLOBALFLAGS_MSG a = (PBASE_GLOBALFLAGS_MSG)&m->u.ApiMessageData;
  167. return( GMEM_DDESHARE );
  168. ReplyStatus; // get rid of unreferenced parameter warning message
  169. }
  170. ULONG
  171. BaseSrvGlobalFree(
  172. IN OUT PCSR_API_MSG m,
  173. IN OUT PCSR_REPLY_STATUS ReplyStatus
  174. )
  175. {
  176. PBASE_GLOBALFREE_MSG a = (PBASE_GLOBALFREE_MSG)&m->u.ApiMessageData;
  177. ULONG Flags;
  178. Flags = HEAP_NO_EXCEPTIONS | HEAP_SERIALIZE;
  179. return( (ULONG)RtlExFreeHeap( BaseSrvGlobalHeap, Flags, a->hMem ) );
  180. ReplyStatus; // get rid of unreferenced parameter warning message
  181. }
  182. #endif // ENABLE_SHARED_MEMORY