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.

115 lines
3.6 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef ZONE_H
  9. #define ZONE_H
  10. #pragma once
  11. #include "tier0/dbg.h"
  12. void Memory_Init (void);
  13. void Memory_Shutdown( void );
  14. void Hunk_OnMapStart( int nEstimatedBytes );
  15. void *Hunk_AllocName (int size, const char *name, bool bClear = true );
  16. int Hunk_LowMark (void);
  17. void Hunk_FreeToLowMark (int mark);
  18. void Hunk_Check (void);
  19. int Hunk_MallocSize();
  20. int Hunk_Size();
  21. void Hunk_Print();
  22. // Deal with memory attribution for CHunkMemory
  23. #define HUNK_ALLOC_CREDIT_( _name_ ) MEM_ALLOC_CREDIT_( _name_ ); CHunkAllocCredit hunkAllocAttributeAlloction( _name_ );
  24. class CHunkAllocCredit
  25. {
  26. public:
  27. CHunkAllocCredit( const char *name ) { PushAllocDbgInfo( name ); };
  28. ~CHunkAllocCredit( void ) { PopAllocDbgInfo(); };
  29. static void PushAllocDbgInfo( const char *name )
  30. {
  31. Assert( name && name[0] );
  32. ++s_DbgInfoStackDepth;
  33. Assert( s_DbgInfoStackDepth < DBG_INFO_STACK_DEPTH );
  34. if ( s_DbgInfoStackDepth < DBG_INFO_STACK_DEPTH )
  35. s_DbgInfoStack[s_DbgInfoStackDepth] = ( name && name[0] ) ? name : "CHunkMemory";
  36. }
  37. static void PopAllocDbgInfo( void )
  38. {
  39. Assert( s_DbgInfoStackDepth >= 0 );
  40. if ( s_DbgInfoStackDepth >= 0 )
  41. s_DbgInfoStack[ s_DbgInfoStackDepth-- ] = NULL;
  42. }
  43. static const char *GetAllocDbgInfo( void )
  44. {
  45. int index = MIN( s_DbgInfoStackDepth, (DBG_INFO_STACK_DEPTH-1) );
  46. return ( index >= 0 ) ? s_DbgInfoStack[index] : "CHunkMemory";
  47. }
  48. static const int DBG_INFO_STACK_DEPTH = 8;
  49. static const char *s_DbgInfoStack[ DBG_INFO_STACK_DEPTH ];
  50. static int s_DbgInfoStackDepth;
  51. };
  52. template< typename T >
  53. class CHunkMemory
  54. {
  55. public:
  56. // constructor, destructor
  57. CHunkMemory( int nGrowSize = 0, int nInitSize = 0 ) { m_pMemory = NULL; m_nAllocated = 0; if ( nInitSize ) Grow( nInitSize ); }
  58. CHunkMemory( T* pMemory, int numElements ) { Assert( 0 ); }
  59. // Can we use this index?
  60. bool IsIdxValid( int i ) const { return (i >= 0) && (i < m_nAllocated); }
  61. // Gets the base address
  62. T* Base() { return (T*)m_pMemory; }
  63. const T* Base() const { return (T*)m_pMemory; }
  64. // element access
  65. T& operator[]( int i ) { Assert( IsIdxValid(i) ); return Base()[i]; }
  66. const T& operator[]( int i ) const { Assert( IsIdxValid(i) ); return Base()[i]; }
  67. T& Element( int i ) { Assert( IsIdxValid(i) ); return Base()[i]; }
  68. const T& Element( int i ) const { Assert( IsIdxValid(i) ); return Base()[i]; }
  69. // Attaches the buffer to external memory....
  70. void SetExternalBuffer( T* pMemory, int numElements ) { Assert( 0 ); }
  71. // Size
  72. int NumAllocated() const { return m_nAllocated; }
  73. int Count() const { return m_nAllocated; }
  74. // Grows the memory, so that at least allocated + num elements are allocated
  75. void Grow( int num = 1 ) { Assert( !m_nAllocated ); m_pMemory = (T *)Hunk_AllocName( num * sizeof(T), CHunkAllocCredit::GetAllocDbgInfo(), false ); m_nAllocated = num; }
  76. // Makes sure we've got at least this much memory
  77. void EnsureCapacity( int num ) { Assert( num <= m_nAllocated ); }
  78. // Memory deallocation
  79. void Purge() { m_nAllocated = 0; }
  80. // Purge all but the given number of elements (NOT IMPLEMENTED IN )
  81. void Purge( int numElements ) { Assert( 0 ); }
  82. // is the memory externally allocated?
  83. bool IsExternallyAllocated() const { return false; }
  84. // Set the size by which the memory grows
  85. void SetGrowSize( int size ) {}
  86. private:
  87. T *m_pMemory;
  88. int m_nAllocated;
  89. };
  90. #endif // ZONE_H