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.

125 lines
2.2 KiB

  1. //#ifdef UNIT_TEST
  2. //#include <windows.h>
  3. //#include "gcache.h"
  4. //#else
  5. #include "stdinc.h"
  6. //#endif
  7. #ifndef _ASSERT
  8. #define _ASSERT( f ) if( (f) ) ; else DebugBreak()
  9. #endif
  10. DWORD CClassAllocator::cbJunk = 0 ;
  11. DWORD CCache::cbJunk = 0 ;
  12. CCache::CCache( void** ppv, DWORD size ) :
  13. m_pCache( ppv ), m_clpv( size ) {
  14. ZeroMemory( ppv, m_clpv * sizeof( void * ) ) ;
  15. }
  16. CCache::~CCache( ) {
  17. for( DWORD i=0; i<m_clpv; i++ ) {
  18. _ASSERT( m_pCache[i] == 0 ) ;
  19. }
  20. }
  21. void*
  22. CCache::InternalFree( void* lpv ) {
  23. for( DWORD i=0; i<m_clpv && lpv != 0 ; i++ ) {
  24. lpv = (void*)InterlockedExchangePointer( &m_pCache[i], lpv ) ;
  25. }
  26. return lpv ;
  27. }
  28. void*
  29. CCache::InternalAlloc() {
  30. LPVOID lpv = 0 ;
  31. for( DWORD i=0; i<m_clpv && lpv == 0; i++ ) {
  32. lpv = (void*)InterlockedExchangePointer( &m_pCache[i], NULL ) ;
  33. }
  34. return lpv ;
  35. }
  36. void
  37. CCache::Free( void* lpv, CClassAllocator* pAllocator ) {
  38. #ifdef DEBUG
  39. _ASSERT( pAllocator->RangeCheck( lpv ) ) ;
  40. pAllocator->Erase( lpv ) ;
  41. #endif
  42. lpv = InternalFree( lpv ) ;
  43. if( lpv != 0 ) {
  44. #ifdef DEBUG
  45. _ASSERT( pAllocator->EraseCheck( lpv ) ) ;
  46. #endif
  47. pAllocator->Release( lpv ) ;
  48. }
  49. }
  50. void*
  51. CCache::Alloc( DWORD size, CClassAllocator* pAllocator, DWORD &cbOut ) {
  52. void* lpv = InternalAlloc() ;
  53. #ifdef DEBUG
  54. _ASSERT( pAllocator->SizeCheck( size ) ) ;
  55. if( lpv != 0 ) {
  56. _ASSERT( pAllocator->EraseCheck( lpv ) ) ;
  57. }
  58. #endif
  59. if( lpv == 0 ) {
  60. lpv = pAllocator->Allocate( size, cbOut ) ;
  61. }
  62. return lpv ;
  63. }
  64. void*
  65. CCache::Empty( ) {
  66. return InternalAlloc() ;
  67. }
  68. void
  69. CCache::Empty( CClassAllocator* pAllocator ) {
  70. LPVOID lpv = 0 ;
  71. for( DWORD i=0; i<m_clpv; i++ ) {
  72. lpv = (void*)InterlockedExchangePointer( &m_pCache[i], NULL ) ;
  73. if( lpv != 0 ) {
  74. pAllocator->Release( lpv ) ;
  75. }
  76. }
  77. }
  78. CClassAllocator::CClassAllocator() {
  79. }
  80. CClassAllocator::~CClassAllocator() {
  81. }
  82. #ifdef DEBUG
  83. void
  84. CClassAllocator::Erase( LPVOID lpv ) {
  85. }
  86. BOOL
  87. CClassAllocator::EraseCheck( LPVOID lpv ) {
  88. return TRUE ;
  89. }
  90. BOOL
  91. CClassAllocator::RangeCheck( LPVOID lpv ) {
  92. return TRUE ;
  93. }
  94. BOOL
  95. CClassAllocator::SizeCheck( DWORD cb ) {
  96. return TRUE ;
  97. }
  98. #endif