Skip to content

Commit

Permalink
Merge branch 'master' into cornu/lex_ontology
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino authored Sep 24, 2024
2 parents 10563ca + 956ae01 commit 335bc74
Showing 1 changed file with 71 additions and 90 deletions.
161 changes: 71 additions & 90 deletions src/ivoc/checkpnt.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <../../nrnconf.h>
#if HAVE_IV // to end of file
#include <map>
#include <memory>

/*
Expand Down Expand Up @@ -192,20 +194,12 @@ static struct HocInst {
{0, 0}};

#define VPfri void*
declareTable(InstTable, VPfri, short)
implementTable(InstTable, VPfri, short)
static InstTable* inst_table_;

declareTable(Symbols, Symbol*, int)
implementTable(Symbols, Symbol*, int)

declareTable(Objects, Object*, int)
implementTable(Objects, Object*, int)
static std::map<VPfri, short>* inst_table_;

class OcCheckpoint {
public:
OcCheckpoint();
virtual ~OcCheckpoint();
virtual ~OcCheckpoint() = default;

bool write(const char*);

Expand Down Expand Up @@ -246,9 +240,9 @@ class OcCheckpoint {
private:
int cnt_;
int nobj_;
Objects* otable_;
bool (OcCheckpoint::*func_)(Symbol*);
Symbols* stable_;
std::unique_ptr<std::map<Object*, int>> otable_{};
bool (OcCheckpoint::*func_)(Symbol*) = nullptr;
std::unique_ptr<std::map<Symbol*, int>> stable_{};
Objectdata* objectdata_;
};

Expand Down Expand Up @@ -359,30 +353,14 @@ int hoc_readcheckpoint(char* fname) {
}

OcCheckpoint::OcCheckpoint() {
func_ = NULL;
stable_ = NULL;
otable_ = NULL;
if (!inst_table_) {
short i;
for (i = 1; hoc_inst_[i].pi; ++i) {
;
}
inst_table_ = new InstTable(2 * i);
for (i = 1; hoc_inst_[i].pi; ++i) {
inst_table_->insert((VPfri) hoc_inst_[i].pi, i);
inst_table_ = new std::map<VPfri, short>{};
for (int i = 1; hoc_inst_[i].pi; ++i) {
inst_table_->emplace((VPfri) hoc_inst_[i].pi, i);
}
}
}

OcCheckpoint::~OcCheckpoint() {
if (stable_) {
delete stable_;
}
if (otable_) {
delete otable_;
}
}

bool OcCheckpoint::write(const char* fname) {
bool b;
int i;
Expand Down Expand Up @@ -427,10 +405,7 @@ bool OcCheckpoint::make_sym_table() {
}
fprintf(f_, "#symbols=%d\n", cnt_);
b = (b && xdr(cnt_));
if (stable_) {
delete stable_;
}
stable_ = new Symbols(2 * cnt_);
stable_ = std::make_unique<std::map<Symbol*, int>>();
cnt_ = 1;
func_ = &OcCheckpoint::sym_table_install;
if (!b) {
Expand All @@ -453,13 +428,13 @@ bool OcCheckpoint::sym_count(Symbol* s) {
}

bool OcCheckpoint::sym_table_install(Symbol* s) {
stable_->insert(s, cnt_);
stable_->emplace(s, cnt_);
++cnt_;
return true;
}
bool OcCheckpoint::sym_out(Symbol* s) {
int val;
stable_->find(val, s);
auto it = stable_->find(s);
int val = it != stable_->end() ? it->second : 0;
fprintf(f_, "%d %s %d %d\n", val, s->name, s->type, s->subtype);
int l1 = strlen(s->name);
bool b = xdr(val) && xdr(s->name, l1) && xdr(s->type) && xdr(s->subtype) && xdr(s->cpublic) &&
Expand Down Expand Up @@ -553,11 +528,12 @@ bool OcCheckpoint::symbol(Symbol* s) {
bool OcCheckpoint::sym_instructions(Symbol* s) {
Proc* p = s->u.u_proc;
if (s->type == FUNCTION || s->type == PROCEDURE) {
int val;
if (!stable_->find(val, s)) {
auto it = stable_->find(s);
if (it == stable_->end()) {
printf("couldn't find %s in table\n", s->name);
return false;
}
int val = it->second;
if (p->size) {
fprintf(f_, "instructions for %d |%s|\n", val, s->name);
fprintf(f_, "size %lu\n", p->size);
Expand All @@ -577,18 +553,17 @@ bool OcCheckpoint::sym_instructions(Symbol* s) {
}
bool OcCheckpoint::instlist(unsigned long size, Inst* in) {
for (unsigned long i = 0; i < size; ++i) {
short val;
int sval;
if (in[i].in == STOP) {
fprintf(f_, " STOP\n");
val = 0;
if (!xdr(val)) {
int sval = 0;
if (!xdr(sval)) {
printf("instlist failed 1\n");
return false;
}
continue;
}
if (inst_table_->find(val, (VPfri) in[i].pf)) {
if (auto it = inst_table_->find((VPfri) in[i].pf); it != inst_table_->end()) {
auto val = it->second;
fprintf(f_, " %d\n", val);
if (!xdr(val)) {
printf("instlist failed 2\n");
Expand All @@ -600,20 +575,22 @@ bool OcCheckpoint::instlist(unsigned long size, Inst* in) {
switch (s[j]) {
case 's':
if (in[i].sym) {
if (!stable_->find(sval, in[i].sym)) {
auto it = stable_->find(in[i].sym);
if (it == stable_->end()) {
printf("couldn't find |%s| in table at instruction index %ld\n",
in[i].sym->name,
i);
return false;
}
auto sval = it->second;
// fprintf(f_, " %d |%s|\n", sval, in[i].sym->name);
if (!xdr(sval)) {
printf("instlist failed 3\n");
return false;
}
} else {
fprintf(f_, " 0 SYMBOL0\n");
sval = 0;
int sval = 0;
if (!xdr(sval)) {
printf("instlist failed 4\n");
return false;
Expand Down Expand Up @@ -643,17 +620,19 @@ bool OcCheckpoint::ctemplate(Symbol* s) {
cTemplate* t = s->u.ctemplate;
if (func_ == &OcCheckpoint::sym_values) {
Objectdata* saveod = objectdata_;
int ti;
bool b;
b = stable_->find(ti, s);
auto it = stable_->find(s);
bool b = it != stable_->end();
auto ti = b ? it->second : 0;
fprintf(f_, "%d %d %s\n", ti, t->count, s->name);
b = b && xdr(ti);
// b = b && xdr(t->count);
hoc_Item* q;
ITERATE(q, t->olist) {
Object* ob = OBJ(q);
int oid;
b = b && otable_->find(oid, ob);
auto it = otable_->find(ob);
bool found = it != otable_->end();
b = b && found;
int oid = found ? it->second : 0;
b = b && xdr(oid);
if (t->constructor) {
if (t->checkpoint) {
Expand All @@ -677,42 +656,42 @@ bool OcCheckpoint::ctemplate(Symbol* s) {
}
}
bool OcCheckpoint::object() {
bool b;
int i;
if (otable_) {
delete otable_;
}
b = xdr(nobj_);
otable_ = new Objects(2 * nobj_ + 1);
bool b = xdr(nobj_);
otable_ = std::make_unique<std::map<Object*, int>>();
nobj_ = 0;
func_ = &OcCheckpoint::objects;
b = pass1();
i = -1;
int i = -1;
b = b && xdr(i);
return b;
}
bool OcCheckpoint::objects(Symbol* s) {
bool b = true;
if (s->type == TEMPLATE) {
int sid;
b = b && stable_->find(sid, s);
b = b && xdr(sid);
cTemplate* t = s->u.ctemplate;
if (s->type != TEMPLATE) {
return true;
}

auto it = stable_->find(s);
bool b = it != stable_->end();
int sid = b ? it->second : 0;
b = b && xdr(sid);
cTemplate* t = s->u.ctemplate;
#undef init
if (t->init) {
b = b && stable_->find(sid, t->init);
} else {
sid = 0;
}
b = b && xdr(sid);
b = b && xdr(t->index) && xdr(t->count) && xdr(t->id);
hoc_Item* q;
ITERATE(q, t->olist) {
Object* ob = OBJ(q);
++nobj_;
otable_->insert(ob, nobj_); // 0 is null object
b = b && xdr(nobj_) && xdr(ob->refcount) && xdr(ob->index);
}
if (t->init) {
auto it2 = stable_->find(t->init);
bool b2 = it2 != stable_->end();
b = b && b2;
sid = b2 ? it2->second : sid;
} else {
sid = 0;
}
b = b && xdr(sid);
b = b && xdr(t->index) && xdr(t->count) && xdr(t->id);
hoc_Item* q;
ITERATE(q, t->olist) {
Object* ob = OBJ(q);
++nobj_;
otable_->emplace(ob, nobj_); // 0 is null object
b = b && xdr(nobj_) && xdr(ob->refcount) && xdr(ob->index);
}
return b;
}
Expand Down Expand Up @@ -764,9 +743,10 @@ bool OcCheckpoint::proc(Proc*) {
}

bool OcCheckpoint::sym_values(Symbol* s) {
int sp;
bool b;
stable_->find(sp, s);
auto it = stable_->find(s);
int sp = it != stable_->end() ? it->second : 0;

if ((s->type == VAR && s->subtype == NOTUSER) || s->type == OBJECTVAR || s->type == STRING ||
s->type == SECTION) {
fprintf(f_, "%d %s\n", sp, s->name);
Expand All @@ -790,8 +770,10 @@ bool OcCheckpoint::sym_values(Symbol* s) {
int i = 0;
b = b && xdr(i);
} else {
int oid;
b = b && otable_->find(oid, ob);
auto it = otable_->find(ob);
bool found = it != otable_->end();
int oid = found ? it->second : 0;
b = b && found;
b = b && xdr(oid);
}
} else if (s->type == STRING) {
Expand Down Expand Up @@ -838,11 +820,10 @@ bool OcCheckpoint::xdr(double& i) {
}

bool OcCheckpoint::xdr(Object*& o) {
int i;
bool b;
b = otable_->find(i, o);
b = b && xdr(i);
return b;
auto it = otable_->find(o);
bool b = it != otable_->end();
int i = b ? it->second : 0;
return b && xdr(i);
}

#undef Chk
Expand Down

0 comments on commit 335bc74

Please sign in to comment.