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.

93 lines
1.9 KiB

  1. //========= Copyright 1996-2005, 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. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. #include "tier0/minidump.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. //-----------------------------------------------------------------------------
  29. // Other DLL-exported methods for particular kinds of memory
  30. //-----------------------------------------------------------------------------
  31. void *MemAllocScratch( int nMemSize )
  32. {
  33. // Minimally allocate 1M scratch
  34. if (s_nBufAllocSize < s_nBufCurSize + nMemSize)
  35. {
  36. s_nBufAllocSize = s_nBufCurSize + nMemSize;
  37. if (s_nBufAllocSize < 2 * 1024)
  38. {
  39. s_nBufAllocSize = 2 * 1024;
  40. }
  41. if (s_pBuf)
  42. {
  43. s_pBuf = (uint8*)PvRealloc( s_pBuf, s_nBufAllocSize );
  44. Assert( s_pBuf );
  45. }
  46. else
  47. {
  48. s_pBuf = (uint8*)PvAlloc( s_nBufAllocSize );
  49. }
  50. }
  51. int nBase = s_nBufCurSize;
  52. s_nBufCurSize += nMemSize;
  53. ++s_nBufDepth;
  54. Assert( s_nBufDepth < MAX_STACK_DEPTH );
  55. s_pBufStackDepth[s_nBufDepth] = nMemSize;
  56. return &s_pBuf[nBase];
  57. }
  58. void MemFreeScratch()
  59. {
  60. Assert( s_nBufDepth >= 0 );
  61. s_nBufCurSize -= s_pBufStackDepth[s_nBufDepth];
  62. --s_nBufDepth;
  63. }
  64. #ifdef POSIX
  65. void ZeroMemory( void *mem, size_t length )
  66. {
  67. memset( mem, 0x0, length );
  68. }
  69. #endif
  70. void MemOutOfMemory( size_t nBytesAttempted )
  71. {
  72. if ( Plat_IsInDebugSession() )
  73. {
  74. DebuggerBreak();
  75. }
  76. else
  77. {
  78. WriteMiniDump();
  79. Plat_ExitProcess( EXIT_FAILURE );
  80. }
  81. }