Skip to content

Commit 14ccb6f

Browse files
Merge pull request #583 from PhracturedBlue/stackfix_mixer_depend
Reduce stack usage of MIXER_SetMixers by ~2200 bytes
2 parents 636e908 + e1a9f32 commit 14ccb6f

1 file changed

Lines changed: 52 additions & 49 deletions

File tree

src/mixer.c

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -579,77 +579,80 @@ int compact_mixers() {
579579
return i;
580580
}
581581

582-
unsigned find_dependencies(unsigned ch, unsigned *deps)
583-
{
584-
unsigned found = 0;
585-
unsigned i;
586-
struct Mixer *mixer;
587-
for (i = 0; i < NUM_SOURCES; i++)
588-
deps[i] = 0;
589-
for (mixer = Model.mixers; mixer < Model.mixers + NUM_MIXERS; mixer++) {
590-
if (MIXER_SRC(mixer->src) && mixer->dest == ch) {
591-
found = 1;
592-
if (MIXER_SRC(mixer->src) > NUM_SOURCES && MIXER_SRC(mixer->src) != NUM_SOURCES + 1 + ch) {
593-
deps[MIXER_SRC(mixer->src) - NUM_SOURCES - 1] = 1;
594-
}
595-
if (MIXER_SRC(mixer->sw) > NUM_SOURCES && MIXER_SRC(mixer->sw) != NUM_SOURCES + 1 + ch) {
596-
deps[MIXER_SRC(mixer->sw) - NUM_SOURCES - 1] = 1;
597-
}
598-
}
582+
void MIXER_ReorderMixers(struct Mixer *mixers, const u8 *neworder, unsigned count)
583+
{
584+
u8 reorder[NUM_MIXERS]; //map oldpos -> new
585+
struct Mixer tmpmixer;
586+
for (unsigned i = 0; i < count; i++) {
587+
reorder[i] = i;
588+
}
589+
for (unsigned newpos = 0; newpos < count; newpos++) {
590+
unsigned oldpos = neworder[newpos];
591+
while (reorder[oldpos] != oldpos)
592+
oldpos = reorder[oldpos];
593+
if (oldpos == newpos)
594+
continue;
595+
tmpmixer = mixers[newpos];
596+
mixers[newpos] = mixers[oldpos];
597+
mixers[oldpos] = tmpmixer;
598+
reorder[newpos] = oldpos;
599599
}
600-
return found;
601600
}
602601

603602
void fix_mixer_dependencies(unsigned mixer_count)
604603
{
605-
unsigned dependencies[NUM_SOURCES];
606-
unsigned placed[NUM_SOURCES];
604+
u8 order[NUM_MIXERS];
605+
u8 placed[NUM_SOURCES];
607606
unsigned pos = 0;
608-
unsigned last_count = 0;
609-
unsigned i;
610-
struct Mixer mixers[NUM_MIXERS];
611-
memset(mixers, 0, sizeof(mixers));
612-
for (i = 0; i < NUM_MIXERS; i++) {
613-
mixers[i].src = 0;
614-
}
615607
memset(placed, 0, sizeof(placed));
616-
while(mixer_count && last_count != mixer_count) {
617-
last_count = mixer_count;
618-
for (i = 0; i < NUM_SOURCES; i++) {
619-
if (placed[i])
620-
continue;
621-
if (! find_dependencies(i, dependencies)) {
622-
placed[i] = 1;
608+
while(pos < mixer_count) {
609+
unsigned last_count = pos;
610+
for (unsigned source = 0; source < NUM_SOURCES; source++) {
611+
if (placed[source])
623612
continue;
624-
}
613+
unsigned newpos = pos;
625614
unsigned ok = 1;
626-
unsigned j;
627-
// determine if all dependencies have been placed
628-
for (j = 0; j < NUM_SOURCES; j++) {
629-
if (dependencies[j] && ! placed[j]) {
630-
ok = 0;
631-
break;
615+
for(unsigned mixidx = 0; mixidx < NUM_MIXERS; mixidx++) {
616+
if (MIXER_SRC(Model.mixers[mixidx].src) && Model.mixers[mixidx].dest == source) {
617+
if ((MIXER_SRC(Model.mixers[mixidx].src) > NUM_SOURCES
618+
&& MIXER_SRC(Model.mixers[mixidx].src) != NUM_SOURCES + 1 + source
619+
&& ! placed[MIXER_SRC(Model.mixers[mixidx].src) - NUM_SOURCES - 1])
620+
||
621+
(MIXER_SRC(Model.mixers[mixidx].sw) > NUM_SOURCES
622+
&& MIXER_SRC(Model.mixers[mixidx].sw) != NUM_SOURCES + 1 + source
623+
&& ! placed[MIXER_SRC(Model.mixers[mixidx].sw) - NUM_SOURCES - 1])
624+
)
625+
{
626+
// We found an unplaced dependency
627+
ok = 0;
628+
break;
629+
}
630+
order[newpos++] = mixidx;
632631
}
633632
}
634633
if (ok) {
635-
unsigned num = MIXER_GetMixers(i, &mixers[pos], NUM_MIXERS);
636-
pos += num;
637-
mixer_count -= num;
638-
placed[i] = 1;
634+
placed[source] = 1;
635+
pos = newpos;
639636
}
640637
}
638+
if (last_count == pos)
639+
break;
641640
}
642-
if (mixer_count) {
641+
if (pos != mixer_count) {
643642
// We found a mixer loop....add missing mixers to the end of the order
644-
printf("Mix loop detected. The following mixers may have a circular dependency!\n");
643+
printf("Mix loop detected (%d mixers affected). The following mixers may have a circular dependency!\n", mixer_count);
645644
for (unsigned source = 0; source < NUM_SOURCES; source++) {
646645
if (placed[source])
647646
continue;
648647
printf(" Mixer source: %d\n", source);
649-
pos += MIXER_GetMixers(source, &mixers[pos], NUM_MIXERS);
648+
for(unsigned mixidx = 0; mixidx < NUM_MIXERS; mixidx++) {
649+
if (MIXER_SRC(Model.mixers[mixidx].src) && Model.mixers[mixidx].dest == source) {
650+
order[pos++] = mixidx;
651+
}
652+
}
650653
}
651654
}
652-
memcpy(Model.mixers, mixers, sizeof(mixers));
655+
MIXER_ReorderMixers(Model.mixers, order, pos);
653656
}
654657

655658
int MIXER_SetMixers(struct Mixer *mixers, int count)

0 commit comments

Comments
 (0)