From a73c0658117e36df1cd11d414821e0817c783104 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 17 Dec 2018 18:28:04 +0100 Subject: [PATCH] - replaced several explicit allocations with TArrays. --- src/posix/sdl/i_system.cpp | 21 +++++----- src/r_data/models/models_voxel.cpp | 6 +-- src/r_data/renderinfo.cpp | 11 ++--- src/r_data/voxels.cpp | 32 +++------------ src/r_data/voxels.h | 4 +- src/resourcefiles/file_pak.cpp | 5 +-- src/textures/formats/jpegtexture.cpp | 15 +++---- src/textures/formats/pcxtexture.cpp | 60 +++++++++++----------------- src/textures/formats/tgatexture.cpp | 20 ++++------ src/win32/i_system.cpp | 7 ++-- 10 files changed, 66 insertions(+), 115 deletions(-) diff --git a/src/posix/sdl/i_system.cpp b/src/posix/sdl/i_system.cpp index 9f4797926..8b73d2f14 100644 --- a/src/posix/sdl/i_system.cpp +++ b/src/posix/sdl/i_system.cpp @@ -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 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) diff --git a/src/r_data/models/models_voxel.cpp b/src/r_data/models/models_voxel.cpp index 8cda437bc..9a01b6487 100644 --- a/src/r_data/models/models_voxel.cpp +++ b/src/r_data/models/models_voxel.cpp @@ -83,7 +83,7 @@ TArray FVoxelTexture::CreatePalettedPixels(int conversion) { // GetPixels gets called when a translated palette is used so we still need to implement it here. TArray 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) { diff --git a/src/r_data/renderinfo.cpp b/src/r_data/renderinfo.cpp index 88526d824..7edeef789 100644 --- a/src/r_data/renderinfo.cpp +++ b/src/r_data/renderinfo.cpp @@ -392,7 +392,7 @@ static void AddToVertex(const sector_t * sec, TArray & list) static void InitVertexData() { - auto vt_sectorlists = new TArray[level.vertexes.Size()]; + TArray> 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 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) diff --git a/src/r_data/voxels.cpp b/src/r_data/voxels.cpp index 9dc0b7006..a9eb9eb68 100644 --- a/src/r_data/voxels.cpp +++ b/src/r_data/voxels.cpp @@ -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(); } diff --git a/src/r_data/voxels.h b/src/r_data/voxels.h index eac1b39ee..60a149be9 100644 --- a/src/r_data/voxels.h +++ b/src/r_data/voxels.h @@ -50,16 +50,14 @@ public: struct FVoxel { + TArray 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(); diff --git a/src/resourcefiles/file_pak.cpp b/src/resourcefiles/file_pak.cpp index d7f091c02..f679a0564 100644 --- a/src/resourcefiles/file_pak.cpp +++ b/src/resourcefiles/file_pak.cpp @@ -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 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; } diff --git a/src/textures/formats/jpegtexture.cpp b/src/textures/formats/jpegtexture.cpp index a441c585b..68ead4a7d 100644 --- a/src/textures/formats/jpegtexture.cpp +++ b/src/textures/formats/jpegtexture.cpp @@ -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 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; } diff --git a/src/textures/formats/pcxtexture.cpp b/src/textures/formats/pcxtexture.cpp index ff37ce8aa..70cb7e2e7 100644 --- a/src/textures/formats/pcxtexture.cpp +++ b/src/textures/formats/pcxtexture.cpp @@ -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 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 line(hdr->bytesPerScanLine, true); + TArray 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 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 FPCXTexture::CreatePalettedPixels(int conversion) } else { - uint8_t * buffer = new uint8_t[Width*Height * 3]; - uint8_t * row = buffer; - ReadPCX24bits (buffer, lump, &header, 3); + TArray buffer(Width*Height * 3, true); + uint8_t * row = buffer.Data(); + ReadPCX24bits (row, lump, &header, 3); for(int y=0; y 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 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; } diff --git a/src/textures/formats/tgatexture.cpp b/src/textures/formats/tgatexture.cpp index 1a26f6036..9936a828e 100644 --- a/src/textures/formats/tgatexture.cpp +++ b/src/textures/formats/tgatexture.cpp @@ -186,7 +186,6 @@ TArray FTGATexture::CreatePalettedPixels(int conversion) TGAHeader hdr; uint16_t w; uint8_t r,g,b,a; - uint8_t * buffer; TArray Pixels(Width*Height, true); lump.Read(&hdr, sizeof(hdr)); @@ -237,18 +236,18 @@ TArray FTGATexture::CreatePalettedPixels(int conversion) } int Size = Width * Height * (hdr.bpp>>3); - buffer = new uint8_t[Size]; + TArray 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 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 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; } diff --git a/src/win32/i_system.cpp b/src/win32/i_system.cpp index 7a2522fd7..45bb50b4d 100644 --- a/src/win32/i_system.cpp +++ b/src/win32/i_system.cpp @@ -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 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)