Source code of Windows XP (NT5)
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.

194 lines
5.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 2000
  5. //
  6. // File: pmalloc.hxx
  7. //
  8. // Contents: Memory allocation classes derived from PMemoryAllocator
  9. //
  10. // Classes: CCoTaskMemAllocator, CNonAlignAllocator
  11. //
  12. // History: 01 May-1998 AlanW Created from proprec.cxx
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. //+-------------------------------------------------------------------------
  17. //
  18. // Class: CCoTaskMemAllocator
  19. //
  20. // Purpose: A PMemoryAllocator implementation that uses OLE memory
  21. //
  22. //+-------------------------------------------------------------------------
  23. class CCoTaskMemAllocator : public PMemoryAllocator
  24. {
  25. public:
  26. void *Allocate(ULONG cbSize)
  27. {
  28. return CoTaskMemAlloc( cbSize );
  29. }
  30. void Free(void *pv)
  31. {
  32. CoTaskMemFree( pv );
  33. }
  34. };
  35. //+-------------------------------------------------------------------------
  36. //
  37. // Class: CNonAlignAllocator
  38. //
  39. // Purpose: A PMemoryAllocator implementation that uses a memory buffer
  40. //
  41. //+-------------------------------------------------------------------------
  42. class CNonAlignAllocator : public PMemoryAllocator
  43. {
  44. public:
  45. inline CNonAlignAllocator(ULONG cbBuffer, VOID *pvBuffer)
  46. {
  47. _cbFree = cbBuffer;
  48. _pvCur = _pvBuffer = pvBuffer;
  49. }
  50. VOID *Allocate(ULONG cb)
  51. {
  52. VOID *pv;
  53. cb = (cb + sizeof(LONGLONG) - 1) & ~(sizeof(LONGLONG) - 1);
  54. if (cb > _cbFree)
  55. {
  56. return NULL;
  57. }
  58. pv = _pvCur;
  59. _pvCur = (BYTE *) _pvCur + cb;
  60. _cbFree -= cb;
  61. return pv;
  62. }
  63. VOID Free(VOID *pv) { }
  64. inline ULONG GetFreeSize(VOID) { return _cbFree; }
  65. private:
  66. ULONG _cbFree;
  67. VOID *_pvCur;
  68. VOID *_pvBuffer;
  69. };
  70. //+-------------------------------------------------------------------------
  71. //
  72. // Class: PVarAllocator
  73. //
  74. // Purpose: Base class for vaiable data allocator
  75. //
  76. //--------------------------------------------------------------------------
  77. class PVarAllocator : public PMemoryAllocator
  78. {
  79. public:
  80. // PMemoryAllocator methods:
  81. // Allocate a piece of memory.
  82. //virtual PVOID Allocate(size_t cbNeeded) = 0;
  83. //
  84. // Free a previously allocated piece of memory.
  85. //virtual void Free(PVOID pMem) = 0;
  86. // Return whether OffsetToPointer has non-null effect
  87. virtual BOOL IsBasedMemory(void) { return FALSE; } ;
  88. // Convert a pointer offset to a pointer.
  89. virtual PVOID OffsetToPointer(ULONG_PTR oBuf) { return (PVOID) oBuf; }
  90. // Convert a pointer to a pointer offset.
  91. virtual ULONG_PTR PointerToOffset(PVOID pBuf) { return (ULONG_PTR) pBuf; }
  92. // Set base address for OffsetToPointer and PointerToOffset
  93. virtual void SetBase(BYTE* pbBase) {}
  94. // Allocate & Free memory for BSTRs
  95. virtual PVOID CopyBSTR(size_t cbNeeded, WCHAR* pwszBuf)
  96. {
  97. return ((BYTE*) CopyTo( cbNeeded + sizeof (DWORD) + sizeof (OLECHAR),
  98. ((BYTE *)pwszBuf) - sizeof (DWORD))) + sizeof (DWORD);
  99. }
  100. virtual PVOID AllocBSTR(size_t cbNeeded)
  101. {
  102. return ((BYTE*) Allocate( cbNeeded + sizeof (DWORD) + sizeof (OLECHAR))) + sizeof (DWORD);
  103. }
  104. virtual void FreeBSTR(PVOID pMem)
  105. {
  106. Free( ((BYTE *)pMem) - sizeof (DWORD) );
  107. }
  108. // Copy data to newly allocated memory.
  109. PVOID CopyTo(size_t cbNeeded, BYTE* pbBuf)
  110. {
  111. void * pRetBuf = Allocate( cbNeeded );
  112. Win4Assert( 0 != pRetBuf);
  113. RtlCopyMemory( pRetBuf, pbBuf, cbNeeded );
  114. return pRetBuf;
  115. }
  116. };
  117. //+-------------------------------------------------------------------------
  118. //
  119. // Class: CSafeArrayAllocator
  120. //
  121. // Purpose: Used for constructing safe arrays.
  122. //
  123. //--------------------------------------------------------------------------
  124. class CSafeArrayAllocator : public PVarAllocator
  125. {
  126. public:
  127. CSafeArrayAllocator( PMemoryAllocator & ma ) : _ma( ma ) {}
  128. void * Allocate(ULONG cbSize) { return _ma.Allocate( cbSize ); }
  129. void Free(void *pv) { _ma.Free( pv ); }
  130. private:
  131. PMemoryAllocator & _ma;
  132. };
  133. BOOL SaCreateAndCopy( PMemoryAllocator &ma,
  134. SAFEARRAY * psaSrc,
  135. SAFEARRAY **ppsaDest );
  136. BOOL SaCreateData( PVarAllocator &ma,
  137. VARTYPE vt,
  138. SAFEARRAY & saSrc,
  139. SAFEARRAY & saDst,
  140. BOOL fAllocBstrUsingAllocator );
  141. inline BOOL SaCreateDataUsingMA(
  142. PMemoryAllocator &ma,
  143. VARTYPE vt,
  144. SAFEARRAY & saSrc,
  145. SAFEARRAY & saDst,
  146. BOOL fAllocBstrUsingAllocator )
  147. {
  148. CSafeArrayAllocator saa( ma );
  149. return SaCreateData( saa, vt, saSrc, saDst, fAllocBstrUsingAllocator );
  150. }
  151. ULONG SaComputeSize( VARTYPE vt, SAFEARRAY & saSrc );
  152. inline unsigned SaCountElements( SAFEARRAY &saSrc )
  153. {
  154. unsigned cDataElements = 1;
  155. //
  156. // get total data memory to allocate, and number of data elements in it.
  157. //
  158. for ( unsigned i = 0; i < saSrc.cDims; i++ )
  159. cDataElements *= saSrc.rgsabound[i].cElements;
  160. return cDataElements;
  161. }