Team Fortress 2 Source Code as on 22/4/2020
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.

116 lines
2.6 KiB

  1. //====== Copyright 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "tier1/utlbinaryblock.h"
  7. //-----------------------------------------------------------------------------
  8. // Base class, containing simple memory management
  9. //-----------------------------------------------------------------------------
  10. CUtlBinaryBlock::CUtlBinaryBlock( int growSize, int initSize ) : m_Memory( growSize, initSize )
  11. {
  12. m_nActualLength = 0;
  13. }
  14. CUtlBinaryBlock::CUtlBinaryBlock( void* pMemory, int nSizeInBytes, int nInitialLength ) : m_Memory( (unsigned char*)pMemory, nSizeInBytes )
  15. {
  16. m_nActualLength = nInitialLength;
  17. }
  18. CUtlBinaryBlock::CUtlBinaryBlock( const void* pMemory, int nSizeInBytes ) : m_Memory( (const unsigned char*)pMemory, nSizeInBytes )
  19. {
  20. m_nActualLength = nSizeInBytes;
  21. }
  22. CUtlBinaryBlock::CUtlBinaryBlock( const CUtlBinaryBlock& src )
  23. {
  24. Set( src.Get(), src.Length() );
  25. }
  26. void CUtlBinaryBlock::Get( void *pValue, int nLen ) const
  27. {
  28. Assert( nLen > 0 );
  29. if ( m_nActualLength < nLen )
  30. {
  31. nLen = m_nActualLength;
  32. }
  33. if ( nLen > 0 )
  34. {
  35. memcpy( pValue, m_Memory.Base(), nLen );
  36. }
  37. }
  38. void CUtlBinaryBlock::SetLength( int nLength )
  39. {
  40. Assert( !m_Memory.IsReadOnly() );
  41. m_nActualLength = nLength;
  42. if ( nLength > m_Memory.NumAllocated() )
  43. {
  44. int nOverFlow = nLength - m_Memory.NumAllocated();
  45. m_Memory.Grow( nOverFlow );
  46. // If the reallocation failed, clamp length
  47. if ( nLength > m_Memory.NumAllocated() )
  48. {
  49. m_nActualLength = m_Memory.NumAllocated();
  50. }
  51. }
  52. #ifdef _DEBUG
  53. if ( m_Memory.NumAllocated() > m_nActualLength )
  54. {
  55. memset( ( ( char * )m_Memory.Base() ) + m_nActualLength, 0xEB, m_Memory.NumAllocated() - m_nActualLength );
  56. }
  57. #endif
  58. }
  59. void CUtlBinaryBlock::Set( const void *pValue, int nLen )
  60. {
  61. Assert( !m_Memory.IsReadOnly() );
  62. if ( !pValue )
  63. {
  64. nLen = 0;
  65. }
  66. SetLength( nLen );
  67. if ( m_nActualLength )
  68. {
  69. if ( ( ( const char * )m_Memory.Base() ) >= ( ( const char * )pValue ) + nLen ||
  70. ( ( const char * )m_Memory.Base() ) + m_nActualLength <= ( ( const char * )pValue ) )
  71. {
  72. memcpy( m_Memory.Base(), pValue, m_nActualLength );
  73. }
  74. else
  75. {
  76. memmove( m_Memory.Base(), pValue, m_nActualLength );
  77. }
  78. }
  79. }
  80. CUtlBinaryBlock &CUtlBinaryBlock::operator=( const CUtlBinaryBlock &src )
  81. {
  82. Assert( !m_Memory.IsReadOnly() );
  83. Set( src.Get(), src.Length() );
  84. return *this;
  85. }
  86. bool CUtlBinaryBlock::operator==( const CUtlBinaryBlock &src ) const
  87. {
  88. if ( src.Length() != Length() )
  89. return false;
  90. return !memcmp( src.Get(), Get(), Length() );
  91. }