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.

477 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: stdalloc.cxx
  7. //
  8. // Contents: 16-bit OLE allocator
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 3-07-94 kevinro Ported from ole2.01 (16-bit)
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <headers.cxx>
  18. #pragma hdrstop
  19. #include <ole2sp.h>
  20. /****** Standard Task/Shared Allocator **********************************/
  21. #define NULLSAB ((__segment)0)
  22. typedef __segment SAB;
  23. // amount of space windows reserves at start of segment
  24. #define cbWinRes 16
  25. // amount of space LocalInit takes (somewhat empirical)
  26. #define cbWinOH (6+10+46)
  27. // total overhead per global block
  28. #define cbTotalOH (cbWinRes + sizeof(StdAllocHdr) + cbWinOH + 32)
  29. // maximum sized object in a SAB (fudged to include space for per-block
  30. // overhead and most anything else we missed).
  31. #define cbMaxSAB (0xfffe - cbTotalOH)
  32. //+---------------------------------------------------------------------------
  33. //
  34. // Class: CStdMalloc ()
  35. //
  36. // Purpose: Standard task allocator
  37. //
  38. // History: 3-04-94 kevinro Created
  39. //
  40. // Notes:
  41. //
  42. //----------------------------------------------------------------------------
  43. class FAR CStdMalloc : public IMalloc
  44. {
  45. public:
  46. CStdMalloc(DWORD memctx)
  47. {
  48. m_refs = 1;
  49. m_pStdAllocHead = NULL;
  50. m_flags = (memctx == MEMCTX_TASK)? GMEM_MOVEABLE :
  51. GMEM_MOVEABLE|GMEM_SHARE;
  52. }
  53. STDMETHOD(QueryInterface)(REFIID iid, void FAR* FAR* ppvObj)
  54. {
  55. VDATEPTROUT( ppvObj, LPVOID );
  56. *ppvObj = NULL;
  57. VDATEIID( iid );
  58. if (iid == IID_IUnknown || iid == IID_IMalloc) {
  59. *ppvObj = this;
  60. ++m_refs;
  61. return NOERROR;
  62. } else {
  63. *ppvObj = NULL;
  64. return ReportResult(0, E_NOINTERFACE, 0, 0);
  65. }
  66. }
  67. STDMETHOD_(ULONG,AddRef)(void) { return ++m_refs; }
  68. STDMETHOD_(ULONG,Release)(void)
  69. {
  70. if (--m_refs == 0) {
  71. // free all memory (includes the memory for this class)
  72. FreeAllMem();
  73. return 0;
  74. } else
  75. return m_refs;
  76. }
  77. STDMETHOD_(void FAR*, Alloc) (ULONG cb);
  78. STDMETHOD_(void FAR*, Realloc) (void FAR* pv, ULONG cb);
  79. STDMETHOD_(void, Free) (void FAR* pv);
  80. STDMETHOD_(ULONG, GetSize) (void FAR* pv);
  81. STDMETHOD_(int, DidAlloc) (void FAR* pv);
  82. STDMETHOD_(void, HeapMinimize) ();
  83. private:
  84. ULONG m_refs;
  85. UINT m_flags;
  86. #define STDALLOC_SIG 0x4D445453 // 'STDM'
  87. struct StdAllocHdr
  88. {
  89. ULONG m_Signature;
  90. HTASK m_hTask; // task which owns this block
  91. StdAllocHdr FAR* m_pStdAllocNext;
  92. };
  93. StdAllocHdr FAR* m_pStdAllocHead;
  94. INTERNAL_(StdAllocHdr FAR*) MapBlockToSA(SAB sab) { return (StdAllocHdr FAR*)MAKELONG(cbWinRes, sab); }
  95. INTERNAL_(SAB) MapSAToBlock(StdAllocHdr FAR* pSA) { return (__segment)pSA; }
  96. INTERNAL_(void FAR*) AllocInBlock(SAB seg, UINT cb);
  97. INTERNAL_(SAB) AllocNewBlock(UINT cb);
  98. INTERNAL_(SAB) MapPtrToBlock(void FAR* pv);
  99. INTERNAL_(CStdMalloc FAR*) MoveSelf(LPVOID lpv);
  100. friend HRESULT STDAPICALLTYPE CoCreateStandardMalloc(DWORD memctx, IMalloc FAR* FAR* ppMalloc);
  101. INTERNAL_(void) FreeAllMem(void);
  102. };
  103. INTERNAL_(void FAR*) CStdMalloc::AllocInBlock(SAB sab, UINT cb)
  104. {
  105. #ifdef _DEBUG
  106. WINDEBUGINFO Olddebuginfo, debuginfo;
  107. //get initial debug state
  108. GetWinDebugInfo(&debuginfo, WDI_OPTIONS);
  109. Olddebuginfo = debuginfo;
  110. //turn off alerts (see bug 3502)
  111. debuginfo.dwOptions |= DBO_SILENT;
  112. SetWinDebugInfo(&debuginfo);
  113. #endif // _DEBUG
  114. // must make alloc of 0 mean alloc something; LocalAlloc fails on cb == 0
  115. if (0==cb)
  116. cb = 2;
  117. _asm push DS;
  118. _asm mov DS, sab;
  119. void NEAR *npv = (void NEAR*)LocalAlloc(LMEM_FIXED, cb);
  120. _asm pop DS;
  121. #ifdef _DEBUG
  122. //restore Debug state
  123. SetWinDebugInfo(&Olddebuginfo);
  124. #endif // _DEBUG
  125. if (npv == NULL) // npv is near pointer
  126. return NULL; // returned value is far pointer
  127. return (void FAR*)MAKELONG(npv, sab);
  128. }
  129. INTERNAL_(SAB) CStdMalloc::AllocNewBlock(UINT cb)
  130. {
  131. if (cb > cbMaxSAB)
  132. // overflow
  133. return NULLSAB;
  134. if (cb < 4096 - cbTotalOH)
  135. // minimum is 4k global block
  136. cb = 4096;
  137. else
  138. // size will be larger than 4K; must include total overhead
  139. cb += cbTotalOH;
  140. // allocate block and get segment value
  141. HGLOBAL h;
  142. if ((h = GlobalAlloc(m_flags, cb)) == NULL)
  143. return NULLSAB;
  144. UINT segment;
  145. segment = HIWORD(GlobalHandle(h));
  146. // init windows local heap
  147. if (!LocalInit(segment, cbWinRes + sizeof(StdAllocHdr), cb)) {
  148. GlobalFree(h);
  149. return NULLSAB;
  150. }
  151. // init block and put it on front of list
  152. StdAllocHdr FAR* pSA;
  153. pSA = MapBlockToSA((SAB)segment);
  154. pSA->m_Signature = STDALLOC_SIG;
  155. pSA->m_hTask = (m_flags & GMEM_SHARE) != 0 ? NULL : GetCurrentTask();
  156. pSA->m_pStdAllocNext = m_pStdAllocHead;
  157. m_pStdAllocHead = pSA;
  158. return (SAB)segment;
  159. }
  160. INTERNAL_(SAB) CStdMalloc::MapPtrToBlock(void FAR* pv)
  161. {
  162. if (pv == NULL)
  163. return NULLSAB;
  164. StdAllocHdr FAR* pSA;
  165. pSA = MapBlockToSA((SAB)pv);
  166. if (pSA->m_Signature != STDALLOC_SIG)
  167. return NULLSAB;
  168. if (m_flags & GMEM_SHARE) {
  169. if (pSA->m_hTask != NULL)
  170. // owned by task; not shared memory
  171. return NULLSAB;
  172. } else {
  173. if (pSA->m_hTask != GetCurrentTask()) {
  174. // if not same task, not this allocator; if different task,
  175. // something is wrong.
  176. AssertSz(pSA->m_hTask == NULL, "pointer used in wrong task");
  177. return NULLSAB;
  178. }
  179. }
  180. return MapSAToBlock(pSA);
  181. }
  182. STDMETHODIMP_(void FAR*) CStdMalloc::Alloc(ULONG cb)
  183. {
  184. if (cb > cbMaxSAB)
  185. // can't deal with objects larger than 64k
  186. return NULL;
  187. // try all sab in order (newest allocated sab on front)
  188. StdAllocHdr FAR* pSA = m_pStdAllocHead;
  189. while (pSA != NULL) {
  190. // try allocating the memory; if successful, return
  191. void FAR* pv;
  192. if ((pv = AllocInBlock(MapSAToBlock(pSA), (UINT)cb)) != NULL)
  193. return pv;
  194. pSA = pSA->m_pStdAllocNext;
  195. }
  196. SAB sab;
  197. if ((sab = AllocNewBlock((UINT)cb)) == NULLSAB)
  198. return NULL;
  199. // this should really succeed (i.e., an assert would be better)
  200. return AllocInBlock(sab, (UINT)cb);
  201. }
  202. STDMETHODIMP_(void FAR*) CStdMalloc::Realloc(void FAR* pv, ULONG cb)
  203. {
  204. if (cb > cbMaxSAB)
  205. // can't deal with objects larger than 64k
  206. return NULL;
  207. SAB sab;
  208. if (pv == NULL)
  209. // same as allocating a new pointer
  210. return Alloc(cb);
  211. //VDATEPTRIN rejects NULL (changed by alexgo 8/3/93)
  212. GEN_VDATEPTRIN( pv, int, (LPVOID)NULL );
  213. if ((sab = MapPtrToBlock(pv)) == NULLSAB)
  214. // attempt to realloc a pointer from some other allocator
  215. return NULL;
  216. else if (cb == 0) {
  217. // Realloc(pv, 0) -> frees and returns NULL; this is C library behavior
  218. Free(pv);
  219. return NULL;
  220. }
  221. void NEAR* npv;
  222. npv = (void NEAR*)(ULONG)pv;
  223. // first try realloc within same sab
  224. Assert(sab != NULLSAB);
  225. {
  226. void NEAR* npvT;
  227. _asm push DS;
  228. _asm mov DS, sab;
  229. npvT = (void NEAR*)LocalReAlloc((HLOCAL)npv, (UINT)cb, LMEM_MOVEABLE);
  230. _asm pop DS;
  231. if (npvT != NULL)
  232. return (void FAR*)MAKELONG(npvT, sab);
  233. }
  234. // now try allocating new sab and copying
  235. void FAR* pvT;
  236. if ((pvT = Alloc(cb)) == NULL)
  237. return NULL;
  238. // only copy the smaller of the old and new size
  239. _asm push ds;
  240. _asm mov ds,sab;
  241. UINT cbCopy = LocalSize((HLOCAL)npv);
  242. _asm pop ds;
  243. if ((UINT)cb < cbCopy)
  244. // new size smaller
  245. cbCopy = (UINT)cb;
  246. _fmemcpy(pvT, pv, cbCopy);
  247. Free(pv);
  248. return pvT;
  249. }
  250. STDMETHODIMP_(void) CStdMalloc::Free(void FAR* pv)
  251. {
  252. SAB sab;
  253. if (pv == NULL)
  254. return;
  255. //VDATEPTRIN rejects NULL (changed by alexgo 8/3/93)
  256. VOID_VDATEPTRIN( pv, int );
  257. if ((sab = MapPtrToBlock(pv)) == NULLSAB) {
  258. #ifdef _DEBUG
  259. AssertSz(FALSE, "Pointer freed by wrong allocator; filling with 0xcc");
  260. // we don't own block; this is an error; fill it with 0xcccc anyway
  261. UINT cb = (UINT)GlobalSize((HGLOBAL)GlobalHandle((__segment)pv));
  262. cb -= (UINT)(ULONG)pv;
  263. _fmemset(pv, 0xcc, (size_t)cb);
  264. #endif
  265. } else {
  266. #ifdef _DEBUG
  267. // we own block; fill it with 0xcccc
  268. _fmemset(pv, 0xcc, (size_t)GetSize(pv));
  269. #endif
  270. _asm push ds;
  271. _asm mov ds,sab;
  272. LocalFree((HLOCAL)(void NEAR*)(ULONG)pv);
  273. _asm pop ds;
  274. }
  275. }
  276. STDMETHODIMP_(ULONG) CStdMalloc::GetSize(void FAR* pv)
  277. {
  278. ULONG size = 0;
  279. SAB sab;
  280. //VDATEPTRIN rejects NULL (added by alexgo 8/3/93)
  281. if( pv == NULL )
  282. return -1;
  283. GEN_VDATEPTRIN( pv , int, 0 );
  284. if ((sab = MapPtrToBlock(pv)) != NULLSAB) {
  285. _asm push ds;
  286. _asm mov ds,sab;
  287. size = LocalSize((HLOCAL)(void NEAR*)(ULONG)pv);
  288. _asm pop ds;
  289. }
  290. return size;
  291. }
  292. STDMETHODIMP_(int) CStdMalloc::DidAlloc(void FAR* pv)
  293. {
  294. if (pv == NULL)
  295. return -1;
  296. //VDATEPTRIN rejects NULL (added by alexgo 8/3/93)
  297. GEN_VDATEPTRIN( pv , int, 0 );
  298. // returns 1 if we allocated, 0 if not; this impl never returns -1.
  299. return MapPtrToBlock(pv) != NULLSAB;
  300. }
  301. STDMETHODIMP_(void) CStdMalloc::HeapMinimize()
  302. {
  303. // LATER : could do local compact here
  304. }
  305. // move this instance of stdmalloc to lpv (which must be large enough)
  306. INTERNAL_(CStdMalloc FAR*) CStdMalloc::MoveSelf(LPVOID lpv)
  307. {
  308. _fmemcpy(lpv, this, sizeof(*this));
  309. m_pStdAllocHead = NULL;
  310. return (CStdMalloc FAR*)lpv;
  311. }
  312. INTERNAL_(void) CStdMalloc::FreeAllMem(void)
  313. {
  314. // get/null out head of list; null now in case we are freeing self.
  315. StdAllocHdr FAR* pSA = m_pStdAllocHead;
  316. m_pStdAllocHead = NULL;
  317. // free all blocks
  318. while (pSA != NULL) {
  319. StdAllocHdr FAR* pSANext = pSA->m_pStdAllocNext;
  320. pSA->m_pStdAllocNext = NULL; // to prevent incorrect compiler opt
  321. GlobalFree(LOWORD(GlobalHandle((UINT)MapSAToBlock(pSA))));
  322. pSA = pSANext;
  323. }
  324. }
  325. /****** Global API for creating *****************************************/
  326. // create and return an impl of IMalloc of given memctx
  327. //+---------------------------------------------------------------------------
  328. //
  329. // Function: CoCreateStandardMalloc, Local
  330. //
  331. // Synopsis:
  332. //
  333. // Effects:
  334. //
  335. // Arguments: [memctx] --
  336. // [ppMalloc] --
  337. //
  338. // Requires:
  339. //
  340. // Returns:
  341. //
  342. // Signals:
  343. //
  344. // Modifies:
  345. //
  346. // Algorithm:
  347. //
  348. // History: 2-28-94 kevinro Created
  349. //
  350. // Notes:
  351. //
  352. //----------------------------------------------------------------------------
  353. STDAPI CoCreateStandardMalloc(DWORD memctx, IMalloc FAR* FAR* ppMalloc)
  354. {
  355. thkDebugOut((DEB_ITRACE, " CoCreateStandardMalloc\n"));
  356. *ppMalloc = NULL;
  357. switch (memctx) {
  358. case MEMCTX_TASK:
  359. case MEMCTX_SHARED:
  360. {
  361. CStdMalloc sm(memctx); // local one
  362. void FAR* lpv;
  363. if ((lpv = sm.Alloc(sizeof(CStdMalloc))) == NULL)
  364. return ResultFromScode(E_OUTOFMEMORY);
  365. *ppMalloc = sm.MoveSelf(lpv); // move to newly allocated memory
  366. return NOERROR;
  367. }
  368. default:
  369. return ResultFromScode(E_INVALIDARG);
  370. }
  371. }