From 41751da6190a6d9a893bbe4cbf91f48cb5fceda5 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Sat, 12 Jan 2013 21:06:52 -0500 Subject: [PATCH] change some free/malloc's back to delete/new's --- src/client/GameSave.cpp | 162 ++++++++++++++++++------------------ src/graphics/Graphics.cpp | 44 +++++----- src/preview/PreviewView.cpp | 4 +- src/search/Thumbnail.cpp | 10 +-- 4 files changed, 109 insertions(+), 111 deletions(-) diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 8d8a95ce3..53736cace 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -259,7 +259,7 @@ void GameSave::setSize(int newWidth, int newHeight) { this->blockWidth = newWidth; this->blockHeight = newHeight; - + particlesCount = 0; particles = new Particle[NPART]; @@ -286,7 +286,7 @@ std::vector GameSave::Serialise() int dataSize; char * data = Serialise(dataSize); std::vector dataVect(data, data+dataSize); - free(data); + delete data; return dataVect; } @@ -438,54 +438,54 @@ void GameSave::readOPS(char * data, int dataLength) int savedVersion = inputData[4]; bson b; bson_iterator iter; - + //Block sizes blockX = 0; blockY = 0; blockW = inputData[6]; blockH = inputData[7]; - + //Full size, normalised fullX = blockX*CELL; fullY = blockY*CELL; fullW = blockW*CELL; fullH = blockH*CELL; - + //From newer version if(savedVersion > SAVE_VERSION) throw ParseException(ParseException::WrongVersion, "Save from newer version"); - + //Incompatible cell size if(inputData[5] > CELL) throw ParseException(ParseException::InvalidDimensions, "Incorrect CELL size"); - + //Too large/off screen if(blockX+blockW > XRES/CELL || blockY+blockH > YRES/CELL) throw ParseException(ParseException::InvalidDimensions, "Save too large"); - + setSize(blockW, blockH); - + bsonDataLen = ((unsigned)inputData[8]); bsonDataLen |= ((unsigned)inputData[9]) << 8; bsonDataLen |= ((unsigned)inputData[10]) << 16; bsonDataLen |= ((unsigned)inputData[11]) << 24; - + bsonData = (unsigned char*)malloc(bsonDataLen+1); if(!bsonData) throw ParseException(ParseException::InternalError, "Unable to allocate memory"); - + //Make sure bsonData is null terminated, since all string functions need null terminated strings //(bson_iterator_key returns a pointer into bsonData, which is then used with strcmp) bsonData[bsonDataLen] = 0; - + if (BZ2_bzBuffToBuffDecompress((char*)bsonData, &bsonDataLen, (char*)(inputData+12), inputDataLen-12, 0, 0)) throw ParseException(ParseException::Corrupt, "Unable to decompress"); - + bson_init_data(&b, (char*)bsonData); bson_iterator_init(&iter, &b); - + std::vector tempSigns; - + while(bson_iterator_next(&iter)) { if(strcmp(bson_iterator_key(&iter), "signs")==0) @@ -502,7 +502,7 @@ void GameSave::readOPS(char * data, int dataLength) { bson_iterator signiter; bson_iterator_subiterator(&subiter, &signiter); - + sign tempSign("", 0, 0, sign::Left); while(bson_iterator_next(&signiter)) { @@ -681,7 +681,7 @@ void GameSave::readOPS(char * data, int dataLength) } } } - + //Read wall and fan data if(wallData) { @@ -743,7 +743,7 @@ void GameSave::readOPS(char * data, int dataLength) } } } - + //Read particle data if(partsData && partsPosData) { @@ -778,7 +778,7 @@ void GameSave::readOPS(char * data, int dataLength) { goto fail; } - + //i+3 because we have 4 bytes of required fields (type (1), descriptor (2), temp (1)) if (i+3 >= partsDataLen) goto fail; @@ -796,19 +796,19 @@ void GameSave::readOPS(char * data, int dataLength) if(newIndex < 0 || newIndex >= NPART) goto fail; - + //Store partsptr index+1 for this saved particle index (0 means not loaded) partsSimIndex[partsCount++] = newIndex+1; //Clear the particle, ready for our new properties memset(&(particles[newIndex]), 0, sizeof(Particle)); - + //Required fields particles[newIndex].type = partsData[i]; particles[newIndex].x = x; particles[newIndex].y = y; i+=3; - + //Read temp if(fieldDescriptor & 0x01) { @@ -823,7 +823,7 @@ void GameSave::readOPS(char * data, int dataLength) tempTemp = (char)partsData[i++]; particles[newIndex].temp = tempTemp+294.15f; } - + //Read life if(fieldDescriptor & 0x02) { @@ -836,7 +836,7 @@ void GameSave::readOPS(char * data, int dataLength) particles[newIndex].life |= (((unsigned)partsData[i++]) << 8); } } - + //Read tmp if(fieldDescriptor & 0x08) { @@ -856,7 +856,7 @@ void GameSave::readOPS(char * data, int dataLength) } } } - + //Read ctype if(fieldDescriptor & 0x20) { @@ -871,7 +871,7 @@ void GameSave::readOPS(char * data, int dataLength) particles[newIndex].ctype |= (((unsigned)partsData[i++]) << 8); } } - + //Read dcolour if(fieldDescriptor & 0x40) { @@ -881,21 +881,21 @@ void GameSave::readOPS(char * data, int dataLength) particles[newIndex].dcolour |= (((unsigned)partsData[i++]) << 8); particles[newIndex].dcolour |= ((unsigned)partsData[i++]); } - + //Read vx if(fieldDescriptor & 0x80) { if(i >= partsDataLen) goto fail; particles[newIndex].vx = (partsData[i++]-127.0f)/16.0f; } - + //Read vy if(fieldDescriptor & 0x100) { if(i >= partsDataLen) goto fail; particles[newIndex].vy = (partsData[i++]-127.0f)/16.0f; } - + //Read tmp2 if(fieldDescriptor & 0x400) { @@ -1008,20 +1008,20 @@ void GameSave::readPSv(char * data, int dataLength) int bx0=0, by0=0, bw, bh, w, h, y0 = 0, x0 = 0; int nf=0, new_format = 0, ttv = 0; int *fp = (int *)malloc(NPART*sizeof(int)); - + std::vector tempSigns; char tempSignText[255]; sign tempSign("", 0, 0, sign::Left); - + //Gol data used to read older saves int goltype[NGOL]; int grule[NGOL+1][10]; - + int golRulesCount; int * golRulesT = LoadGOLRules(golRulesCount); memcpy(grule, golRulesT, sizeof(int) * (golRulesCount*10)); free(golRulesT); - + int golTypesCount; int * golTypesT = LoadGOLTypes(golTypesCount); memcpy(goltype, golTypesT, sizeof(int) * (golTypesCount)); @@ -1031,10 +1031,10 @@ void GameSave::readPSv(char * data, int dataLength) try { - + //New file header uses PSv, replacing fuC. This is to detect if the client uses a new save format for temperatures //This creates a problem for old clients, that display and "corrupt" error instead of a "newer version" error - + if (dataLength<16) throw ParseException(ParseException::Corrupt, "No save data"); if (!(c[2]==0x43 && c[1]==0x75 && c[0]==0x66) && !(c[2]==0x76 && c[1]==0x53 && c[0]==0x50)) @@ -1045,7 +1045,7 @@ void GameSave::readPSv(char * data, int dataLength) if (c[4]>SAVE_VERSION) throw ParseException(ParseException::WrongVersion, "Save from newer version"); ver = c[4]; - + if (ver<34) { legacyEnable = 1; @@ -1070,7 +1070,7 @@ void GameSave::readPSv(char * data, int dataLength) } } } - + bw = c[6]; bh = c[7]; if (bx0+bw > XRES/CELL) @@ -1081,7 +1081,7 @@ void GameSave::readPSv(char * data, int dataLength) bx0 = 0; if (by0 < 0) by0 = 0; - + if (c[5]!=CELL || bx0+bw>XRES/CELL || by0+bh>YRES/CELL) throw ParseException(ParseException::InvalidDimensions, "Save too large"); i = (unsigned)c[8]; @@ -1091,9 +1091,9 @@ void GameSave::readPSv(char * data, int dataLength) d = (unsigned char *)malloc(i); if (!d) throw ParseException(ParseException::Corrupt, "Cannot allocate memory"); - + setSize(bw, bh); - + int bzStatus = 0; if (bzStatus = BZ2_bzBuffToBuffDecompress((char *)d, (unsigned *)&i, (char *)(c+12), dataLength-12, 0, 0)) { @@ -1106,22 +1106,22 @@ void GameSave::readPSv(char * data, int dataLength) #ifdef DEBUG std::cout << "Parsing " << dataLength << " bytes of data, version " << ver << std::endl; #endif - + if (dataLength < bw*bh) throw ParseException(ParseException::Corrupt, "Save data corrupt (missing data)"); - + // normalize coordinates x0 = bx0*CELL; y0 = by0*CELL; w = bw *CELL; h = bh *CELL; - + if (ver<46) { gravityMode = 0; airMode = 0; } m = (int *)calloc(XRES*YRES, sizeof(int)); - + // load the required air state for (y=by0; y0.0f || fabs(particles[i-1].vy)>0.0f) particles[i-1].flags |= FLAG_MOVABLE; } - + if (ver<48 && (ty==OLD_PT_WIND || (ty==PT_BRAY&&particles[i-1].life==0))) { // Replace invisible particles with something sensible and add decoration to hide it @@ -1559,7 +1559,7 @@ void GameSave::readPSv(char * data, int dataLength) } } } - + if (p >= dataLength) goto version1; j = d[p++]; @@ -1586,7 +1586,7 @@ void GameSave::readPSv(char * data, int dataLength) tempSigns.push_back(tempSign); p += x; } - + for (i = 0; i < tempSigns.size(); i++) { if(signs.size() == MAXSIGNS) @@ -1614,7 +1614,7 @@ void GameSave::readPSv(char * data, int dataLength) } throw; } - + version1: if (m) { @@ -1651,17 +1651,17 @@ char * GameSave::serialiseOPS(int & dataLength) //Get coords in blocks blockX = 0;//orig_x0/CELL; blockY = 0;//orig_y0/CELL; - + //Snap full coords to block size fullX = blockX*CELL; fullY = blockY*CELL; - + //Original size + offset of original corner from snapped corner, rounded up by adding CELL-1 blockW = blockWidth;//(blockWidth-fullX+CELL-1)/CELL; blockH = blockHeight;//(blockHeight-fullY+CELL-1)/CELL; fullW = blockW*CELL; fullH = blockH*CELL; - + //Copy fan and wall data wallData = (unsigned char*)malloc(blockW*blockH); wallDataLen = blockW*blockH; @@ -1697,7 +1697,7 @@ char * GameSave::serialiseOPS(int & dataLength) free(wallData); wallData = NULL; } - + //Index positions of all particles, using linked lists //partsPosFirstMap is pmap for the first particle in each position //partsPosLastMap is pmap for the last particle in each position @@ -1731,7 +1731,7 @@ char * GameSave::serialiseOPS(int & dataLength) partsPosCount[y*fullW + x]++; } } - + //Store number of particles in each position partsPosData = (unsigned char*)malloc(fullW*fullH*3); partsPosDataLen = 0; @@ -1745,7 +1745,7 @@ char * GameSave::serialiseOPS(int & dataLength) partsPosData[partsPosDataLen++] = (posCount&0x000000FF); } } - + //Copy parts data /* Field descriptor format: | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | @@ -1762,27 +1762,27 @@ char * GameSave::serialiseOPS(int & dataLength) { //Find the first particle in this position i = partsPosFirstMap[y*fullW + x]; - + //Loop while there is a pmap entry while (i) { unsigned short fieldDesc = 0; int fieldDescLoc = 0, tempTemp, vTemp; - + //Turn pmap entry into a particles index i = i>>8; - + //Store saved particle index+1 for this partsptr index (0 means not saved) partsSaveIndex[i] = (partsCount++) + 1; //Type (required) partsData[partsDataLen++] = particles[i].type; elementCount[particles[i].type]++; - + //Location of the field descriptor fieldDescLoc = partsDataLen++; partsDataLen++; - + //Extra Temperature (2nd byte optional, 1st required), 1 to 2 bytes //Store temperature as an offset of 21C(294.15K) or go into a 16byte int and store the whole thing if(fabs(particles[i].temp-294.15f)<127) @@ -1797,7 +1797,7 @@ char * GameSave::serialiseOPS(int & dataLength) partsData[partsDataLen++] = tempTemp; partsData[partsDataLen++] = tempTemp >> 8; } - + //Life (optional), 1 to 2 bytes if(particles[i].life) { @@ -1809,7 +1809,7 @@ char * GameSave::serialiseOPS(int & dataLength) partsData[partsDataLen++] = particles[i].life >> 8; } } - + //Tmp (optional), 1 to 2 bytes if(particles[i].tmp) { @@ -1827,7 +1827,7 @@ char * GameSave::serialiseOPS(int & dataLength) } } } - + //Ctype (optional), 1 or 4 bytes if(particles[i].ctype) { @@ -1841,7 +1841,7 @@ char * GameSave::serialiseOPS(int & dataLength) partsData[partsDataLen++] = (particles[i].ctype&0x0000FF00)>>8; } } - + //Dcolour (optional), 4 bytes if(particles[i].dcolour && (particles[i].dcolour & 0xFF000000)) { @@ -1851,7 +1851,7 @@ char * GameSave::serialiseOPS(int & dataLength) partsData[partsDataLen++] = (particles[i].dcolour&0x0000FF00)>>8; partsData[partsDataLen++] = (particles[i].dcolour&0x000000FF); } - + //VX (optional), 1 byte if(fabs(particles[i].vx) > 0.001f) { @@ -1861,7 +1861,7 @@ char * GameSave::serialiseOPS(int & dataLength) if (vTemp>255) vTemp=255; partsData[partsDataLen++] = vTemp; } - + //VY (optional), 1 byte if(fabs(particles[i].vy) > 0.001f) { @@ -1871,7 +1871,7 @@ char * GameSave::serialiseOPS(int & dataLength) if (vTemp>255) vTemp=255; partsData[partsDataLen++] = vTemp; } - + //Tmp2 (optional), 1 or 2 bytes if(particles[i].tmp2) { @@ -1883,11 +1883,11 @@ char * GameSave::serialiseOPS(int & dataLength) partsData[partsDataLen++] = particles[i].tmp2 >> 8; } } - + //Write the field descriptor; partsData[fieldDescLoc] = fieldDesc; partsData[fieldDescLoc+1] = fieldDesc>>8; - + //Get the pmap entry for the next particle in the same position i = partsPosLink[i]; } @@ -1940,7 +1940,7 @@ char * GameSave::serialiseOPS(int & dataLength) free(partsData); partsData = NULL; } - + bson_init(&b); bson_append_bool(&b, "waterEEnabled", waterEEnabled); bson_append_bool(&b, "legacyEnable", legacyEnable); @@ -1948,7 +1948,7 @@ char * GameSave::serialiseOPS(int & dataLength) bson_append_bool(&b, "paused", paused); bson_append_int(&b, "gravityMode", gravityMode); bson_append_int(&b, "airMode", airMode); - + //bson_append_int(&b, "leftSelectedElement", sl); //bson_append_int(&b, "rightSelectedElement", sr); //bson_append_int(&b, "activeMenu", active_menu); @@ -2000,12 +2000,12 @@ char * GameSave::serialiseOPS(int & dataLength) #ifdef DEBUG bson_print(&b); #endif - + finalData = (unsigned char *)bson_data(&b); finalDataLen = bson_size(&b); outputDataLen = finalDataLen*2+12; - outputData = (unsigned char *)malloc(outputDataLen); - + outputData = new unsigned char[outputDataLen]; + outputData[0] = 'O'; outputData[1] = 'P'; outputData[2] = 'S'; @@ -2018,7 +2018,7 @@ char * GameSave::serialiseOPS(int & dataLength) outputData[9] = finalDataLen >> 8; outputData[10] = finalDataLen >> 16; outputData[11] = finalDataLen >> 24; - + if (BZ2_bzBuffToBuffCompress((char*)(outputData+12), &outputDataLen, (char*)finalData, bson_size(&b), 9, 0, 0) != BZ_OK) { puts("Save Error\n"); @@ -2027,12 +2027,12 @@ char * GameSave::serialiseOPS(int & dataLength) outputData = NULL; goto fin; } - + #ifdef DEBUG printf("compressed data: %d\n", outputDataLen); #endif dataLength = outputDataLen + 12; - + fin: bson_destroy(&b); if(partsData) @@ -2047,7 +2047,7 @@ fin: free(partsSaveIndex); if (soapLinkData) free(soapLinkData); - + return (char*)outputData; } diff --git a/src/graphics/Graphics.cpp b/src/graphics/Graphics.cpp index 54544d334..f8519655b 100644 --- a/src/graphics/Graphics.cpp +++ b/src/graphics/Graphics.cpp @@ -141,7 +141,7 @@ char * Graphics::GenerateGradient(pixel * colours, float * points, int pointcoun temp = points[j-1]; points[j-1] = points[j]; points[j] = temp; - + ptemp = colours[j-1]; colours[j-1] = colours[j]; colours[j] = ptemp; @@ -178,7 +178,7 @@ void *Graphics::ptif_pack(pixel *src, int w, int h, int *result_size){ unsigned char *blue_chan = (unsigned char*)calloc(1, w*h); unsigned char *data = (unsigned char*)malloc(((w*h)*3)+8); unsigned char *result = (unsigned char*)malloc(((w*h)*3)+8); - + for(cx = 0; cx>8; result[6] = h; result[7] = h>>8; - + i -= 8; - + if(BZ2_bzBuffToBuffCompress((char *)(result+8), (unsigned *)&i, (char *)data, datalen, 9, 0, 0) != 0){ free(data); free(result); return NULL; } - + *result_size = i+8; free(data); return result; @@ -234,14 +234,14 @@ pixel *Graphics::ptif_unpack(void *datain, int size, int *w, int *h){ } width = data[4]|(data[5]<<8); height = data[6]|(data[7]<<8); - + i = (width*height)*3; undata = (unsigned char*)calloc(1, (width*height)*3); red_chan = (unsigned char*)calloc(1, width*height); green_chan = (unsigned char*)calloc(1, width*height); blue_chan = (unsigned char *)calloc(1, width*height); result = (pixel *)calloc(width*height, PIXELSIZE); - + resCode = BZ2_bzBuffToBuffDecompress((char *)undata, (unsigned *)&i, (char *)(data+8), size-8, 0, 0); if (resCode){ printf("Decompression failure, %d\n", resCode); @@ -264,13 +264,13 @@ pixel *Graphics::ptif_unpack(void *datain, int size, int *w, int *h){ memcpy(red_chan, undata, width*height); memcpy(green_chan, undata+(width*height), width*height); memcpy(blue_chan, undata+((width*height)*2), width*height); - + for(cx = 0; cxput_line(&samples[c][0])) { printf("Out of memory!\n"); return NULL; } - } + } //Perform resample and Copy components from resampler result samples to image buffer for ( ; ; ) { int comp_index; for (comp_index = 0; comp_index < PIXELCHANNELS; comp_index++) - { + { const float* resultSamples = resamplers[comp_index]->get_line(); if (!resultSamples) break; @@ -364,9 +364,9 @@ pixel *Graphics::resample_img(pixel *src, int sw, int sh, int rw, int rh) *resultPixel = (unsigned char)c; resultPixel += PIXELSIZE; } - } + } if (comp_index < PIXELCHANNELS) - break; + break; resultY++; } @@ -440,7 +440,7 @@ pixel *Graphics::resample_img(pixel *src, int sw, int sh, int rw, int rh) (int)(((((float)PIXR(tl))*(1.0f-fxc))+(((float)PIXR(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXR(bl))*(1.0f-fxc))+(((float)PIXR(br))*(fxc)))*(fyc)), (int)(((((float)PIXG(tl))*(1.0f-fxc))+(((float)PIXG(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXG(bl))*(1.0f-fxc))+(((float)PIXG(br))*(fxc)))*(fyc)), (int)(((((float)PIXB(tl))*(1.0f-fxc))+(((float)PIXB(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXB(bl))*(1.0f-fxc))+(((float)PIXB(br))*(fxc)))*(fyc)) - ); + ); } } else { //Stairstepping @@ -483,7 +483,7 @@ pixel *Graphics::resample_img(pixel *src, int sw, int sh, int rw, int rh) (int)(((((float)PIXR(tl))*(1.0f-fxc))+(((float)PIXR(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXR(bl))*(1.0f-fxc))+(((float)PIXR(br))*(fxc)))*(fyc)), (int)(((((float)PIXG(tl))*(1.0f-fxc))+(((float)PIXG(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXG(bl))*(1.0f-fxc))+(((float)PIXG(br))*(fxc)))*(fyc)), (int)(((((float)PIXB(tl))*(1.0f-fxc))+(((float)PIXB(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXB(bl))*(1.0f-fxc))+(((float)PIXB(br))*(fxc)))*(fyc)) - ); + ); } free(oq); oq = q; @@ -1116,4 +1116,4 @@ VideoBuffer Graphics::DumpFrame() std::copy(vid, vid+((XRES+BARSIZE)*(YRES+MENUSIZE)), newBuffer.Buffer); return newBuffer; #endif -} \ No newline at end of file +} diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index fca7fa005..95066ebed 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -196,7 +196,7 @@ PreviewView::PreviewView(): } void PreviewView::AttachController(PreviewController * controller) -{ +{ c = controller; saveIDTextbox->SetText(format::NumberToString(c->SaveID())); } @@ -411,7 +411,7 @@ void PreviewView::NotifySaveChanged(PreviewModel * sender) float factorY = ((float)YRES/2)/((float)savePreview->Size.Y); float scaleFactor = factorY < factorX ? factorY : factorX; savePreview->Data = Graphics::resample_img(oldData, savePreview->Size.X, savePreview->Size.Y, savePreview->Size.X*scaleFactor, savePreview->Size.Y*scaleFactor); - free(oldData); + delete oldData; savePreview->Size.X *= scaleFactor; savePreview->Size.Y *= scaleFactor; } diff --git a/src/search/Thumbnail.cpp b/src/search/Thumbnail.cpp index e1bc6f835..81ea8e910 100644 --- a/src/search/Thumbnail.cpp +++ b/src/search/Thumbnail.cpp @@ -16,7 +16,7 @@ Thumbnail::Thumbnail(const Thumbnail & thumb): //Ensure the actual thumbnail data is copied if(thumb.Data) { - Data = (pixel *)malloc((thumb.Size.X*thumb.Size.Y) * PIXELSIZE); + Data = new pixel[thumb.Size.X*thumb.Size.Y]; memcpy(Data, thumb.Data, (thumb.Size.X*thumb.Size.Y) * PIXELSIZE); } else @@ -33,7 +33,7 @@ Thumbnail::Thumbnail(int _id, int _datestamp, pixel * _data, ui::Point _size): { if(_data) { - Data = (pixel *)malloc((_size.X*_size.Y) * PIXELSIZE); + Data = new pixel[_size.X*_size.Y]; memcpy(Data, _data, (_size.X*_size.Y) * PIXELSIZE); } else @@ -69,14 +69,12 @@ void Thumbnail::Resize(ui::Point newSize) Data = Graphics::resample_img(thumbData, Size.X, Size.Y, Size.X * scaleFactor, Size.Y * scaleFactor); Size.X *= scaleFactor; Size.Y *= scaleFactor; - free(thumbData); + delete thumbData; } } Thumbnail::~Thumbnail() { if(Data) - { - free(Data); - } + delete Data; }