common: prevent buffer overflow
[supercollider.git] / server / scsynth / SC_Unit.cpp
blob8a14aacbb77d6d3814d664e3f17ead36862970dd
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <stdio.h>
23 #include "SC_Endian.h"
24 #include "SC_Unit.h"
25 #include "SC_UnitSpec.h"
26 #include "SC_UnitDef.h"
27 #include "SC_World.h"
28 #include "SC_Wire.h"
29 #include "Unroll.h"
30 #include "SC_Prototypes.h"
32 void Unit_ChooseMulAddFunc(Unit* unit);
34 Unit* Unit_New(World *inWorld, UnitSpec *inUnitSpec, char*& memory)
36 UnitDef *def = inUnitSpec->mUnitDef;
38 Unit *unit = (Unit*)memory;
39 memory += def->mAllocSize;
41 unit->mWorld = inWorld;
42 unit->mUnitDef = def;
44 int numInputs = inUnitSpec->mNumInputs;
45 int numOutputs = inUnitSpec->mNumOutputs;
46 unit->mNumInputs = numInputs;
47 unit->mNumOutputs = numOutputs;
48 int numPorts = numInputs + numOutputs;
50 unit->mInput = (Wire**)memory;
51 memory += numPorts * sizeof(Wire*);
53 unit->mOutput = unit->mInput + numInputs;
55 unit->mInBuf = (float**)memory;
56 memory += numPorts * sizeof(float*);
58 unit->mOutBuf = unit->mInBuf + numInputs;
60 unit->mCalcRate = inUnitSpec->mCalcRate;
61 unit->mSpecialIndex = inUnitSpec->mSpecialIndex;
62 Rate* rateInfo = unit->mRate = inUnitSpec->mRateInfo;
63 unit->mBufLength = rateInfo->mBufLength;
65 unit->mDone = false;
67 return unit;
70 void Unit_Dtor(Unit *inUnit)
74 void Unit_ZeroOutputs(Unit *unit, int inNumSamples)
76 long numOuts = unit->mNumOutputs;
77 for (int i=0; i<numOuts; ++i) {
78 float *out = OUT(i);
79 Clear(inNumSamples, out);
84 void Unit_EndCalc(Unit *inUnit, int inNumSamples)
86 inUnit->mDone = true;
87 Unit_ZeroOutputs(inUnit, inNumSamples);
90 void Unit_End(Unit *inUnit)
92 inUnit->mCalcFunc = (UnitCalcFunc)&Unit_EndCalc;