internal.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /////////////////////////////////////////////////////////////////////////EA-V1
  19. // $File: //depot/GeneralsMD/Staging/code/Libraries/Source/profile/internal.h $
  20. // $Author: mhoffe $
  21. // $Revision: #3 $
  22. // $DateTime: 2003/07/09 10:57:23 $
  23. //
  24. // ©2003 Electronic Arts
  25. //
  26. // Internal header
  27. //////////////////////////////////////////////////////////////////////////////
  28. #ifdef _MSC_VER
  29. # pragma once
  30. #endif
  31. #ifndef INTERNAL_H // Include guard
  32. #define INTERNAL_H
  33. #include "../debug/debug.h"
  34. #include "internal_funclevel.h"
  35. #include "internal_highlevel.h"
  36. #include "internal_cmd.h"
  37. #include "internal_result.h"
  38. class ProfileFastCS
  39. {
  40. ProfileFastCS(const ProfileFastCS&);
  41. ProfileFastCS& operator=(const ProfileFastCS&);
  42. volatile unsigned m_Flag;
  43. static HANDLE testEvent;
  44. void ThreadSafeSetFlag()
  45. {
  46. volatile unsigned& nFlag=m_Flag;
  47. #define ts_lock _emit 0xF0
  48. DASSERT(((unsigned)&nFlag % 4) == 0);
  49. __asm mov ebx, [nFlag]
  50. __asm ts_lock
  51. __asm bts dword ptr [ebx], 0
  52. __asm jc The_Bit_Was_Previously_Set_So_Try_Again
  53. return;
  54. The_Bit_Was_Previously_Set_So_Try_Again:
  55. // can't use SwitchToThread() here because Win9X doesn't have it!
  56. if (testEvent)
  57. ::WaitForSingleObject(testEvent,1);
  58. __asm mov ebx, [nFlag]
  59. __asm ts_lock
  60. __asm bts dword ptr [ebx], 0
  61. __asm jc The_Bit_Was_Previously_Set_So_Try_Again
  62. }
  63. void ThreadSafeClearFlag()
  64. {
  65. m_Flag=0;
  66. }
  67. public:
  68. ProfileFastCS(void):
  69. m_Flag(0)
  70. {
  71. }
  72. class Lock
  73. {
  74. Lock(const Lock&);
  75. Lock& operator=(const Lock&);
  76. ProfileFastCS& CriticalSection;
  77. public:
  78. Lock(ProfileFastCS& cs):
  79. CriticalSection(cs)
  80. {
  81. CriticalSection.ThreadSafeSetFlag();
  82. }
  83. ~Lock()
  84. {
  85. CriticalSection.ThreadSafeClearFlag();
  86. }
  87. };
  88. friend class Lock;
  89. };
  90. void *ProfileAllocMemory(unsigned numBytes);
  91. void *ProfileReAllocMemory(void *oldPtr, unsigned newSize);
  92. void ProfileFreeMemory(void *ptr);
  93. __forceinline void ProfileGetTime(__int64 &t)
  94. {
  95. _asm
  96. {
  97. mov ecx,[t]
  98. push eax
  99. push edx
  100. rdtsc
  101. mov [ecx],eax
  102. mov [ecx+4],edx
  103. pop edx
  104. pop eax
  105. };
  106. }
  107. #endif // INTERNAL_H