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.

218 lines
4.9 KiB

  1. //
  2. // SHM.C
  3. // Shared Memory Manager
  4. //
  5. // Copyright(c) Microsoft 1997-
  6. //
  7. #include <as16.h>
  8. //
  9. // SHM_StartAccess
  10. //
  11. LPVOID SHM_StartAccess(int block)
  12. {
  13. LPBUFFER_CONTROL pControl;
  14. LPVOID pMemBlock;
  15. DebugEntry(SHM_StartAccess);
  16. //
  17. // Test for shared memory present
  18. //
  19. ASSERT(g_asSharedMemory != NULL);
  20. //
  21. // Determine which data block we are handling...
  22. //
  23. switch (block)
  24. {
  25. case SHM_OA_DATA:
  26. pControl = &g_asSharedMemory->displayToCore;
  27. break;
  28. case SHM_OA_FAST:
  29. case SHM_BA_FAST:
  30. case SHM_CM_FAST:
  31. pControl = &g_asSharedMemory->fastPath;
  32. break;
  33. default:
  34. ERROR_OUT(("Unknown type %d", block));
  35. break;
  36. }
  37. //
  38. // Mark the double-buffer as busy.
  39. //
  40. pControl->busyFlag = TRUE;
  41. //
  42. // Set up the current buffer pointer if this is the first access to the
  43. // shared memory.
  44. //
  45. pControl->indexCount++;
  46. if (pControl->indexCount == 1)
  47. {
  48. //
  49. // Set up the 'in use' buffer pointer
  50. //
  51. pControl->currentBuffer = pControl->newBuffer;
  52. //
  53. // Mark the buffer as busy so that the Share Core knows where we
  54. // are.
  55. //
  56. pControl->bufferBusy[pControl->currentBuffer] = 1;
  57. }
  58. //
  59. // Get the pointer to the block to return
  60. //
  61. switch (block)
  62. {
  63. case SHM_OA_DATA:
  64. pMemBlock = g_poaData[pControl->currentBuffer];
  65. break;
  66. case SHM_OA_FAST:
  67. pMemBlock = &(g_asSharedMemory->oaFast[pControl->currentBuffer]);
  68. break;
  69. case SHM_BA_FAST:
  70. pMemBlock = &(g_asSharedMemory->baFast[pControl->currentBuffer]);
  71. break;
  72. case SHM_CM_FAST:
  73. pMemBlock = &(g_asSharedMemory->cmFast[pControl->currentBuffer]);
  74. break;
  75. default:
  76. ERROR_OUT(("Unknown type %d", block));
  77. break;
  78. }
  79. DebugExitDWORD(SHM_StartAccess, (DWORD)pMemBlock);
  80. return(pMemBlock);
  81. }
  82. //
  83. // SHM_StopAccess
  84. //
  85. void SHM_StopAccess(int block)
  86. {
  87. LPBUFFER_CONTROL pControl;
  88. DebugEntry(SHM_StopAccess);
  89. ASSERT(g_asSharedMemory != NULL);
  90. //
  91. // Determine which data block we are handling...
  92. //
  93. switch (block)
  94. {
  95. case SHM_OA_DATA:
  96. pControl = &g_asSharedMemory->displayToCore;
  97. break;
  98. case SHM_OA_FAST:
  99. case SHM_BA_FAST:
  100. case SHM_CM_FAST:
  101. pControl = &g_asSharedMemory->fastPath;
  102. break;
  103. default:
  104. ERROR_OUT(("Unknown type %d", block));
  105. break;
  106. }
  107. //
  108. // Decrement usage count - if we have finally finished with the memory,
  109. // clear the busy flags so that the Share Core knows it won't tread on
  110. // the display driver's toes.
  111. //
  112. pControl->indexCount--;
  113. if (pControl->indexCount == 0)
  114. {
  115. BOOL fPulseLock;
  116. //
  117. // If this is the order heap, and it is more than half full,
  118. // strobe the win16lock so the core has a chance to run and pick up
  119. // the pending orders. This will NOT cause interthread sends to
  120. // get received on this guy.
  121. //
  122. fPulseLock = FALSE;
  123. if (block == SHM_OA_DATA)
  124. {
  125. LPOA_SHARED_DATA pMemBlock = g_poaData[pControl->currentBuffer];
  126. ASSERT(pMemBlock);
  127. if (pMemBlock->totalOrderBytes >=
  128. ((g_oaFlow == OAFLOW_FAST ? OA_FAST_HEAP : OA_SLOW_HEAP) / 2))
  129. {
  130. fPulseLock = TRUE;
  131. TRACE_OUT(("Pulsing Win16lock since order heap size %08ld is getting full",
  132. pMemBlock->totalOrderBytes));
  133. }
  134. }
  135. pControl->bufferBusy[pControl->currentBuffer] = 0;
  136. pControl->busyFlag = 0;
  137. if (fPulseLock)
  138. {
  139. _LeaveWin16Lock();
  140. _EnterWin16Lock();
  141. TRACE_OUT(("Done pulsing Win16lock to flush order heap"));
  142. }
  143. }
  144. DebugExitVOID(SHM_StopAccess);
  145. }
  146. #ifdef _DEBUG
  147. //
  148. // SHM_CheckPointer - see shm.h
  149. //
  150. void SHM_CheckPointer(LPVOID ptr)
  151. {
  152. DebugEntry(SHMCheckPointer);
  153. //
  154. // Is it even accessible?
  155. //
  156. ASSERT(!IsBadWritePtr(ptr, 4));
  157. //
  158. // Is it in the proper range? NOTE--our shared memory is not one
  159. // contiguous block. Therefore we need to determine which chunk it
  160. // is in. Since each chunk already has a limit built in, we just
  161. // need to make sure the selector is cool.
  162. //
  163. ASSERT(g_asSharedMemory);
  164. ASSERT(g_poaData[0]);
  165. ASSERT(g_poaData[1]);
  166. if ((SELECTOROF(ptr) != SELECTOROF(g_asSharedMemory)) &&
  167. (SELECTOROF(ptr) != SELECTOROF(g_poaData[0])) &&
  168. (SELECTOROF(ptr) != SELECTOROF(g_poaData[1])))
  169. {
  170. ERROR_OUT(("Pointer not in any shared memory block"));
  171. }
  172. DebugExitVOID(SHM_CheckPointer);
  173. }
  174. #endif // _DEBUG