-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathClass.cpp
346 lines (267 loc) · 8.24 KB
/
Class.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//==============================================================================
//
// Class.cpp
//
// Copyright (C) 2013-2022 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "Class.h"
#include "Permanent.h"
#include <bitset>
#include <cstdint>
#include <ostream>
#include <string>
#include "Algorithms.h"
#include "ClassRegistry.h"
#include "Debug.h"
#include "Formatters.h"
#include "FunctionGuard.h"
#include "Memory.h"
#include "NbTypes.h"
#include "Restart.h"
#include "Singleton.h"
using std::ostream;
using std::string;
//------------------------------------------------------------------------------
namespace NodeBase
{
// Data that changes too frequently to unprotect and reprotect memory
// when it needs to be modified.
//
struct ClassDynamic : public Permanent
{
// Constructor.
//
ClassDynamic() = default;
// Used by Create to block-initialize a new object.
//
std::unique_ptr<Object> template_;
// The quasi-singleton instance.
//
std::unique_ptr<Object> singleton_;
};
//==============================================================================
Class::Class(ClassId cid, size_t size) :
size_(size),
vptr_(BAD_POINTER)
{
Debug::ft("Class.ctor");
dyn_.reset(new ClassDynamic);
cid_.SetId(cid);
Singleton<ClassRegistry>::Instance()->BindClass(*this);
}
//------------------------------------------------------------------------------
fn_name Class_dtor = "Class.dtor";
Class::~Class()
{
Debug::ftnt(Class_dtor);
Debug::SwLog(Class_dtor, UnexpectedInvocation, 0);
Singleton<ClassRegistry>::Extant()->UnbindClass(*this);
}
//------------------------------------------------------------------------------
ptrdiff_t Class::CellDiff()
{
uintptr_t local;
auto fake = reinterpret_cast<const Class*>(&local);
return ptrdiff(&fake->cid_, fake);
}
//------------------------------------------------------------------------------
void Class::ClaimBlocks()
{
Debug::ft("Class.ClaimBlocks");
if(dyn_->template_ != nullptr) dyn_->template_->ClaimBlocks();
if(dyn_->singleton_ != nullptr) dyn_->singleton_->ClaimBlocks();
}
//------------------------------------------------------------------------------
fn_name Class_Create = "Class.Create";
Object* Class::Create()
{
Debug::ft(Class_Create);
// Get a block for a new object and use its template to initialize it.
// Call PostInitialize to set any members that depend on run-time data.
//
if(dyn_->template_ == nullptr)
{
Debug::SwLog(Class_Create, "null template", Cid());
return nullptr;
}
Object* obj;
if(dyn_->singleton_ != nullptr)
obj = dyn_->singleton_.release();
else
obj = New(size_);
if(obj != nullptr)
{
Memory::Copy(obj, dyn_->template_.get(), size_);
obj->PostInitialize();
}
return obj;
}
//------------------------------------------------------------------------------
void Class::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
Immutable::Display(stream, prefix, options);
stream << prefix << "cid : " << cid_.to_str() << CRLF;
stream << prefix << "size : " << size_ << CRLF;
stream << prefix << "vptr : " << vptr_ << CRLF;
stream << prefix << "template : ";
if((dyn_->template_ != nullptr) && options.test(DispVerbose))
{
stream << CRLF;
dyn_->template_->Display(stream, prefix + spaces(2), options);
}
else
{
stream << strObj(dyn_->template_.get()) << CRLF;
}
stream << prefix << "singleton : ";
if((dyn_->singleton_ != nullptr) && options.test(DispVerbose))
{
stream << CRLF;
dyn_->singleton_->Display(stream, prefix + spaces(2), options);
}
else
{
stream << strObj(dyn_->singleton_.get()) << CRLF;
}
}
//------------------------------------------------------------------------------
void Class::FreeQuasiSingleton(Object* obj)
{
Debug::ft("Class.FreeQuasiSingleton");
// If a quasi-singleton is already available, return OBJ to its pool,
// else make it the quasi-singleton.
//
if(dyn_->singleton_ == nullptr)
dyn_->singleton_.reset(obj);
else
delete obj;
}
//------------------------------------------------------------------------------
Object* Class::GetQuasiSingleton()
{
Debug::ft("Class.GetQuasiSingleton");
// If the quasi-singleton is available, return it, else allocate a block
// from the pool associated with this class.
//
if(dyn_->singleton_ != nullptr)
{
return dyn_->singleton_.release();
}
return New(size_);
}
//------------------------------------------------------------------------------
fn_name Class_Initialize = "Class.Initialize";
void Class::Initialize()
{
Debug::ft(Class_Initialize);
Debug::SwLog(Class_Initialize, strOver(this), Cid());
}
//------------------------------------------------------------------------------
Object* Class::New(size_t size)
{
Debug::ft("Class.New");
auto type = MemType();
auto addr = Memory::Alloc(size, type);
return (Object*) addr;
}
//------------------------------------------------------------------------------
fn_name Class_ObjType = "Class.ObjType";
MemoryType Class::ObjType() const
{
Debug::ft(Class_ObjType);
Debug::SwLog(Class_ObjType, strOver(this), 0);
return MemNull;
}
//------------------------------------------------------------------------------
void Class::Patch(sel_t selector, void* arguments)
{
Immutable::Patch(selector, arguments);
}
//------------------------------------------------------------------------------
fn_name Class_SetQuasiSingleton = "Class.SetQuasiSingleton";
bool Class::SetQuasiSingleton(Object& obj)
{
Debug::ft(Class_SetQuasiSingleton);
// Verify OBJ and make it the quasi-singleton if it wasn't already
// registered as this class's template.
//
if(ObjType() != MemDynamic) return false;
if(!VerifyClass(obj)) return false;
if(&obj == dyn_->template_.get())
{
Debug::SwLog(Class_SetQuasiSingleton, "object is template", Cid());
return false;
}
dyn_->singleton_.reset(&obj);
return true;
}
//------------------------------------------------------------------------------
fn_name Class_SetTemplate = "Class.SetTemplate";
bool Class::SetTemplate(Object& obj)
{
Debug::ft(Class_SetTemplate);
// Verify OBJ and make it the template if it wasn't already registered
// as this class's quasi-singleton.
//
if(!VerifyClass(obj)) return false;
if(&obj == dyn_->singleton_.get())
{
Debug::SwLog(Class_SetTemplate, "object is quasi-singleton", Cid());
return false;
}
dyn_->template_.reset(&obj);
return true;
}
//------------------------------------------------------------------------------
bool Class::SetVptr(const Object& obj)
{
Debug::ft("Class.SetVptr");
// Verify OBJ and save its vptr.
//
if(!VerifyClass(obj)) return false;
FunctionGuard guard(Guard_ImmUnprotect);
vptr_ = obj.Vptr();
return true;
}
//------------------------------------------------------------------------------
void Class::Shutdown(RestartLevel level)
{
Debug::ft("Class.Shutdown");
Restart::Release(dyn_->template_);
Restart::Release(dyn_->singleton_);
}
//------------------------------------------------------------------------------
fn_name Class_VerifyClass = "Class.VerifyClass";
bool Class::VerifyClass(const Object& obj) const
{
Debug::ft(Class_VerifyClass);
auto c = obj.GetClass();
if(c == nullptr)
{
Debug::SwLog(Class_VerifyClass, "class not found", Cid());
return false;
}
if(c != this)
{
Debug::SwLog(Class_VerifyClass, "unexpected class", Cid());
return false;
}
return true;
}
}