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.

79 lines
1.7 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. #ifndef STEAM
  14. #define PvRealloc realloc
  15. #define PvAlloc malloc
  16. #define PvExpand _expand
  17. #endif
  18. enum
  19. {
  20. MAX_STACK_DEPTH = 32
  21. };
  22. static uint8 *s_pBuf = NULL;
  23. static int s_pBufStackDepth[MAX_STACK_DEPTH];
  24. static int s_nBufDepth = -1;
  25. static int s_nBufCurSize = 0;
  26. static int s_nBufAllocSize = 0;
  27. //-----------------------------------------------------------------------------
  28. // Other DLL-exported methods for particular kinds of memory
  29. //-----------------------------------------------------------------------------
  30. void *MemAllocScratch( int nMemSize )
  31. {
  32. // Minimally allocate 1M scratch
  33. if (s_nBufAllocSize < s_nBufCurSize + nMemSize)
  34. {
  35. s_nBufAllocSize = s_nBufCurSize + nMemSize;
  36. if (s_nBufAllocSize < 2 * 1024)
  37. {
  38. s_nBufAllocSize = 2 * 1024;
  39. }
  40. if (s_pBuf)
  41. {
  42. s_pBuf = (uint8*)PvRealloc( s_pBuf, s_nBufAllocSize );
  43. Assert( s_pBuf );
  44. }
  45. else
  46. {
  47. s_pBuf = (uint8*)PvAlloc( s_nBufAllocSize );
  48. }
  49. }
  50. int nBase = s_nBufCurSize;
  51. s_nBufCurSize += nMemSize;
  52. ++s_nBufDepth;
  53. Assert( s_nBufDepth < MAX_STACK_DEPTH );
  54. s_pBufStackDepth[s_nBufDepth] = nMemSize;
  55. return &s_pBuf[nBase];
  56. }
  57. void MemFreeScratch()
  58. {
  59. Assert( s_nBufDepth >= 0 );
  60. s_nBufCurSize -= s_pBufStackDepth[s_nBufDepth];
  61. --s_nBufDepth;
  62. }
  63. #ifdef POSIX
  64. void ZeroMemory( void *mem, size_t length )
  65. {
  66. memset( mem, 0x0, length );
  67. }
  68. #endif