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.

134 lines
2.3 KiB

  1. //====== Copyright � 1996-2010, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. // The CUtlAllocation class:
  5. // A single allocation in the style of CUtlMemory/CUtlString/CUtlBuffer
  6. // as compact as possible, no virtuals or extraneous data
  7. // to be used primarily to replace CUtlBuffer
  8. //=============================================================================
  9. #ifndef UTLALLOCATION_H
  10. #define UTLALLOCATION_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "tier1/utlmemory.h"
  15. class CUtlAllocation
  16. {
  17. public:
  18. // constructor, destructor
  19. CUtlAllocation()
  20. {
  21. m_pMemory = NULL;
  22. }
  23. CUtlAllocation( const void *pMemory, int cub )
  24. {
  25. m_pMemory = NULL;
  26. Copy( pMemory, cub );
  27. }
  28. CUtlAllocation( CUtlAllocation const &src )
  29. {
  30. m_pMemory = NULL;
  31. Copy( src );
  32. }
  33. ~CUtlAllocation()
  34. {
  35. Purge();
  36. }
  37. CUtlAllocation &operator=( CUtlAllocation const &src )
  38. {
  39. Copy( src );
  40. return *this;
  41. }
  42. bool operator==( CUtlAllocation const &src )
  43. {
  44. if ( Count() != src.Count() )
  45. return false;
  46. return Q_memcmp( Base(), src.Base(), Count() ) == 0;
  47. }
  48. void Copy( const void *pMemory, int cub )
  49. {
  50. if ( cub == 0 || pMemory == NULL )
  51. {
  52. Purge();
  53. return;
  54. }
  55. if ( cub != Count() )
  56. {
  57. Purge();
  58. m_pMemory = (ActualMemory_t *)malloc( cub + sizeof( int ) );
  59. m_pMemory->cub = cub;
  60. }
  61. Q_memcpy( Base(), pMemory, cub );
  62. }
  63. // Gets the base address
  64. uint8* Base()
  65. {
  66. if ( m_pMemory == NULL )
  67. return NULL;
  68. return m_pMemory->rgub;
  69. }
  70. const uint8* Base() const
  71. {
  72. if ( m_pMemory == NULL )
  73. return NULL;
  74. return m_pMemory->rgub;
  75. }
  76. // Size
  77. int Count() const
  78. {
  79. if ( m_pMemory == NULL )
  80. return 0;
  81. return m_pMemory->cub;
  82. }
  83. // Memory deallocation
  84. void Purge()
  85. {
  86. if ( m_pMemory )
  87. free(m_pMemory);
  88. m_pMemory = NULL;
  89. }
  90. void Copy( const CUtlAllocation &alloc )
  91. {
  92. Copy( alloc.Base(), alloc.Count() );
  93. }
  94. void Swap( CUtlAllocation &alloc )
  95. {
  96. ActualMemory_t *pTemp = m_pMemory;
  97. m_pMemory = alloc.m_pMemory;
  98. alloc.m_pMemory = pTemp;
  99. }
  100. void Alloc( int cub )
  101. {
  102. Purge();
  103. m_pMemory = (ActualMemory_t *)malloc( cub + sizeof( int ) );
  104. m_pMemory->cub = cub;
  105. }
  106. private:
  107. struct ActualMemory_t
  108. {
  109. int cub;
  110. uint8 rgub[4]; // i'd prefer to make this 0 but the compiler whines when i do
  111. };
  112. ActualMemory_t *m_pMemory;
  113. };
  114. #endif // UTLALLOCATION_H