Skip to content

Commit

Permalink
Refactor all destructor functions from free<thing>() -> `destroy<th…
Browse files Browse the repository at this point in the history
…ing>()` so the autocomplete doesn't collide with system free()

Refactor destructor APIs to be a bit neater, they now free the pointer passed in so you don't have to do it yourself.
  • Loading branch information
vkoskiv committed Feb 3, 2020
1 parent 9b8f3ed commit 5a8657b
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 106 deletions.
6 changes: 3 additions & 3 deletions src/acceleration/kdtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ bool rayIntersectsWithNode(const struct kdTreeNode *node, const struct lightRay
return false;
}

void freeTree(struct kdTreeNode *node) {
void destroyTree(struct kdTreeNode *node) {
if (node) {
freeTree(node->left);
freeTree(node->right);
destroyTree(node->left);
destroyTree(node->right);
if (node->bbox) free(node->bbox);
if (node->polygons) free(node->polygons);
free(node);
Expand Down
2 changes: 1 addition & 1 deletion src/acceleration/kdtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ int checkTree(const struct kdTreeNode *node);

/// Free a given tree
/// @param node Root node of a tree to free
void freeTree(struct kdTreeNode *node);
void destroyTree(struct kdTreeNode *node);
9 changes: 3 additions & 6 deletions src/c-ray.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void crInitSDL() {
void crDestroySDL() {
#ifdef UI_ENABLED
ASSERT(grenderer->mainDisplay->window);
freeDisplay(grenderer->mainDisplay);
destroyDisplay(grenderer->mainDisplay);
#endif
}

Expand Down Expand Up @@ -93,11 +93,8 @@ void crInitRenderer() {

void crDestroyRenderer() {
ASSERT(grenderer);
freeRenderer(grenderer);
if (currentImage) {
freeTexture(currentImage);
free(currentImage);
}
destroyRenderer(grenderer);
destroyTexture(currentImage);
}

int crLoadSceneFromFile(char *filePath) {
Expand Down
3 changes: 2 additions & 1 deletion src/datatypes/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ void transformCameraIntoView(struct camera *cam) {
transformCameraView(cam, &cam->up);
}

void freeCamera(struct camera *cam) {
void destroyCamera(struct camera *cam) {
if (cam->transforms) {
free(cam->transforms);
free(cam);
}
}
2 changes: 1 addition & 1 deletion src/datatypes/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ void initCamera(struct camera *cam);
void transformCameraView(struct camera *cam, struct vector *direction); //For transforming direction in renderer
void transformCameraIntoView(struct camera *cam); //Run once in scene.c to calculate pos, up, left

void freeCamera(struct camera *cam);
void destroyCamera(struct camera *cam);
7 changes: 2 additions & 5 deletions src/datatypes/material.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ bool dialectricBSDF(struct hitRecord *isect, const struct lightRay *ray, struct
return true;
}

void freeMaterial(struct material *mat) {
void destroyMaterial(struct material *mat) {
if (mat->textureFilePath) {
free(mat->textureFilePath);
}
Expand All @@ -349,9 +349,6 @@ void freeMaterial(struct material *mat) {
free(mat->name);
}
if (mat->hasTexture) {
if (mat->texture) {
freeTexture(mat->texture);
free(mat->texture);
}
destroyTexture(mat->texture);
}
}
2 changes: 1 addition & 1 deletion src/datatypes/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ bool dialectricBSDF(struct hitRecord *isect, const struct lightRay *ray, struct

void assignBSDF(struct material *mat);

void freeMaterial(struct material *mat);
void destroyMaterial(struct material *mat);
6 changes: 3 additions & 3 deletions src/datatypes/mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void transformMesh(struct mesh *mesh) {
free(ntformed);
}

void freeMesh(struct mesh *mesh) {
void destroyMesh(struct mesh *mesh) {
if (mesh->name) {
free(mesh->name);
}
Expand All @@ -68,11 +68,11 @@ void freeMesh(struct mesh *mesh) {
}
}
if (mesh->tree) {
freeTree(mesh->tree);
destroyTree(mesh->tree);
}
if (mesh->materials) {
for (int i = 0; i < mesh->materialCount; i++) {
freeMaterial(&mesh->materials[i]);
destroyMaterial(&mesh->materials[i]);
}
free(mesh->materials);
}
Expand Down
2 changes: 1 addition & 1 deletion src/datatypes/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ struct mesh {
void addTransform(struct mesh *mesh, struct transform transform);
void transformMesh(struct mesh *mesh);

void freeMesh(struct mesh *mesh);
void destroyMesh(struct mesh *mesh);
32 changes: 15 additions & 17 deletions src/datatypes/scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,21 @@ int loadScene(struct renderer *r, char *input) {
}

//Free scene data
void freeScene(struct world *scene) {
if (scene->hdr) {
freeTexture(scene->hdr);
free(scene->hdr);
}

if (scene->meshes) {
for (int i = 0; i < scene->meshCount; i++) {
freeMesh(&scene->meshes[i]);
void destroyScene(struct world *scene) {
if (scene) {
destroyTexture(scene->hdr);
if (scene->meshes) {
for (int i = 0; i < scene->meshCount; i++) {
destroyMesh(&scene->meshes[i]);
}
free(scene->meshes);
}
free(scene->meshes);
}
if (scene->spheres) {
free(scene->spheres);
}
if (scene->camera) {
freeCamera(scene->camera);
free(scene->camera);
if (scene->spheres) {
free(scene->spheres);
}
if (scene->camera) {
destroyCamera(scene->camera);
}
free(scene);
}
}
2 changes: 1 addition & 1 deletion src/datatypes/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ struct world {

int loadScene(struct renderer *r, char *input);

void freeScene(struct world *scene);
void destroyScene(struct world *scene);
31 changes: 17 additions & 14 deletions src/datatypes/texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct texture *newTexture(enum precision p, int width, int height, int channels
t->byte_data = calloc(channels * width * height, sizeof(unsigned char));
if (!t->byte_data) {
logr(warning, "Failed to allocate %ix%i texture.\n", width, height);
freeTexture(t);
destroyTexture(t);
return NULL;
}
}
Expand All @@ -104,7 +104,7 @@ struct texture *newTexture(enum precision p, int width, int height, int channels
t->float_data = calloc(channels * width * height, sizeof(float));
if (!t->float_data) {
logr(warning, "Failed to allocate %ix%i texture.\n", width, height);
freeTexture(t);
destroyTexture(t);
return NULL;
}
}
Expand Down Expand Up @@ -135,17 +135,20 @@ void textureToSRGB(struct texture *t) {
t->colorspace = sRGB;
}

void freeTexture(struct texture *t) {
if (t->fileName) {
free(t->fileName);
}
if (t->filePath) {
free(t->filePath);
}
if (t->byte_data) {
free(t->byte_data);
}
if (t->float_data) {
free(t->float_data);
void destroyTexture(struct texture *t) {
if (t) {
if (t->fileName) {
free(t->fileName);
}
if (t->filePath) {
free(t->filePath);
}
if (t->byte_data) {
free(t->byte_data);
}
if (t->float_data) {
free(t->float_data);
}
free(t);
}
}
2 changes: 1 addition & 1 deletion src/datatypes/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ void textureFromSRGB(struct texture *t);
/// @param t Texture to convert
void textureToSRGB(struct texture *t);

void freeTexture(struct texture *tex);
void destroyTexture(struct texture *tex);
28 changes: 14 additions & 14 deletions src/datatypes/vertexbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,20 @@ int normalCount;
struct coord *textureArray;
int textureCount;

void freeVertexBuffer() {
void allocVertexBuffer() {
ASSERT(!vertexArray);
vertexArray = calloc(1, sizeof(struct vector));
normalArray = calloc(1, sizeof(struct vector));
textureArray = calloc(1, sizeof(struct coord));
polygonArray = calloc(1, sizeof(struct poly));

vertexCount = 0;
normalCount = 0;
textureCount = 0;
polyCount = 0;
}

void destroyVertexBuffer() {
if (vertexArray) {
free(vertexArray);
}
Expand All @@ -41,16 +54,3 @@ void freeVertexBuffer() {
free(polygonArray);
}
}

void allocVertexBuffer() {
ASSERT(!vertexArray);
vertexArray = calloc(1, sizeof(struct vector));
normalArray = calloc(1, sizeof(struct vector));
textureArray = calloc(1, sizeof(struct coord));
polygonArray = calloc(1, sizeof(struct poly));

vertexCount = 0;
normalCount = 0;
textureCount = 0;
polyCount = 0;
}
2 changes: 1 addition & 1 deletion src/datatypes/vertexbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ extern struct coord *textureArray;
extern int textureCount;

void allocVertexBuffer(void);
void freeVertexBuffer(void);
void destroyVertexBuffer(void);
26 changes: 8 additions & 18 deletions src/renderer/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,32 +348,22 @@ struct renderer *newRenderer() {
return r;
}

void freeRenderer(struct renderer *r) {
void destroyRenderer(struct renderer *r) {
if (r->state.timer) {
free(r->state.timer);
}
if (r->scene) {
freeScene(r->scene);
free(r->scene);
}
destroyScene(r->scene);
if (r->state.renderTiles) {
free(r->state.renderTiles);
}
if (r->state.renderBuffer) {
freeTexture(r->state.renderBuffer);
free(r->state.renderBuffer);
}
if (r->state.uiBuffer) {
freeTexture(r->state.uiBuffer);
free(r->state.uiBuffer);
}

destroyTexture(r->state.renderBuffer);
destroyTexture(r->state.uiBuffer);

if (r->state.threadStates) {
free(r->state.threadStates);
}
if (r->mainDisplay) {
freeDisplay(r->mainDisplay);
free(r->mainDisplay);
}
destroyDisplay(r->mainDisplay);
if (r->prefs.imgFileName) {
free(r->prefs.imgFileName);
}
Expand All @@ -382,7 +372,7 @@ void freeRenderer(struct renderer *r) {
}

if (vertexArray) {
freeVertexBuffer();
destroyVertexBuffer();
}

free(r);
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ struct renderer *newRenderer(void);
struct texture *renderFrame(struct renderer *r);

//Free renderer allocations
void freeRenderer(struct renderer *r);
void destroyRenderer(struct renderer *r);
6 changes: 2 additions & 4 deletions src/utils/loaders/textureloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ struct texture *loadTexture(char *filePath) {
new->float_data = stbi_loadf(filePath, (int*)&new->width, (int*)&new->height, &new->channels, 0);
new->precision = float_p;
if (!new->float_data) {
freeTexture(new);
free(new);
destroyTexture(new);
logr(warning, "Error while loading HDR from %s - Does the file exist?\n");
return NULL;
}
Expand All @@ -66,8 +65,7 @@ struct texture *loadTexture(char *filePath) {
new->byte_data = stbi_load(filePath, (int*)&new->width, (int*)&new->height, &new->channels, 3);
if (!new->byte_data) {
logr(warning, "Error while loading texture from %s - Does the file exist?\n", filePath);
freeTexture(new);
free(new);
destroyTexture(new);
return NULL;
}
new->fileType = buffer;
Expand Down
27 changes: 15 additions & 12 deletions src/utils/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,22 @@ int initSDL(struct display *d) {
return 0;
}

void freeDisplay(struct display *d) {
void destroyDisplay(struct display *d) {
#ifdef UI_ENABLED
if (d->window) {
SDL_DestroyWindow(d->window);
}
if (d->renderer) {
SDL_DestroyRenderer(d->renderer);
}
if (d->texture) {
SDL_DestroyTexture(d->texture);
}
if (d->overlayTexture) {
SDL_DestroyTexture(d->overlayTexture);
if (d) {
if (d->window) {
SDL_DestroyWindow(d->window);
}
if (d->renderer) {
SDL_DestroyRenderer(d->renderer);
}
if (d->texture) {
SDL_DestroyTexture(d->texture);
}
if (d->overlayTexture) {
SDL_DestroyTexture(d->overlayTexture);
}
free(d);
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct display {
};

int initSDL(struct display *d);
void freeDisplay(struct display *d);
void destroyDisplay(struct display *d);

void printDuration(uint64_t ms);
void getKeyboardInput(struct renderer *r);
Expand Down

0 comments on commit 5a8657b

Please sign in to comment.