Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deinit functions #1859

Merged
merged 28 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) ||
vlazzarini marked this conversation as resolved.
Show resolved Hide resolved
(oentry->perf != 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.

126 changes: 73 additions & 53 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,39 @@ 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;
/*
vlazzarini marked this conversation as resolved.
Show resolved Hide resolved
if(csound->oparms->realtime)
csoundLockMutex(csound->init_pass_threadlock);
csound->mode=1
*/
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);
}
}
/*
csound->mode = 1;
if(csound->oparms->realtime)
csoundUnlockMutex(csound->init_pass_threadlock);
CSL */
}

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 Down Expand Up @@ -953,7 +979,7 @@ static void deact(CSOUND *csound, INSDS *ip)
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 @@ -1276,7 +1302,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 +1433,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 +1453,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 +1464,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 +1606,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 +1632,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 +2133,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 +2175,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 +2290,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 +2399,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 +2580,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 +2662,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 +2735,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 +2768,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;
}
else {
prvpds = prvpds->nxtp = opds;
opds->opadr = ep->kopadr;
}
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->init != NULL) { /* init */
prvids = prvids->nxti = opds; /* link into ichain */
opds->init = ep->init; /* & set exec adr */
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 init = %p\n",
ep->opname,(void*) opds->perf);
}
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);
}
if(ep->deinit != NULL) { /* deinit */
prvpdd = prvpdd->nxtd = opds; /* link into dchain */
opds->deinit = ep->deinit; /* deinit */
if (UNLIKELY(odebug))
csound->Message(csound, "%s deinit = %p\n",
ep->opname,(void*) opds->perf);
}

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