Skip to content

Commit

Permalink
Merge pull request #1859 from csound/feature/deinit
Browse files Browse the repository at this point in the history
Deinit functions
  • Loading branch information
kunstmusik committed May 12, 2024
2 parents f24e981 + eaa2b31 commit d7bc57c
Show file tree
Hide file tree
Showing 165 changed files with 11,559 additions and 13,402 deletions.
12 changes: 8 additions & 4 deletions Engine/csound_orc_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1935,10 +1935,14 @@ if (engineState != &csound->engineState) {
continue;
if (PARSER_DEBUG)
csound->DebugMsg(csound, "Instr 0 check on opcode=%s\n", bp->t.opcod);
if (UNLIKELY((thread = oentry->thread) & 06 ||
(!thread && bp->t.pftype != 'b'))) {
csound->DebugMsg(csound, "***opcode=%s thread=%d pftype=%c\n",
bp->t.opcod, thread, bp->t.pftype);
/* VL: now the check is simply for oentry->perf, which is the
only condition possible for perf-time code
*/
if (UNLIKELY(oentry->perf != NULL ||
(oentry->init == NULL && bp->t.pftype != 'b'))) {
thread = oentry->init && oentry->perf ? 3 : (oentry->init ? 1 : 2);
csound->DebugMsg(csound, "***opcode=%s thread=%d",
bp->t.opcod, thread);
/* synterr(csound,
Str("perf-pass statements illegal in header blk (%s)\n"),
oentry->opname);*/
Expand Down
9 changes: 4 additions & 5 deletions Engine/csound_orc_semantics.c
Original file line number Diff line number Diff line change
Expand Up @@ -2205,7 +2205,7 @@ int add_struct_definition(CSOUND* csound, TREE* structDefTree) {
// csound->Message(csound, "Member Found: %s : %s\n", memBase, typedIdentArg);

CONS_CELL* member = csound->Calloc(csound, sizeof(CONS_CELL));
member->value = var;
member->value = var;
type->members = cs_cons_append(type->members, member);
current = current->next;
}
Expand All @@ -2220,10 +2220,9 @@ int add_struct_definition(CSOUND* csound, TREE* structDefTree) {
oentry.opname = cs_strdup(csound, temp);
oentry.dsblksiz = sizeof(INIT_STRUCT_VAR);
oentry.flags = 0;
oentry.thread = 1;
oentry.iopadr = initStructVar;
oentry.kopadr = NULL;
oentry.aopadr = NULL;
oentry.init = initStructVar;
oentry.perf = NULL;
oentry.deinit = NULL;
oentry.useropinfo = NULL;

/* FIXME - this is not yet implemented */
Expand Down
1,962 changes: 986 additions & 976 deletions Engine/entry1.c

Large diffs are not rendered by default.

117 changes: 63 additions & 54 deletions Engine/insert.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static int init_pass(CSOUND *csound, INSDS *ip) {
csound->op = csound->ids->optext->t.oentry->opname;
csound->Message(csound, "init %s:\n", csound->op);
}
error = (*csound->ids->iopadr)(csound, csound->ids);
error = (*csound->ids->init)(csound, csound->ids);
}
csound->mode = 0;
if(csound->oparms->realtime)
Expand All @@ -140,11 +140,11 @@ static int reinit_pass(CSOUND *csound, INSDS *ip, OPDS *ids) {
csound->ids = ids;
csound->mode = 1;
while (error == 0 && (csound->ids = csound->ids->nxti) != NULL &&
(csound->ids->iopadr != (SUBR) rireturn)){
(csound->ids->init != (SUBR) rireturn)){
csound->op = csound->ids->optext->t.oentry->opname;
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "reinit %s:\n", csound->op);
error = (*csound->ids->iopadr)(csound, csound->ids);
error = (*csound->ids->init)(csound, csound->ids);
}
csound->mode = 0;

Expand Down Expand Up @@ -263,7 +263,7 @@ int init0(CSOUND *csound)
csound->mode = 1;
while ((csound->ids = csound->ids->nxti) != NULL) {
csound->op = csound->ids->optext->t.oentry->opname;
(*csound->ids->iopadr)(csound, csound->ids); /* run all i-code */
(*csound->ids->init)(csound, csound->ids); /* run all i-code */
}
csound->mode = 0;
return csound->inerrcnt; /* return errcnt */
Expand Down Expand Up @@ -918,13 +918,29 @@ static void schedofftim(CSOUND *csound, INSDS *ip)
extern int csoundDeinitialiseOpcodes(CSOUND *csound, INSDS *ip);
int useropcd(CSOUND *, UOPCODE*);


void deinit_pass(CSOUND *csound, INSDS *ip) {
int error = 0;
OPDS *dds = (OPDS *) ip;
const char* op;
while (error == 0 && (dds = dds->nxtd) != NULL) {
if (UNLIKELY(csound->oparms->odebug)) {
op = dds->optext->t.oentry->opname;
csound->Message(csound, "deinit %s:\n", op);
}
error = (*dds->deinit)(csound, dds);
if(error) {
op = dds->optext->t.oentry->opname;
csound->ErrorMsg(csound, "%s deinit error\n", op);
}
}
}

static void deact(CSOUND *csound, INSDS *ip)
{ /* unlink single instr from activ chain */
INSDS *nxtp; /* and mark it inactive */
/* close any files in fd chain */

if (ip->nxtd != NULL)
csoundDeinitialiseOpcodes(csound, ip);
/* do deinit pass */
deinit_pass(csound, ip);
/* remove an active instrument */
csound->engineState.instrtxtp[ip->insno]->active--;
if (ip->xtratim > 0)
Expand All @@ -949,11 +965,11 @@ static void deact(CSOUND *csound, INSDS *ip)
p->cvt_out[k] = NULL; // clear pointer
}
else break; // first null indicates end of cvt list

deact(csound, p->ip); /* deactivate */
p->ip = NULL;
/* IV - Oct 26 2002: set perf routine to "not initialised" */
p->h.opadr = (SUBR) useropcd;
p->h.perf = (SUBR) useropcd;
ip->opcod_deact = NULL;
}
if (ip->subins_deact) {
Expand Down Expand Up @@ -997,7 +1013,6 @@ int kill_instance(CSOUND *csound, KILLOP *p) {
void xturnoff(CSOUND *csound, INSDS *ip) /* turnoff a particular insalloc */
{ /* called by inexclus on ctrl 111 */
MCHNBLK *chn;

if (UNLIKELY(ip->relesing))
return; /* already releasing: nothing to do */

Expand Down Expand Up @@ -1276,7 +1291,7 @@ int subinstrset_(CSOUND *csound, SUBINST *p, int instno)
int n, init_op, inarg_ofs;
INSDS *pip = p->h.insdshead;

init_op = (p->h.opadr == NULL ? 1 : 0);
init_op = (p->h.perf == NULL ? 1 : 0);
inarg_ofs = (init_op ? 0 : SUBINSTNUMOUTS);
if (UNLIKELY(instno < 0)) return NOTOK;
/* IV - Oct 9 2002: need this check */
Expand Down Expand Up @@ -1407,7 +1422,7 @@ int subinstrset_(CSOUND *csound, SUBINST *p, int instno)
csound->mode = 1;
while ((csound->ids = csound->ids->nxti) != NULL) {
csound->op = csound->ids->optext->t.oentry->opname;
(*csound->ids->iopadr)(csound, csound->ids);
(*csound->ids->init)(csound, csound->ids);
}
csound->mode = 0;
p->ip->init_done = 1;
Expand All @@ -1427,7 +1442,7 @@ int subinstrset_(CSOUND *csound, SUBINST *p, int instno)
int subinstrset_S(CSOUND *csound, SUBINST *p){
int instno, init_op, inarg_ofs;
/* check if we are using subinstrinit or subinstr */
init_op = (p->h.opadr == NULL ? 1 : 0);
init_op = (p->h.perf == NULL ? 1 : 0);
inarg_ofs = (init_op ? 0 : SUBINSTNUMOUTS);
instno = strarg2insno(csound, ((STRINGDAT *)p->ar[inarg_ofs])->data, 1);
if (UNLIKELY(instno==NOT_AN_INSTRUMENT)) instno = -1;
Expand All @@ -1438,7 +1453,7 @@ int subinstrset_S(CSOUND *csound, SUBINST *p){
int subinstrset(CSOUND *csound, SUBINST *p){
int instno, init_op, inarg_ofs;
/* check if we are using subinstrinit or subinstr */
init_op = (p->h.opadr == NULL ? 1 : 0);
init_op = (p->h.perf == NULL ? 1 : 0);
inarg_ofs = (init_op ? 0 : SUBINSTNUMOUTS);
instno = (int) *(p->ar[inarg_ofs]);
return subinstrset_(csound,p,instno);
Expand Down Expand Up @@ -1580,7 +1595,7 @@ int useropcdset(CSOUND *csound, UOPCODE *p)
buf->iflag = 0;
while (csound->ids != NULL) {
csound->op = csound->ids->optext->t.oentry->opname;
(*csound->ids->iopadr)(csound, csound->ids);
(*csound->ids->init)(csound, csound->ids);
csound->ids = csound->ids->nxti;
}
csound->mode = 0;
Expand All @@ -1606,12 +1621,12 @@ int useropcdset(CSOUND *csound, UOPCODE *p)
int ksmps_scale = lcurip->ksmps / parent_ip->ksmps;
parent_ip->xtratim = lcurip->xtratim * ksmps_scale;
if(lcurip->esr == parent_ip->esr) // (1) local sr == parent sr
p->h.opadr = (SUBR) useropcd1;
p->h.perf = (SUBR) useropcd1;
else // (2) local sr < parent sr
p->h.opadr = (SUBR) useropcd2;
p->h.perf = (SUBR) useropcd2;
} else { // (3) local sr >= parent sr
parent_ip->xtratim = lcurip->xtratim;
p->h.opadr = (SUBR) useropcd2;
p->h.perf = (SUBR) useropcd2;
}
// debug msg
if (UNLIKELY(csound->oparms->odebug))
Expand Down Expand Up @@ -2107,7 +2122,7 @@ int subinstr(CSOUND *csound, SUBINST *p)
if ((CS_PDS = (OPDS *) (ip->nxtp)) != NULL) {
CS_PDS->insdshead->pds = NULL;
do {
error = (*CS_PDS->opadr)(csound, CS_PDS);
error = (*CS_PDS->perf)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL) {
CS_PDS = CS_PDS->insdshead->pds;
CS_PDS->insdshead->pds = NULL;
Expand Down Expand Up @@ -2149,7 +2164,7 @@ int subinstr(CSOUND *csound, SUBINST *p)
memset(p->ar, 0, sizeof(MYFLT)*CS_KSMPS*p->OUTCOUNT);
goto endin;
}
error = (*CS_PDS->opadr)(csound, CS_PDS);
error = (*CS_PDS->perf)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL) {
CS_PDS = CS_PDS->insdshead->pds;
CS_PDS->insdshead->pds = NULL;
Expand Down Expand Up @@ -2264,7 +2279,7 @@ int useropcd1(CSOUND *csound, UOPCODE *p)
CS_PDS->insdshead->pds = NULL;
do {
if(UNLIKELY(!ATOMIC_GET8(p->ip->actflg))) goto endop;
error = (*CS_PDS->opadr)(csound, CS_PDS);
error = (*CS_PDS->perf)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL &&
CS_PDS->insdshead->pds->insdshead) {
CS_PDS = CS_PDS->insdshead->pds;
Expand Down Expand Up @@ -2373,7 +2388,7 @@ int useropcd1(CSOUND *csound, UOPCODE *p)
CS_PDS->insdshead->pds = NULL;
do {
if(UNLIKELY(!ATOMIC_GET8(p->ip->actflg))) goto endop;
error = (*CS_PDS->opadr)(csound, CS_PDS);
error = (*CS_PDS->perf)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL &&
CS_PDS->insdshead->pds->insdshead) {
CS_PDS = CS_PDS->insdshead->pds;
Expand Down Expand Up @@ -2554,7 +2569,7 @@ int useropcd2(CSOUND *csound, UOPCODE *p)
p->ip->kcounter++; /* kcount should be incremented BEFORE perf */
do {
if(UNLIKELY(!ATOMIC_GET8(p->ip->actflg))) goto endop;
error = (*CS_PDS->opadr)(csound, CS_PDS);
error = (*CS_PDS->perf)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL &&
CS_PDS->insdshead->pds->insdshead) {
CS_PDS = CS_PDS->insdshead->pds;
Expand Down Expand Up @@ -2636,7 +2651,7 @@ static void instance(CSOUND *csound, int insno)
INSTRTXT *tp;
INSDS *ip;
OPTXT *optxt;
OPDS *opds, *prvids, *prvpds;
OPDS *opds, *prvids, *prvpds, *prvpdd;
const OENTRY *ep;
int i, n, pextent, pextra, pextrab;
char *nxtopds, *opdslim;
Expand Down Expand Up @@ -2709,7 +2724,7 @@ static void instance(CSOUND *csound, int insno)
Str("instr %d allocated at %p\n\tlclbas %p, opds %p\n"),
insno, ip, lclbas, nxtopds);
optxt = (OPTXT*) tp;
prvids = prvpds = (OPDS*) ip;
prvids = prvpds = prvpdd = (OPDS*) ip;
// prvids->insdshead = ip;

/* initialize vars for CS_TYPE */
Expand Down Expand Up @@ -2742,38 +2757,32 @@ static void instance(CSOUND *csound, int insno)
LBLBLK *lblbp = (LBLBLK *) opds;
lblbp->prvi = prvids; /* save i/p links */
lblbp->prvp = prvpds;
lblbp->prvd = prvpdd;
continue; /* for later refs */
}
// ******** This needs revisipn with no distinction between k- and a- rate ****
if ((ep->thread & 03) == 0) { /* thread 1 OR 2: */
if (ttp->pftype == 'b') {
prvids = prvids->nxti = opds;
opds->iopadr = ep->iopadr;
}

if (ep->init != NULL) { /* init */
prvids = prvids->nxti = opds; /* link into ichain */
opds->init = ep->init; /* & set exec adr */
if (UNLIKELY(odebug))
csound->Message(csound, "%s init = %p\n",
ep->opname,(void*) opds->init);
}
else {
prvpds = prvpds->nxtp = opds;
opds->opadr = ep->kopadr;
if (ep->perf != NULL) { /* perf */
prvpds = prvpds->nxtp = opds; /* link into pchain */
opds->perf = ep->perf; /* perf */
if (UNLIKELY(odebug))
csound->Message(csound, "%s perf = %p\n",
ep->opname,(void*) opds->perf);
}
goto args;
}
if ((ep->thread & 01) != 0) { /* thread 1: */
prvids = prvids->nxti = opds; /* link into ichain */
opds->iopadr = ep->iopadr; /* & set exec adr */
if (UNLIKELY(opds->iopadr == NULL))
csoundDie(csound, Str("null iopadr"));
}
if ((n = ep->thread & 02) != 0) { /* thread 2 : */
prvpds = prvpds->nxtp = opds; /* link into pchain */
/* if (!(n & 04) || */
/* ((ttp->pftype == 'k' || ttp->pftype == 'c') && ep->kopadr != NULL)) */
opds->opadr = ep->kopadr; /* krate or */
/* else opds->opadr = ep->aopadr; /\* arate *\/ */
if(ep->deinit != NULL) { /* deinit */
prvpdd = prvpdd->nxtd = opds; /* link into dchain */
opds->deinit = ep->deinit; /* deinit */
if (UNLIKELY(odebug))
csound->Message(csound, "opadr = %p\n", (void*) opds->opadr);
if (UNLIKELY(opds->opadr == NULL))
csoundDie(csound, Str("null opadr"));
}
args:
csound->Message(csound, "%s deinit = %p\n",
ep->opname,(void*) opds->deinit);
}

if (ep->useropinfo == NULL)
argpp = (MYFLT **) ((char *) opds + sizeof(OPDS));
else /* user defined opcodes are a special case */
Expand Down
8 changes: 4 additions & 4 deletions Engine/symbtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ OENTRY* csound_find_internal_oentry(CSOUND* csound, OENTRY* oentry) {

while (items != NULL) {
ep = items->value;
if (oentry->iopadr == ep->iopadr &&
oentry->kopadr == ep->kopadr &&
oentry->aopadr == ep->aopadr &&
if (oentry->init == ep->init &&
oentry->perf == ep->perf &&
oentry->deinit == ep->deinit &&
strcmp(oentry->opname, ep->opname) == 0 &&
strcmp(oentry->outypes, ep->outypes) == 0 &&
strcmp(oentry->intypes, ep->intypes) == 0) {
Expand Down Expand Up @@ -484,7 +484,7 @@ int add_udo_definition(CSOUND *csound, char *opname,
memcpy(&tmpEntry, opc, sizeof(OENTRY));
tmpEntry.opname = cs_strdup(csound, opname);

csound->AppendOpcodes(csound, &tmpEntry, 1);
csoundAppendOpcodes(csound, &tmpEntry, 1);
newopc = csound_find_internal_oentry(csound, &tmpEntry);

newopc->useropinfo = (void*) inm; /* ptr to opcode parameters */
Expand Down
23 changes: 7 additions & 16 deletions Engine/ugen.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ PUBLIC UGEN* ugen_new(UGEN_FACTORY* factory, char* opName, char* outargTypes, ch

opds = ugen->opcodeMem;
opds->insdshead = insds;
opds->iopadr = oentry->iopadr;
opds->opadr = oentry->kopadr;
opds->init = oentry->init;
opds->perf = oentry->perf;
opds->optext = optxt;


Expand Down Expand Up @@ -308,6 +308,7 @@ PUBLIC UGEN* ugen_new(UGEN_FACTORY* factory, char* opName, char* outargTypes, ch
ugen->data = (MYFLT*)csound->Calloc(csound, ugen->outPool->poolSize + ugen->inPool->poolSize);

/*MYFLT* temp = (MYFLT*)this->opcodeMem +(sizeof(OPDS) / sizeof(MYFLT));*/

/*MYFLT** p = (MYFLT**) temp;*/
/*int outOffset = outPool->poolSize / sizeof(MYFLT);*/
/*int count = 0;*/
Expand Down Expand Up @@ -345,28 +346,18 @@ PUBLIC int ugen_init(UGEN* ugen) {
OPDS* opds = (OPDS*)ugen->opcodeMem;
OENTRY* oentry = ugen->oentry;
opds->optext->t.inArgCount = ugen->inocount;
if (oentry->iopadr != NULL) {
return (*oentry->iopadr)(ugen->csound, ugen->opcodeMem);
if (oentry->init != NULL) {
return (*oentry->init)(ugen->csound, ugen->opcodeMem);
}
return CSOUND_SUCCESS;
}

PUBLIC int ugen_perform(UGEN* ugen) {
// TODO - check how csound chooses kopadr vs. aopadr
OENTRY* oentry = ugen->oentry;
CSOUND* csound = ugen->csound;
void* opcodeMem = ugen->opcodeMem;
if((oentry->thread & 2) == 2) {
if (oentry->kopadr != NULL) {
return (*oentry->kopadr)(csound, opcodeMem);
}
}
if((oentry->thread & 4) == 4) {
if (oentry->aopadr != NULL) {
return (*oentry->aopadr)(csound, opcodeMem);
}
}

if (oentry->perf != NULL)
return (*oentry->perf)(csound, opcodeMem);
return CSOUND_SUCCESS;
}

Expand Down
3 changes: 3 additions & 0 deletions H/diskin2.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ int32_t diskin2_init_array_S(CSOUND *csound, DISKIN2_ARRAY *p);
int32_t diskin_init_array_I(CSOUND *csound, DISKIN2_ARRAY *p);
int32_t diskin_init_array_S(CSOUND *csound, DISKIN2_ARRAY *p);
int32_t diskin2_perf_array(CSOUND *csound, DISKIN2_ARRAY *p);
int32_t diskin2_async_deinit(CSOUND *csound, DISKIN2 *p);
int32_t soundout_deinit(CSOUND *csound, void *pp);
int32_t diskin2_async_deinit_array(CSOUND *csound, DISKIN2_ARRAY *p);

typedef struct {
OPDS h;
Expand Down

0 comments on commit d7bc57c

Please sign in to comment.