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.

102 lines
1.9 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLMEM_H__
  11. #define __ATLMEM_H__
  12. #pragma once
  13. #include <atlbase.h>
  14. #include <limits.h>
  15. namespace ATL
  16. {
  17. template< typename N >
  18. inline N AtlAlignUp( N n, ULONG nAlign ) throw()
  19. {
  20. return( N( (n+(nAlign-1))&~(N( nAlign )-1) ) );
  21. }
  22. __interface __declspec(uuid("654F7EF5-CFDF-4df9-A450-6C6A13C622C0")) IAtlMemMgr
  23. {
  24. public:
  25. void* Allocate( size_t nBytes ) throw();
  26. void Free( void* p ) throw();
  27. void* Reallocate( void* p, size_t nBytes ) throw();
  28. size_t GetSize( void* p ) throw();
  29. };
  30. class CWin32Heap :
  31. public IAtlMemMgr
  32. {
  33. public:
  34. CWin32Heap( HANDLE hHeap ) throw() :
  35. m_hHeap( hHeap ),
  36. m_bOwnHeap( false )
  37. {
  38. ATLASSERT( hHeap != NULL );
  39. }
  40. ~CWin32Heap() throw()
  41. {
  42. if( m_bOwnHeap && (m_hHeap != NULL) )
  43. {
  44. BOOL bSuccess;
  45. bSuccess = ::HeapDestroy( m_hHeap );
  46. ATLASSERT( bSuccess );
  47. }
  48. }
  49. // IAtlMemMgr
  50. virtual void* Allocate( size_t nBytes ) throw()
  51. {
  52. return( ::HeapAlloc( m_hHeap, 0, nBytes ) );
  53. }
  54. virtual void Free( void* p ) throw()
  55. {
  56. if( p != NULL )
  57. {
  58. BOOL bSuccess;
  59. bSuccess = ::HeapFree( m_hHeap, 0, p );
  60. ATLASSERT( bSuccess );
  61. }
  62. }
  63. virtual void* Reallocate( void* p, size_t nBytes ) throw()
  64. {
  65. if( p == NULL )
  66. {
  67. return( Allocate( nBytes ) );
  68. }
  69. else
  70. {
  71. return( ::HeapReAlloc( m_hHeap, 0, p, nBytes ) );
  72. }
  73. }
  74. virtual size_t GetSize( void* p ) throw()
  75. {
  76. return( ::HeapSize( m_hHeap, 0, p ) );
  77. }
  78. public:
  79. HANDLE m_hHeap;
  80. bool m_bOwnHeap;
  81. };
  82. }; // namespace ATL
  83. #endif //__ATLMEM_H__