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.

97 lines
2.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // Purpose:
  9. //
  10. // $Workfile: $
  11. // $Date: $
  12. //
  13. //-----------------------------------------------------------------------------
  14. // $Log: $
  15. //
  16. // $NoKeywords: $
  17. //=============================================================================
  18. #ifndef TSMULTIMEMPOOL_H
  19. #define TSMULTIMEMPOOL_H
  20. #ifdef _WIN32
  21. #pragma once
  22. #endif
  23. #include "tier1/utlmap.h"
  24. #include "tier1/mempool.h"
  25. #include "tier1/tsmempool.h"
  26. //-----------------------------------------------------------------------------
  27. // Purpose: A container of a range of mem pool sizes (for network buffers for example)
  28. // and a raw alloc capability (for sizes greater than any contained mem pool).
  29. //-----------------------------------------------------------------------------
  30. class CThreadSafeMultiMemoryPool
  31. {
  32. public:
  33. struct MemPoolConfig_t
  34. {
  35. uint32 m_cubBlockSize;
  36. uint32 m_cubDefaultPoolSize;
  37. };
  38. CThreadSafeMultiMemoryPool( const MemPoolConfig_t *pnBlock, int cnMemPoolConfig, int nGrowMode = UTLMEMORYPOOL_GROW_FAST );
  39. ~CThreadSafeMultiMemoryPool();
  40. // Allocate a block of at least nAllocSize bytes
  41. void* Alloc( uint32 cubAlloc );
  42. // Free a previously alloc'd block
  43. void Free(void *pvMem);
  44. // ReAllocate a previously allocated block to a new size
  45. void* ReAlloc( void *pvMem, uint32 cubAlloc );
  46. // Frees everything
  47. void Clear();
  48. // alloc size for this bit of alloc'd memory
  49. int CubAllocSize( void *pvMem );
  50. // prints details about our contained memory
  51. void PrintStats();
  52. // total number of alloc'd elements
  53. int Count();
  54. // Return the total size in MB allocated for this pool
  55. int CMBPoolSize() const;
  56. // Return the amount of memory in use
  57. int CMBPoolSizeInUse() const;
  58. #ifdef DBGFLAG_VALIDATE
  59. void Validate( CValidator &validator, const char *pchName ); // Validate our internal structures
  60. #endif // DBGFLAG_VALIDATE
  61. private:
  62. struct MemPoolRecord_t
  63. {
  64. CThreadSafeMemoryPool *m_pMemPool;
  65. uint32 m_nBlockSize;
  66. };
  67. CUtlVector<MemPoolRecord_t> m_VecMemPool; // stores our list of mem pools
  68. uint32 m_nBlockSizeMax;
  69. CUtlVector<MemPoolRecord_t *> m_VecMemPoolLookup; // quick lookup table of mempools
  70. struct RawAllocation_t
  71. {
  72. void *m_pvMem;
  73. uint32 m_nBlockSize;
  74. };
  75. CUtlMap<void *,RawAllocation_t,int> m_MapRawAllocation; // stores our list of raw alloc'd mem
  76. CThreadFastMutex m_mutexRawAllocations;
  77. uint32 m_cubReallocedTotal;
  78. };
  79. #endif // TSMULTIMEMPOOL_H