- Changed a few allocations into usin TArrays

S_SoundCurve in particular looked like a candidate for leaking memory.
This commit is contained in:
Christoph Oelckers 2018-11-30 17:07:42 +01:00
parent 024870ba11
commit 25ad71a113
5 changed files with 43 additions and 63 deletions

View file

@ -150,8 +150,7 @@ FSoundChan *Channels;
FSoundChan *FreeChannels;
FRolloffInfo S_Rolloff;
uint8_t *S_SoundCurve;
int S_SoundCurveSize;
TArray<uint8_t> S_SoundCurve;
FBoolCVar noisedebug ("noise", false, 0); // [RH] Print sound debugging info?
CUSTOM_CVAR (Int, snd_channels, 128, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) // number of channels available
@ -311,20 +310,16 @@ void S_Init ()
atterm (S_Shutdown);
// remove old data (S_Init can be called multiple times!)
if (S_SoundCurve != NULL)
{
delete[] S_SoundCurve;
S_SoundCurve = NULL;
}
// Heretic and Hexen have sound curve lookup tables. Doom does not.
curvelump = Wads.CheckNumForName ("SNDCURVE");
if (curvelump >= 0)
{
S_SoundCurveSize = Wads.LumpLength (curvelump);
S_SoundCurve = new uint8_t[S_SoundCurveSize];
Wads.ReadLump(curvelump, S_SoundCurve);
S_SoundCurve.Resize(Wads.LumpLength (curvelump));
Wads.ReadLump(curvelump, S_SoundCurve.Data());
}
else
{
S_SoundCurve.Clear();
}
// Free all channels for use.
@ -376,11 +371,6 @@ void S_Shutdown ()
}
FreeChannels = NULL;
if (S_SoundCurve != NULL)
{
delete[] S_SoundCurve;
S_SoundCurve = NULL;
}
if (PlayList != NULL)
{
delete PlayList;
@ -1486,34 +1476,32 @@ sfxinfo_t *S_LoadSound(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer)
if (size > 0)
{
auto wlump = Wads.OpenLumpReader(sfx->lumpnum);
uint8_t *sfxdata = new uint8_t[size];
wlump.Read(sfxdata, size);
int32_t dmxlen = LittleLong(((int32_t *)sfxdata)[1]);
auto sfxdata = wlump.Read(size);
int32_t dmxlen = LittleLong(((int32_t *)sfxdata.Data())[1]);
std::pair<SoundHandle,bool> snd;
// If the sound is voc, use the custom loader.
if (strncmp ((const char *)sfxdata, "Creative Voice File", 19) == 0)
if (strncmp ((const char *)sfxdata.Data(), "Creative Voice File", 19) == 0)
{
snd = GSnd->LoadSoundVoc(sfxdata, size);
snd = GSnd->LoadSoundVoc(sfxdata.Data(), size);
}
// If the sound is raw, just load it as such.
else if (sfx->bLoadRAW)
{
snd = GSnd->LoadSoundRaw(sfxdata, size, sfx->RawRate, 1, 8, sfx->LoopStart);
snd = GSnd->LoadSoundRaw(sfxdata.Data(), size, sfx->RawRate, 1, 8, sfx->LoopStart);
}
// Otherwise, try the sound as DMX format.
else if (((uint8_t *)sfxdata)[0] == 3 && ((uint8_t *)sfxdata)[1] == 0 && dmxlen <= size - 8)
else if (((uint8_t *)sfxdata.Data())[0] == 3 && ((uint8_t *)sfxdata.Data())[1] == 0 && dmxlen <= size - 8)
{
int frequency = LittleShort(((uint16_t *)sfxdata)[1]);
int frequency = LittleShort(((uint16_t *)sfxdata.Data())[1]);
if (frequency == 0) frequency = 11025;
snd = GSnd->LoadSoundRaw(sfxdata+8, dmxlen, frequency, 1, 8, sfx->LoopStart);
snd = GSnd->LoadSoundRaw(sfxdata.Data()+8, dmxlen, frequency, 1, 8, sfx->LoopStart);
}
// If that fails, let the sound system try and figure it out.
else
{
snd = GSnd->LoadSound(sfxdata, size, false, pBuffer);
snd = GSnd->LoadSound(sfxdata.Data(), size, false, pBuffer);
}
delete[] sfxdata;
sfx->data = snd.first;
if(snd.second)
@ -1554,33 +1542,31 @@ static void S_LoadSound3D(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer)
if (size <= 0) return;
auto wlump = Wads.OpenLumpReader(sfx->lumpnum);
uint8_t *sfxdata = new uint8_t[size];
wlump.Read(sfxdata, size);
int32_t dmxlen = LittleLong(((int32_t *)sfxdata)[1]);
auto sfxdata = wlump.Read(size);
int32_t dmxlen = LittleLong(((int32_t *)sfxdata.Data())[1]);
// If the sound is voc, use the custom loader.
if (strncmp((const char *)sfxdata, "Creative Voice File", 19) == 0)
if (strncmp((const char *)sfxdata.Data(), "Creative Voice File", 19) == 0)
{
snd = GSnd->LoadSoundVoc(sfxdata, size, true);
snd = GSnd->LoadSoundVoc(sfxdata.Data(), size, true);
}
// If the sound is raw, just load it as such.
else if (sfx->bLoadRAW)
{
snd = GSnd->LoadSoundRaw(sfxdata, size, sfx->RawRate, 1, 8, sfx->LoopStart, true);
snd = GSnd->LoadSoundRaw(sfxdata.Data(), size, sfx->RawRate, 1, 8, sfx->LoopStart, true);
}
// Otherwise, try the sound as DMX format.
else if (((uint8_t *)sfxdata)[0] == 3 && ((uint8_t *)sfxdata)[1] == 0 && dmxlen <= size - 8)
else if (((uint8_t *)sfxdata.Data())[0] == 3 && ((uint8_t *)sfxdata.Data())[1] == 0 && dmxlen <= size - 8)
{
int frequency = LittleShort(((uint16_t *)sfxdata)[1]);
int frequency = LittleShort(((uint16_t *)sfxdata.Data())[1]);
if (frequency == 0) frequency = 11025;
snd = GSnd->LoadSoundRaw(sfxdata + 8, dmxlen, frequency, 1, 8, sfx->LoopStart, -1, true);
snd = GSnd->LoadSoundRaw(sfxdata.Data() + 8, dmxlen, frequency, 1, 8, sfx->LoopStart, -1, true);
}
// If that fails, let the sound system try and figure it out.
else
{
snd = GSnd->LoadSound(sfxdata, size, true, pBuffer);
snd = GSnd->LoadSound(sfxdata.Data(), size, true, pBuffer);
}
delete[] sfxdata;
}
sfx->data3d = snd.first;
@ -2256,9 +2242,9 @@ float S_GetRolloff(FRolloffInfo *rolloff, float distance, bool logarithmic)
}
float volume = (rolloff->MaxDistance - distance) / (rolloff->MaxDistance - rolloff->MinDistance);
if (rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve != NULL)
if (rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve.Size() > 0)
{
volume = S_SoundCurve[int(S_SoundCurveSize * (1 - volume))] / 127.f;
volume = S_SoundCurve[int(S_SoundCurve.Size() * (1 - volume))] / 127.f;
}
if (logarithmic)
{

View file

@ -165,8 +165,8 @@ public:
};
extern FRolloffInfo S_Rolloff;
extern uint8_t *S_SoundCurve;
extern int S_SoundCurveSize;
extern TArray<uint8_t> S_SoundCurve;
// Information about one playing sound.
struct sector_t;

