- replaced several explicit allocations with TArrays.

This commit is contained in:
Christoph Oelckers 2018-12-17 18:28:04 +01:00
parent d68cd3aa80
commit a73c065811
10 changed files with 66 additions and 115 deletions

View File

@ -240,30 +240,29 @@ void I_DebugPrint(const char *cp)
{
}
void I_PrintStr (const char *cp)
void I_PrintStr(const char *cp)
{
// Strip out any color escape sequences before writing to the log file
char * copy = new char[strlen(cp)+1];
// Strip out any color escape sequences before writing to debug output
TArray<char> copy(strlen(cp) + 1, true);
const char * srcp = cp;
char * dstp = copy;
char * dstp = copy.Data();
while (*srcp != 0)
{
if (*srcp!=0x1c && *srcp!=0x1d && *srcp!=0x1e && *srcp!=0x1f)
if (*srcp != 0x1c && *srcp != 0x1d && *srcp != 0x1e && *srcp != 0x1f)
{
*dstp++=*srcp++;
*dstp++ = *srcp++;
}
else
{
if (srcp[1]!=0) srcp+=2;
if (srcp[1] != 0) srcp += 2;
else break;
}
}
*dstp=0;
*dstp = 0;
fputs (copy, stdout);
delete [] copy;
fflush (stdout);
fputs(copy.Data(), stdout);
fflush(stdout);
}
int I_PickIWad (WadStuff *wads, int numwads, bool showwin, int defaultiwad)

View File

