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.

369 lines
7.8 KiB

  1. //
  2. // smblock.h
  3. //
  4. #ifndef SMBLOCK_H
  5. #define SMBLOCK_H
  6. #include "private.h"
  7. #include "cicmutex.h"
  8. #define SZSHAREDFILEMAP __TEXT("SFM.")
  9. #define SZSHAREDMUTEX __TEXT("MUTEX.")
  10. class CSharedHeap9x;
  11. //////////////////////////////////////////////////////////////////////////////
  12. //
  13. // instead of wsprintf
  14. //
  15. //////////////////////////////////////////////////////////////////////////////
  16. #define ONEDWORDCCHFORSETNAME 9
  17. __inline BOOL SetName(TCHAR *pszDst, UINT cchDst, const TCHAR *pszSrc, DWORD dw)
  18. {
  19. TCHAR *psz;
  20. StringCchCopy(pszDst, cchDst, pszSrc);
  21. psz = pszDst + lstrlen(pszDst);
  22. if (cchDst < (UINT)(psz - pszDst)/sizeof(TCHAR) + ONEDWORDCCHFORSETNAME)
  23. {
  24. Assert(0);
  25. return FALSE;
  26. }
  27. while (dw)
  28. {
  29. *psz = 'A' + ((char)dw & 0x0F);
  30. dw >>= 4;
  31. psz++;
  32. }
  33. *psz = '\0';
  34. return TRUE;
  35. }
  36. __inline BOOL SetName2(TCHAR *pszDst, UINT cchDst, const TCHAR *pszSrc, DWORD dw, DWORD dw2, DWORD dw3 = 0)
  37. {
  38. TCHAR *psz;
  39. StringCchCopy(pszDst, cchDst, pszSrc);
  40. psz = pszDst + lstrlen(pszDst);
  41. if (cchDst < (UINT)(psz - pszDst)/sizeof(TCHAR) +
  42. ONEDWORDCCHFORSETNAME * 2 +
  43. (dw3 ? ONEDWORDCCHFORSETNAME : 0))
  44. {
  45. Assert(0);
  46. return FALSE;
  47. }
  48. while (dw)
  49. {
  50. *psz = 'A' + ((char)dw & 0x0F);
  51. dw >>= 4;
  52. psz++;
  53. }
  54. *psz = '.';
  55. psz++;
  56. while (dw2)
  57. {
  58. *psz = 'A' + ((char)dw2 & 0x0F);
  59. dw2 >>= 4;
  60. psz++;
  61. }
  62. if (dw3)
  63. {
  64. *psz = '.';
  65. psz++;
  66. while (dw3)
  67. {
  68. *psz = 'A' + ((char)dw3 & 0x0F);
  69. dw3 >>= 4;
  70. psz++;
  71. }
  72. }
  73. *psz = '\0';
  74. return TRUE;
  75. }
  76. //////////////////////////////////////////////////////////////////////////////
  77. //
  78. // func
  79. //
  80. //////////////////////////////////////////////////////////////////////////////
  81. CSharedHeap *EnsureSharedHeap(SYSTHREAD *psfn);
  82. void DestroySharedHeap(SYSTHREAD *psfn);
  83. CSharedBlock *EnsureSharedBlockForThread(SYSTHREAD *psfn, DWORD dwThreadId);
  84. void DestroySharedBlocks(SYSTHREAD *psfn);
  85. //////////////////////////////////////////////////////////////////////////////
  86. //
  87. // CSharedBlock
  88. //
  89. //////////////////////////////////////////////////////////////////////////////
  90. class CSharedBlock
  91. {
  92. public:
  93. CSharedBlock(const char *pszPrefix, DWORD dwThread)
  94. {
  95. _pszPrefix = pszPrefix;
  96. _dwThread = dwThread;
  97. }
  98. virtual ~CSharedBlock()
  99. {
  100. _mutex.Uninit();
  101. }
  102. virtual HRESULT Init(SECURITY_DESCRIPTOR *pSecDes,
  103. ULONG ulSize,
  104. ULONG ulCommitSize,
  105. void *pvBase,
  106. BOOL fCreate,
  107. BOOL *pfAlreadyExists = NULL) = 0;
  108. virtual HRESULT Commit(ULONG ulNewSize) = 0;
  109. virtual HRESULT Reset() = 0;
  110. void *GetBase() {return _pvBase;}
  111. virtual void *GetPtr(ULONG ulOffset) = 0;
  112. virtual void *GetPtrFromBlockId(ULONG ulBlockId) = 0;
  113. virtual BOOL IsValidPtr(void *pv) = 0;
  114. ULONG GetOffset(void *pv)
  115. {
  116. return (ULONG)((BYTE *)pv - (BYTE *)_pvBase);
  117. }
  118. ULONG GetCommitSize()
  119. {
  120. return _ulCommitSize;
  121. }
  122. ULONG GetInitCommitSize()
  123. {
  124. return _ulInitCommitSize;
  125. }
  126. DWORD GetThreadId()
  127. {
  128. return _dwThread;
  129. }
  130. CCicMutex *GetMutex()
  131. {
  132. return &_mutex;
  133. }
  134. protected:
  135. void *_pvBase;
  136. ULONG _ulCommitSize;
  137. ULONG _ulInitCommitSize;
  138. CCicMutex _mutex;
  139. DWORD _dwThread;
  140. const char *_pszPrefix;
  141. };
  142. //////////////////////////////////////////////////////////////////////////////
  143. //
  144. // CSharedBlockNT
  145. //
  146. //////////////////////////////////////////////////////////////////////////////
  147. class CSharedBlockNT : public CSharedBlock
  148. {
  149. public:
  150. CSharedBlockNT(const char *pszPrefix, DWORD dwThread, BOOL fUseUniqueName);
  151. ~CSharedBlockNT();
  152. HRESULT Init(SECURITY_DESCRIPTOR *pSecDes,
  153. ULONG ulSize,
  154. ULONG ulCommitSize,
  155. void *pvBase,
  156. BOOL fCreate,
  157. BOOL *pfAlreadyExists);
  158. HRESULT Commit(ULONG ulNewSize);
  159. HRESULT Reset();
  160. void *GetPtrFromBlockId(ULONG ulBlockId);
  161. void *GetPtr(ULONG ulOffset)
  162. {
  163. return (void *)(((BYTE *)_pvBase) + ulOffset);
  164. }
  165. BOOL IsValidPtr(void *pv)
  166. {
  167. if (pv < _pvBase)
  168. return FALSE;
  169. if (pv > (BYTE *)_pvBase + _ulCommitSize)
  170. return FALSE;
  171. return TRUE;
  172. }
  173. private:
  174. HANDLE _hfm;
  175. BOOL _fUseUniqueName;
  176. };
  177. //////////////////////////////////////////////////////////////////////////////
  178. //
  179. // CSharedBlock9x
  180. //
  181. //////////////////////////////////////////////////////////////////////////////
  182. #ifndef _WIN64
  183. class CSharedBlock9x : public CSharedBlock
  184. {
  185. public:
  186. CSharedBlock9x(const char *pszPrefix, DWORD dwThread);
  187. ~CSharedBlock9x();
  188. HRESULT Init(SECURITY_DESCRIPTOR *pSecDes,
  189. ULONG ulSize,
  190. ULONG ulCommitSize,
  191. void *pvBase,
  192. BOOL fCreate,
  193. BOOL *pfAlreadyExists);
  194. HRESULT Commit(ULONG ulNewSize);
  195. HRESULT Reset();
  196. void *GetPtrFromBlockId(ULONG ulOffset)
  197. {
  198. Assert(ulOffset >= 0x80000000);
  199. return (void *)ulOffset;
  200. }
  201. void *GetPtr(ULONG ulOffset)
  202. {
  203. return (void *)(((BYTE *)_pvBase) + ulOffset);
  204. }
  205. private:
  206. friend CSharedHeap9x;
  207. BOOL IsValidPtr(void *pv)
  208. {
  209. if (!_hsheap || !pv)
  210. return FALSE;
  211. // return HeapValid(_hsheap, 0, pv);
  212. return TRUE;
  213. }
  214. HANDLE _hsheap;
  215. };
  216. #endif // !_WIN64
  217. //////////////////////////////////////////////////////////////////////////////
  218. //
  219. // CShraedHeap
  220. //
  221. //////////////////////////////////////////////////////////////////////////////
  222. #define BLK_FREE 0x01
  223. class CSharedHeap
  224. {
  225. public:
  226. CSharedHeap(DWORD dwThread);
  227. ~CSharedHeap();
  228. HRESULT Init(SECURITY_DESCRIPTOR *pSecDes,
  229. ULONG ulSize,
  230. ULONG ulCommitSize);
  231. void *Alloc(ULONG ulSize);
  232. #ifdef LATER
  233. void *Realloc(void *pv, ULONG ulSize);
  234. #endif
  235. BOOL Free(void *pv);
  236. ULONG GetBlockId(void *pv)
  237. {
  238. #ifndef _WIN64
  239. if (!IsOnNT())
  240. return (ULONG)pv;
  241. #endif
  242. return _psb->GetOffset(pv);
  243. }
  244. CSharedBlock *GetBlock()
  245. {
  246. return _psb;
  247. }
  248. static BOOL IsValidBlock(CSharedBlock *psb, void *pv);
  249. typedef struct tag_HEAPHDR {
  250. ULONG ulList;
  251. ULONG ulSize;
  252. } HEAPHDR;
  253. private:
  254. void InitHeap(ULONG ulInitSize);
  255. typedef struct tag_BLOCKHDR {
  256. ULONG ulPrev;
  257. ULONG ulNext;
  258. ULONG ulSize;
  259. ULONG ulFlags;
  260. ULONG u[4];
  261. ULONG GetSize()
  262. {
  263. return ulSize;
  264. }
  265. void SetSize(ULONG ulNewSize)
  266. {
  267. Assert(!(ulNewSize & 0x001f));
  268. ulSize = ulNewSize;
  269. }
  270. void SetFree(BOOL fFree)
  271. {
  272. if (fFree)
  273. ulFlags |= BLK_FREE;
  274. else
  275. ulFlags &= ~BLK_FREE;
  276. }
  277. BOOL IsFree()
  278. {
  279. return (ulFlags & BLK_FREE) ? TRUE : FALSE;
  280. }
  281. void *GetPtr()
  282. {
  283. BYTE *pb = (BYTE *)this;
  284. return pb + sizeof(tag_BLOCKHDR);
  285. }
  286. } BLOCKHDR;
  287. void MergeFreeBlock(BLOCKHDR *pbhdr);
  288. #ifdef DEBUG
  289. void _dbg_HeapCheck();
  290. #else
  291. void _dbg_HeapCheck() {};
  292. #endif
  293. CSharedBlock *_psb;
  294. DWORD _dwThread;
  295. };
  296. #endif // SMBLOCK_H