sharemem.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // File: sharemem.h
  3. //
  4. // Descrition:
  5. // This file contains structure information and a class definition
  6. // for managing shared memory between a test tool and a perfmon DLL.
  7. // Code for the class can be found in sharemem.cpp. This file
  8. // does NOT need to be modified if customizing this code to your
  9. // personalized test tool.
  10. //
  11. // History:
  12. // February 20, 1997 jfink Created file.
  13. //
  14. #ifndef _SHAREMEM_H_
  15. #define _SHAREMEM_H_
  16. //
  17. // An instance element in the shared memory.
  18. //
  19. #define MAX_BYTES_FOR_INSTANCE_COUNTERS 192
  20. #define MAX_INSTANCE_NAME_LENGTH 32
  21. #define DWORD_NULL ((DWORD) -1)
  22. #define ALIGN_ON_QWORD(x) ((VOID *)( ((DWORD) x & 0x00000007) ? ( ((DWORD) x & 0xFFFFFFF8) + 8 ) : ( (DWORD) x ) ))
  23. typedef struct _SHARE_INSTANCE
  24. {
  25. WCHAR wszInstanceName[MAX_INSTANCE_NAME_LENGTH + 4];
  26. DWORD dwFirstCounter;
  27. BYTE Data[MAX_BYTES_FOR_INSTANCE_COUNTERS];
  28. DWORD dwDataSize;
  29. DWORD dwNextInList;
  30. struct _SHARE_INSTANCE * pNextUsedByClient;
  31. } SHARE_INSTANCE, *PSHARE_INSTANCE;
  32. //
  33. // The header information for the shared memory.
  34. //
  35. typedef struct _SHARE_HEADER
  36. {
  37. DWORD dwNumberOfInstances;
  38. DWORD dwInstanceHeader;
  39. DWORD dwFreeHeader;
  40. } SHARE_HEADER, *PSHARE_HEADER;
  41. //
  42. // Constant definitions.
  43. //
  44. #define MAX_INSTANCES_OF_ALL_SHARES 1000
  45. #define SHARE_SIZE (sizeof(SHARE_HEADER) + \
  46. MAX_INSTANCES_OF_ALL_SHARES * \
  47. sizeof(SHARE_INSTANCE))
  48. #define FIRST_COUNTERA "First Counter"
  49. #define FIRST_COUNTERW L"First Counter"
  50. #define FIRST_COUNTER TEXT("First Counter")
  51. #define SHARE_FILE_NAME TEXT("PerfDLLShareFile")
  52. #define SHARE_MUTEX_NAME TEXT("PerfDLLShareMutex")
  53. #define SHARE_MUTEX_TIMEOUT (1000L)
  54. //
  55. // The class for managing the shared memory. Documentation follows.
  56. //
  57. class CPerfShare
  58. {
  59. private:
  60. HANDLE mhSharedMemory;
  61. HANDLE mhSharedMutex;
  62. PSHARE_HEADER mpShareHeader;
  63. PSHARE_INSTANCE mpShareInstance;
  64. LONG mlLockCount;
  65. BOOL InitializeHeader();
  66. BOOL GetFirstCounterValue( PCHAR szServiceName,
  67. PDWORD pdwFirst);
  68. BOOL GetFirstCounterValue( PWCHAR wszServiceName,
  69. PDWORD pdwFirstCounter);
  70. BOOL GetFirstCounterValueA(PCHAR szServiceName,
  71. PDWORD pdwFirstCounter);
  72. BOOL GetFirstCounterValueW(PWCHAR wszServiceName,
  73. PDWORD pdwFirstCounter);
  74. PVOID AllocateLocalCounters(DWORD dwDataSize);
  75. PVOID AllocateCounters(PWCHAR wszInstanceName, DWORD dwFirstCounter,
  76. DWORD dwDataSize);
  77. public:
  78. CPerfShare();
  79. ~CPerfShare();
  80. BOOL Initialize(BOOL bReadOnly = FALSE);
  81. VOID Terminate();
  82. PVOID AllocateCounters(PWCHAR wszServiceName, PWCHAR wszInstanceName,
  83. DWORD dwDataSize);
  84. PVOID AllocateCounters(PCHAR szServiceName, PCHAR szInstanceName,
  85. DWORD dwDataSize);
  86. VOID FreeCounters(PVOID pvCounters);
  87. BOOL Lock();
  88. VOID Unlock();
  89. PSHARE_HEADER GetShareHeader();
  90. BOOL Initialized();
  91. } ;
  92. typedef CPerfShare * PPerfShare;
  93. //
  94. // Function definitions follow.
  95. //
  96. //
  97. // BOOL InitializeHeader()
  98. //
  99. // Description:
  100. // Initialize the shared header. This function is only called
  101. // by Initialize if the shared memory did not exist previously.
  102. //
  103. // Returns:
  104. // TRUE on success.
  105. // FALSE otherwise (could not MapViewOfFile).
  106. //
  107. //
  108. // BOOL GetFirstCounterValue(PWCHAR wszServiceName, PDWORD pdwFirstCounter)
  109. //
  110. // Description:
  111. // Given the service name, open the registry and find that service's
  112. // first counter value.
  113. //
  114. // Inputs:
  115. // wszServiceName [IN] - The name of the service.
  116. // pdwFirstCounter [MODIFY] - A pointer to a DWORD to store the value.
  117. //
  118. // Returns:
  119. // TRUE on success.
  120. // FALSE otherwise (value not in registry).
  121. //
  122. //
  123. // BOOL Initialize(BOOL bReadOnly = FALSE)
  124. //
  125. // Description:
  126. // Initialize the shared memory segment and the mutex used to
  127. // provide process syncronization.
  128. //
  129. // Inputs:
  130. // bReadOnly [IN] - If TRUE, the memory is opened in readonly mode.
  131. // If FALSE, the memory can be written to.
  132. //
  133. // Returns:
  134. // TRUE on success.
  135. // FALSE otherwise (failure to acquire shared memory or mutex).
  136. //
  137. // Remarks:
  138. // The bReadOnly defaults to FALSE because the tools using this
  139. // code need to be able to write to this memory. The perfmon DLL
  140. // does not need to and can Initialize with a value of TRUE. The
  141. // first class instance to have initialized called is always opened
  142. // with the value FALSE because it has to initialize the shared
  143. // memory segment.
  144. //
  145. //
  146. // VOID Terminate()
  147. //
  148. // Description:
  149. // Clean up the mess made by this class. UnMap the shared memory,
  150. // close handles, etc.
  151. //
  152. //
  153. // PVOID AllocateCounters(PWCHAR wszServiceName, PWCHAR wszInstanceName)
  154. //
  155. // Description:
  156. // Allocate a shared memory "element" for this tool. The tool should
  157. // provide an instance name so that perfmon can list this name in
  158. // its list.
  159. //
  160. // Inputs:
  161. // wszInstanceName [IN] - The wide character name for the instance.
  162. //
  163. // Returns:
  164. // A pointer to the TOOL_COUNTERS structure defined by the tool
  165. // developer on success.
  166. // NULL on failure (no free elements left in the free list).
  167. //
  168. // Remarks:
  169. // This function will return a pointer to the already existing
  170. // counter structure if AllocateCounters has been called previously
  171. // and DeallocateCounters has not been called.
  172. //
  173. //
  174. // PVOID AllocateCounters(PCHAR szServiceName, PCHAR szInstanceName)
  175. //
  176. // Description:
  177. // Allocate a shared memory "element" for this tool. The tool should
  178. // provide an instance name so that perfmon can list this name in
  179. // its list.
  180. //
  181. // Inputs:
  182. // szInstanceName [IN] - The ASCII character name for the instance.
  183. //
  184. // Remarks:
  185. // This function will return a pointer to the already existing
  186. // counter structure if AllocateCounters has been called previously
  187. // and DeallocateCounters has not been called.
  188. //
  189. //
  190. // VOID FreeCounters(PVOID pvCounters)
  191. //
  192. // Description:
  193. // Deallocate the shared memory "element" for this tool.
  194. //
  195. // Inputs:
  196. // pvCounters [IN] - Pointer to a previously allocated counter block.
  197. //
  198. // Remarks:
  199. // If AllocateCounters has not been successfully called,
  200. // this function will do nothing.
  201. //
  202. //
  203. // BOOL Lock()
  204. //
  205. // Description:
  206. // Lock the shared memory list header so that no other class instance
  207. // can modify it.
  208. //
  209. // Returns:
  210. // TRUE on success.
  211. // FALSE otherwise (failure to acquire mutex, class not initialized).
  212. //
  213. // Remarks:
  214. // This function does not need to be called by the tool developer.
  215. // The only reason it is public is because the perfmon DLL needs
  216. // to lock the list when it gathers performance data.
  217. //
  218. // If the Lock() function is called multiple times by the same class
  219. // instance, an internal counter is incremented. This guarantees
  220. // that a single class instance can not cause deadlock for the
  221. // entire system. The shared memory segment will not be unlocked
  222. // until the Unlock() method is called an equal number of times.
  223. //
  224. //
  225. // VOID Unlock()
  226. //
  227. // Description:
  228. // Unlock the shared memory list header so that other processes and
  229. // threads can modify it.
  230. //
  231. // Remarks:
  232. // See the Lock() method.
  233. //
  234. //
  235. // PSHARE_LIST GetShareHeader()
  236. //
  237. // Description:
  238. // Return a pointer to the shared memory list header.
  239. //
  240. // Returns:
  241. // A pointer to the header on success.
  242. // NULL otherwise (class instance not initialized).
  243. //
  244. // Remarks:
  245. // This function does not need to be called by the tool developer.
  246. // The only reason it is public is because the perfmon DLL needs
  247. // to lock the list when it gathers performance data.
  248. //
  249. //
  250. // BOOL Initialized()
  251. //
  252. // Description:
  253. // Return a boolean indicating if the class instance has been
  254. // initialized successfully.
  255. //
  256. // Returns:
  257. // TRUE on success.
  258. // FALSE otherwise.
  259. //
  260. #endif