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.

399 lines
9.5 KiB

  1. //+---------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1993 - 1993.
  5. //
  6. // File: $(COMTOOLS)\memory\ctmem.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 27-Oct-93 DarrylA Created.
  15. // 1-May-94 DeanE Added Heap validation macros.
  16. // 26-May-94 NaveenB ported to build for 16-bit
  17. // 06-Oct-94 NaveenB added sifting of the test code
  18. // capability
  19. // 10-Oct-94 NaveenB added IMallocAllocCtm and
  20. // IMallocFreeCtm
  21. //
  22. //----------------------------------------------------------------------
  23. #include <comtpch.hxx>
  24. #pragma hdrstop
  25. #include <ctmem.hxx>
  26. #ifdef WIN16
  27. #include <types16.h>
  28. #include <stdio.h>
  29. #define NON_TASK_ALLOCATOR
  30. #define _TEXT(quote) quote
  31. #else
  32. #include <tchar.h>
  33. #endif
  34. #ifdef SIFTING_TEST_CODE
  35. #include <sift.hxx>
  36. ISift *g_psftSiftObject = NULL;
  37. #define SIMULATE_FAILURE( dwRes ) \
  38. ((NULL != g_psftSiftObject) && \
  39. (g_psftSiftObject->SimFail( ( dwRes ) )))
  40. #endif
  41. DWORD dwCtValidateHeap = 1;
  42. #if defined(WIN32) && !defined(_CHICAGO_) && !defined(_MAC)
  43. DWORD dwCtExceptionCode = (DWORD) CT_EXCEPTION_HEAP;
  44. #endif
  45. //+---------------------------------------------------------------------
  46. //
  47. // Function: new, new(FAIL_BEHAVIOR), delete
  48. //
  49. // Synopsis: Overrides to support the default behavior of the
  50. // commnot new and delete. Simply uses malloc and free.
  51. //
  52. // History: 06-Oct-93 DarrylA Created.
  53. //
  54. // Notes:
  55. //
  56. //----------------------------------------------------------------------
  57. PVOID __cdecl operator new(unsigned int nSize, FAIL_BEHAVIOR enfb)
  58. {
  59. VDATEHEAP();
  60. #ifdef SIFTING_TEST_CODE
  61. // Check for memory failure if sifting
  62. if (SIMULATE_FAILURE(SR_PUBLIC_MEMORY))
  63. {
  64. if(ExceptOnFail == enfb)
  65. {
  66. #if defined(WIN16) || defined(_MAC)
  67. OutputDebugString(
  68. "Can not call new (ExceptOnFail) in Win16 or Mac\n");
  69. #else
  70. RaiseException((ULONG) E_OUTOFMEMORY, 0, 0, NULL);
  71. #endif
  72. }
  73. else
  74. {
  75. return(NULL);
  76. }
  77. }
  78. #endif
  79. #ifdef NON_TASK_ALLOCATOR
  80. PVOID pvTmp;
  81. pvTmp = (PVOID) malloc(nSize);
  82. if(ExceptOnFail == enfb)
  83. {
  84. #if defined(WIN16) || defined(_MAC)
  85. OutputDebugString(
  86. "Can not call new (ExceptOnFail) in Win16 or Mac\n");
  87. #endif
  88. if(NULL == pvTmp)
  89. {
  90. #if !defined(WIN16) && !defined(_MAC)
  91. RaiseException((ULONG) E_OUTOFMEMORY, 0, 0, NULL);
  92. #endif
  93. }
  94. }
  95. return pvTmp;
  96. #else // TASK_ALLOCATOR
  97. PVOID pvTmp = NULL;
  98. pvTmp = CoTaskMemAlloc(nSize);
  99. if (NULL == pvTmp)
  100. {
  101. if (ExceptOnFail==enfb)
  102. {
  103. #if !defined(WIN16) && !defined(_MAC)
  104. RaiseException((ULONG) E_OUTOFMEMORY, 0, 0, NULL);
  105. #endif
  106. }
  107. }
  108. return(pvTmp);
  109. #endif
  110. }
  111. PVOID __cdecl operator new(unsigned int nSize)
  112. {
  113. VDATEHEAP();
  114. #ifdef SIFTING_TEST_CODE
  115. // Check for memory failure if sifting
  116. if (SIMULATE_FAILURE(SR_PUBLIC_MEMORY))
  117. {
  118. return(NULL);
  119. }
  120. #endif
  121. #ifdef NON_TASK_ALLOCATOR
  122. return (PVOID) malloc(nSize);
  123. #else // TASK_ALLOCATOR
  124. PVOID pvTmp = CoTaskMemAlloc(nSize);
  125. return(pvTmp);
  126. #endif
  127. }
  128. VOID __cdecl operator delete(void *pbData)
  129. {
  130. VDATEHEAP();
  131. #ifdef NON_TASK_ALLOCATOR
  132. if(NULL != pbData)
  133. {
  134. free(pbData);
  135. }
  136. #else // TASK_ALLOCATOR
  137. CoTaskMemFree(pbData);
  138. #endif
  139. }
  140. //+---------------------------------------------------------------------
  141. //
  142. // Function: CtRealloc
  143. //
  144. // Synopsis: Adds support for realloc
  145. //
  146. // History: 31-Dec-97 VaraT Created.
  147. //
  148. // Notes:
  149. //
  150. //----------------------------------------------------------------------
  151. PVOID __cdecl CtRealloc(PVOID memBlock,
  152. size_t nSize, FAIL_BEHAVIOR enfb)
  153. {
  154. VDATEHEAP();
  155. #ifdef SIFTING_TEST_CODE
  156. // Check for memory failure if sifting
  157. if (SIMULATE_FAILURE(SR_PUBLIC_MEMORY))
  158. {
  159. if(ExceptOnFail == enfb)
  160. {
  161. #if defined(WIN16) || defined(_MAC)
  162. OutputDebugString(
  163. "Can not call Realloc (ExceptOnFail) in Win16 or Mac\n");
  164. #else
  165. RaiseException((ULONG) E_OUTOFMEMORY, 0, 0, NULL);
  166. #endif
  167. }
  168. else
  169. {
  170. return(NULL);
  171. }
  172. }
  173. #endif
  174. #ifdef NON_TASK_ALLOCATOR
  175. PVOID pvTmp;
  176. pvTmp = (PVOID) realloc(memBlock, nSize);
  177. if(ExceptOnFail == enfb)
  178. {
  179. #if defined(WIN16) || defined(_MAC)
  180. OutputDebugString(
  181. "Can not call new (ExceptOnFail) in Win16 or Mac\n");
  182. #endif
  183. if(NULL == pvTmp && nSize != 0)
  184. {
  185. #if !defined(WIN16) && !defined(_MAC)
  186. RaiseException((ULONG) E_OUTOFMEMORY, 0, 0, NULL);
  187. #endif
  188. }
  189. }
  190. return pvTmp;
  191. #else // TASK_ALLOCATOR
  192. PVOID pvTmp = NULL;
  193. pvTmp = CoTaskMemRealloc(memBlock, nSize);
  194. if (NULL == pvTmp && nSize != 0)
  195. {
  196. if (ExceptOnFail==enfb)
  197. {
  198. #if !defined(WIN16) && !defined(_MAC)
  199. RaiseException((ULONG) E_OUTOFMEMORY, 0, 0, NULL);
  200. #endif
  201. }
  202. }
  203. return(pvTmp);
  204. #endif
  205. }
  206. PVOID __cdecl CtRealloc(PVOID memBlock, size_t nSize)
  207. {
  208. VDATEHEAP();
  209. #ifdef SIFTING_TEST_CODE
  210. // Check for memory failure if sifting
  211. if (SIMULATE_FAILURE(SR_PUBLIC_MEMORY))
  212. {
  213. return(NULL);
  214. }
  215. #endif
  216. #ifdef NON_TASK_ALLOCATOR
  217. return (PVOID) realloc(memBlock, nSize);
  218. #else // TASK_ALLOCATOR
  219. PVOID pvTmp = CoTaskMemRealloc(memBlock, nSize);
  220. return(pvTmp);
  221. #endif
  222. }
  223. //+--------------------------------------------------------------
  224. // Function: IMallocAllocCtm
  225. //
  226. // Synopsis: Calls the Ole memory allocator and returns the pointer
  227. // of the new allocated block. Sifting capability is present
  228. //
  229. // Arguments: dwMemctx specifies whether the memory block is
  230. // private to a task or shared between
  231. // processes
  232. // ulCb size of the memory block
  233. //
  234. // Returns: pointer to the allocated block if succeeded else NULL.
  235. //
  236. // History: 10-Oct-94 NaveenB Created
  237. //---------------------------------------------------------------
  238. VOID FAR* IMallocAllocCtm(DWORD dwMemctx, ULONG ulCb)
  239. {
  240. void FAR* pMemory = NULL;
  241. LPMALLOC lpIMalloc = NULL;
  242. #ifdef SIFTING_TEST_CODE
  243. // Check for memory failure if sifting
  244. if (SIMULATE_FAILURE(SR_PUBLIC_MEMORY))
  245. {
  246. return(NULL);
  247. }
  248. #endif
  249. if (SUCCEEDED(CoGetMalloc(dwMemctx, &lpIMalloc)))
  250. {
  251. if (lpIMalloc != NULL)
  252. {
  253. pMemory = lpIMalloc->Alloc(ulCb);
  254. lpIMalloc->Release();
  255. }
  256. }
  257. else
  258. {
  259. OutputDebugString(_TEXT("CoGetMalloc Failed \n"));
  260. }
  261. return pMemory;
  262. }
  263. //+--------------------------------------------------------------
  264. // Function: IMallocFreeCtm
  265. //
  266. // Synopsis: Calls the Ole memory allocator and returns the pointer
  267. // of the new allocated block
  268. //
  269. // Arguments: dwMemctx specifies whether the memory block is
  270. // private to a task or shared between
  271. // processes
  272. // pv pointer to the memory block to free
  273. //
  274. // Returns: None
  275. //
  276. // History: 10-Oct-94 NaveenB Created
  277. //---------------------------------------------------------------
  278. VOID IMallocFreeCtm(DWORD dwMemctx, void FAR* pv)
  279. {
  280. LPMALLOC lpIMalloc = NULL;
  281. if (pv != NULL)
  282. {
  283. if (SUCCEEDED(CoGetMalloc(dwMemctx, &lpIMalloc)))
  284. {
  285. if (lpIMalloc != NULL)
  286. {
  287. lpIMalloc->Free(pv);
  288. lpIMalloc->Release();
  289. }
  290. }
  291. else
  292. {
  293. OutputDebugString(_TEXT("CoGetMalloc Failed \n"));
  294. }
  295. }
  296. }
  297. //+--------------------------------------------------------------
  298. // Function: IMallocReallocCtm
  299. //
  300. // Synopsis: Calls the Ole memory allocator and returns the pointer
  301. // of the new allocated block
  302. //
  303. // Arguments: dwMemctx specifies whether the memory block is
  304. // private to a task or shared between
  305. // processes
  306. // pv pointer to the memory block to free
  307. // cb New count of allocated memory
  308. //
  309. // Returns: None
  310. //
  311. // History: 5-Nov-97 VaraT Created
  312. //---------------------------------------------------------------
  313. VOID FAR * IMallocReallocCtm(DWORD dwMemctx, void FAR* pv, DWORD ulCb)
  314. {
  315. LPMALLOC lpIMalloc = NULL;
  316. void FAR* pMemory = NULL;
  317. #ifdef SIFTING_TEST_CODE
  318. // Check for memory failure if sifting
  319. if (SIMULATE_FAILURE(SR_PUBLIC_MEMORY))
  320. {
  321. return(NULL);
  322. }
  323. #endif
  324. if (SUCCEEDED(CoGetMalloc(dwMemctx, &lpIMalloc)))
  325. {
  326. if (lpIMalloc != NULL)
  327. {
  328. pMemory = lpIMalloc->Realloc(pv, ulCb);
  329. lpIMalloc->Release();
  330. }
  331. }
  332. else
  333. {
  334. OutputDebugString(_TEXT("CoGetMalloc Failed \n"));
  335. }
  336. return pMemory;
  337. }