00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _cvc3__include__context_h_
00023 #define _cvc3__include__context_h_
00024
00025 #include <string>
00026 #include <vector>
00027 #include <stdlib.h>
00028 #include "debug.h"
00029 #include "memory_manager_context.h"
00030 #include "os.h"
00031
00032 namespace CVC3 {
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 class Context;
00043 class ContextManager;
00044 class ContextNotifyObj;
00045 class ContextObj;
00046 class ContextObjChain;
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 class Scope {
00061 friend class ContextObj;
00062 friend class ContextObjChain;
00063 friend class CDFlags;
00064
00065 Context* d_context;
00066
00067
00068 ContextMemoryManager* d_cmm;
00069
00070
00071 Scope* d_prevScope;
00072
00073
00074 int d_level;
00075
00076
00077
00078 ContextObjChain* d_restoreChain;
00079
00080
00081 void addToChain(ContextObjChain* obj);
00082
00083 public:
00084
00085 Scope(Context* context, ContextMemoryManager* cmm,
00086 Scope* prevScope = NULL)
00087 : d_context(context), d_cmm(cmm), d_prevScope(prevScope),
00088 d_restoreChain(NULL)
00089 { if (prevScope) d_level = prevScope->level() + 1; else d_level = 0; }
00090
00091 ~Scope() {}
00092
00093
00094 Scope* prevScope() const { return d_prevScope; }
00095 int level(void) const { return d_level; }
00096 bool isCurrent(void) const;
00097 Scope* topScope() const;
00098 Context* getContext() const { return d_context; }
00099 ContextMemoryManager* getCMM() const { return d_cmm; }
00100
00101 void* operator new(size_t size, MemoryManager* mm)
00102 { return mm->newData(size); }
00103 void operator delete(void* pMem, MemoryManager* mm) {
00104 mm->deleteData(pMem);
00105 }
00106 void operator delete(void*) { }
00107
00108
00109 void restore(void);
00110
00111
00112 void finalize(void);
00113
00114
00115 void check(void);
00116
00117
00118 unsigned long getMemory(int verbosity);
00119 };
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 class ContextObjChain {
00134 friend class Scope;
00135 friend class ContextObj;
00136 friend class CDFlags;
00137 private:
00138
00139 ContextObjChain* d_restoreChainNext;
00140
00141
00142
00143
00144 ContextObjChain** d_restoreChainPrev;
00145
00146
00147 ContextObjChain* d_restore;
00148
00149
00150 ContextObj* d_data;
00151
00152
00153 ContextObj* d_master;
00154
00155
00156 ContextObjChain(ContextObj* data, ContextObj* master,
00157 ContextObjChain* restore)
00158 : d_restoreChainNext(NULL), d_restoreChainPrev(NULL),
00159 d_restore(restore), d_data(data), d_master(master)
00160 { }
00161
00162
00163 ContextObjChain* restore(void);
00164 public:
00165
00166 ~ContextObjChain() {}
00167
00168 void* operator new(size_t size, MemoryManager* mm) {
00169 return mm->newData(size);
00170 }
00171 void operator delete(void* pMem, MemoryManager* mm) {
00172 mm->deleteData(pMem);
00173 }
00174
00175 void operator delete(void*) { }
00176
00177
00178
00179 void* operator new(size_t size, bool b) {
00180 return malloc(size);
00181 }
00182 void operator delete(void* pMem, bool b) {
00183 free(pMem);
00184 }
00185
00186 IF_DEBUG(std::string name() const;)
00187 };
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 class CVC_DLL ContextObj {
00201 friend class Scope;
00202 friend class ContextObjChain;
00203 friend class CDFlags;
00204 private:
00205
00206 Scope* d_scope;
00207
00208
00209
00210 ContextObjChain* d_restore;
00211
00212 IF_DEBUG(std::string d_name;)
00213 IF_DEBUG(bool d_active;)
00214
00215
00216 void update(int scope = -1);
00217
00218 protected:
00219
00220 ContextObj(const ContextObj& co)
00221 : d_scope(co.d_scope), d_restore(co.d_restore) {
00222 IF_DEBUG(d_name=co.d_name;)
00223 DebugAssert(co.d_active, "ContextObj["+co.name()+"] copy constructor");
00224 IF_DEBUG(d_active = co.d_active;)
00225
00226 }
00227
00228
00229 ContextObj& operator=(const ContextObj& co) {
00230 DebugAssert(false, "ContextObj::operator=(): shouldn't be called");
00231 return *this;
00232 }
00233
00234
00235
00236 virtual ContextObj* makeCopy(ContextMemoryManager* cmm) = 0;
00237
00238
00239 virtual void restoreData(ContextObj* data) {
00240 FatalAssert(false,
00241 "ContextObj::restoreData(): call in the base abstract class");
00242 }
00243
00244 const ContextObj* getRestore() {
00245 return d_restore ? d_restore->d_data : NULL;
00246 }
00247
00248
00249 virtual void setNull(void) = 0;
00250
00251
00252 IF_DEBUG(virtual std::string name() const { return d_name; })
00253
00254
00255 ContextMemoryManager* getCMM() { return d_scope->getCMM(); }
00256
00257 public:
00258
00259
00260
00261
00262
00263
00264
00265
00266 ContextObj(Context* context);
00267 virtual ~ContextObj();
00268
00269 int level() const { return (d_scope==NULL)? 0 : d_scope->level(); }
00270 bool isCurrent(int scope = -1) const {
00271 if(scope >= 0) return d_scope->level() == scope;
00272 else return d_scope->isCurrent();
00273 }
00274 void makeCurrent(int scope = -1) { if (!isCurrent(scope)) update(scope); }
00275 IF_DEBUG(void setName(const std::string& name) { d_name=name; })
00276
00277 void* operator new(size_t size, MemoryManager* mm) {
00278 return mm->newData(size);
00279 }
00280 void operator delete(void* pMem, MemoryManager* mm) {
00281 mm->deleteData(pMem);
00282 }
00283
00284
00285
00286 void* operator new(size_t size, bool b) {
00287 return malloc(size);
00288 }
00289 void operator delete(void* pMem, bool b) {
00290 free(pMem);
00291 }
00292
00293 void operator delete(void*) { }
00294 };
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 class CVC_DLL Context {
00307
00308 ContextManager* d_cm;
00309
00310
00311 std::string d_name;
00312
00313
00314 int d_id;
00315
00316
00317 Scope* d_topScope;
00318 Scope* d_bottomScope;
00319
00320
00321 std::vector<ContextNotifyObj*> d_notifyObjList;
00322
00323
00324 std::vector<ContextMemoryManager*> d_cmmStack;
00325
00326 public:
00327 Context(ContextManager* cm, const std::string& name, int id);
00328 ~Context();
00329
00330
00331 ContextManager* getCM() const { return d_cm; }
00332 const std::string& name() const { return d_name; }
00333 int id() const { return d_id; }
00334 Scope* topScope() const { return d_topScope; }
00335 Scope* bottomScope() const { return d_bottomScope; }
00336 int level() const { return d_topScope->level(); }
00337
00338 void push();
00339 void pop();
00340 void popto(int toLevel);
00341 void addNotifyObj(ContextNotifyObj* obj) { d_notifyObjList.push_back(obj); }
00342 void deleteNotifyObj(ContextNotifyObj* obj);
00343 unsigned long getMemory(int verbosity);
00344 };
00345
00346
00347 inline bool Scope::isCurrent(void) const
00348 { return this == d_context->topScope(); }
00349
00350 inline void Scope::addToChain(ContextObjChain* obj) {
00351 if(d_restoreChain != NULL)
00352 d_restoreChain->d_restoreChainPrev = &(obj->d_restoreChainNext);
00353 obj->d_restoreChainNext = d_restoreChain;
00354 obj->d_restoreChainPrev = &d_restoreChain;
00355 d_restoreChain = obj;
00356 }
00357
00358 inline Scope* Scope::topScope() const { return d_context->topScope(); }
00359
00360 inline void Scope::restore(void) {
00361
00362 while (d_restoreChain != NULL) d_restoreChain = d_restoreChain->restore();
00363
00364 }
00365
00366
00367
00368
00369
00370
00371 inline ContextObj::ContextObj(Context* context)
00372 IF_DEBUG(: d_name("ContextObj"))
00373 {
00374 IF_DEBUG(d_active=true;)
00375 DebugAssert(context != NULL, "NULL context pointer");
00376 d_scope = context->bottomScope();
00377 d_restore = new(true) ContextObjChain(NULL, this, NULL);
00378 d_scope->addToChain(d_restore);
00379
00380
00381 }
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 class ContextManager {
00394 Context* d_curContext;
00395 std::vector<Context*> d_contexts;
00396
00397 public:
00398 ContextManager();
00399 ~ContextManager();
00400
00401 void push() { d_curContext->push(); }
00402 void pop() { d_curContext->pop(); }
00403 void popto(int toLevel) { d_curContext->popto(toLevel); }
00404 int scopeLevel() { return d_curContext->level(); }
00405 Context* createContext(const std::string& name="");
00406 Context* getCurrentContext() { return d_curContext; }
00407 Context* switchContext(Context* context);
00408 unsigned long getMemory(int verbosity);
00409 };
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 class ContextNotifyObj {
00424 friend class Context;
00425 protected:
00426 Context* d_context;
00427 public:
00428 ContextNotifyObj(Context* context): d_context(context)
00429 { context->addNotifyObj(this); }
00430 virtual ~ContextNotifyObj() {
00431
00432
00433
00434 if(d_context!=NULL) d_context->deleteNotifyObj(this);
00435 }
00436 virtual void notifyPre(void) {}
00437 virtual void notify(void) {}
00438 virtual unsigned long getMemory(int verbosity) { return sizeof(ContextNotifyObj); }
00439 };
00440
00441
00442
00443 }
00444
00445 #endif