View file

@ -249,14 +249,10 @@ bool FScanner::OpenFile (const char *name)
FileReader fr;
if (!fr.OpenFile(name)) return false;
auto filesize = fr.GetLength();
auto filebuf = new uint8_t[filesize];
if (fr.Read(filebuf, filesize) != filesize)
{
delete[] filebuf;
return false;
}
ScriptBuffer = FString((const char *)filebuf, filesize);
delete[] filebuf;
auto filebuff = fr.Read();
if (filebuff.Size() == 0 && filesize > 0) return false;
ScriptBuffer = FString((const char *)filebuff.Data(), filesize);
ScriptName = name; // This is used for error messages so the full file name is preferable
LumpNum = -1;
PrepareScript ();

View file

@ -688,8 +688,8 @@ static float GetRolloff(const FRolloffInfo *rolloff, float distance)
if(rolloff->RolloffType == ROLLOFF_Linear)
return volume;
if(rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve != NULL)
return S_SoundCurve[int(S_SoundCurveSize * (1.f - volume))] / 127.f;
if(rolloff->RolloffType == ROLLOFF_Custom && S_SoundCurve.Size() > 0)
return S_SoundCurve[int(S_SoundCurve.Size() * (1.f - volume))] / 127.f;
return (powf(10.f, volume) - 1.f) / 9.f;
}

View file

@ -357,7 +357,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
int i;
FTextureID lump;
char buffer[12];
FTexture **charlumps;
TArray<FTexture*> charLumps;
int maxyoffs;
bool doomtemplate = gameinfo.gametype & GAME_DoomChex ? strncmp (nametemplate, "STCFN", 5) == 0 : false;
bool stcfn121 = false;
@ -365,7 +365,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
noTranslate = notranslate;
Lump = fdlump;
Chars = new CharData[count];
charlumps = new FTexture *[count];
charLumps.Resize(count);
PatchRemap = new uint8_t[256];
FirstChar = first;
LastChar = first + count - 1;
@ -381,7 +381,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
for (i = 0; i < count; i++)
{
charlumps[i] = NULL;
charLumps[i] = NULL;
mysnprintf (buffer, countof(buffer), nametemplate, i + start);
lump = TexMan.CheckForTexture(buffer, ETextureType::MiscPatch);
@ -395,7 +395,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
!TexMan.CheckForTexture("STCFN122", ETextureType::MiscPatch).isValid())
{
// insert the incorrectly named '|' graphic in its correct position.
if (count > 124-start) charlumps[124-start] = TexMan[lump];
if (count > 124-start) charLumps[124-start] = TexMan[lump];
lump.SetInvalid();
stcfn121 = true;
}
@ -408,7 +408,7 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
{
// set the lump here only if it represents a valid texture
if (i != 124-start || !stcfn121)
charlumps[i] = pic;
charLumps[i] = pic;
int height = pic->GetScaledHeight();
int yoffs = pic->GetScaledTopOffset(0);
@ -425,10 +425,10 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
}
}
if (charlumps[i] != NULL)
if (charLumps[i] != nullptr)
{
if (!noTranslate) Chars[i].Pic = new FFontChar1 (charlumps[i]);
else Chars[i].Pic = charlumps[i];
if (!noTranslate) Chars[i].Pic = new FFontChar1 (charLumps[i]);
else Chars[i].Pic = charLumps[i];
Chars[i].XMove = Chars[i].Pic->GetScaledWidth();
}
else
@ -454,8 +454,6 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
FixXMoves();
if (!noTranslate) LoadTranslations();
delete[] charlumps;
}
//==========================================================================