@ -83,7 +83,7 @@ TArray<uint8_t> FVoxelTexture::CreatePalettedPixels(int conversion)
{
// GetPixels gets called when a translated palette is used so we still need to implement it here.
TArray<uint8_t> Pixels(256, true);
uint8_t *pp = SourceVox->Palette;
uint8_t *pp = SourceVox->Palette.Data();
if(pp != NULL)
{
@ -120,9 +120,9 @@ int FVoxelTexture::CopyPixels(FBitmap *bmp, int conversion)
{
PalEntry pe[256];
uint8_t bitmap[256];
uint8_t *pp = SourceVox->Palette;
uint8_t *pp = SourceVox->Palette.Data();
if(pp != NULL)
if(pp != nullptr)
{
for(int i=0;i<256;i++, pp+=3)
{

View File

@ -392,7 +392,7 @@ static void AddToVertex(const sector_t * sec, TArray<int> & list)
static void InitVertexData()
{
auto vt_sectorlists = new TArray<int>[level.vertexes.Size()];
TArray<TArray<int>> vt_sectorlists(level.vertexes.Size(), true);
for(auto &line : level.lines)
{
@ -437,8 +437,6 @@ static void InitVertexData()
vert.numsectors=0;
}
}
delete [] vt_sectorlists;
}
//==========================================================================
@ -478,15 +476,15 @@ static int segcmp(const void *a, const void *b)
static void PrepareSegs()
{
auto numsides = level.sides.Size();
int *segcount = new int[numsides];
TArray<int> segcount(numsides, true);
int realsegs = 0;
// count the segs
memset(segcount, 0, numsides * sizeof(int));
memset(segcount.Data(), 0, numsides * sizeof(int));
for(auto &seg : level.segs)
{
if (seg.sidedef == NULL) continue; // miniseg
if (seg.sidedef == nullptr) continue; // miniseg
int sidenum = seg.sidedef->Index();
realsegs++;
@ -509,7 +507,6 @@ static void PrepareSegs()
level.sides[i].segs = level.sides[i-1].segs + segcount[i-1];
level.sides[i].numsegs = 0;
}
delete [] segcount;
// assign the segs
for (auto &seg : level.segs)

View File

@ -303,8 +303,8 @@ FVoxel *R_LoadKVX(int lumpnum)
}
voxel->LumpNum = lumpnum;
voxel->Palette = new uint8_t[768];
memcpy(voxel->Palette, rawvoxel + voxelsize - 768, 768);
voxel->Palette.Resize(768);
memcpy(voxel->Palette.Data(), rawvoxel + voxelsize - 768, 768);
return voxel;
}
@ -382,22 +382,6 @@ uint8_t *FVoxelMipLevel::GetSlabData(bool wantremapped) const
return SlabData;
}
//==========================================================================
//
// FVoxel Constructor
//
//==========================================================================
FVoxel::FVoxel()
{
Palette = NULL;
}
FVoxel::~FVoxel()
{
if (Palette != NULL) delete [] Palette;
}
//==========================================================================
//
// Create true color version of the slab data
@ -430,7 +414,7 @@ void FVoxel::CreateBgraSlabData()
int colorIndex = src->col[j];
uint32_t red, green, blue;
if (Palette)
if (Palette.Size())
{
red = (Palette[colorIndex * 3 + 0] << 2) | (Palette[colorIndex * 3 + 0] >> 4);
green = (Palette[colorIndex * 3 + 1] << 2) | (Palette[colorIndex * 3 + 1] >> 4);
@ -464,9 +448,9 @@ void FVoxel::Remap()
{
if (Remapped) return;
Remapped = true;
if (Palette != NULL)
if (Palette.Size())
{
uint8_t *remap = GetVoxelRemap(Palette);
uint8_t *remap = GetVoxelRemap(Palette.Data());
for (int i = 0; i < NumMips; ++i)
{
int size = Mips[i].OffsetX[Mips[i].SizeX];
@ -487,11 +471,7 @@ void FVoxel::Remap()
void FVoxel::RemovePalette()
{
if (Palette != NULL)
{
delete [] Palette;
Palette = NULL;
}
Palette.Reset();
}

View File

@ -50,16 +50,14 @@ public:
struct FVoxel
{
TArray<uint8_t> Palette;
int LumpNum;
int NumMips;
int VoxelIndex;
uint8_t *Palette;
FVoxelMipLevel Mips[MAXVOXMIPS];
bool Remapped = false;
bool Bgramade = false;
FVoxel();
~FVoxel();
void CreateBgraSlabData();
void Remap();
void RemovePalette();

View File

@ -96,9 +96,9 @@ bool FPakFile::Open(bool quiet)
NumLumps = LittleLong(header.dirlen) / sizeof(dpackfile_t);
header.dirofs = LittleLong(header.dirofs);
dpackfile_t *fileinfo = new dpackfile_t[NumLumps];
TArray<dpackfile_t> fileinfo(NumLumps, true);
Reader.Seek (header.dirofs, FileReader::SeekSet);
Reader.Read (fileinfo, NumLumps * sizeof(dpackfile_t));
Reader.Read (fileinfo.Data(), NumLumps * sizeof(dpackfile_t));
Lumps.Resize(NumLumps);
@ -113,7 +113,6 @@ bool FPakFile::Open(bool quiet)
Lumps[i].CheckEmbedded();
}
delete [] fileinfo;
return true;
}

View File

@ -387,7 +387,6 @@ int FJPEGTexture::CopyPixels(FBitmap *bmp, int conversion)
PalEntry pe[256];
auto lump = Wads.OpenLumpReader (SourceLump);
JSAMPLE *buff = NULL;
jpeg_decompress_struct cinfo;
jpeg_error_mgr jerr;
@ -414,12 +413,11 @@ int FJPEGTexture::CopyPixels(FBitmap *bmp, int conversion)
jpeg_start_decompress(&cinfo);
int yc = 0;
buff = new uint8_t[cinfo.output_height * cinfo.output_width * cinfo.output_components];
TArray<uint8_t> buff(cinfo.output_height * cinfo.output_width * cinfo.output_components, true);
while (cinfo.output_scanline < cinfo.output_height)
{
uint8_t * ptr = buff + cinfo.output_width * cinfo.output_components * yc;
uint8_t * ptr = buff.Data() + cinfo.output_width * cinfo.output_components * yc;
jpeg_read_scanlines(&cinfo, &ptr, 1);
yc++;
}
@ -427,23 +425,23 @@ int FJPEGTexture::CopyPixels(FBitmap *bmp, int conversion)
switch (cinfo.out_color_space)
{
case JCS_RGB:
bmp->CopyPixelDataRGB(0, 0, buff, cinfo.output_width, cinfo.output_height,
bmp->CopyPixelDataRGB(0, 0, buff.Data(), cinfo.output_width, cinfo.output_height,
3, cinfo.output_width * cinfo.output_components, 0, CF_RGB);
break;
case JCS_GRAYSCALE:
for (int i = 0; i < 256; i++) pe[i] = PalEntry(255, i, i, i); // default to a gray map
bmp->CopyPixelData(0, 0, buff, cinfo.output_width, cinfo.output_height,
bmp->CopyPixelData(0, 0, buff.Data(), cinfo.output_width, cinfo.output_height,
1, cinfo.output_width, 0, pe);
break;
case JCS_CMYK:
bmp->CopyPixelDataRGB(0, 0, buff, cinfo.output_width, cinfo.output_height,
bmp->CopyPixelDataRGB(0, 0, buff.Data(), cinfo.output_width, cinfo.output_height,
4, cinfo.output_width * cinfo.output_components, 0, CF_CMYK);
break;
case JCS_YCbCr:
bmp->CopyPixelDataRGB(0, 0, buff, cinfo.output_width, cinfo.output_height,
bmp->CopyPixelDataRGB(0, 0, buff.Data(), cinfo.output_width, cinfo.output_height,
4, cinfo.output_width * cinfo.output_components, 0, CF_YCbCr);
break;
@ -459,7 +457,6 @@ int FJPEGTexture::CopyPixels(FBitmap *bmp, int conversion)
Printf(TEXTCOLOR_ORANGE "JPEG error in %s\n", Wads.GetLumpFullPath(SourceLump).GetChars());
}
jpeg_destroy_decompress(&cinfo);
if (buff != NULL) delete [] buff;
return 0;
}

View File

@ -161,9 +161,8 @@ void FPCXTexture::ReadPCX1bit (uint8_t *dst, FileReader & lump, PCXHeader *hdr)
int rle_count = 0;
uint8_t rle_value = 0;
uint8_t * srcp = new uint8_t[lump.GetLength() - sizeof(PCXHeader)];
lump.Read(srcp, lump.GetLength() - sizeof(PCXHeader));
uint8_t * src = srcp;
TArray<uint8_t> srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader));
uint8_t * src = srcp.Data();
for (y = 0; y < Height; ++y)
{
@ -196,7 +195,6 @@ void FPCXTexture::ReadPCX1bit (uint8_t *dst, FileReader & lump, PCXHeader *hdr)
}
}
}
delete [] srcp;
}
//==========================================================================
@ -210,12 +208,11 @@ void FPCXTexture::ReadPCX4bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr)
int rle_count = 0, rle_value = 0;
int x, y, c;
int bytes;
uint8_t * line = new uint8_t[hdr->bytesPerScanLine];
uint8_t * colorIndex = new uint8_t[Width];
TArray<uint8_t> line(hdr->bytesPerScanLine, true);
TArray<uint8_t> colorIndex(Width, true);
uint8_t * srcp = new uint8_t[lump.GetLength() - sizeof(PCXHeader)];
lump.Read(srcp, lump.GetLength() - sizeof(PCXHeader));
uint8_t * src = srcp;
TArray<uint8_t> srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader));
uint8_t * src = srcp.Data();
for (y = 0; y < Height; ++y)
{
@ -224,7 +221,7 @@ void FPCXTexture::ReadPCX4bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr)
for (c = 0; c < 4; ++c)
{
uint8_t * pLine = line;
uint8_t * pLine = line.Data();
bytes = hdr->bytesPerScanLine;
@ -255,11 +252,6 @@ void FPCXTexture::ReadPCX4bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr)
ptr[x] += (1 << c);
}
}
/* release memory */
delete [] colorIndex;
delete [] line;
delete [] srcp;
}
//==========================================================================
@ -273,9 +265,8 @@ void FPCXTexture::ReadPCX8bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr)
int rle_count = 0, rle_value = 0;
int y, bytes;
uint8_t * srcp = new uint8_t[lump.GetLength() - sizeof(PCXHeader)];
lump.Read(srcp, lump.GetLength() - sizeof(PCXHeader));
uint8_t * src = srcp;
auto srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader));
uint8_t * src = srcp.Data();
for (y = 0; y < Height; ++y)
{
@ -301,7 +292,6 @@ void FPCXTexture::ReadPCX8bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr)
*ptr++ = rle_value;
}
}
delete [] srcp;
}
//==========================================================================
@ -316,9 +306,8 @@ void FPCXTexture::ReadPCX24bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr
int y, c;
int bytes;
uint8_t * srcp = new uint8_t[lump.GetLength() - sizeof(PCXHeader)];
lump.Read(srcp, lump.GetLength() - sizeof(PCXHeader));
uint8_t * src = srcp;
auto srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader));
uint8_t * src = srcp.Data();
for (y = 0; y < Height; ++y)
{
@ -349,7 +338,6 @@ void FPCXTexture::ReadPCX24bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr
}
}
}
delete [] srcp;
}
//==========================================================================
@ -423,9 +411,9 @@ TArray<uint8_t> FPCXTexture::CreatePalettedPixels(int conversion)
}
else
{
uint8_t * buffer = new uint8_t[Width*Height * 3];
uint8_t * row = buffer;
ReadPCX24bits (buffer, lump, &header, 3);
TArray<uint8_t> buffer(Width*Height * 3, true);
uint8_t * row = buffer.Data();
ReadPCX24bits (row, lump, &header, 3);
for(int y=0; y<Height; y++)
{
for(int x=0; x < Width; x++)
@ -434,7 +422,6 @@ TArray<uint8_t> FPCXTexture::CreatePalettedPixels(int conversion)
row+=3;
}
}
delete [] buffer;
}
return Pixels;
}
@ -452,7 +439,7 @@ int FPCXTexture::CopyPixels(FBitmap *bmp, int conversion)
PalEntry pe[256];
PCXHeader header;
int bitcount;
uint8_t * Pixels;
TArray<uint8_t> Pixels;
auto lump = Wads.OpenLumpReader(SourceLump);
@ -462,7 +449,7 @@ int FPCXTexture::CopyPixels(FBitmap *bmp, int conversion)
if (bitcount < 24)
{
Pixels = new uint8_t[Width*Height];
Pixels.Resize(Width*Height);
if (bitcount < 8)
{
switch (bitcount)
@ -471,7 +458,7 @@ int FPCXTexture::CopyPixels(FBitmap *bmp, int conversion)
case 1:
pe[0] = PalEntry(255, 0, 0, 0);
pe[1] = PalEntry(255, 255, 255, 255);
ReadPCX1bit (Pixels, lump, &header);
ReadPCX1bit (Pixels.Data(), lump, &header);
break;
case 4:
@ -479,7 +466,7 @@ int FPCXTexture::CopyPixels(FBitmap *bmp, int conversion)
{
pe[i] = PalEntry(255, header.palette[i * 3], header.palette[i * 3 + 1], header.palette[i * 3 + 2]);
}
ReadPCX4bits (Pixels, lump, &header);
ReadPCX4bits (Pixels.Data(), lump, &header);
break;
}
}
@ -500,17 +487,16 @@ int FPCXTexture::CopyPixels(FBitmap *bmp, int conversion)
pe[i] = PalEntry(255, r,g,b);
}
lump.Seek(sizeof(header), FileReader::SeekSet);
ReadPCX8bits (Pixels, lump, &header);
ReadPCX8bits (Pixels.Data(), lump, &header);
}
bmp->CopyPixelData(0, 0, Pixels, Width, Height, 1, Width, 0, pe);
bmp->CopyPixelData(0, 0, Pixels.Data(), Width, Height, 1, Width, 0, pe);
}
else
{
Pixels = new uint8_t[Width*Height * 3];
ReadPCX24bits (Pixels, lump, &header, 3);
bmp->CopyPixelDataRGB(0, 0, Pixels, Width, Height, 3, Width*3, 0, CF_RGB);
Pixels.Resize(Width*Height*4);
ReadPCX24bits (Pixels.Data(), lump, &header, 3);
bmp->CopyPixelDataRGB(0, 0, Pixels.Data(), Width, Height, 3, Width*3, 0, CF_RGB);
}
delete [] Pixels;
return 0;
}

View File

@ -186,7 +186,6 @@ TArray<uint8_t> FTGATexture::CreatePalettedPixels(int conversion)
TGAHeader hdr;
uint16_t w;
uint8_t r,g,b,a;
uint8_t * buffer;
TArray<uint8_t> Pixels(Width*Height, true);
lump.Read(&hdr, sizeof(hdr));
@ -237,18 +236,18 @@ TArray<uint8_t> FTGATexture::CreatePalettedPixels(int conversion)
}
int Size = Width * Height * (hdr.bpp>>3);
buffer = new uint8_t[Size];
TArray<uint8_t> buffer(Size, true);
if (hdr.img_type < 4) // uncompressed
{
lump.Read(buffer, Size);
lump.Read(buffer.Data(), Size);
}
else // compressed
{
ReadCompressed(lump, buffer, hdr.bpp>>3);
ReadCompressed(lump, buffer.Data(), hdr.bpp>>3);
}
uint8_t * ptr = buffer;
uint8_t * ptr = buffer.Data();
int step_x = (hdr.bpp>>3);
int Pitch = Width * step_x;
@ -378,7 +377,6 @@ TArray<uint8_t> FTGATexture::CreatePalettedPixels(int conversion)
default:
break;
}
delete [] buffer;
return Pixels;
}
@ -395,7 +393,6 @@ int FTGATexture::CopyPixels(FBitmap *bmp, int conversion)
TGAHeader hdr;
uint16_t w;
uint8_t r,g,b,a;
uint8_t * sbuffer;
int transval = 0;
lump.Read(&hdr, sizeof(hdr));
@ -447,18 +444,18 @@ int FTGATexture::CopyPixels(FBitmap *bmp, int conversion)
}
int Size = Width * Height * (hdr.bpp>>3);
sbuffer = new uint8_t[Size];
TArray<uint8_t> sbuffer(Size);
if (hdr.img_type < 4) // uncompressed
{
lump.Read(sbuffer, Size);
lump.Read(sbuffer.Data(), Size);
}
else // compressed
{
ReadCompressed(lump, sbuffer, hdr.bpp>>3);
ReadCompressed(lump, sbuffer.Data(), hdr.bpp>>3);
}
uint8_t * ptr = sbuffer;
uint8_t * ptr = sbuffer.Data();
int step_x = (hdr.bpp>>3);
int Pitch = Width * step_x;
@ -530,6 +527,5 @@ int FTGATexture::CopyPixels(FBitmap *bmp, int conversion)
default:
break;
}
delete [] sbuffer;
return transval;
}

View File

@ -709,9 +709,9 @@ void I_PrintStr(const char *cp)
if (con_debugoutput)
{
// Strip out any color escape sequences before writing to debug output
char * copy = new char[strlen(cp)+1];
TArray<char> copy(strlen(cp) + 1, true);
const char * srcp = cp;
char * dstp = copy;
char * dstp = copy.Data();
while (*srcp != 0)
{
@ -727,8 +727,7 @@ void I_PrintStr(const char *cp)
}
*dstp=0;
OutputDebugStringA(copy);
delete [] copy;
OutputDebugStringA(copy.Data());
}
if (ConWindowHidden)