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.

96 lines
2.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Memory allocation!
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "pch_tier0.h"
  8. #include "tier0/mem.h"
  9. #include <malloc.h>
  10. #include "tier0/dbg.h"
  11. #include "tier0/minidump.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. #ifndef STEAM
  15. #define PvRealloc realloc
  16. #define PvAlloc malloc
  17. #define PvExpand _expand
  18. #endif
  19. enum
  20. {
  21. MAX_STACK_DEPTH = 32
  22. };
  23. static uint8 *s_pBuf = NULL;
  24. static int s_pBufStackDepth[MAX_STACK_DEPTH];
  25. static int s_nBufDepth = -1;
  26. static int s_nBufCurSize = 0;
  27. static int s_nBufAllocSize = 0;
  28. static bool s_oomerror_called = false;
  29. void MemAllocOOMError( size_t nSize )
  30. {
  31. if ( !s_oomerror_called )
  32. {
  33. s_oomerror_called = true;
  34. MinidumpUserStreamInfoAppend( "MemAllocOOMError: %u bytes\n", (uint)nSize );
  35. //$ TODO: Need a good error message here.
  36. // A basic advice to try lowering texture settings is just most-likely to help users who are exhausting address
  37. // space, but not necessarily the cause. Ideally the engine wouldn't let you get here because of too-high settings.
  38. Error( "Out of memory or address space. Texture quality setting may be too high.\n" );
  39. }
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Other DLL-exported methods for particular kinds of memory
  43. //-----------------------------------------------------------------------------
  44. void *MemAllocScratch( int nMemSize )
  45. {
  46. // Minimally allocate 1M scratch
  47. if (s_nBufAllocSize < s_nBufCurSize + nMemSize)
  48. {
  49. s_nBufAllocSize = s_nBufCurSize + nMemSize;
  50. if (s_nBufAllocSize < 1024 * 1024)
  51. {
  52. s_nBufAllocSize = 1024 * 1024;
  53. }
  54. if (s_pBuf)
  55. {
  56. s_pBuf = (uint8*)PvRealloc( s_pBuf, s_nBufAllocSize );
  57. Assert( s_pBuf );
  58. }
  59. else
  60. {
  61. s_pBuf = (uint8*)PvAlloc( s_nBufAllocSize );
  62. }
  63. }
  64. int nBase = s_nBufCurSize;
  65. s_nBufCurSize += nMemSize;
  66. ++s_nBufDepth;
  67. Assert( s_nBufDepth < MAX_STACK_DEPTH );
  68. s_pBufStackDepth[s_nBufDepth] = nMemSize;
  69. return &s_pBuf[nBase];
  70. }
  71. void MemFreeScratch()
  72. {
  73. Assert( s_nBufDepth >= 0 );
  74. s_nBufCurSize -= s_pBufStackDepth[s_nBufDepth];
  75. --s_nBufDepth;
  76. }
  77. #ifdef POSIX
  78. void ZeroMemory( void *mem, size_t length )
  79. {
  80. memset( mem, 0x0, length );
  81. }
  82. #endif