Counter Strike : Global Offensive Source Code
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.

126 lines
2.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "singleplayersharedmemory.h"
  7. #include "tier1/utlstring.h"
  8. #include "tier0/memdbgon.h"
  9. CSPSharedMemoryManager s_SinglePlayerSharedMemoryManager;
  10. CSPSharedMemoryManager *g_pSinglePlayerSharedMemoryManager = &s_SinglePlayerSharedMemoryManager;
  11. class CSPSharedMemory : public ISPSharedMemory
  12. {
  13. public:
  14. CSPSharedMemory( void ) : m_pMemory(NULL), m_Size(0), m_iRefCount(0) { };
  15. ~CSPSharedMemory( void );
  16. virtual bool Init( size_t iSize ); //Initial implementation assumes the size is fixed/hardcoded
  17. virtual uint8 * Base( void );
  18. virtual size_t Size( void );
  19. virtual void AddRef( void );
  20. virtual void Release( void );
  21. CUtlString m_Name;
  22. int m_iEntNum;
  23. uint8 *m_pMemory;
  24. size_t m_Size;
  25. unsigned int m_iRefCount;
  26. };
  27. CSPSharedMemory::~CSPSharedMemory( void )
  28. {
  29. Assert( m_iRefCount == 0 );
  30. //if( m_pMemory )
  31. // delete []m_pMemory;
  32. }
  33. bool CSPSharedMemory::Init( size_t iSize )
  34. {
  35. Assert( iSize != 0 );
  36. Assert( (m_Size == 0) || (m_Size == iSize) );
  37. if( m_Size == 0 )
  38. {
  39. m_pMemory = new uint8 [iSize];
  40. m_Size = iSize;
  41. memset( m_pMemory, 0, iSize );
  42. return true;
  43. }
  44. return false;
  45. }
  46. uint8 * CSPSharedMemory::Base( void )
  47. {
  48. Assert( m_pMemory != NULL );
  49. return m_pMemory;
  50. }
  51. size_t CSPSharedMemory::Size( void )
  52. {
  53. return m_Size;
  54. }
  55. void CSPSharedMemory::AddRef( void )
  56. {
  57. ++m_iRefCount;
  58. }
  59. void CSPSharedMemory::Release( void )
  60. {
  61. --m_iRefCount;
  62. if( m_iRefCount == 0 )
  63. {
  64. for( int i = s_SinglePlayerSharedMemoryManager.m_SharedSpaces.Count(); --i >= 0; )
  65. {
  66. if( s_SinglePlayerSharedMemoryManager.m_SharedSpaces[i] == this )
  67. {
  68. s_SinglePlayerSharedMemoryManager.m_SharedSpaces.FastRemove(i);
  69. break;
  70. }
  71. }
  72. //delete this;
  73. }
  74. }
  75. CSPSharedMemoryManager::~CSPSharedMemoryManager( void )
  76. {
  77. Assert( m_SharedSpaces.Count() == 0 );
  78. for( int i = m_SharedSpaces.Count(); --i >= 0; )
  79. {
  80. Warning( "Shared memory space %s (%i) still has %i references.\n", m_SharedSpaces[i]->m_Name.String(), m_SharedSpaces[i]->m_iEntNum, m_SharedSpaces[i]->m_iRefCount );
  81. //leak it?
  82. }
  83. }
  84. ISPSharedMemory *CSPSharedMemoryManager::GetSharedMemory( const char *szName, int ent_num )
  85. {
  86. for( int i = m_SharedSpaces.Count(); --i >= 0; )
  87. {
  88. if( m_SharedSpaces[i]->m_Name == szName && m_SharedSpaces[i]->m_iEntNum == ent_num )
  89. {
  90. m_SharedSpaces[i]->AddRef();
  91. return m_SharedSpaces[i];
  92. }
  93. }
  94. //create a new one
  95. CSPSharedMemory *pNew = new CSPSharedMemory;
  96. pNew->m_Name = szName;
  97. pNew->m_iEntNum = ent_num;
  98. m_SharedSpaces.AddToTail( pNew );
  99. pNew->AddRef();
  100. return pNew;
  101. }