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.

318 lines
8.5 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: mem.cxx
  7. //
  8. // Contents: IMalloc interface implementations
  9. // Note that these functions do little more than
  10. // the normal delete and new. They are provided
  11. // so that existing code can be ported to the ref.
  12. // impl. easily.
  13. //
  14. //---------------------------------------------------------------
  15. #ifndef __MEM__C__
  16. #define __MEM__C__
  17. #include "exphead.cxx"
  18. #include "h/mem.hxx"
  19. #include "memory.h"
  20. #include "h/dfexcept.hxx"
  21. static CAllocator theAllocator; // global allocator
  22. //+--------------------------------------------------------------
  23. //
  24. // Function: CoGetMalloc, public
  25. //
  26. // Synopsis: Retrieves a pointer to the default OLE task memory
  27. // allocator (which supports the system implementation
  28. // of the IMalloc interface) so applications can call
  29. // its methods to manage memory.
  30. //
  31. // Arguments: [dwMemContext] - Indicates if memory is private or shared
  32. // [ppMalloc] - Receives pointer to memory allocator on return
  33. //
  34. // Returns: Appropriate status code
  35. //
  36. // Modifies: [ppMalloc]
  37. //
  38. //---------------------------------------------------------------
  39. STDAPI
  40. CoGetMalloc( DWORD dwMemContext, LPMALLOC * ppMalloc )
  41. {
  42. if (FAILED(ValidatePtrBuffer(ppMalloc))
  43. || dwMemContext != 1)
  44. return ResultFromScode(E_INVALIDARG);
  45. else
  46. {
  47. *ppMalloc = &theAllocator;
  48. return S_OK;
  49. }
  50. }
  51. //+--------------------------------------------------------------
  52. //
  53. // Function: CoTaskMemAlloc, public
  54. //
  55. // Synopsis: Allocates a block of task memory in the same way
  56. // that IMalloc::Alloc does.
  57. //
  58. // Arguments: [cb] - Size in bytes of memory block to be allocated
  59. //
  60. // Returns: Allocated memory block
  61. // Indicates memory block allocated successfully.
  62. // NULL
  63. // Indicates insufficient memory available.
  64. //
  65. // Modifies: [ppMalloc]
  66. //
  67. //---------------------------------------------------------------
  68. STDAPI_(LPVOID)
  69. CoTaskMemAlloc( ULONG cb )
  70. {
  71. return theAllocator.Alloc(cb);
  72. }
  73. //+--------------------------------------------------------------
  74. //
  75. // Function: CoTaskMemFree, public
  76. //
  77. // Synopsis: Frees a block of task memory previously allocated
  78. // through a call to the CoTaskMemAlloc or
  79. // CoTaskMemRealloc function.
  80. //
  81. // Arguments: [pv] - Points to memory block to be freed
  82. //
  83. // Modifies: nothing
  84. //
  85. //---------------------------------------------------------------
  86. STDAPI_(void)
  87. CoTaskMemFree( void* pv )
  88. {
  89. theAllocator.Free(pv);
  90. }
  91. //+--------------------------------------------------------------
  92. //
  93. // Function: CoTaskMemRealloc, public
  94. //
  95. // Synopsis: Changes the size of a previously allocated block
  96. // of task memory.
  97. //
  98. // Arguments: [pv] - Points to memory block to be reallocated
  99. // [cb] - Size in bytes of block to be reallocated
  100. //
  101. // Returns: Reallocated memory block
  102. // Indicates memory block successfully reallocated.
  103. // NULL
  104. // Indicates insufficient memory or cb is zero
  105. // and pv is not NULL.
  106. //
  107. // Modifies: nothing
  108. //
  109. //---------------------------------------------------------------
  110. STDAPI_(LPVOID)
  111. CoTaskMemRealloc( LPVOID pv, ULONG cb )
  112. {
  113. return theAllocator.Realloc(pv, cb);
  114. }
  115. //+---------------------------------------------------------------------------
  116. //
  117. // Member: CAllocator::QueryInterface, public
  118. //
  119. // Synopsis: Standard QI
  120. //
  121. // Arguments: [iid] - Interface ID
  122. // [ppvObj] - Object return
  123. //
  124. // Returns: Appropriate status code
  125. //
  126. //
  127. //----------------------------------------------------------------------------
  128. STDMETHODIMP CAllocator::QueryInterface(REFIID iid, void **ppvObj)
  129. {
  130. SCODE sc = S_OK;
  131. if (IsEqualIID(iid, IID_IMalloc) || IsEqualIID(iid, IID_IUnknown))
  132. {
  133. *ppvObj = (IMalloc *) this;
  134. CAllocator::AddRef();
  135. }
  136. else
  137. sc = E_NOINTERFACE;
  138. return ResultFromScode(sc);
  139. }
  140. //+---------------------------------------------------------------------------
  141. //
  142. // Member: CAllocator::AddRef, public
  143. //
  144. // Synopsis: Add reference
  145. //
  146. //
  147. //----------------------------------------------------------------------------
  148. STDMETHODIMP_(ULONG) CAllocator::AddRef(void)
  149. {
  150. //return ++_cRefs;
  151. return 1; // return a dummy value
  152. }
  153. //+---------------------------------------------------------------------------
  154. //
  155. // Member: CAllocator::Release, public
  156. //
  157. // Synopsis: Release
  158. //
  159. //
  160. //----------------------------------------------------------------------------
  161. STDMETHODIMP_(ULONG) CAllocator::Release(void)
  162. {
  163. return 0; // return a dummy value
  164. }
  165. //+---------------------------------------------------------------------------
  166. //
  167. // Member: CAllocator::Alloc, public
  168. //
  169. // Synopsis: Allocate memory
  170. //
  171. // Arguments: [cb] -- Number of bytes to allocate
  172. //
  173. // Returns: Pointer to block, NULL if failure
  174. //
  175. //----------------------------------------------------------------------------
  176. STDMETHODIMP_(void *) CAllocator::Alloc ( ULONG cb )
  177. {
  178. // make sure the retuned value is 8 byte aligned
  179. return (void *) new LONGLONG[ (cb+7)/sizeof(LONGLONG) ];
  180. }
  181. //+---------------------------------------------------------------------------
  182. //
  183. // Member: CAllocator::Realloc, public
  184. //
  185. // Synopsis: Resize the block given
  186. //
  187. // Arguments: [pv] -- Pointer to block to realloc
  188. // [cb] -- New size for block
  189. //
  190. // Returns: Pointer to new block, NULL if failure
  191. //
  192. // Note: The current implementation will copy cb # of
  193. // bytes to the new block, even if the old block
  194. // has size smaller then cb.
  195. // ==> potential problems esp if pv is at a
  196. // memory boundary.
  197. //----------------------------------------------------------------------------
  198. STDMETHODIMP_(void *)
  199. CAllocator::Realloc( void *pv, ULONG cb )
  200. {
  201. void* pvNew=NULL;
  202. if (!pv)
  203. pvNew = Alloc(cb);
  204. else
  205. {
  206. // make sure the new pointer is 8-byte aligned
  207. pvNew = (void *) (new LONGLONG [(cb+7)/sizeof(LONGLONG)]);
  208. if (pvNew)
  209. {
  210. memcpy(pvNew, pv, cb);
  211. delete[] pv;
  212. }
  213. }
  214. return pvNew;
  215. }
  216. //+---------------------------------------------------------------------------
  217. //
  218. // Member: CSmAllocator::Free, public
  219. //
  220. // Synopsis: Free a memory block
  221. //
  222. // Arguments: [pv] -- Pointer to block to free
  223. //
  224. // Returns: void
  225. //
  226. //----------------------------------------------------------------------------
  227. STDMETHODIMP_(void) CAllocator::Free(void *pv)
  228. {
  229. delete[] pv;
  230. }
  231. //+---------------------------------------------------------------------------
  232. //
  233. // Member: CAllocator::GetSize, public
  234. //
  235. // Synopsis: Return the size of the given block
  236. //
  237. // Arguments: [pv] -- Block to get size of
  238. //
  239. // Returns: (should) Size of block pointer to by
  240. // (now) 0
  241. //----------------------------------------------------------------------------
  242. STDMETHODIMP_(ULONG) CAllocator::GetSize(void * pv)
  243. {
  244. UNREFERENCED_PARM(pv);
  245. return 0;
  246. }
  247. //+---------------------------------------------------------------------------
  248. //
  249. // Member: CAllocator::DidAlloc, public
  250. //
  251. // Synopsis: Return '1' if this heap allocated pointer at pv
  252. //
  253. // Arguments: [pv] -- Pointer to block
  254. //
  255. // Returns: '1' == This heap allocated block.
  256. // '0' == This heap did not allocate block.
  257. // '-1' == Could not determine if this heap allocated block.
  258. //
  259. //----------------------------------------------------------------------------
  260. STDMETHODIMP_(int) CAllocator::DidAlloc(void FAR * pv)
  261. {
  262. UNREFERENCED_PARM(pv);
  263. return -1;
  264. }
  265. //+---------------------------------------------------------------------------
  266. //
  267. // Member: CAllocator::HeapMinimize, public
  268. //
  269. // Synopsis: Minimize the heap
  270. //
  271. // Arguments: None.
  272. //
  273. // Returns: void.
  274. //
  275. //----------------------------------------------------------------------------
  276. STDMETHODIMP_(void) CAllocator::HeapMinimize(void)
  277. {
  278. // do nothing;
  279. return;
  280. }
  281. #endif // __MEM__C__