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.

175 lines
4.3 KiB

  1. //--------------------------------------------------------------------------------------------------
  2. // qhMemory.inl
  3. //
  4. // Copyright(C) 2011 by D. Gregorius. All rights reserved.
  5. //--------------------------------------------------------------------------------------------------
  6. #include "qhTypes.h"
  7. #ifndef NULL
  8. #define NULL 0
  9. #endif
  10. //--------------------------------------------------------------------------------------------------
  11. // qhPool
  12. //--------------------------------------------------------------------------------------------------
  13. template < typename T > inline
  14. qhPool< T >::qhPool( void )
  15. : mSize( 0 )
  16. , mPool( NULL )
  17. , mFree( -1 )
  18. {
  19. }
  20. //--------------------------------------------------------------------------------------------------
  21. template < typename T > inline
  22. qhPool< T >::~qhPool( void )
  23. {
  24. qhFree( mPool );
  25. }
  26. //--------------------------------------------------------------------------------------------------
  27. template < typename T > inline
  28. void qhPool< T >::Clear( void )
  29. {
  30. mSize = 0;
  31. qhFree( mPool );
  32. mPool = NULL;
  33. mFree = -1;
  34. }
  35. //--------------------------------------------------------------------------------------------------
  36. template < typename T > inline
  37. void qhPool< T >::Resize( int Size )
  38. {
  39. QH_ASSERT( mSize == 0 && mPool == NULL );
  40. mSize = Size;
  41. mPool = (T*)qhAlloc( Size * sizeof( T ) );
  42. mFree = 0;
  43. for ( int i = 0; i < mSize - 1; ++i )
  44. {
  45. int* Next = (int*)( mPool + i );
  46. *Next = i + 1;
  47. }
  48. int* Next = (int*)( mPool + mSize - 1 );
  49. *Next = -1;
  50. }
  51. //--------------------------------------------------------------------------------------------------
  52. template < typename T > inline
  53. T* qhPool< T >::Allocate( void )
  54. {
  55. QH_ASSERT( mFree >= 0 );
  56. int Next = mFree;
  57. mFree = *(int*)( mPool + mFree );
  58. #ifdef QH_DEBUG
  59. memset( mPool + Next, 0xcd, sizeof( T ) );
  60. #endif
  61. return mPool + Next;
  62. }
  63. //--------------------------------------------------------------------------------------------------
  64. template < typename T > inline
  65. void qhPool< T >::Free( T* Address )
  66. {
  67. #ifdef QH_DEBUG
  68. QH_ASSERT( Address != NULL );
  69. memset( Address, 0xdb, sizeof( T ) );
  70. #endif
  71. QH_ASSERT( mPool <= Address && Address < mPool + mSize );
  72. int* Next = (int*)Address;
  73. *Next = mFree;
  74. mFree = int( Address - mPool );
  75. QH_ASSERT( 0 <= mFree && mFree < mSize );
  76. }
  77. //--------------------------------------------------------------------------------------------------
  78. // Memory utilities
  79. //--------------------------------------------------------------------------------------------------
  80. template < typename T > inline
  81. void qhConstruct( T* Address )
  82. {
  83. new ( Address ) T;
  84. }
  85. //--------------------------------------------------------------------------------------------------
  86. template < typename T > inline
  87. void qhConstruct( T* Address, int N )
  88. {
  89. // If n < 0 no objects are constructed (e.g. if shrinking an array)
  90. for ( int i = 0; i < N; ++i )
  91. {
  92. new ( Address + i ) T;
  93. }
  94. }
  95. //--------------------------------------------------------------------------------------------------
  96. template < typename T > inline
  97. void qhCopyConstruct( T* Address, const T& Other )
  98. {
  99. new ( Address ) T( Other );
  100. }
  101. //--------------------------------------------------------------------------------------------------
  102. template < typename T > inline
  103. void qhDestroy( T* Address )
  104. {
  105. ( Address )->~T();
  106. }
  107. //--------------------------------------------------------------------------------------------------
  108. template < typename T > inline
  109. void qhDestroy( T* Address, int N )
  110. {
  111. // If n < 0 no objects are destroyed (e.g. if growing an array)
  112. for ( int i = 0; i < N; ++i )
  113. {
  114. ( Address + i )->~T();
  115. }
  116. }
  117. //-------------------------------------------------------------------------------------------------
  118. template < typename T >
  119. void qhMove( T* Address, T* Begin, T* End )
  120. {
  121. while ( Begin != End )
  122. {
  123. new ( Address ) T;
  124. qhSwap( *Address++, *Begin++ );
  125. }
  126. }
  127. //-------------------------------------------------------------------------------------------------
  128. template <typename T> inline
  129. void qhSwap( T& Lhs, T& Rhs )
  130. {
  131. T Temp = Lhs;
  132. Lhs = Rhs;
  133. Rhs = Temp;
  134. }
  135. //--------------------------------------------------------------------------------------------------
  136. inline const void* qhAddByteOffset( const void* Address, size_t Bytes )
  137. {
  138. return reinterpret_cast< const unsigned char* >( Address ) + Bytes;
  139. }