get rid of malloc()

This commit is contained in:
ehofman
2003-04-10 09:26:03 +00:00
parent 192db1c343
commit 5712053ad8
2 changed files with 51 additions and 50 deletions
+50 -50
View File
@@ -12,8 +12,6 @@
* $Id$ * $Id$
*/ */
#include <stdlib.h> // malloc()
#include <GL/glu.h> #include <GL/glu.h>
#include <zlib.h> #include <zlib.h>
@@ -22,13 +20,17 @@
SGTexture::SGTexture() SGTexture::SGTexture()
{ {
texture_data = NULL; texture_data = 0;
}
SGTexture::SGTexture(unsigned int width, unsigned int height)
{
texture_data = new GLubyte[ width*height*3 ];
} }
SGTexture::~SGTexture() SGTexture::~SGTexture()
{ {
if (texture_data) delete texture_data;
free(texture_data);
} }
void void
@@ -159,7 +161,7 @@ SGTexture::read_alpha_texture(const char *name)
int y; int y;
if (texture_data) if (texture_data)
free(texture_data); delete texture_data;
image = ImageOpen(name); image = ImageOpen(name);
if(!image) { if(!image) {
@@ -176,9 +178,7 @@ SGTexture::read_alpha_texture(const char *name)
return; return;
} }
texture_data = (GLubyte *) texture_data = new GLubyte[ image->xsize * image->ysize ];
malloc(image->xsize*image->ysize*sizeof(GLubyte));
if (!texture_data) if (!texture_data)
return; return;
@@ -199,7 +199,7 @@ SGTexture::read_rgb_texture(const char *name)
int y; int y;
if (texture_data) if (texture_data)
free(texture_data); delete texture_data;
image = ImageOpen(name); image = ImageOpen(name);
if(!image) if(!image)
@@ -212,17 +212,17 @@ SGTexture::read_rgb_texture(const char *name)
return; return;
} }
texture_data = (GLubyte *)malloc(image->xsize*image->ysize*sizeof(GLubyte)*3); texture_data = new GLubyte[ image->xsize * image->ysize * 3];
rbuf = (GLubyte *)malloc(image->xsize*sizeof(GLubyte)); rbuf = new GLubyte[ image->xsize ];
gbuf = (GLubyte *)malloc(image->xsize*sizeof(GLubyte)); gbuf = new GLubyte[ image->xsize ];
bbuf = (GLubyte *)malloc(image->xsize*sizeof(GLubyte)); bbuf = new GLubyte[ image->xsize ];
abuf = (GLubyte *)malloc(image->xsize*sizeof(GLubyte)); abuf = new GLubyte[ image->xsize ];
if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) { if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
if (texture_data) free(texture_data); delete texture_data;
if (rbuf) free(rbuf); delete rbuf;
if (gbuf) free(gbuf); delete gbuf;
if (bbuf) free(bbuf); delete bbuf;
if (abuf) free(abuf); delete abuf;
return; return;
} }
@@ -245,10 +245,10 @@ SGTexture::read_rgb_texture(const char *name)
} }
ImageClose(image); ImageClose(image);
free(rbuf); delete rbuf;
free(gbuf); delete gbuf;
free(bbuf); delete bbuf;
free(abuf); delete abuf;
} }
void void
@@ -259,7 +259,7 @@ SGTexture::read_raw_texture(const char *name)
int y; int y;
if (texture_data) if (texture_data)
free(texture_data); delete texture_data;
image = RawImageOpen(name); image = RawImageOpen(name);
@@ -269,7 +269,7 @@ SGTexture::read_raw_texture(const char *name)
texture_width = 256; texture_width = 256;
texture_height = 256; texture_height = 256;
texture_data = (GLubyte *)malloc(256*256*sizeof(GLubyte)*3); texture_data = new GLubyte[ 256 * 256 * 3 ];
if(!texture_data) if(!texture_data)
return; return;
@@ -290,7 +290,7 @@ SGTexture::read_r8_texture(const char *name)
int xy; int xy;
if (texture_data) if (texture_data)
free(texture_data); delete texture_data;
//it wouldn't make sense to write a new function ... //it wouldn't make sense to write a new function ...
image = RawImageOpen(name); image = RawImageOpen(name);
@@ -301,7 +301,7 @@ SGTexture::read_r8_texture(const char *name)
texture_width = 256; texture_width = 256;
texture_height = 256; texture_height = 256;
texture_data = (GLubyte *)malloc(256*256*sizeof(GLubyte)*3); texture_data = new GLubyte [ 256 * 256 * 3 ];
if(!texture_data) if(!texture_data)
return; return;
@@ -339,13 +339,13 @@ SGTexture::ImageOpen(const char *fileName)
swapFlag = 0; swapFlag = 0;
} }
image = (SGTexture::ImageRec *)malloc(sizeof(SGTexture::ImageRec)); image = new SGTexture::ImageRec;
if (image == NULL) { if (image == 0) {
// fprintf(stderr, "Out of memory!\n"); // fprintf(stderr, "Out of memory!\n");
exit(1); return 0;
} }
if ((image->file = gzopen(fileName, "rb")) == NULL) { if ((image->file = gzopen(fileName, "rb")) == 0) {
return NULL; return 0;
} }
gzread(image->file, image, 12); gzread(image->file, image, 12);
@@ -354,19 +354,19 @@ SGTexture::ImageOpen(const char *fileName)
ConvertShort(&image->imagic, 6); ConvertShort(&image->imagic, 6);
} }
image->tmp = (GLubyte *)malloc(image->xsize*256); image->tmp = new GLubyte[ image->xsize * 256 ];
if (image->tmp == NULL) { if (image->tmp == 0) {
// fprintf(stderr, "\nOut of memory!\n"); // fprintf(stderr, "\nOut of memory!\n");
exit(1); return 0;
} }
if ((image->type & 0xFF00) == 0x0100) { if ((image->type & 0xFF00) == 0x0100) {
x = image->ysize * image->zsize * (int) sizeof(unsigned); x = image->ysize * image->zsize * (int) sizeof(unsigned);
image->rowStart = (unsigned *)malloc(x); image->rowStart = new unsigned[x];
image->rowSize = (int *)malloc(x); image->rowSize = new int[x];
if (image->rowStart == NULL || image->rowSize == NULL) { if (image->rowStart == 0 || image->rowSize == 0) {
// fprintf(stderr, "\nOut of memory!\n"); // fprintf(stderr, "\nOut of memory!\n");
exit(1); return 0;
} }
image->rleEnd = 512 + (2 * x); image->rleEnd = 512 + (2 * x);
gzseek(image->file, 512, SEEK_SET); gzseek(image->file, 512, SEEK_SET);
@@ -384,8 +384,8 @@ SGTexture::ImageOpen(const char *fileName)
void void
SGTexture::ImageClose(SGTexture::ImageRec *image) { SGTexture::ImageClose(SGTexture::ImageRec *image) {
gzclose(image->file); gzclose(image->file);
free(image->tmp); delete image->tmp;
free(image); delete image;
} }
@@ -407,13 +407,13 @@ SGTexture::RawImageOpen(const char *fileName)
swapFlag = 0; swapFlag = 0;
} }
image = (SGTexture::ImageRec *)malloc(sizeof(SGTexture::ImageRec)); image = new SGTexture::ImageRec;
if (image == NULL) { if (image == 0) {
// fprintf(stderr, "Out of memory!\n"); // fprintf(stderr, "Out of memory!\n");
exit(1); return 0;
} }
if ((image->file = gzopen(fileName, "rb")) == NULL) { if ((image->file = gzopen(fileName, "rb")) == 0) {
return NULL; return 0;
} }
gzread(image->file, image, 12); gzread(image->file, image, 12);
@@ -424,11 +424,11 @@ SGTexture::RawImageOpen(const char *fileName)
//just allocate a pseudo value as I'm too lazy to change ImageClose()... //just allocate a pseudo value as I'm too lazy to change ImageClose()...
image->tmp = (GLubyte *)malloc(1); image->tmp = new GLubyte;
if (image->tmp == NULL) { if (image->tmp == 0) {
// fprintf(stderr, "\nOut of memory!\n"); // fprintf(stderr, "\nOut of memory!\n");
exit(1); return 0;
} }
return image; return image;
+1
View File
@@ -47,6 +47,7 @@ protected:
public: public:
SGTexture(); SGTexture();
SGTexture(unsigned int width, unsigned int height);
~SGTexture(); ~SGTexture();
/* Copyright (c) Mark J. Kilgard, 1997. */ /* Copyright (c) Mark J. Kilgard, 1997. */