mirror of
https://github.com/ZDoom/qzdoom.git
synced 2024-11-24 21:11:39 +00:00
- replaced a few temporary allocations with TArray and added a few convenience loader functions for this.
Amazingly with today's optimizers this creates code which is just as good as doing it all manually with the added benefit of being safer.
This commit is contained in:
parent
602ea8f723
commit
3448749de6
10 changed files with 60 additions and 39 deletions
|
@ -184,6 +184,14 @@ public:
|
||||||
return mReader->Read(buffer, (long)len);
|
return mReader->Read(buffer, (long)len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TArray<uint8_t> Read(size_t len)
|
||||||
|
{
|
||||||
|
TArray<uint8_t> buffer((int)len, true);
|
||||||
|
Size length = mReader->Read(&buffer[0], (long)len);
|
||||||
|
buffer.Clamp((int)length);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
TArray<uint8_t> Read()
|
TArray<uint8_t> Read()
|
||||||
{
|
{
|
||||||
TArray<uint8_t> buffer(mReader->Length, true);
|
TArray<uint8_t> buffer(mReader->Length, true);
|
||||||
|
|
|
@ -1691,15 +1691,10 @@ uint16_t MakeSkill(int flags)
|
||||||
|
|
||||||
void P_LoadThings (MapData * map)
|
void P_LoadThings (MapData * map)
|
||||||
{
|
{
|
||||||
int lumplen = map->Size(ML_THINGS);
|
|
||||||
int numthings = lumplen / sizeof(mapthing_t);
|
|
||||||
|
|
||||||
char *mtp;
|
|
||||||
mapthing_t *mt;
|
mapthing_t *mt;
|
||||||
|
auto mtp = map->Read(ML_THINGS);
|
||||||
mtp = new char[lumplen];
|
int numthings = mtp.Size() / sizeof(mapthing_t);
|
||||||
map->Read(ML_THINGS, mtp);
|
mt = (mapthing_t*)mtp.Data();
|
||||||
mt = (mapthing_t*)mtp;
|
|
||||||
|
|
||||||
MapThingsConverted.Resize(numthings);
|
MapThingsConverted.Resize(numthings);
|
||||||
FMapThing *mti = &MapThingsConverted[0];
|
FMapThing *mti = &MapThingsConverted[0];
|
||||||
|
@ -1767,7 +1762,6 @@ void P_LoadThings (MapData * map)
|
||||||
if (flags & BTF_NOTSINGLE) mti[i].flags &= ~MTF_SINGLE;
|
if (flags & BTF_NOTSINGLE) mti[i].flags &= ~MTF_SINGLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete [] mtp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//===========================================================================
|
//===========================================================================
|
||||||
|
@ -2109,29 +2103,24 @@ void P_LoadLineDefs (MapData * map)
|
||||||
{
|
{
|
||||||
int i, skipped;
|
int i, skipped;
|
||||||
line_t *ld;
|
line_t *ld;
|
||||||
int lumplen = map->Size(ML_LINEDEFS);
|
|
||||||
char * mldf;
|
|
||||||
maplinedef_t *mld;
|
maplinedef_t *mld;
|
||||||
|
|
||||||
int numlines = lumplen / sizeof(maplinedef_t);
|
auto mldf = map->Read(ML_LINEDEFS);
|
||||||
|
int numlines = mldf.Size() / sizeof(maplinedef_t);
|
||||||
linemap.Resize(numlines);
|
linemap.Resize(numlines);
|
||||||
|
|
||||||
mldf = new char[lumplen];
|
|
||||||
map->Read(ML_LINEDEFS, mldf);
|
|
||||||
|
|
||||||
// [RH] Count the number of sidedef references. This is the number of
|
// [RH] Count the number of sidedef references. This is the number of
|
||||||
// sidedefs we need. The actual number in the SIDEDEFS lump might be less.
|
// sidedefs we need. The actual number in the SIDEDEFS lump might be less.
|
||||||
// Lines with 0 length are also removed.
|
// Lines with 0 length are also removed.
|
||||||
|
|
||||||
for (skipped = sidecount = i = 0; i < numlines; )
|
for (skipped = sidecount = i = 0; i < numlines; )
|
||||||
{
|
{
|
||||||
mld = ((maplinedef_t*)mldf) + i;
|
mld = ((maplinedef_t*)mldf.Data()) + i;
|
||||||
unsigned v1 = LittleShort(mld->v1);
|
unsigned v1 = LittleShort(mld->v1);
|
||||||
unsigned v2 = LittleShort(mld->v2);
|
unsigned v2 = LittleShort(mld->v2);
|
||||||
|
|
||||||
if (v1 >= level.vertexes.Size() || v2 >= level.vertexes.Size())
|
if (v1 >= level.vertexes.Size() || v2 >= level.vertexes.Size())
|
||||||
{
|
{
|
||||||
delete [] mldf;
|
|
||||||
I_Error ("Line %d has invalid vertices: %d and/or %d.\nThe map only contains %u vertices.", i+skipped, v1, v2, level.vertexes.Size());
|
I_Error ("Line %d has invalid vertices: %d and/or %d.\nThe map only contains %u vertices.", i+skipped, v1, v2, level.vertexes.Size());
|
||||||
}
|
}
|
||||||
else if (v1 == v2 ||
|
else if (v1 == v2 ||
|
||||||
|
@ -2164,7 +2153,7 @@ void P_LoadLineDefs (MapData * map)
|
||||||
|
|
||||||
P_AllocateSideDefs (map, sidecount);
|
P_AllocateSideDefs (map, sidecount);
|
||||||
|
|
||||||
mld = (maplinedef_t *)mldf;
|
mld = (maplinedef_t *)mldf.Data();
|
||||||
ld = &level.lines[0];
|
ld = &level.lines[0];
|
||||||
for (i = 0; i < numlines; i++, mld++, ld++)
|
for (i = 0; i < numlines; i++, mld++, ld++)
|
||||||
{
|
{
|
||||||
|
@ -2203,7 +2192,6 @@ void P_LoadLineDefs (MapData * map)
|
||||||
if (level.flags2 & LEVEL2_WRAPMIDTEX) ld->flags |= ML_WRAP_MIDTEX;
|
if (level.flags2 & LEVEL2_WRAPMIDTEX) ld->flags |= ML_WRAP_MIDTEX;
|
||||||
if (level.flags2 & LEVEL2_CHECKSWITCHRANGE) ld->flags |= ML_CHECKSWITCHRANGE;
|
if (level.flags2 & LEVEL2_CHECKSWITCHRANGE) ld->flags |= ML_CHECKSWITCHRANGE;
|
||||||
}
|
}
|
||||||
delete[] mldf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//===========================================================================
|
//===========================================================================
|
||||||
|
|
|
@ -104,6 +104,13 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TArray<uint8_t> Read(unsigned lumpindex)
|
||||||
|
{
|
||||||
|
TArray<uint8_t> buffer(Size(lumpindex), true);
|
||||||
|
Read(lumpindex, buffer.Data(), (int)buffer.Size());
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t Size(unsigned int lumpindex)
|
uint32_t Size(unsigned int lumpindex)
|
||||||
{
|
{
|
||||||
if (lumpindex<countof(MapLumps) && MapLumps[lumpindex].Reader.isOpen())
|
if (lumpindex<countof(MapLumps) && MapLumps[lumpindex].Reader.isOpen())
|
||||||
|
|
|
@ -2045,15 +2045,11 @@ public:
|
||||||
|
|
||||||
void ParseTextMap(MapData *map)
|
void ParseTextMap(MapData *map)
|
||||||
{
|
{
|
||||||
char *buffer = new char[map->Size(ML_TEXTMAP)];
|
|
||||||
|
|
||||||
isTranslated = true;
|
isTranslated = true;
|
||||||
isExtended = false;
|
isExtended = false;
|
||||||
floordrop = false;
|
floordrop = false;
|
||||||
|
|
||||||
map->Read(ML_TEXTMAP, buffer);
|
sc.OpenMem(Wads.GetLumpFullName(map->lumpnum), map->Read(ML_TEXTMAP));
|
||||||
sc.OpenMem(Wads.GetLumpFullName(map->lumpnum), buffer, map->Size(ML_TEXTMAP));
|
|
||||||
delete [] buffer;
|
|
||||||
sc.SetCMode(true);
|
sc.SetCMode(true);
|
||||||
if (sc.CheckString("namespace"))
|
if (sc.CheckString("namespace"))
|
||||||
{
|
{
|
||||||
|
|
|
@ -473,10 +473,7 @@ class USDFParser : public UDMFParserBase
|
||||||
public:
|
public:
|
||||||
bool Parse(int lumpnum, FileReader &lump, int lumplen)
|
bool Parse(int lumpnum, FileReader &lump, int lumplen)
|
||||||
{
|
{
|
||||||
char *buffer = new char[lumplen];
|
sc.OpenMem(Wads.GetLumpFullName(lumpnum), lump.Read(lumplen));
|
||||||
lump.Read(buffer, lumplen);
|
|
||||||
sc.OpenMem(Wads.GetLumpFullName(lumpnum), buffer, lumplen);
|
|
||||||
delete [] buffer;
|
|
||||||
sc.SetCMode(true);
|
sc.SetCMode(true);
|
||||||
// Namespace must be the first field because everything else depends on it.
|
// Namespace must be the first field because everything else depends on it.
|
||||||
if (sc.CheckString("namespace"))
|
if (sc.CheckString("namespace"))
|
||||||
|
|
|
@ -323,15 +323,12 @@ void FParseContext::ParseLump(const char *lumpname)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the lump into a buffer and add a 0-terminator
|
// Read the lump into a buffer and add a 0-terminator
|
||||||
int lumplen = Wads.LumpLength(lumpno);
|
auto lumpdata = Wads.ReadLumpIntoArray(lumpno, 1);
|
||||||
char *lumpdata = new char[lumplen+1];
|
|
||||||
Wads.ReadLump(lumpno, lumpdata);
|
|
||||||
lumpdata[lumplen] = 0;
|
|
||||||
|
|
||||||
SourceLine = 0;
|
SourceLine = 0;
|
||||||
SourceFile = lumpname;
|
SourceFile = lumpname;
|
||||||
|
|
||||||
char *sourcep = lumpdata;
|
char *sourcep = (char*)lumpdata.Data();
|
||||||
while ( (tokentype = GetToken(sourcep, &token)) )
|
while ( (tokentype = GetToken(sourcep, &token)) )
|
||||||
{
|
{
|
||||||
// It is much easier to handle include statements outside the main parser.
|
// It is much easier to handle include statements outside the main parser.
|
||||||
|
@ -349,7 +346,6 @@ void FParseContext::ParseLump(const char *lumpname)
|
||||||
Parse(pParser, tokentype, token, this);
|
Parse(pParser, tokentype, token, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete [] lumpdata;
|
|
||||||
SourceLine = SavedSourceLine;
|
SourceLine = SavedSourceLine;
|
||||||
SourceFile = SavedSourceFile;
|
SourceFile = SavedSourceFile;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,10 @@ public:
|
||||||
void Open(const char *lumpname);
|
void Open(const char *lumpname);
|
||||||
bool OpenFile(const char *filename);
|
bool OpenFile(const char *filename);
|
||||||
void OpenMem(const char *name, const char *buffer, int size);
|
void OpenMem(const char *name, const char *buffer, int size);
|
||||||
|
void OpenMem(const char *name, TArray<uint8_t> &buffer)
|
||||||
|
{
|
||||||
|
OpenMem(name, (const char*)buffer.Data(), buffer.Size());
|
||||||
|
}
|
||||||
void OpenString(const char *name, FString buffer);
|
void OpenString(const char *name, FString buffer);
|
||||||
void OpenLumpNum(int lump);
|
void OpenLumpNum(int lump);
|
||||||
void Close();
|
void Close();
|
||||||
|
|
10
src/tarray.h
10
src/tarray.h
|
@ -157,12 +157,12 @@ public:
|
||||||
Count = 0;
|
Count = 0;
|
||||||
Array = NULL;
|
Array = NULL;
|
||||||
}
|
}
|
||||||
TArray (int max, bool reserve = false)
|
TArray (size_t max, bool reserve = false)
|
||||||
{
|
{
|
||||||
Most = max;
|
Most = (unsigned)max;
|
||||||
Count = reserve? max : 0;
|
Count = (unsigned)(reserve? max : 0);
|
||||||
Array = (T *)M_Malloc (sizeof(T)*max);
|
Array = (T *)M_Malloc (sizeof(T)*max);
|
||||||
if (reserve)
|
if (reserve && Count > 0)
|
||||||
{
|
{
|
||||||
ConstructEmpty(0, Count - 1);
|
ConstructEmpty(0, Count - 1);
|
||||||
}
|
}
|
||||||
|
@ -436,7 +436,7 @@ public:
|
||||||
Grow (amount);
|
Grow (amount);
|
||||||
unsigned int place = Count;
|
unsigned int place = Count;
|
||||||
Count += amount;
|
Count += amount;
|
||||||
ConstructEmpty(place, Count - 1);
|
if (Count > 0) ConstructEmpty(place, Count - 1);
|
||||||
return place;
|
return place;
|
||||||
}
|
}
|
||||||
unsigned int Size () const
|
unsigned int Size () const
|
||||||
|
|
|
@ -1281,6 +1281,30 @@ void FWadCollection::ReadLump (int lump, void *dest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//==========================================================================
|
||||||
|
//
|
||||||
|
// W_ReadLump
|
||||||
|
//
|
||||||
|
// Loads the lump into a TArray and returns it.
|
||||||
|
//
|
||||||
|
//==========================================================================
|
||||||
|
|
||||||
|
TArray<uint8_t> FWadCollection::ReadLumpIntoArray(int lump, int pad)
|
||||||
|
{
|
||||||
|
auto lumpr = OpenLumpReader(lump);
|
||||||
|
auto size = lumpr.GetLength();
|
||||||
|
TArray<uint8_t> data(size + pad);
|
||||||
|
auto numread = lumpr.Read(data.Data(), size);
|
||||||
|
|
||||||
|
if (numread != size)
|
||||||
|
{
|
||||||
|
I_Error("W_ReadLump: only read %ld of %ld on lump %i\n",
|
||||||
|
numread, size, lump);
|
||||||
|
}
|
||||||
|
if (pad > 0) memset(&data[size], 0, pad);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
//==========================================================================
|
//==========================================================================
|
||||||
//
|
//
|
||||||
// ReadLump - variant 2
|
// ReadLump - variant 2
|
||||||
|
|
|
@ -148,6 +148,7 @@ public:
|
||||||
|
|
||||||
|
|
||||||
void ReadLump (int lump, void *dest);
|
void ReadLump (int lump, void *dest);
|
||||||
|
TArray<uint8_t> ReadLumpIntoArray(int lump, int pad = 0); // reads lump into a writable buffer and optionally adds some padding at the end. (FMemLump isn't writable!)
|
||||||
FMemLump ReadLump (int lump);
|
FMemLump ReadLump (int lump);
|
||||||
FMemLump ReadLump (const char *name) { return ReadLump (GetNumForName (name)); }
|
FMemLump ReadLump (const char *name) { return ReadLump (GetNumForName (name)); }
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue