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.

84 lines
2.4 KiB

  1. //========= Copyright 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_Alloc(int size, bool bClear = true );
  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. template< typename T >
  23. class CHunkMemory
  24. {
  25. public:
  26. // constructor, destructor
  27. CHunkMemory( int nGrowSize = 0, int nInitSize = 0 ) { m_pMemory = NULL; m_nAllocated = 0; if ( nInitSize ) Grow( nInitSize ); }
  28. CHunkMemory( T* pMemory, int numElements ) { Assert( 0 ); }
  29. // Can we use this index?
  30. bool IsIdxValid( int i ) const { return (i >= 0) && (i < m_nAllocated); }
  31. // Gets the base address
  32. T* Base() { return (T*)m_pMemory; }
  33. const T* Base() const { return (T*)m_pMemory; }
  34. // element access
  35. T& operator[]( int i ) { Assert( IsIdxValid(i) ); return Base()[i]; }
  36. const T& operator[]( int i ) const { Assert( IsIdxValid(i) ); return Base()[i]; }
  37. T& Element( int i ) { Assert( IsIdxValid(i) ); return Base()[i]; }
  38. const T& Element( int i ) const { Assert( IsIdxValid(i) ); return Base()[i]; }
  39. // Attaches the buffer to external memory....
  40. void SetExternalBuffer( T* pMemory, int numElements ) { Assert( 0 ); }
  41. // Size
  42. int NumAllocated() const { return m_nAllocated; }
  43. int Count() const { return m_nAllocated; }
  44. // Grows the memory, so that at least allocated + num elements are allocated
  45. void Grow( int num = 1 ) { Assert( !m_nAllocated ); m_pMemory = (T *)Hunk_Alloc( num * sizeof(T), false ); m_nAllocated = num; }
  46. // Makes sure we've got at least this much memory
  47. void EnsureCapacity( int num ) { Assert( num <= m_nAllocated ); }
  48. // Memory deallocation
  49. void Purge() { m_nAllocated = 0; }
  50. // Purge all but the given number of elements (NOT IMPLEMENTED IN )
  51. void Purge( int numElements ) { Assert( 0 ); }
  52. // is the memory externally allocated?
  53. bool IsExternallyAllocated() const { return false; }
  54. // Set the size by which the memory grows
  55. void SetGrowSize( int size ) {}
  56. private:
  57. T *m_pMemory;
  58. int m_nAllocated;
  59. };
  60. #endif // ZONE_H