refcount.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. ** Command & Conquer Renegade(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. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Westwood Library *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/refcount.cpp $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 8/28/01 9:24a $*
  29. * *
  30. * $Revision:: 17 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * RefCountClass::Add_Active_Ref -- add a new referenced object to the list *
  35. * RefCountClass::Set_Ref_Owner -- update the owner file/line for the given object *
  36. * RefCountClass::Remove_Active_Ref -- remove an object from the active refs list *
  37. * RefCountClass::Validate_Active_Ref -- Confirm a pointer has a node in the active ref list *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include "refcount.h"
  40. #include <windows.h>
  41. #ifndef NDEBUG
  42. // #define PARANOID_REFCOUNTS
  43. /*
  44. ** Static variables for the reference counting system
  45. */
  46. int RefCountClass::TotalRefs = 0;
  47. RefCountListClass RefCountClass::ActiveRefList;
  48. /***********************************************************************************************
  49. * RefCountClass::Add_Active_Ref -- add a new referenced object to the list *
  50. * *
  51. * INPUT: *
  52. * *
  53. * OUTPUT: *
  54. * *
  55. * WARNINGS: *
  56. * *
  57. * HISTORY: *
  58. * 3/16/98 GTH : Created. *
  59. *=============================================================================================*/
  60. RefCountClass * RefCountClass::Add_Active_Ref(RefCountClass *obj)
  61. {
  62. ActiveRefList.Add_Head(&(obj->ActiveRefNode));
  63. obj->ActiveRefInfo.File = NULL; // default to no debug information added.
  64. obj->ActiveRefInfo.Line = 0;
  65. return obj;
  66. }
  67. /***********************************************************************************************
  68. * RefCountClass::Set_Ref_Owner -- update the owner file/line for the given object *
  69. * *
  70. * INPUT: *
  71. * *
  72. * OUTPUT: *
  73. * *
  74. * WARNINGS: *
  75. * *
  76. * HISTORY: *
  77. * 3/16/98 GTH : Created. *
  78. *=============================================================================================*/
  79. RefCountClass * RefCountClass::Set_Ref_Owner(RefCountClass *obj,char * file,int line)
  80. {
  81. // static RefCountClass *hunt = (RefCountClass *)0x06558890;
  82. static RefCountClass *hunt = (RefCountClass *)0x0;
  83. if (obj == hunt) {
  84. assert(0);
  85. }
  86. obj->ActiveRefInfo.File = file;
  87. obj->ActiveRefInfo.Line = line;
  88. return obj;
  89. }
  90. /***********************************************************************************************
  91. * RefCountClass::Remove_Active_Ref -- remove an object from the active refs list *
  92. * *
  93. * INPUT: *
  94. * *
  95. * OUTPUT: *
  96. * *
  97. * WARNINGS: *
  98. * *
  99. * HISTORY: *
  100. * 3/16/98 GTH : Created. *
  101. *=============================================================================================*/
  102. void RefCountClass::Remove_Active_Ref(RefCountClass * obj)
  103. {
  104. #ifdef PARANOID_REFCOUNTS
  105. assert(Validate_Active_Ref(obj));
  106. #endif
  107. obj->ActiveRefNode.Unlink();
  108. }
  109. /***********************************************************************************************
  110. * RefCountClass::Validate_Active_Ref -- Confirm a pointer has a node in the active ref list *
  111. * *
  112. * INPUT: *
  113. * *
  114. * OUTPUT: *
  115. * *
  116. * WARNINGS: *
  117. * *
  118. * HISTORY: *
  119. * 2/06/99 EHC: Created. *
  120. *=============================================================================================*/
  121. bool RefCountClass::Validate_Active_Ref(RefCountClass * obj)
  122. {
  123. RefCountNodeClass *node = ActiveRefList.First();
  124. while (node) {
  125. if (node->Get() == obj) return true;
  126. node = node->Next();
  127. }
  128. return false;
  129. }
  130. /***********************************************************************************************
  131. * RefCountClass::Inc_Total_Refs -- increments the total reference count *
  132. * *
  133. * INPUT: *
  134. * *
  135. * OUTPUT: *
  136. * *
  137. * WARNINGS: *
  138. * *
  139. * HISTORY: *
  140. * 2/06/99 EHC: Created. *
  141. *=============================================================================================*/
  142. void RefCountClass::Inc_Total_Refs(RefCountClass * obj)
  143. {
  144. #ifdef PARANOID_REFCOUNTS
  145. assert(Validate_Active_Ref(obj));
  146. #endif
  147. TotalRefs++;
  148. }
  149. // SKB 7/21/99 Set BreakOnRefernce to a pointer and it will break when called.
  150. // This is used for debugging, please do not deleted.
  151. RefCountClass* BreakOnReference = 0;
  152. #ifndef NDEBUG
  153. void RefCountClass::Add_Ref(void)
  154. {
  155. NumRefs++;
  156. // See if programmer set break on for a specific address.
  157. if (this == BreakOnReference) {
  158. DebugBreak(); // trigger the debugger
  159. }
  160. Inc_Total_Refs(this);
  161. }
  162. #endif
  163. /***********************************************************************************************
  164. * RefCountClass::Validate_Active_Ref -- decrements the total reference count *
  165. * *
  166. * INPUT: *
  167. * *
  168. * OUTPUT: *
  169. * *
  170. * WARNINGS: *
  171. * *
  172. * HISTORY: *
  173. * 2/06/99 EHC: Created. *
  174. *=============================================================================================*/
  175. void RefCountClass::Dec_Total_Refs(RefCountClass * obj)
  176. {
  177. #ifdef PARANOID_REFCOUNTS
  178. assert(Validate_Active_Ref(obj));
  179. #endif
  180. TotalRefs--;
  181. // See if programmer set break on for a specific address.
  182. if (obj == BreakOnReference) {
  183. DebugBreak(); // trigger the debugger
  184. }
  185. }
  186. #endif