Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit e54e845

Browse files
authored
Allow multiple BEFORE/AFTER blocks of same type in a mod file. (#790)
* Update coreneuron/io/phase2.cpp
1 parent 22e6e40 commit e54e845

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

coreneuron/io/phase2.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -575,19 +575,28 @@ void Phase2::fill_before_after_lists(NrnThread& nt, const std::vector<Memb_func>
575575
for (size_t ii = 0; ii < memb_func.size(); ++ii) {
576576
before_after_map[ii] = nullptr;
577577
}
578+
// Save first before-after block only. In case of multiple before-after blocks with the
579+
// same mech type, we will get subsequent ones using linked list below.
578580
for (auto bam = corenrn.get_bamech()[i]; bam; bam = bam->next) {
579-
before_after_map[bam->type] = bam;
581+
if (!before_after_map[bam->type]) {
582+
before_after_map[bam->type] = bam;
583+
}
580584
}
581-
/* unnecessary but keep in order anyway */
585+
// necessary to keep in order wrt multiple BAMech with same mech type
582586
NrnThreadBAList** ptbl = nt.tbl + i;
583587
for (auto tml = nt.tml; tml; tml = tml->next) {
584588
if (before_after_map[tml->index]) {
585-
auto tbl = (NrnThreadBAList*) emalloc(sizeof(NrnThreadBAList));
586-
tbl->next = nullptr;
587-
tbl->bam = before_after_map[tml->index];
588-
tbl->ml = tml->ml;
589-
*ptbl = tbl;
590-
ptbl = &(tbl->next);
589+
int mtype = tml->index;
590+
Memb_list* ml = tml->ml;
591+
for (auto bam = before_after_map[mtype]; bam && bam->type == mtype;
592+
bam = bam->next) {
593+
auto tbl = (NrnThreadBAList*) emalloc(sizeof(NrnThreadBAList));
594+
*ptbl = tbl;
595+
tbl->next = nullptr;
596+
tbl->bam = bam;
597+
tbl->ml = tml->ml;
598+
ptbl = &(tbl->next);
599+
}
591600
}
592601
}
593602
}

coreneuron/mechanism/register_mech.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,16 @@ void hoc_reg_ba(int mt, mod_f_t f, int type) {
391391
auto bam = (BAMech*) emalloc(sizeof(BAMech));
392392
bam->f = f;
393393
bam->type = mt;
394-
bam->next = corenrn.get_bamech()[type];
395-
corenrn.get_bamech()[type] = bam;
394+
bam->next = nullptr;
395+
// keep in call order
396+
if (!corenrn.get_bamech()[type]) {
397+
corenrn.get_bamech()[type] = bam;
398+
} else {
399+
BAMech* last;
400+
for (last = corenrn.get_bamech()[type]; last->next; last = last->next) {
401+
}
402+
last->next = bam;
403+
}
396404
}
397405

398406
void _nrn_thread_reg0(int i, void (*f)(ThreadDatum*)) {

0 commit comments

Comments
 (0)