Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
pointerremap.cpp
Go to the documentation of this file.
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
19/***********************************************************************************************
20 *** 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 ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWSaveLoad *
24 * *
25 * $Archive:: /Commando/Code/wwsaveload/pointerremap.cpp $*
26 * *
27 * Author:: Greg Hjelstrom *
28 * *
29 * $Modtime:: 5/09/01 11:36a $*
30 * *
31 * $Revision:: 9 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37
38#include "pointerremap.h"
39#include "refcount.h"
40#include "wwdebug.h"
41
42
44
45
47{
48 PointerPairTable.Set_Growth_Step(POINTER_TABLES_GROWTH_STEP);
49 PointerRequestTable.Set_Growth_Step(POINTER_TABLES_GROWTH_STEP);
50 RefCountRequestTable.Set_Growth_Step(POINTER_TABLES_GROWTH_STEP);
51}
52
56
58{
59 PointerPairTable.Delete_All();
60 PointerRequestTable.Delete_All();
61 RefCountRequestTable.Delete_All();
62}
63
65{
66 if ( PointerPairTable.Count() > 0 ) {
67 qsort(&PointerPairTable[0], PointerPairTable.Count(), sizeof(PointerPairTable[0]), ptr_pair_compare_function);
68 }
69
70 if ( PointerRequestTable.Count() > 0 ) {
71 WWASSERT( PointerPairTable.Count() > 0 );
72 qsort(&PointerRequestTable[0],PointerRequestTable.Count(), sizeof(PointerRequestTable[0]), ptr_request_compare_function);
73 Process_Request_Table(PointerRequestTable,false);
74 }
75
76 // remap the ref-counted pointers
77 if ( RefCountRequestTable.Count() > 0 ) {
78 WWASSERT( PointerPairTable.Count() > 0 );
79 qsort(&RefCountRequestTable[0],RefCountRequestTable.Count(), sizeof(RefCountRequestTable[0]), ptr_request_compare_function);
80 Process_Request_Table(RefCountRequestTable,true);
81 }
82}
83
84void PointerRemapClass::Process_Request_Table(DynamicVectorClass<PtrRemapStruct> & request_table,bool refcount)
85{
86 // Remap the pointers
87 int pointer_index = 0;
88 int pair_index = 0;
89
90 for (pointer_index = 0; pointer_index < request_table.Count(); pointer_index++) {
91
92 void * pointer_to_remap = *(request_table[pointer_index].PointerToRemap);
93 int pre_search_index = pair_index;
94
95 // Find the pair which contains the pointer we are looking for as its "old" pointer
96 while ( (pair_index < PointerPairTable.Count()) &&
97 (PointerPairTable[pair_index].OldPointer < pointer_to_remap) )
98 {
99 pair_index++;
100 }
101
102 if ((pair_index < PointerPairTable.Count()) && (PointerPairTable[pair_index].OldPointer == pointer_to_remap)) {
103
104 // we found the match, plug in the new pointer and add a ref if needed.
105 *request_table[pointer_index].PointerToRemap = PointerPairTable[pair_index].NewPointer;
106
107 if (refcount) {
108 RefCountClass * refptr = (RefCountClass *)(*request_table[pointer_index].PointerToRemap);
109 refptr->Add_Ref();
110 }
111
112 } else {
113
114 // Failed to re-map the pointer.
115 // warn the user, set pointer to NULL, reset index to the pre_search_index.
116 // If this happens, things could be going very wrong. (find out why its happening!)
117 pair_index = pre_search_index;
118 *request_table[pointer_index].PointerToRemap = NULL;
119#ifdef WWDEBUG
120 const char * file = request_table[pointer_index].File;
121 int line = request_table[pointer_index].Line;
122 WWDEBUG_SAY(("Warning! Failed to re-map pointer! old_ptr = 0x%X file = %s line = %d\r\n",(unsigned int)pointer_to_remap,file,line));
123 WWASSERT( 0 );
124#endif
125 }
126 }
127}
128
129void PointerRemapClass::Register_Pointer (void *old_pointer, void *new_pointer)
130{
131 PointerPairTable.Add(PtrPairStruct(old_pointer,new_pointer));
132}
133
134#ifdef WWDEBUG
135void PointerRemapClass::Request_Pointer_Remap(void **pointer_to_convert,const char * file,int line)
136{
137 PtrRemapStruct remap;
138 remap.PointerToRemap = pointer_to_convert;
139 remap.File = file;
140 remap.Line = line;
141 PointerRequestTable.Add(remap);
142}
143
144void PointerRemapClass::Request_Ref_Counted_Pointer_Remap (RefCountClass **pointer_to_convert,const char * file, int line)
145{
146 PtrRemapStruct remap;
147 remap.PointerToRemap = (void**)pointer_to_convert;
148 remap.File = file;
149 remap.Line = line;
150 RefCountRequestTable.Add(remap);
151}
152
153#else
154
155void PointerRemapClass::Request_Pointer_Remap (void **pointer_to_convert)
156{
157 PtrRemapStruct remap;
158 remap.PointerToRemap = pointer_to_convert;
159 PointerRequestTable.Add(remap);
160}
161
163{
164 PtrRemapStruct remap;
165 remap.PointerToRemap = (void**)pointer_to_convert;
166 RefCountRequestTable.Add(remap);
167}
168
169#endif
170
171/*
172** sort compare function for pointer pair structures
173** sorts by the old pointer value
174*/
175int __cdecl PointerRemapClass::ptr_pair_compare_function(void const * ptr1, void const * ptr2)
176{
177 void * old1 = ((PointerRemapClass::PtrPairStruct const *)ptr1)->OldPointer;
178 void * old2 = ((PointerRemapClass::PtrPairStruct const *)ptr2)->OldPointer;
179
180 if (old1 == old2) {
181 return(0);
182 }
183 if (old1 < old2) {
184 return(-1);
185 }
186 return(1);
187}
188
189/*
190** sort compare function for pointer remap structures
191** sorts by the old pointer value
192*/
193int __cdecl PointerRemapClass::ptr_request_compare_function(void const * ptr1, void const * ptr2)
194{
195 PtrRemapStruct * remap1 = (PtrRemapStruct *)ptr1;
196 PtrRemapStruct * remap2 = (PtrRemapStruct *)ptr2;
197
198 void * old1 = *(remap1->PointerToRemap);
199 void * old2 = *(remap2->PointerToRemap);
200
201 if (old1 == old2) {
202 return(0);
203 }
204 if (old1 < old2) {
205 return(-1);
206 }
207 return(1);
208}
209
210
#define NULL
Definition BaseType.h:92
#define WWASSERT
#define __cdecl
Definition IFF.H:44
int Count(void) const
Definition Vector.H:507
bool Add(T const &object)
Definition Vector.H:671
void Register_Pointer(void *old_pointer, void *new_pointer)
void Request_Ref_Counted_Pointer_Remap(RefCountClass **pointer_to_convert)
void Request_Pointer_Remap(void **pointer_to_convert)
void Add_Ref(void) const
Definition refcount.cpp:171
const int POINTER_TABLES_GROWTH_STEP
#define WWDEBUG_SAY(x)
Definition wwdebug.h:114