From 2b087cb45ae91f90492a935625570d7d42ee3ecb Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 7 Apr 2010 14:26:15 -0400 Subject: New dmnsn_malloc() function, and friends. I'm tired of checking for malloc failures everywhere, considering it never happens. So just bail out whenever it does. A lot of stuff is guaranteed to succeed if it returns now. --- bench/libdimension/bvst.c | 4 - dimension/common.prologue | 6 +- dimension/common.rules | 17 +-- dimension/lexer.l | 15 +-- dimension/main.c | 31 +---- dimension/parse.c | 50 ++------ dimension/realize.c | 60 --------- dimension/tokenize.c | 18 +-- libdimension/Makefile.am | 2 + libdimension/ambient.c | 20 ++- libdimension/bvst.c | 13 +- libdimension/camera.c | 9 +- libdimension/canvas.c | 27 ++--- libdimension/csg.c | 248 +++++++++++++------------------------- libdimension/cube.c | 13 +- libdimension/diffuse.c | 19 +-- libdimension/dimension.h | 1 + libdimension/dimension/array.h | 32 ++--- libdimension/dimension/malloc.h | 31 +++++ libdimension/dimension/raytrace.h | 2 +- libdimension/error.c | 5 +- libdimension/finish_combination.c | 65 ++++------ libdimension/gl.c | 23 +--- libdimension/interior.c | 11 +- libdimension/light.c | 13 +- libdimension/malloc.c | 50 ++++++++ libdimension/object.c | 22 ++-- libdimension/perspective.c | 21 ++-- libdimension/phong.c | 21 ++-- libdimension/png.c | 118 ++++++------------ libdimension/point_light.c | 22 ++-- libdimension/progress.c | 67 +++------- libdimension/raytrace.c | 125 ++++++------------- libdimension/reflective.c | 24 ++-- libdimension/scene.c | 37 +++--- libdimension/solid_pigment.c | 20 +-- libdimension/sphere.c | 11 +- libdimension/texture.c | 35 ++---- tests/libdimension/gl.c | 10 -- tests/libdimension/png.c | 10 +- tests/libdimension/tests.c | 69 +---------- 41 files changed, 438 insertions(+), 959 deletions(-) create mode 100644 libdimension/dimension/malloc.h create mode 100644 libdimension/malloc.c diff --git a/bench/libdimension/bvst.c b/bench/libdimension/bvst.c index fe9cf1f..de568d5 100644 --- a/bench/libdimension/bvst.c +++ b/bench/libdimension/bvst.c @@ -122,10 +122,6 @@ main() for (i = 0; i < nobjects; ++i) { objects[i] = dmnsn_new_object(); - if (!objects[i]) { - fprintf(stderr, "--- Couldn't allocate object! ---\n"); - return EXIT_FAILURE; - } /* Generate a bounding box in (-1, -1, -1), (1, 1, 1) */ dmnsn_randomize_bounding_box(objects[i]); diff --git a/dimension/common.prologue b/dimension/common.prologue index 68f4f1a..06ccdb3 100644 --- a/dimension/common.prologue +++ b/dimension/common.prologue @@ -57,17 +57,13 @@ dmnsn_new_astnode(dmnsn_astnode_type type, YYLTYPE lloc) .children = dmnsn_new_array(sizeof(dmnsn_astnode)), .ptr = NULL, .free_fn = NULL, - .refcount = malloc(sizeof(unsigned int)), + .refcount = dmnsn_malloc(sizeof(unsigned int)), .filename = lloc.first_filename, .line = lloc.first_line, .col = lloc.first_column }; - if (!astnode.refcount) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate reference count."); - } *astnode.refcount = 1; - return astnode; } diff --git a/dimension/common.rules b/dimension/common.rules index e477df5..e604b9f 100644 --- a/dimension/common.rules +++ b/dimension/common.rules @@ -29,10 +29,7 @@ IDENTIFIER: "identifier" { symbol = dmnsn_find_symbol(symtable, id); } $$ = dmnsn_new_astnode(DMNSN_AST_IDENTIFIER, @$); - $$.ptr = strdup(id); - if (!$$.ptr) - dmnsn_error(DMNSN_SEVERITY_HIGH, - "Couldn't allocate room for identifier."); + $$.ptr = dmnsn_strdup(id); free($1); } ; @@ -410,21 +407,13 @@ FLOAT: ARITH_EXPR { FLOAT_LITERAL: "integer" { $$ = dmnsn_new_astnode(DMNSN_AST_INTEGER, @$); - $$.ptr = malloc(sizeof(long)); - if (!$$.ptr) - dmnsn_error(DMNSN_SEVERITY_HIGH, - "Failed to allocate room for integer."); - + $$.ptr = dmnsn_malloc(sizeof(long)); *(long *)$$.ptr = strtol($1, NULL, 0); free($1); } | "float" { $$ = dmnsn_new_astnode(DMNSN_AST_FLOAT, @$); - $$.ptr = malloc(sizeof(double)); - if (!$$.ptr) - dmnsn_error(DMNSN_SEVERITY_HIGH, - "Failed to allocate room for float."); - + $$.ptr = dmnsn_malloc(sizeof(double)); *(double *)$$.ptr = strtod($1, NULL); free($1); } diff --git a/dimension/lexer.l b/dimension/lexer.l index 77772ae..89dd7fd 100644 --- a/dimension/lexer.l +++ b/dimension/lexer.l @@ -74,20 +74,14 @@ #define RETURN_VALUE_TOKEN(token_type) \ do { \ NEW_TOKEN(token_type); \ - lvalp->value = strdup(yytext); \ - if (!lvalp->value) \ - dmnsn_error(DMNSN_SEVERITY_HIGH, \ - "Couldn't allocate space for token value."); \ + lvalp->value = dmnsn_strdup(yytext); \ RETURN(); \ } while (0) #define STRING_TOKEN() \ do { \ NEW_TOKEN(DMNSN_T_STRING); \ - lvalp->value = malloc(string_extent); \ - if (!lvalp->value) \ - dmnsn_error(DMNSN_SEVERITY_HIGH, \ - "Couldn't allocate space for token value."); \ + lvalp->value = dmnsn_malloc(string_extent); \ lvalp->value[0] = '\0'; \ CALCULATE_COLUMN(); \ } while (0) @@ -96,10 +90,7 @@ do { \ if (string_length + len + 1 >= string_length) { \ string_extent = 2*(string_length + len + 1); \ - lvalp->value = realloc(lvalp->value, string_extent); \ - if (!lvalp->value) \ - dmnsn_error(DMNSN_SEVERITY_HIGH, \ - "Couldn't allocate space for token value."); \ + lvalp->value = dmnsn_realloc(lvalp->value, string_extent); \ } \ \ strncpy(lvalp->value + string_length, str, len + 1); \ diff --git a/dimension/main.c b/dimension/main.c index 9c7a5cf..8a268b9 100644 --- a/dimension/main.c +++ b/dimension/main.c @@ -214,11 +214,6 @@ main(int argc, char **argv) { /* Allocate a canvas */ scene->canvas = dmnsn_new_canvas(width, height); - if (!scene->canvas) { - dmnsn_delete_scene(scene); - fprintf(stderr, "Couldn't allocate canvas!\n"); - return EXIT_FAILURE; - } /* Set the new number of threads if --threads changed it */ if (nthreads) @@ -231,30 +226,16 @@ main(int argc, char **argv) { /* Generate a default output filename by replacing the extension of the basename of the input file with ".png" */ if (!output) { - char *input_copy = strdup(input); - if (!input_copy) { - fprintf(stderr, "Couldn't allocate space for output filename!\n"); - return EXIT_FAILURE; - } - + char *input_copy = dmnsn_strdup(input); char *base = basename(input_copy); char *ext = strrchr(base, '.'); if (ext) { - output = malloc(ext - base + 5); - if (!output) { - fprintf(stderr, "Couldn't allocate space for output filename!\n"); - return EXIT_FAILURE; - } - + output = dmnsn_malloc(ext - base + 5); strncpy(output, base, ext - base + 5); ext = output + (ext - base); } else { size_t len = strlen(base); - output = malloc(len + 5); - if (!output) { - fprintf(stderr, "Couldn't allocate space for output filename!\n"); - return EXIT_FAILURE; - } + output = dmnsn_malloc(len + 5); strcpy(output, base); ext = output + len; } @@ -277,12 +258,6 @@ main(int argc, char **argv) { } dmnsn_progress *render_progress = dmnsn_raytrace_scene_async(scene); - if (!render_progress) { - dmnsn_delete_scene(scene); - fprintf(stderr, "Error starting render!\n"); - return EXIT_FAILURE; - } - dmnsn_progressbar(scene->nthreads > 1 ? "Rendering scene with %u threads" : "Rendering scene with %u thread", diff --git a/dimension/parse.c b/dimension/parse.c index 4a8df8b..19ba24c 100644 --- a/dimension/parse.c +++ b/dimension/parse.c @@ -58,16 +58,8 @@ dmnsn_delete_patricia_trie(dmnsn_patricia_trie *trie) dmnsn_patricia_trie * dmnsn_new_patricia_trie() { - dmnsn_patricia_trie *trie = malloc(sizeof(dmnsn_patricia_trie)); - if (!trie) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate PATRICIA trie."); - } - - trie->prefix = strdup(""); - if (!trie->prefix) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate PATRICIA trie."); - } - + dmnsn_patricia_trie *trie = dmnsn_malloc(sizeof(dmnsn_patricia_trie)); + trie->prefix = dmnsn_strdup(""); trie->leaf = false; trie->children = dmnsn_new_array(sizeof(dmnsn_patricia_trie *)); return trie; @@ -89,9 +81,7 @@ dmnsn_patricia_insert(dmnsn_patricia_trie *trie, && dmnsn_array_size(trie->children) == 0) { /* Replace an empty tree with a single-element tree */ - trie->prefix = realloc(trie->prefix, strlen(id) + 1); - if (!trie->prefix) - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate room for prefix."); + trie->prefix = dmnsn_realloc(trie->prefix, strlen(id) + 1); strcpy(trie->prefix, id); trie->leaf = true; @@ -135,9 +125,7 @@ dmnsn_patricia_insert(dmnsn_patricia_trie *trie, } else { /* Split the tree */ dmnsn_patricia_trie *copy = dmnsn_new_patricia_trie(); - copy->prefix = realloc(copy->prefix, strlen(prefix) + 1); - if (!trie->prefix) - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate room for prefix."); + copy->prefix = dmnsn_realloc(copy->prefix, strlen(prefix) + 1); strcpy(copy->prefix, prefix); *prefix = '\0'; @@ -339,17 +327,13 @@ dmnsn_new_astnode(dmnsn_astnode_type type) .children = NULL, .ptr = NULL, .free_fn = NULL, - .refcount = malloc(sizeof(unsigned int)), + .refcount = dmnsn_malloc(sizeof(unsigned int)), .filename = "", .line = -1, .col = -1, }; - if (!astnode.refcount) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate reference count."); - } *astnode.refcount = 0; - return astnode; } @@ -365,9 +349,7 @@ static void dmnsn_make_ast_integer(dmnsn_astnode *astnode, long value) { astnode->type = DMNSN_AST_INTEGER; - astnode->ptr = malloc(sizeof(long)); - if (!astnode->ptr) - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate room for integer."); + astnode->ptr = dmnsn_malloc(sizeof(long)); *(long *)astnode->ptr = value; } @@ -383,9 +365,7 @@ static void dmnsn_make_ast_float(dmnsn_astnode *astnode, double value) { astnode->type = DMNSN_AST_FLOAT; - astnode->ptr = malloc(sizeof(double)); - if (!astnode->ptr) - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate room for integer."); + astnode->ptr = dmnsn_malloc(sizeof(double)); *(double *)astnode->ptr = value; } @@ -469,9 +449,7 @@ dmnsn_astnode dmnsn_new_ast_string(const char *value) { dmnsn_astnode astnode = dmnsn_new_astnode(DMNSN_AST_STRING); - astnode.ptr = strdup(value); - if (!astnode.ptr) - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate room for string."); + astnode.ptr = dmnsn_strdup(value); return astnode; } @@ -512,17 +490,13 @@ dmnsn_copy_astnode(dmnsn_astnode astnode) .type = astnode.type, .children = dmnsn_new_array(sizeof(dmnsn_astnode)), .ptr = NULL, - .refcount = malloc(sizeof(unsigned int)), + .refcount = dmnsn_malloc(sizeof(unsigned int)), .filename = astnode.filename, .line = astnode.line, .col = astnode.col }; - if (!copy.refcount) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate reference count."); - } *copy.refcount = 1; - return copy; } @@ -564,10 +538,8 @@ dmnsn_vector_promote(dmnsn_astnode astnode, dmnsn_symbol_table *symtable) component = dmnsn_copy_astnode(component); component.type = DMNSN_AST_INTEGER; - long *val = malloc(sizeof(long)); - if (!val) - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate room for integer."); - *val = 0; + long *val = dmnsn_malloc(sizeof(long)); + *val = 0; component.ptr = val; dmnsn_array_push(promoted.children, &component); diff --git a/dimension/realize.c b/dimension/realize.c index e70f1e3..215d891 100644 --- a/dimension/realize.c +++ b/dimension/realize.c @@ -397,9 +397,6 @@ dmnsn_realize_pigment(dmnsn_astnode astnode) case DMNSN_AST_VECTOR: color = dmnsn_realize_color(color_node); pigment = dmnsn_new_solid_pigment(color); - if (!pigment) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't create pigment."); - } break; default: @@ -443,9 +440,6 @@ dmnsn_realize_reflection(dmnsn_astnode astnode) } dmnsn_finish *reflection = dmnsn_new_reflective_finish(min, max, falloff); - if (!reflection) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate a reflection."); - } return reflection; } @@ -530,10 +524,6 @@ dmnsn_realize_finish(dmnsn_astnode astnode) finish = dmnsn_new_finish_combination(reflection, finish); } - if (!finish) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate finish."); - } - return finish; } @@ -543,9 +533,6 @@ dmnsn_realize_texture(dmnsn_astnode astnode) dmnsn_assert(astnode.type == DMNSN_AST_TEXTURE, "Expected a texture."); dmnsn_texture *texture = dmnsn_new_texture(); - if (!texture) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate texture."); - } unsigned int i; for (i = 0; i < dmnsn_array_size(astnode.children); ++i) { @@ -577,9 +564,6 @@ dmnsn_realize_interior(dmnsn_astnode astnode) dmnsn_assert(astnode.type == DMNSN_AST_INTERIOR, "Expected a texture."); dmnsn_interior *interior = dmnsn_new_interior(); - if (!interior) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate interior."); - } unsigned int i; for (i = 0; i < dmnsn_array_size(astnode.children); ++i) { @@ -674,9 +658,6 @@ dmnsn_realize_box(dmnsn_astnode astnode) x2 = dmnsn_realize_vector(corner2); dmnsn_object *box = dmnsn_new_cube(); - if (!box) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate box."); - } box->trans = dmnsn_scale_matrix( dmnsn_new_vector(fabs(x2.x - x1.x)/2.0, @@ -710,9 +691,6 @@ dmnsn_realize_sphere(dmnsn_astnode astnode) double r = dmnsn_realize_float(radius); dmnsn_object *sphere = dmnsn_new_sphere(); - if (!sphere) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate sphere."); - } sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(r, r, r)); sphere->trans = dmnsn_matrix_mul(dmnsn_translation_matrix(x0), sphere->trans); @@ -740,9 +718,6 @@ dmnsn_realize_union(dmnsn_astnode astnode) dmnsn_object *o2 = dmnsn_realize_object(o2node); dmnsn_object *csg = dmnsn_new_csg_union(o1, o2); - if (!csg) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate union."); - } unsigned int i; for (i = 2; i < dmnsn_array_size(objects.children); ++i) { @@ -751,9 +726,6 @@ dmnsn_realize_union(dmnsn_astnode astnode) dmnsn_object *object = dmnsn_realize_object(onode); csg = dmnsn_new_csg_union(csg, object); - if (!csg) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate union."); - } } dmnsn_astnode modifiers; @@ -780,9 +752,6 @@ dmnsn_realize_intersection(dmnsn_astnode astnode) dmnsn_object *o2 = dmnsn_realize_object(o2node); dmnsn_object *csg = dmnsn_new_csg_intersection(o1, o2); - if (!csg) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate intersection."); - } unsigned int i; for (i = 2; i < dmnsn_array_size(objects.children); ++i) { @@ -791,9 +760,6 @@ dmnsn_realize_intersection(dmnsn_astnode astnode) dmnsn_object *object = dmnsn_realize_object(onode); csg = dmnsn_new_csg_intersection(csg, object); - if (!csg) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate intersection."); - } } dmnsn_astnode modifiers; @@ -819,9 +785,6 @@ dmnsn_realize_difference(dmnsn_astnode astnode) dmnsn_object *o2 = dmnsn_realize_object(o2node); dmnsn_object *csg = dmnsn_new_csg_difference(o1, o2); - if (!csg) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate difference."); - } unsigned int i; for (i = 2; i < dmnsn_array_size(objects.children); ++i) { @@ -830,9 +793,6 @@ dmnsn_realize_difference(dmnsn_astnode astnode) dmnsn_object *object = dmnsn_realize_object(onode); csg = dmnsn_new_csg_difference(csg, object); - if (!csg) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate difference."); - } } dmnsn_astnode modifiers; @@ -858,9 +818,6 @@ dmnsn_realize_merge(dmnsn_astnode astnode) dmnsn_object *o2 = dmnsn_realize_object(o2node); dmnsn_object *csg = dmnsn_new_csg_merge(o1, o2); - if (!csg) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate merge."); - } unsigned int i; for (i = 2; i < dmnsn_array_size(objects.children); ++i) { @@ -869,9 +826,6 @@ dmnsn_realize_merge(dmnsn_astnode astnode) dmnsn_object *object = dmnsn_realize_object(onode); csg = dmnsn_new_csg_merge(csg, object); - if (!csg) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate merge."); - } } dmnsn_astnode modifiers; @@ -918,9 +872,6 @@ dmnsn_realize_light_source(dmnsn_astnode astnode) dmnsn_color color = dmnsn_realize_color(color_node); dmnsn_light *light = dmnsn_new_point_light(x0, color); - if (!light) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate light."); - } return light; } @@ -929,9 +880,6 @@ static dmnsn_scene * dmnsn_realize_astree(const dmnsn_astree *astree) { dmnsn_scene *scene = dmnsn_new_scene(); - if (!scene) { - return NULL; - } /* Default finish */ scene->default_texture->finish = dmnsn_new_finish_combination( @@ -940,20 +888,12 @@ dmnsn_realize_astree(const dmnsn_astree *astree) ), dmnsn_new_diffuse_finish(0.6) ); - if (!scene->default_texture->finish) { - dmnsn_delete_scene(scene); - return NULL; - } /* Background color */ scene->background = dmnsn_black; /* Create the default perspective camera */ scene->camera = dmnsn_new_perspective_camera(); - if (!scene->camera) { - dmnsn_delete_scene(scene); - return NULL; - } /* * Now parse the abstract syntax tree diff --git a/dimension/tokenize.c b/dimension/tokenize.c index f20f394..b5a72f1 100644 --- a/dimension/tokenize.c +++ b/dimension/tokenize.c @@ -47,10 +47,7 @@ typedef struct dmnsn_token_buffer { static dmnsn_token_buffer * dmnsn_new_token_buffer(int type, dmnsn_token_buffer *prev, const char *filename) { - dmnsn_token_buffer *tbuffer = malloc(sizeof(dmnsn_token_buffer)); - if (!tbuffer) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Failed to allocate token buffer."); - } + dmnsn_token_buffer *tbuffer = dmnsn_malloc(sizeof(dmnsn_token_buffer)); tbuffer->type = type; tbuffer->buffered = dmnsn_new_array(sizeof(dmnsn_buffered_token)); @@ -222,13 +219,9 @@ dmnsn_include_buffer(int token, dmnsn_token_buffer *prev, dmnsn_assert(inode->type == DMNSN_AST_STRING, "$include has wrong type."); const char *include = inode->ptr; - char *filename_copy = strdup(filename); - if (!filename_copy) - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate space for filename."); + char *filename_copy = dmnsn_strdup(filename); char *localdir = dirname(filename_copy); - char *local_include = malloc(strlen(localdir) + strlen(include) + 2); - if (!local_include) - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate space for filename."); + char *local_include = dmnsn_malloc(strlen(localdir) + strlen(include) + 2); strcpy(local_include, localdir); strcat(local_include, "/"); strcat(local_include, include); @@ -823,10 +816,7 @@ dmnsn_yylex_wrapper(dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp, token = buffered.type; if (buffered.lval.value) { - lvalp->value = strdup(buffered.lval.value); - if (!lvalp->value) - dmnsn_error(DMNSN_SEVERITY_HIGH, - "Couldn't allocate space for token value."); + lvalp->value = dmnsn_strdup(buffered.lval.value); } else { lvalp->value = NULL; } diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am index 2dda68c..9774202 100644 --- a/libdimension/Makefile.am +++ b/libdimension/Makefile.am @@ -34,6 +34,7 @@ nobase_include_HEADERS = dimension.h \ dimension/interior.h \ dimension/light.h \ dimension/lights.h \ + dimension/malloc.h \ dimension/object.h \ dimension/objects.h \ dimension/pigments.h \ @@ -62,6 +63,7 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \ inlines.c \ interior.c \ light.c \ + malloc.c \ object.c \ perspective.c \ phong.c \ diff --git a/libdimension/ambient.c b/libdimension/ambient.c index 66fabe0..aa1153a 100644 --- a/libdimension/ambient.c +++ b/libdimension/ambient.c @@ -21,7 +21,6 @@ #include "dimension.h" #include #include -#include /* For malloc */ /* * Ambient finish @@ -41,18 +40,13 @@ dmnsn_finish * dmnsn_new_ambient_finish(dmnsn_color ambient) { dmnsn_finish *finish = dmnsn_new_finish(); - if (finish) { - dmnsn_color *param = malloc(sizeof(dmnsn_color)); - if (!param) { - dmnsn_delete_finish(finish); - errno = ENOMEM; - return NULL; - } - *param = ambient; - finish->ptr = param; - finish->ambient_fn = &dmnsn_ambient_finish_fn; - finish->free_fn = &free; - } + dmnsn_color *param = dmnsn_malloc(sizeof(dmnsn_color)); + *param = ambient; + + finish->ptr = param; + finish->ambient_fn = &dmnsn_ambient_finish_fn; + finish->free_fn = &free; + return finish; } diff --git a/libdimension/bvst.c b/libdimension/bvst.c index 6e23d6d..958273d 100644 --- a/libdimension/bvst.c +++ b/libdimension/bvst.c @@ -25,22 +25,15 @@ dmnsn_bvst * dmnsn_new_bvst() { - dmnsn_bvst *tree = malloc(sizeof(dmnsn_bvst)); - if (tree) { - tree->root = NULL; - } else { - dmnsn_error(DMNSN_SEVERITY_HIGH, "BVST allocation failed."); - } + dmnsn_bvst *tree = dmnsn_malloc(sizeof(dmnsn_bvst)); + tree->root = NULL; return tree; } static dmnsn_bvst_node * dmnsn_new_bvst_node() { - dmnsn_bvst_node *node = malloc(sizeof(dmnsn_bvst_node)); - if (!node) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "BVST node allocation failed."); - } + dmnsn_bvst_node *node = dmnsn_malloc(sizeof(dmnsn_bvst_node)); return node; } diff --git a/libdimension/camera.c b/libdimension/camera.c index eeb399c..9d30731 100644 --- a/libdimension/camera.c +++ b/libdimension/camera.c @@ -20,18 +20,13 @@ #include "dimension.h" #include -#include /* For malloc */ /* Allocate a new dummy camera */ dmnsn_camera * dmnsn_new_camera() { - dmnsn_camera *camera = malloc(sizeof(dmnsn_camera)); - if (camera) { - camera->free_fn = NULL; - } else { - errno = ENOMEM; - } + dmnsn_camera *camera = dmnsn_malloc(sizeof(dmnsn_camera)); + camera->free_fn = NULL; return camera; } diff --git a/libdimension/canvas.c b/libdimension/canvas.c index d18d0b7..d6c3c23 100644 --- a/libdimension/canvas.c +++ b/libdimension/canvas.c @@ -21,33 +21,24 @@ #include "dimension.h" #include #include -#include /* For malloc(), free() */ +#include /* For free() */ /* Allocate a new canvas, of width x and height y */ dmnsn_canvas * dmnsn_new_canvas(unsigned int x, unsigned int y) { /* Allocate the dmnsn_canvas struct */ - dmnsn_canvas *canvas = malloc(sizeof(dmnsn_canvas)); + dmnsn_canvas *canvas = dmnsn_malloc(sizeof(dmnsn_canvas)); - if (canvas) { - /* Set the width and height */ - canvas->x = x; - canvas->y = y; + /* Set the width and height */ + canvas->x = x; + canvas->y = y; - /* Allocate room for the optimizers */ - canvas->optimizers = dmnsn_new_array(sizeof(dmnsn_canvas_optimizer)); + /* Allocate room for the optimizers */ + canvas->optimizers = dmnsn_new_array(sizeof(dmnsn_canvas_optimizer)); - /* Allocate the pixels */ - canvas->pixels = malloc(sizeof(dmnsn_color)*x*y); - if (!canvas->pixels) { - dmnsn_delete_canvas(canvas); - errno = ENOMEM; - return NULL; - } - } else { - errno = ENOMEM; - } + /* Allocate the pixels */ + canvas->pixels = dmnsn_malloc(sizeof(dmnsn_color)*x*y); return canvas; } diff --git a/libdimension/csg.c b/libdimension/csg.c index 78df625..6342432 100644 --- a/libdimension/csg.c +++ b/libdimension/csg.c @@ -98,48 +98,28 @@ dmnsn_csg_union_inside_fn(const dmnsn_object *csg, dmnsn_vector point) dmnsn_object * dmnsn_new_csg_union(dmnsn_object *A, dmnsn_object *B) { - if (A && B) { - A->trans_inv = dmnsn_matrix_inverse(A->trans); - B->trans_inv = dmnsn_matrix_inverse(B->trans); - - dmnsn_object *csg = dmnsn_new_object(); - if (csg) { - dmnsn_object **params = malloc(2*sizeof(dmnsn_object *)); - if (!params) { - dmnsn_delete_object(csg); - dmnsn_delete_object(B); - dmnsn_delete_object(A); - errno = ENOMEM; - return NULL; - } - - params[0] = A; - params[1] = B; - - csg->ptr = params; - csg->intersection_fn = &dmnsn_csg_union_intersection_fn; - csg->inside_fn = &dmnsn_csg_union_inside_fn; - csg->free_fn = &dmnsn_csg_free_fn; - - dmnsn_bounding_box Abox - = dmnsn_matrix_bounding_box_mul(A->trans, A->bounding_box); - dmnsn_bounding_box Bbox - = dmnsn_matrix_bounding_box_mul(B->trans, B->bounding_box); - csg->bounding_box.min = dmnsn_vector_min(Abox.min, Bbox.min); - csg->bounding_box.max = dmnsn_vector_max(Abox.max, Bbox.max); - - return csg; - } else { - dmnsn_delete_object(B); - dmnsn_delete_object(A); - } - } else if (A) { - dmnsn_delete_object(B); - } else if (B) { - dmnsn_delete_object(A); - } + A->trans_inv = dmnsn_matrix_inverse(A->trans); + B->trans_inv = dmnsn_matrix_inverse(B->trans); + + dmnsn_object *csg = dmnsn_new_object(); + + dmnsn_object **params = dmnsn_malloc(2*sizeof(dmnsn_object *)); + params[0] = A; + params[1] = B; + + csg->ptr = params; + csg->intersection_fn = &dmnsn_csg_union_intersection_fn; + csg->inside_fn = &dmnsn_csg_union_inside_fn; + csg->free_fn = &dmnsn_csg_free_fn; + + dmnsn_bounding_box Abox + = dmnsn_matrix_bounding_box_mul(A->trans, A->bounding_box); + dmnsn_bounding_box Bbox + = dmnsn_matrix_bounding_box_mul(B->trans, B->bounding_box); + csg->bounding_box.min = dmnsn_vector_min(Abox.min, Bbox.min); + csg->bounding_box.max = dmnsn_vector_max(Abox.max, Bbox.max); - return NULL; + return csg; } /* Intersections */ @@ -237,48 +217,28 @@ dmnsn_csg_intersection_inside_fn(const dmnsn_object *csg, dmnsn_vector point) dmnsn_object * dmnsn_new_csg_intersection(dmnsn_object *A, dmnsn_object *B) { - if (A && B) { - A->trans_inv = dmnsn_matrix_inverse(A->trans); - B->trans_inv = dmnsn_matrix_inverse(B->trans); - - dmnsn_object *csg = dmnsn_new_object(); - if (csg) { - dmnsn_object **params = malloc(2*sizeof(dmnsn_object *)); - if (!params) { - dmnsn_delete_object(csg); - dmnsn_delete_object(B); - dmnsn_delete_object(A); - errno = ENOMEM; - return NULL; - } - - params[0] = A; - params[1] = B; - - csg->ptr = params; - csg->intersection_fn = &dmnsn_csg_intersection_intersection_fn; - csg->inside_fn = &dmnsn_csg_intersection_inside_fn; - csg->free_fn = &dmnsn_csg_free_fn; - - dmnsn_bounding_box Abox - = dmnsn_matrix_bounding_box_mul(A->trans, A->bounding_box); - dmnsn_bounding_box Bbox - = dmnsn_matrix_bounding_box_mul(B->trans, B->bounding_box); - csg->bounding_box.min = dmnsn_vector_min(Abox.min, Bbox.min); - csg->bounding_box.max = dmnsn_vector_max(Abox.max, Bbox.max); - - return csg; - } else { - dmnsn_delete_object(B); - dmnsn_delete_object(A); - } - } else if (A) { - dmnsn_delete_object(B); - } else if (B) { - dmnsn_delete_object(A); - } + A->trans_inv = dmnsn_matrix_inverse(A->trans); + B->trans_inv = dmnsn_matrix_inverse(B->trans); + + dmnsn_object *csg = dmnsn_new_object(); + + dmnsn_object **params = dmnsn_malloc(2*sizeof(dmnsn_object *)); + params[0] = A; + params[1] = B; + + csg->ptr = params; + csg->intersection_fn = &dmnsn_csg_intersection_intersection_fn; + csg->inside_fn = &dmnsn_csg_intersection_inside_fn; + csg->free_fn = &dmnsn_csg_free_fn; + + dmnsn_bounding_box Abox + = dmnsn_matrix_bounding_box_mul(A->trans, A->bounding_box); + dmnsn_bounding_box Bbox + = dmnsn_matrix_bounding_box_mul(B->trans, B->bounding_box); + csg->bounding_box.min = dmnsn_vector_min(Abox.min, Bbox.min); + csg->bounding_box.max = dmnsn_vector_max(Abox.max, Bbox.max); - return NULL; + return csg; } /* Differences */ @@ -376,48 +336,28 @@ dmnsn_csg_difference_inside_fn(const dmnsn_object *csg, dmnsn_vector point) dmnsn_object * dmnsn_new_csg_difference(dmnsn_object *A, dmnsn_object *B) { - if (A && B) { - A->trans_inv = dmnsn_matrix_inverse(A->trans); - B->trans_inv = dmnsn_matrix_inverse(B->trans); - - dmnsn_object *csg = dmnsn_new_object(); - if (csg) { - dmnsn_object **params = malloc(2*sizeof(dmnsn_object *)); - if (!params) { - dmnsn_delete_object(csg); - dmnsn_delete_object(B); - dmnsn_delete_object(A); - errno = ENOMEM; - return NULL; - } - - params[0] = A; - params[1] = B; - - csg->ptr = params; - csg->intersection_fn = &dmnsn_csg_difference_intersection_fn; - csg->inside_fn = &dmnsn_csg_difference_inside_fn; - csg->free_fn = &dmnsn_csg_free_fn; - - dmnsn_bounding_box Abox - = dmnsn_matrix_bounding_box_mul(A->trans, A->bounding_box); - dmnsn_bounding_box Bbox - = dmnsn_matrix_bounding_box_mul(B->trans, B->bounding_box); - csg->bounding_box.min = dmnsn_vector_min(Abox.min, Bbox.min); - csg->bounding_box.max = dmnsn_vector_max(Abox.max, Bbox.max); - - return csg; - } else { - dmnsn_delete_object(B); - dmnsn_delete_object(A); - } - } else if (A) { - dmnsn_delete_object(B); - } else if (B) { - dmnsn_delete_object(A); - } + A->trans_inv = dmnsn_matrix_inverse(A->trans); + B->trans_inv = dmnsn_matrix_inverse(B->trans); + + dmnsn_object *csg = dmnsn_new_object(); + + dmnsn_object **params = dmnsn_malloc(2*sizeof(dmnsn_object *)); + params[0] = A; + params[1] = B; + + csg->ptr = params; + csg->intersection_fn = &dmnsn_csg_difference_intersection_fn; + csg->inside_fn = &dmnsn_csg_difference_inside_fn; + csg->free_fn = &dmnsn_csg_free_fn; + + dmnsn_bounding_box Abox + = dmnsn_matrix_bounding_box_mul(A->trans, A->bounding_box); + dmnsn_bounding_box Bbox + = dmnsn_matrix_bounding_box_mul(B->trans, B->bounding_box); + csg->bounding_box.min = dmnsn_vector_min(Abox.min, Bbox.min); + csg->bounding_box.max = dmnsn_vector_max(Abox.max, Bbox.max); - return NULL; + return csg; } /* Merges */ @@ -515,46 +455,26 @@ dmnsn_csg_merge_inside_fn(const dmnsn_object *csg, dmnsn_vector point) dmnsn_object * dmnsn_new_csg_merge(dmnsn_object *A, dmnsn_object *B) { - if (A && B) { - A->trans_inv = dmnsn_matrix_inverse(A->trans); - B->trans_inv = dmnsn_matrix_inverse(B->trans); - - dmnsn_object *csg = dmnsn_new_object(); - if (csg) { - dmnsn_object **params = malloc(2*sizeof(dmnsn_object *)); - if (!params) { - dmnsn_delete_object(csg); - dmnsn_delete_object(B); - dmnsn_delete_object(A); - errno = ENOMEM; - return NULL; - } - - params[0] = A; - params[1] = B; - - csg->ptr = params; - csg->intersection_fn = &dmnsn_csg_merge_intersection_fn; - csg->inside_fn = &dmnsn_csg_merge_inside_fn; - csg->free_fn = &dmnsn_csg_free_fn; - - dmnsn_bounding_box Abox - = dmnsn_matrix_bounding_box_mul(A->trans, A->bounding_box); - dmnsn_bounding_box Bbox - = dmnsn_matrix_bounding_box_mul(B->trans, B->bounding_box); - csg->bounding_box.min = dmnsn_vector_min(Abox.min, Bbox.min); - csg->bounding_box.max = dmnsn_vector_max(Abox.max, Bbox.max); - - return csg; - } else { - dmnsn_delete_object(B); - dmnsn_delete_object(A); - } - } else if (A) { - dmnsn_delete_object(B); - } else if (B) { - dmnsn_delete_object(A); - } + A->trans_inv = dmnsn_matrix_inverse(A->trans); + B->trans_inv = dmnsn_matrix_inverse(B->trans); + + dmnsn_object *csg = dmnsn_new_object(); + + dmnsn_object **params = dmnsn_malloc(2*sizeof(dmnsn_object *)); + params[0] = A; + params[1] = B; + + csg->ptr = params; + csg->intersection_fn = &dmnsn_csg_merge_intersection_fn; + csg->inside_fn = &dmnsn_csg_merge_inside_fn; + csg->free_fn = &dmnsn_csg_free_fn; + + dmnsn_bounding_box Abox + = dmnsn_matrix_bounding_box_mul(A->trans, A->bounding_box); + dmnsn_bounding_box Bbox + = dmnsn_matrix_bounding_box_mul(B->trans, B->bounding_box); + csg->bounding_box.min = dmnsn_vector_min(Abox.min, Bbox.min); + csg->bounding_box.max = dmnsn_vector_max(Abox.max, Bbox.max); - return NULL; + return csg; } diff --git a/libdimension/cube.c b/libdimension/cube.c index a6f656d..033d2fb 100644 --- a/libdimension/cube.c +++ b/libdimension/cube.c @@ -19,8 +19,7 @@ *************************************************************************/ #include "dimension.h" -#include /* For malloc */ -#include /* For sqrt */ +#include /* For sqrt */ /* * Cube @@ -37,12 +36,10 @@ dmnsn_object * dmnsn_new_cube() { dmnsn_object *cube = dmnsn_new_object(); - if (cube) { - cube->intersection_fn = &dmnsn_cube_intersection_fn; - cube->inside_fn = &dmnsn_cube_inside_fn; - cube->bounding_box.min = dmnsn_new_vector(-1.0, -1.0, -1.0); - cube->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 1.0); - } + cube->intersection_fn = &dmnsn_cube_intersection_fn; + cube->inside_fn = &dmnsn_cube_inside_fn; + cube->bounding_box.min = dmnsn_new_vector(-1.0, -1.0, -1.0); + cube->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 1.0); return cube; } diff --git a/libdimension/diffuse.c b/libdimension/diffuse.c index d6ecf37..dc41944 100644 --- a/libdimension/diffuse.c +++ b/libdimension/diffuse.c @@ -20,7 +20,6 @@ #include "dimension.h" #include -#include /* For malloc */ #include /* @@ -45,19 +44,13 @@ dmnsn_finish * dmnsn_new_diffuse_finish(double diffuse) { dmnsn_finish *finish = dmnsn_new_finish(); - if (finish) { - double *param = malloc(sizeof(double)); - if (!param) { - dmnsn_delete_finish(finish); - errno = ENOMEM; - return NULL; - } - *param = diffuse; + double *param = dmnsn_malloc(sizeof(double)); + *param = diffuse; + + finish->ptr = param; + finish->diffuse_fn = &dmnsn_diffuse_finish_fn; + finish->free_fn = &free; - finish->ptr = param; - finish->diffuse_fn = &dmnsn_diffuse_finish_fn; - finish->free_fn = &free; - } return finish; } diff --git a/libdimension/dimension.h b/libdimension/dimension.h index e693841..5aca9b3 100644 --- a/libdimension/dimension.h +++ b/libdimension/dimension.h @@ -61,6 +61,7 @@ typedef void dmnsn_free_fn(void *ptr); /* Include all the libdimension headers */ #include +#include #include #include #include diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h index c1e28cd..a301de5 100644 --- a/libdimension/dimension/array.h +++ b/libdimension/dimension/array.h @@ -28,7 +28,7 @@ #define DIMENSION_ARRAY_H #include /* For pthread_rwlock_t */ -#include /* For size_t, malloc */ +#include /* For size_t */ #include /* For memcpy */ typedef struct { @@ -46,26 +46,17 @@ dmnsn_delete_array(dmnsn_array *array) } } -/* Array allocation never returns NULL - if dmnsn_new_array returns, it - succeeded */ +/* Array allocation */ DMNSN_INLINE dmnsn_array * dmnsn_new_array(size_t obj_size) { - dmnsn_array *array = (dmnsn_array *)malloc(sizeof(dmnsn_array)); - if (array) { - array->obj_size = obj_size; - array->length = 0; - array->capacity = 4; /* Start with capacity of 4 */ - - /* Allocate the memory */ - array->ptr = malloc(array->capacity*array->obj_size); - if (!array->ptr) { - dmnsn_delete_array(array); - dmnsn_error(DMNSN_SEVERITY_HIGH, "Array allocation failed."); - } - } else { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Array allocation failed."); - } + dmnsn_array *array = (dmnsn_array *)dmnsn_malloc(sizeof(dmnsn_array)); + array->obj_size = obj_size; + array->length = 0; + array->capacity = 4; /* Start with capacity of 4 */ + + /* Allocate the memory */ + array->ptr = dmnsn_malloc(array->capacity*array->obj_size); return array; } @@ -84,10 +75,7 @@ dmnsn_array_resize(dmnsn_array *array, size_t length) if (length > array->capacity) { /* Resize if we don't have enough capacity */ array->capacity = length*2; /* We are greedy */ - array->ptr = realloc(array->ptr, array->obj_size*array->capacity); - if (!array->ptr) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Resizing array failed."); - } + array->ptr = dmnsn_realloc(array->ptr, array->obj_size*array->capacity); } array->length = length; diff --git a/libdimension/dimension/malloc.h b/libdimension/dimension/malloc.h new file mode 100644 index 0000000..8ca4f9d --- /dev/null +++ b/libdimension/dimension/malloc.h @@ -0,0 +1,31 @@ +/************************************************************************* + * Copyright (C) 2010 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +/* + * Seriously, how often does malloc fail? And how often can you do something + * better than bail out when it does? dmnsn_malloc() is like malloc in every + * way except it calls dmnsn_error() on failure. + */ + +#include /* For size_t */ + +void *dmnsn_malloc(size_t size); +void *dmnsn_realloc(void *ptr, size_t size); +char *dmnsn_strdup(const char *s); diff --git a/libdimension/dimension/raytrace.h b/libdimension/dimension/raytrace.h index 1c7d875..914f944 100644 --- a/libdimension/dimension/raytrace.h +++ b/libdimension/dimension/raytrace.h @@ -26,7 +26,7 @@ #define DIMENSION_RAYTRACE_H /* Render a scene by raytracing */ -int dmnsn_raytrace_scene(dmnsn_scene *scene); +void dmnsn_raytrace_scene(dmnsn_scene *scene); dmnsn_progress *dmnsn_raytrace_scene_async(dmnsn_scene *scene); #endif /* DIMENSION_RAYTRACE_H */ diff --git a/libdimension/error.c b/libdimension/error.c index 118dd54..222f5eb 100644 --- a/libdimension/error.c +++ b/libdimension/error.c @@ -153,8 +153,9 @@ dmnsn_default_fatal_error_fn() if (pid == tid) { exit(EXIT_FAILURE); } else { - int *ret = malloc(sizeof(int)); - *ret = 1; + int *ret = malloc(sizeof(int)); /* Don't use dmnsn_malloc */ + if (ret) + *ret = 1; pthread_exit(ret); } #else diff --git a/libdimension/finish_combination.c b/libdimension/finish_combination.c index 472dc23..c281285 100644 --- a/libdimension/finish_combination.c +++ b/libdimension/finish_combination.c @@ -20,7 +20,6 @@ #include "dimension.h" #include -#include /* For malloc */ #include /* @@ -119,47 +118,27 @@ dmnsn_finish_combination_free_fn(void *ptr) dmnsn_finish * dmnsn_new_finish_combination(dmnsn_finish *f1, dmnsn_finish *f2) { - if (f1 && f2) { - dmnsn_finish *finish = dmnsn_new_finish(); - if (finish) { - dmnsn_finish **params = malloc(2*sizeof(dmnsn_finish *)); - if (!params) { - dmnsn_delete_finish(finish); - dmnsn_delete_finish(f2); - dmnsn_delete_finish(f1); - errno = ENOMEM; - return NULL; - } - - params[0] = f1; - params[1] = f2; - - finish->ptr = params; - - if (f1->diffuse_fn || f2->diffuse_fn) - finish->diffuse_fn = &dmnsn_finish_combination_diffuse_fn; - - if (f1->specular_fn || f2->specular_fn) - finish->specular_fn = &dmnsn_finish_combination_specular_fn; - - if (f1->ambient_fn || f2->ambient_fn) - finish->ambient_fn = &dmnsn_finish_combination_ambient_fn; - - if (f1->reflection_fn || f2->reflection_fn) - finish->reflection_fn = &dmnsn_finish_combination_reflection_fn; - - finish->free_fn = &dmnsn_finish_combination_free_fn; - - return finish; - } else { - dmnsn_delete_finish(f2); - dmnsn_delete_finish(f1); - } - } else if (f1) { - dmnsn_delete_finish(f1); - } else if (f2) { - dmnsn_delete_finish(f2); - } + dmnsn_finish *finish = dmnsn_new_finish(); + + dmnsn_finish **params = dmnsn_malloc(2*sizeof(dmnsn_finish *)); + params[0] = f1; + params[1] = f2; + + finish->ptr = params; + + if (f1->diffuse_fn || f2->diffuse_fn) + finish->diffuse_fn = &dmnsn_finish_combination_diffuse_fn; + + if (f1->specular_fn || f2->specular_fn) + finish->specular_fn = &dmnsn_finish_combination_specular_fn; + + if (f1->ambient_fn || f2->ambient_fn) + finish->ambient_fn = &dmnsn_finish_combination_ambient_fn; + + if (f1->reflection_fn || f2->reflection_fn) + finish->reflection_fn = &dmnsn_finish_combination_reflection_fn; + + finish->free_fn = &dmnsn_finish_combination_free_fn; - return NULL; + return finish; } diff --git a/libdimension/gl.c b/libdimension/gl.c index 0136ac3..86f41e4 100644 --- a/libdimension/gl.c +++ b/libdimension/gl.c @@ -48,11 +48,7 @@ dmnsn_gl_optimize_canvas(dmnsn_canvas *canvas) optimizer.free_fn = &free; /* Allocate a buffer to hold RGB values */ - optimizer.ptr = malloc(4*canvas->x*canvas->y*sizeof(GLushort)); - if (!optimizer.ptr) { - errno = ENOTSUP; - return -1; - } + optimizer.ptr = dmnsn_malloc(4*canvas->x*canvas->y*sizeof(GLushort)); /* Set a new optimizer */ dmnsn_optimize_canvas(canvas, optimizer); @@ -83,11 +79,7 @@ dmnsn_gl_write_canvas(const dmnsn_canvas *canvas) } /* We couldn't, so transform the canvas to RGB now */ - pixels = malloc(4*width*height*sizeof(GLushort)); - if (!pixels) { - errno = ENOMEM; - return -1; - } + pixels = dmnsn_malloc(4*width*height*sizeof(GLushort)); for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { @@ -146,16 +138,7 @@ dmnsn_gl_read_canvas(unsigned int x0, unsigned int y0, unsigned int x, y; canvas = dmnsn_new_canvas(width, height); - if (!canvas) { - return NULL; - } - - pixels = malloc(4*width*height*sizeof(GLushort)); - if (!pixels) { - dmnsn_delete_canvas(canvas); - errno = ENOMEM; - return NULL; - } + pixels = dmnsn_malloc(4*width*height*sizeof(GLushort)); glReadPixels(x0, y0, width, height, GL_RGBA, GL_UNSIGNED_SHORT, pixels); diff --git a/libdimension/interior.c b/libdimension/interior.c index 546a420..452d879 100644 --- a/libdimension/interior.c +++ b/libdimension/interior.c @@ -20,19 +20,14 @@ #include "dimension.h" #include -#include /* For malloc */ /* Allocate an interior */ dmnsn_interior * dmnsn_new_interior() { - dmnsn_interior *interior = malloc(sizeof(dmnsn_interior)); - if (interior) { - interior->ior = 1.0; - interior->free_fn = NULL; - } else { - errno = ENOMEM; - } + dmnsn_interior *interior = dmnsn_malloc(sizeof(dmnsn_interior)); + interior->ior = 1.0; + interior->free_fn = NULL; return interior; } diff --git a/libdimension/light.c b/libdimension/light.c index cc2c961..8be90e0 100644 --- a/libdimension/light.c +++ b/libdimension/light.c @@ -20,20 +20,15 @@ #include "dimension.h" #include -#include /* For malloc */ /* Allocate a new dummy light */ dmnsn_light * dmnsn_new_light() { - dmnsn_light *light = malloc(sizeof(dmnsn_light)); - if (light) { - light->light_fn = NULL; - light->free_fn = NULL; - light->ptr = NULL; - } else { - errno = ENOMEM; - } + dmnsn_light *light = dmnsn_malloc(sizeof(dmnsn_light)); + light->light_fn = NULL; + light->free_fn = NULL; + light->ptr = NULL; return light; } diff --git a/libdimension/malloc.c b/libdimension/malloc.c new file mode 100644 index 0000000..f1f234e --- /dev/null +++ b/libdimension/malloc.c @@ -0,0 +1,50 @@ +/************************************************************************* + * Copyright (C) 2010 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +#include "dimension.h" +#include + +void * +dmnsn_malloc(size_t size) +{ + void *ptr = malloc(size); + if (!ptr) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Memory allocation failed."); + } + return ptr; +} + +void * +dmnsn_realloc(void *ptr, size_t size) +{ + ptr = realloc(ptr, size); + if (!ptr) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Memory allocation failed."); + } + return ptr; +} + +char * +dmnsn_strdup(const char *s) +{ + char *copy = dmnsn_malloc(strlen(s) + 1); + strcpy(copy, s); + return copy; +} diff --git a/libdimension/object.c b/libdimension/object.c index 9f17971..51db42f 100644 --- a/libdimension/object.c +++ b/libdimension/object.c @@ -20,16 +20,12 @@ #include "dimension.h" #include -#include /* For malloc */ -/* Allocate an intersection - cannot fail */ +/* Allocate an intersection */ dmnsn_intersection * dmnsn_new_intersection() { - dmnsn_intersection *intersection = malloc(sizeof(dmnsn_intersection)); - if (!intersection) { - dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't allocate an intersection."); - } + dmnsn_intersection *intersection = dmnsn_malloc(sizeof(dmnsn_intersection)); return intersection; } @@ -44,15 +40,11 @@ dmnsn_delete_intersection(dmnsn_intersection *intersection) dmnsn_object * dmnsn_new_object() { - dmnsn_object *object = malloc(sizeof(dmnsn_object)); - if (object) { - object->texture = NULL; - object->interior = NULL; - object->trans = dmnsn_identity_matrix(); - object->free_fn = NULL; - } else { - errno = ENOMEM; - } + dmnsn_object *object = dmnsn_malloc(sizeof(dmnsn_object)); + object->texture = NULL; + object->interior = NULL; + object->trans = dmnsn_identity_matrix(); + object->free_fn = NULL; return object; } diff --git a/libdimension/perspective.c b/libdimension/perspective.c index df679ae..678c87b 100644 --- a/libdimension/perspective.c +++ b/libdimension/perspective.c @@ -35,22 +35,15 @@ static dmnsn_line dmnsn_perspective_camera_ray_fn(const dmnsn_camera *camera, dmnsn_camera * dmnsn_new_perspective_camera() { - dmnsn_matrix *ptr; dmnsn_camera *camera = dmnsn_new_camera(); - if (camera) { - /* Allocate room for the transformation matrix */ - ptr = malloc(sizeof(dmnsn_matrix)); - if (!ptr) { - dmnsn_delete_camera(camera); - errno = ENOMEM; - return NULL; - } - *ptr = dmnsn_identity_matrix(); - camera->ray_fn = &dmnsn_perspective_camera_ray_fn; - camera->free_fn = &free; - camera->ptr = ptr; - } + dmnsn_matrix *ptr = dmnsn_malloc(sizeof(dmnsn_matrix)); + *ptr = dmnsn_identity_matrix(); + + camera->ray_fn = &dmnsn_perspective_camera_ray_fn; + camera->free_fn = &free; + camera->ptr = ptr; + return camera; } diff --git a/libdimension/phong.c b/libdimension/phong.c index 8188488..ae7b2ce 100644 --- a/libdimension/phong.c +++ b/libdimension/phong.c @@ -20,7 +20,6 @@ #include "dimension.h" #include -#include /* For malloc */ #include /* @@ -55,20 +54,14 @@ dmnsn_finish * dmnsn_new_phong_finish(double specular, double exp) { dmnsn_finish *finish = dmnsn_new_finish(); - if (finish) { - double *params = malloc(2*sizeof(double)); - if (!params) { - dmnsn_delete_finish(finish); - errno = ENOMEM; - return NULL; - } - params[0] = specular; - params[1] = exp; + double *params = dmnsn_malloc(2*sizeof(double)); + params[0] = specular; + params[1] = exp; + + finish->ptr = params; + finish->specular_fn = &dmnsn_phong_specular_fn; + finish->free_fn = &free; - finish->ptr = params; - finish->specular_fn = &dmnsn_phong_specular_fn; - finish->free_fn = &free; - } return finish; } diff --git a/libdimension/png.c b/libdimension/png.c index 4f622dc..76bace9 100644 --- a/libdimension/png.c +++ b/libdimension/png.c @@ -50,11 +50,7 @@ dmnsn_png_optimize_canvas(dmnsn_canvas *canvas) optimizer.optimizer_fn = &dmnsn_png_optimizer_fn; optimizer.free_fn = &free; - optimizer.ptr = malloc(4*canvas->x*canvas->y*sizeof(uint16_t)); - if (!optimizer.ptr) { - errno = ENOMEM; - return -1; - } + optimizer.ptr = dmnsn_malloc(4*canvas->x*canvas->y*sizeof(uint16_t)); dmnsn_optimize_canvas(canvas, optimizer); return 0; @@ -140,28 +136,20 @@ dmnsn_progress * dmnsn_png_write_canvas_async(const dmnsn_canvas *canvas, FILE *file) { dmnsn_progress *progress = dmnsn_new_progress(); - dmnsn_png_write_payload *payload; - if (progress) { - payload = malloc(sizeof(dmnsn_png_write_payload)); - if (!payload) { - dmnsn_delete_progress(progress); - errno = ENOMEM; - return NULL; - } + dmnsn_png_write_payload *payload + = dmnsn_malloc(sizeof(dmnsn_png_write_payload)); + payload->progress = progress; + payload->canvas = canvas; + payload->file = file; - payload->progress = progress; - payload->canvas = canvas; - payload->file = file; - - /* Create the worker thread */ - if (pthread_create(&progress->thread, NULL, &dmnsn_png_write_canvas_thread, - payload) != 0) - { - free(payload); - dmnsn_delete_progress(progress); - return NULL; - } + /* Create the worker thread */ + if (pthread_create(&progress->thread, NULL, &dmnsn_png_write_canvas_thread, + payload) != 0) + { + free(payload); + dmnsn_delete_progress(progress); + return NULL; } return progress; @@ -182,28 +170,18 @@ dmnsn_progress * dmnsn_png_read_canvas_async(dmnsn_canvas **canvas, FILE *file) { dmnsn_progress *progress = dmnsn_new_progress(); - dmnsn_png_read_payload *payload; + dmnsn_png_read_payload *payload + = dmnsn_malloc(sizeof(dmnsn_png_write_payload)); - if (progress) { - payload = malloc(sizeof(dmnsn_png_write_payload)); - if (!payload) { - dmnsn_delete_progress(progress); - errno = ENOMEM; - return NULL; - } + payload->progress = progress; + payload->canvas = canvas; + payload->file = file; - payload->progress = progress; - payload->canvas = canvas; - payload->file = file; - - /* Create the worker thread */ - if (pthread_create(&progress->thread, NULL, &dmnsn_png_read_canvas_thread, - payload) != 0) - { - free(payload); - dmnsn_delete_progress(progress); - return NULL; - } + /* Create the worker thread */ + if (pthread_create(&progress->thread, NULL, &dmnsn_png_read_canvas_thread, + payload) != 0) + { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't start worker thread."); } return progress; @@ -221,11 +199,9 @@ static void * dmnsn_png_write_canvas_thread(void *ptr) { dmnsn_png_write_payload *payload = ptr; - int *retval = malloc(sizeof(int)); - if (retval) { - *retval = dmnsn_png_write_canvas_impl(payload->progress, - payload->canvas, payload->file); - } + int *retval = dmnsn_malloc(sizeof(int)); + *retval = dmnsn_png_write_canvas_impl(payload->progress, + payload->canvas, payload->file); dmnsn_done_progress(payload->progress); free(payload); return retval; @@ -235,12 +211,12 @@ static void * dmnsn_png_read_canvas_thread(void *ptr) { dmnsn_png_read_payload *payload = ptr; - int *retval = malloc(sizeof(int)); - if (retval) { - *payload->canvas = dmnsn_png_read_canvas_impl(payload->progress, - payload->file); - *retval = *payload->canvas ? 0 : -1; /* Fail if it returned NULL */ - } + *payload->canvas = dmnsn_png_read_canvas_impl(payload->progress, + payload->file); + + int *retval = dmnsn_malloc(sizeof(int)); + *retval = *payload->canvas ? 0 : -1; /* Fail if it returned NULL */ + dmnsn_done_progress(payload->progress); free(payload); return retval; @@ -331,12 +307,7 @@ dmnsn_png_write_canvas_impl(dmnsn_progress *progress, } /* Allocate the temporary row of RGBA values */ - row = malloc(4*sizeof(uint16_t)*width); - if (!row) { - png_destroy_write_struct(&png_ptr, &info_ptr); - errno = ENOMEM; - return -1; - } + row = dmnsn_malloc(4*sizeof(uint16_t)*width); /* Write the pixels */ for (y = 0; y < height; ++y) { @@ -524,22 +495,10 @@ dmnsn_png_read_canvas_impl(dmnsn_progress *progress, FILE *file) rowbytes = png_get_rowbytes(png_ptr, info_ptr); /* Allocate the temporary image buffer */ - image = malloc(rowbytes*height); - if (!image) { - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - errno = ENOMEM; - return NULL; - } + image = dmnsn_malloc(rowbytes*height); /* Allocate and set an array of pointers to rows in image */ - - row_pointers = malloc(sizeof(png_bytep)*height); - if (!row_pointers) { - free(image); - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - errno = ENOMEM; - return NULL; - } + row_pointers = dmnsn_malloc(sizeof(png_bytep)*height); for (y = 0; y < height; ++y) { row_pointers[y] = image + y*rowbytes; @@ -551,13 +510,6 @@ dmnsn_png_read_canvas_impl(dmnsn_progress *progress, FILE *file) /* Allocate the canvas */ canvas = dmnsn_new_canvas(width, height); - if (!canvas) { - free(row_pointers); - free(image); - png_read_end(png_ptr, NULL); - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - return NULL; - } /* Now we convert the image to our canvas format. This depends on the image bit depth (which has been scaled up to at least 8 or 16), and the presence diff --git a/libdimension/point_light.c b/libdimension/point_light.c index 1ca3c96..02ab2ea 100644 --- a/libdimension/point_light.c +++ b/libdimension/point_light.c @@ -35,20 +35,14 @@ dmnsn_light * dmnsn_new_point_light(dmnsn_vector x0, dmnsn_color color) { dmnsn_light *light = dmnsn_new_light(); - if (light) { - /* Allocate room for the transformation matrix */ - dmnsn_color *ptr = malloc(sizeof(dmnsn_color)); - if (!ptr) { - dmnsn_delete_light(light); - errno = ENOMEM; - return NULL; - } - *ptr = color; - light->x0 = x0; - light->light_fn = &dmnsn_point_light_fn; - light->free_fn = &free; - light->ptr = ptr; - } + dmnsn_color *ptr = dmnsn_malloc(sizeof(dmnsn_color)); + *ptr = color; + + light->x0 = x0; + light->light_fn = &dmnsn_point_light_fn; + light->free_fn = &free; + light->ptr = ptr; + return light; } diff --git a/libdimension/progress.c b/libdimension/progress.c index 998af8e..bc8830b 100644 --- a/libdimension/progress.c +++ b/libdimension/progress.c @@ -21,7 +21,6 @@ #include "dimension.h" #include #include -#include /* For malloc */ /* For thread synchronization */ static void dmnsn_progress_rdlock(const dmnsn_progress *progress); @@ -32,70 +31,44 @@ static void dmnsn_progress_unlock(const dmnsn_progress *progress); dmnsn_progress * dmnsn_new_progress() { - dmnsn_progress_element element = { .progress = 0, .total = 1 }; - dmnsn_progress *progress = malloc(sizeof(dmnsn_progress)); - if (progress) { - progress->elements = dmnsn_new_array(sizeof(dmnsn_progress_element)); - dmnsn_array_push(progress->elements, &element); + dmnsn_progress *progress = dmnsn_malloc(sizeof(dmnsn_progress)); + progress->elements = dmnsn_new_array(sizeof(dmnsn_progress_element)); - /* Initialize the rwlock, condition variable, and mutex */ + dmnsn_progress_element element = { .progress = 0, .total = 1 }; + dmnsn_array_push(progress->elements, &element); - progress->rwlock = NULL; - progress->mutex = NULL; - progress->cond = NULL; + /* Initialize the rwlock, condition variable, and mutex */ - progress->rwlock = malloc(sizeof(pthread_rwlock_t)); - if (!progress->rwlock) { - dmnsn_delete_progress(progress); - errno = ENOMEM; - return NULL; - } - if (pthread_rwlock_init(progress->rwlock, NULL) != 0) { - dmnsn_delete_progress(progress); - return NULL; - } + progress->rwlock = dmnsn_malloc(sizeof(pthread_rwlock_t)); + if (pthread_rwlock_init(progress->rwlock, NULL) != 0) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't initialize read-write lock."); + } - progress->cond = malloc(sizeof(pthread_cond_t)); - if (!progress->cond) { - dmnsn_delete_progress(progress); - errno = ENOMEM; - return NULL; - } - if (pthread_cond_init(progress->cond, NULL) != 0) { - dmnsn_delete_progress(progress); - return NULL; - } + progress->cond = dmnsn_malloc(sizeof(pthread_cond_t)); + if (pthread_cond_init(progress->cond, NULL) != 0) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't initialize condition variable."); + } - progress->mutex = malloc(sizeof(pthread_mutex_t)); - if (!progress->mutex) { - dmnsn_delete_progress(progress); - errno = ENOMEM; - return NULL; - } - if (pthread_mutex_init(progress->mutex, NULL) != 0) { - dmnsn_delete_progress(progress); - return NULL; - } - } else { - errno = ENOMEM; + progress->mutex = dmnsn_malloc(sizeof(pthread_mutex_t)); + if (pthread_mutex_init(progress->mutex, NULL) != 0) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't initialize mutex."); } return progress; } -/* Delete a dmnsn_progress*, which has not yet been associated with a thread; - mostly for failed returns from *_async() functions. */ +/* Delete a dmnsn_progress*, which has not yet been associated with a thread */ void dmnsn_delete_progress(dmnsn_progress *progress) { if (progress) { - if (progress->rwlock && pthread_rwlock_destroy(progress->rwlock) != 0) { + if (pthread_rwlock_destroy(progress->rwlock) != 0) { dmnsn_error(DMNSN_SEVERITY_LOW, "Leaking rwlock."); } - if (progress->mutex && pthread_mutex_destroy(progress->mutex) != 0) { + if (pthread_mutex_destroy(progress->mutex) != 0) { dmnsn_error(DMNSN_SEVERITY_LOW, "Leaking mutex."); } - if (progress->cond && pthread_cond_destroy(progress->cond) != 0) { + if (pthread_cond_destroy(progress->cond) != 0) { dmnsn_error(DMNSN_SEVERITY_LOW, "Leaking condition variable."); } diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c index 840f6f2..812b6a6 100644 --- a/libdimension/raytrace.c +++ b/libdimension/raytrace.c @@ -40,68 +40,55 @@ typedef struct { static void *dmnsn_raytrace_scene_thread(void *ptr); /* Raytrace a scene */ -int +void dmnsn_raytrace_scene(dmnsn_scene *scene) { dmnsn_progress *progress = dmnsn_raytrace_scene_async(scene); - return dmnsn_finish_progress(progress); + dmnsn_finish_progress(progress); } /* Raytrace a scene in the background */ dmnsn_progress * dmnsn_raytrace_scene_async(dmnsn_scene *scene) { - unsigned int i; - dmnsn_object *object; - dmnsn_raytrace_payload *payload; dmnsn_progress *progress = dmnsn_new_progress(); - if (progress) { - payload = malloc(sizeof(dmnsn_raytrace_payload)); - if (!payload) { - dmnsn_delete_progress(progress); - errno = ENOMEM; - return NULL; - } - - payload->progress = progress; - payload->scene = scene; - payload->bvst = dmnsn_new_bvst(); + dmnsn_raytrace_payload *payload + = dmnsn_malloc(sizeof(dmnsn_raytrace_payload)); + payload->progress = progress; + payload->scene = scene; + payload->bvst = dmnsn_new_bvst(); - for (i = 0; i < dmnsn_array_size(payload->scene->objects); ++i) { - dmnsn_array_get(payload->scene->objects, i, &object); - dmnsn_bvst_insert(payload->bvst, object); - } + unsigned int i; + for (i = 0; i < dmnsn_array_size(payload->scene->objects); ++i) { + dmnsn_object *object; + dmnsn_array_get(payload->scene->objects, i, &object); + dmnsn_bvst_insert(payload->bvst, object); + } - if (pthread_create(&progress->thread, NULL, &dmnsn_raytrace_scene_thread, - payload) != 0) - { - dmnsn_delete_bvst(payload->bvst); - free(payload); - dmnsn_delete_progress(progress); - return NULL; - } + if (pthread_create(&progress->thread, NULL, &dmnsn_raytrace_scene_thread, + payload) != 0) + { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't start worker thread."); } return progress; } /* Start the multi-threaded implementation */ -static int dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload); +static void dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload); /* Thread callback */ static void * dmnsn_raytrace_scene_thread(void *ptr) { dmnsn_raytrace_payload *payload = ptr; - int *retval = malloc(sizeof(int)); - if (retval) { - *retval = dmnsn_raytrace_scene_multithread(payload); - } else { - errno = ENOMEM; - } + dmnsn_raytrace_scene_multithread(payload); dmnsn_done_progress(payload->progress); free(payload); + + int *retval = dmnsn_malloc(sizeof(int)); + *retval = 0; return retval; } @@ -109,12 +96,9 @@ dmnsn_raytrace_scene_thread(void *ptr) static void *dmnsn_raytrace_scene_multithread_thread(void *ptr); /* Set up the multi-threaded engine */ -static int +static void dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload) { - int i, j; - void *ptr; - int retval = 0; dmnsn_raytrace_payload *payloads; pthread_t *threads; @@ -123,24 +107,15 @@ dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload) if (nthreads < 1) nthreads = 1; - payloads = malloc(nthreads*sizeof(dmnsn_raytrace_payload)); - if (!payloads) { - errno = ENOMEM; - return -1; - } - - threads = malloc(nthreads*sizeof(pthread_t)); - if (!threads) { - free(payloads); - errno = ENOMEM; - return -1; - } + payloads = dmnsn_malloc(nthreads*sizeof(dmnsn_raytrace_payload)); + threads = dmnsn_malloc(nthreads*sizeof(pthread_t)); /* Set up the progress object */ dmnsn_new_progress_element(payload->progress, payload->scene->canvas->y); /* Create the payloads */ + unsigned int i; for (i = 0; i < nthreads; ++i) { payloads[i] = *payload; payloads[i].index = i; @@ -156,62 +131,38 @@ dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload) &dmnsn_raytrace_scene_multithread_thread, &payloads[i]) != 0) { - for (j = 0; j < i; ++j) { - if (pthread_join(threads[j], &ptr)) { - dmnsn_error(DMNSN_SEVERITY_MEDIUM, - "Couldn't join worker thread in failed raytrace engine" - " initialization."); - } else { - /* Only free on a successful join - otherwise we might free a pointer - out from under a running thread */ - dmnsn_delete_bvst(payloads[j].bvst); - free(ptr); - } - } - - free(payloads); - return -1; + dmnsn_error(DMNSN_SEVERITY_HIGH, + "Couldn't start worker thread in raytrace engine."); } } for (i = 0; i < nthreads; ++i) { - if (pthread_join(threads[i], &ptr)) { + if (pthread_join(threads[i], NULL)) { dmnsn_error(DMNSN_SEVERITY_MEDIUM, "Couldn't join worker thread in raytrace engine."); } else { - if (retval == 0) { - retval = *(int *)ptr; - } dmnsn_delete_bvst(payloads[i].bvst); - free(ptr); } } free(threads); free(payloads); - return retval; } /* Actual raytracing implementation */ -static int dmnsn_raytrace_scene_impl(dmnsn_progress *progress, - dmnsn_scene *scene, - dmnsn_bvst *bvst, - unsigned int index, unsigned int threads); +static void dmnsn_raytrace_scene_impl(dmnsn_progress *progress, + dmnsn_scene *scene, + dmnsn_bvst *bvst, + unsigned int index, unsigned int threads); /* Multi-threading thread callback */ static void * dmnsn_raytrace_scene_multithread_thread(void *ptr) { dmnsn_raytrace_payload *payload = ptr; - int *retval = malloc(sizeof(int)); - if (retval) { - *retval = dmnsn_raytrace_scene_impl(payload->progress, payload->scene, - payload->bvst, - payload->index, payload->threads); - } else { - errno = ENOMEM; - } - return retval; + dmnsn_raytrace_scene_impl(payload->progress, payload->scene, + payload->bvst, payload->index, payload->threads); + return NULL; } /* @@ -242,7 +193,7 @@ static dmnsn_color dmnsn_raytrace_shoot(dmnsn_raytrace_state *state, dmnsn_line ray); /* Actually raytrace a scene */ -static int +static void dmnsn_raytrace_scene_impl(dmnsn_progress *progress, dmnsn_scene *scene, dmnsn_bvst *bvst, unsigned int index, unsigned int threads) @@ -283,8 +234,6 @@ dmnsn_raytrace_scene_impl(dmnsn_progress *progress, dmnsn_scene *scene, dmnsn_increment_progress(progress); } - - return 0; } #define ITEXTURE(state) (state->intersection->texture) diff --git a/libdimension/reflective.c b/libdimension/reflective.c index 2e90b8f..24b6efb 100644 --- a/libdimension/reflective.c +++ b/libdimension/reflective.c @@ -20,7 +20,6 @@ #include "dimension.h" #include -#include /* For malloc */ #include /* @@ -50,21 +49,16 @@ dmnsn_finish * dmnsn_new_reflective_finish(dmnsn_color min, dmnsn_color max, double falloff) { dmnsn_finish *finish = dmnsn_new_finish(); - if (finish) { - dmnsn_reflection_params *params = malloc(sizeof(dmnsn_reflection_params)); - if (!params) { - dmnsn_delete_finish(finish); - errno = ENOMEM; - return NULL; - } - params->min = min; - params->max = max; - params->falloff = falloff; + dmnsn_reflection_params *params + = dmnsn_malloc(sizeof(dmnsn_reflection_params)); + params->min = min; + params->max = max; + params->falloff = falloff; + + finish->ptr = params; + finish->reflection_fn = &dmnsn_reflective_finish_fn; + finish->free_fn = &free; - finish->ptr = params; - finish->reflection_fn = &dmnsn_reflective_finish_fn; - finish->free_fn = &free; - } return finish; } diff --git a/libdimension/scene.c b/libdimension/scene.c index 4595afd..dbda90b 100644 --- a/libdimension/scene.c +++ b/libdimension/scene.c @@ -20,37 +20,28 @@ #include "dimension.h" #include -#include /* For malloc */ #include /* For sysconf */ /* Allocate an empty scene */ dmnsn_scene * dmnsn_new_scene() { - dmnsn_scene *scene = malloc(sizeof(dmnsn_scene)); - if (scene) { - scene->default_texture = dmnsn_new_texture(); - if (!scene->default_texture) { - dmnsn_delete_scene(scene); - errno = ENOMEM; - return NULL; - } + dmnsn_scene *scene = dmnsn_malloc(sizeof(dmnsn_scene)); - scene->camera = NULL; - scene->canvas = NULL; - scene->objects = dmnsn_new_array(sizeof(dmnsn_object *)); - scene->lights = dmnsn_new_array(sizeof(dmnsn_light *)); - scene->quality = DMNSN_RENDER_FULL; - scene->reclimit = 5; + scene->default_texture = dmnsn_new_texture(); + scene->camera = NULL; + scene->canvas = NULL; + scene->objects = dmnsn_new_array(sizeof(dmnsn_object *)); + scene->lights = dmnsn_new_array(sizeof(dmnsn_light *)); + scene->quality = DMNSN_RENDER_FULL; + scene->reclimit = 5; + + /* Find the number of processors/cores running (TODO: do this portably) */ + int nprocs = sysconf(_SC_NPROCESSORS_ONLN); + if (nprocs < 1) + nprocs = 1; + scene->nthreads = nprocs; - /* Find the number of processors/cores running (TODO: do this portably) */ - int nprocs = sysconf(_SC_NPROCESSORS_ONLN); - if (nprocs < 1) - nprocs = 1; - scene->nthreads = nprocs; - } else { - errno = ENOMEM; - } return scene; } diff --git a/libdimension/solid_pigment.c b/libdimension/solid_pigment.c index a69e25a..e414fc2 100644 --- a/libdimension/solid_pigment.c +++ b/libdimension/solid_pigment.c @@ -20,7 +20,6 @@ #include "dimension.h" #include -#include /* For malloc */ /* Solid color pigment callback */ static dmnsn_color dmnsn_solid_pigment_fn(const dmnsn_pigment *pigment, @@ -30,21 +29,14 @@ static dmnsn_color dmnsn_solid_pigment_fn(const dmnsn_pigment *pigment, dmnsn_pigment * dmnsn_new_solid_pigment(dmnsn_color color) { - dmnsn_color *solid; dmnsn_pigment *pigment = dmnsn_new_pigment(); - if (pigment) { - solid = malloc(sizeof(dmnsn_color)); - if (!solid) { - dmnsn_delete_pigment(pigment); - errno = ENOMEM; - return NULL; - } - *solid = color; - pigment->pigment_fn = &dmnsn_solid_pigment_fn; - pigment->free_fn = &free; - pigment->ptr = solid; - } + dmnsn_color *solid = dmnsn_malloc(sizeof(dmnsn_color)); + *solid = color; + + pigment->pigment_fn = &dmnsn_solid_pigment_fn; + pigment->free_fn = &free; + pigment->ptr = solid; return pigment; } diff --git a/libdimension/sphere.c b/libdimension/sphere.c index 7c2894b..77fd1d4 100644 --- a/libdimension/sphere.c +++ b/libdimension/sphere.c @@ -19,7 +19,6 @@ *************************************************************************/ #include "dimension.h" -#include /* For malloc */ #include /* For sqrt */ /* @@ -39,12 +38,10 @@ dmnsn_object * dmnsn_new_sphere() { dmnsn_object *sphere = dmnsn_new_object(); - if (sphere) { - sphere->intersection_fn = &dmnsn_sphere_intersection_fn; - sphere->inside_fn = &dmnsn_sphere_inside_fn; - sphere->bounding_box.min = dmnsn_new_vector(-1.0, -1.0, -1.0); - sphere->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 1.0); - } + sphere->intersection_fn = &dmnsn_sphere_intersection_fn; + sphere->inside_fn = &dmnsn_sphere_inside_fn; + sphere->bounding_box.min = dmnsn_new_vector(-1.0, -1.0, -1.0); + sphere->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 1.0); return sphere; } diff --git a/libdimension/texture.c b/libdimension/texture.c index 28f5033..7feb0e0 100644 --- a/libdimension/texture.c +++ b/libdimension/texture.c @@ -20,18 +20,13 @@ #include "dimension.h" #include -#include /* For malloc */ /* Allocate a dummy pigment */ dmnsn_pigment * dmnsn_new_pigment() { - dmnsn_pigment *pigment = malloc(sizeof(dmnsn_pigment)); - if (pigment) { - pigment->free_fn = NULL; - } else { - errno = ENOMEM; - } + dmnsn_pigment *pigment = dmnsn_malloc(sizeof(dmnsn_pigment)); + pigment->free_fn = NULL; return pigment; } @@ -51,16 +46,12 @@ dmnsn_delete_pigment(dmnsn_pigment *pigment) dmnsn_finish * dmnsn_new_finish() { - dmnsn_finish *finish = malloc(sizeof(dmnsn_finish)); - if (finish) { - finish->diffuse_fn = NULL; - finish->specular_fn = NULL; - finish->ambient_fn = NULL; - finish->reflection_fn = NULL; - finish->free_fn = NULL; - } else { - errno = ENOMEM; - } + dmnsn_finish *finish = dmnsn_malloc(sizeof(dmnsn_finish)); + finish->diffuse_fn = NULL; + finish->specular_fn = NULL; + finish->ambient_fn = NULL; + finish->reflection_fn = NULL; + finish->free_fn = NULL; return finish; } @@ -80,13 +71,9 @@ dmnsn_delete_finish(dmnsn_finish *finish) dmnsn_texture * dmnsn_new_texture() { - dmnsn_texture *texture = malloc(sizeof(dmnsn_texture)); - if (texture) { - texture->pigment = NULL; - texture->finish = NULL; - } else { - errno = ENOMEM; - } + dmnsn_texture *texture = dmnsn_malloc(sizeof(dmnsn_texture)); + texture->pigment = NULL; + texture->finish = NULL; return texture; } diff --git a/tests/libdimension/gl.c b/tests/libdimension/gl.c index 00f9819..a2538e3 100644 --- a/tests/libdimension/gl.c +++ b/tests/libdimension/gl.c @@ -33,10 +33,6 @@ main() { /* Create the default test scene */ scene = dmnsn_new_default_scene(); - if (!scene) { - fprintf(stderr, "--- Couldn't create default scene! ---\n"); - return EXIT_FAILURE; - } /* Optimize the canvas for GL drawing */ if (dmnsn_gl_optimize_canvas(scene->canvas) != 0) { @@ -59,12 +55,6 @@ main() { printf("Rendering scene\n"); progress = dmnsn_raytrace_scene_async(scene); - if (!progress) { - dmnsn_delete_display(display); - dmnsn_delete_scene(scene); - fprintf(stderr, "--- Couldn't start raytracing worker thread! ---\n"); - return EXIT_FAILURE; - } /* Display the scene as it's rendered */ while (dmnsn_get_progress(progress) < 1.0) { diff --git a/tests/libdimension/png.c b/tests/libdimension/png.c index f0878dc..2073310 100644 --- a/tests/libdimension/png.c +++ b/tests/libdimension/png.c @@ -36,10 +36,6 @@ main() { /* Allocate our default scene */ scene = dmnsn_new_default_scene(); - if (!scene) { - fprintf(stderr, "--- Allocation of default scene failed! ---\n"); - return EXIT_FAILURE; - } /* Optimize the canvas for PNG export */ if (dmnsn_png_optimize_canvas(scene->canvas) != 0) { @@ -51,11 +47,7 @@ main() { /* Render scene */ printf("Rendering scene\n"); - if (dmnsn_raytrace_scene(scene) != 0) { - dmnsn_delete_scene(scene); - fprintf(stderr, "--- Raytracing failed! ---\n"); - return EXIT_FAILURE; - } + dmnsn_raytrace_scene(scene); /* Write the image to PNG */ diff --git a/tests/libdimension/tests.c b/tests/libdimension/tests.c index b23c0df..cf8e752 100644 --- a/tests/libdimension/tests.c +++ b/tests/libdimension/tests.c @@ -24,9 +24,6 @@ dmnsn_new_default_scene() { /* Allocate a new scene */ dmnsn_scene *scene = dmnsn_new_scene(); - if (!scene) { - return NULL; - } /* Default finish */ scene->default_texture->finish = dmnsn_new_ambient_finish( @@ -40,10 +37,6 @@ dmnsn_new_default_scene() dmnsn_new_phong_finish(0.2, 40.0), scene->default_texture->finish ); - if (!scene->default_texture->finish) { - dmnsn_delete_scene(scene); - return NULL; - } /* Background color */ scene->background = dmnsn_color_mul(0.1, dmnsn_blue); @@ -51,10 +44,6 @@ dmnsn_new_default_scene() /* Allocate a canvas */ scene->canvas = dmnsn_new_canvas(768, 480); - if (!scene->canvas) { - dmnsn_delete_scene(scene); - return NULL; - } /* Set up the transformation matrix for the perspective camera */ dmnsn_matrix trans = dmnsn_scale_matrix( @@ -73,81 +62,32 @@ dmnsn_new_default_scene() /* Create a perspective camera */ scene->camera = dmnsn_new_perspective_camera(); - if (!scene->camera) { - dmnsn_delete_scene(scene); - return NULL; - } dmnsn_set_perspective_camera_trans(scene->camera, trans); /* Now make our objects */ dmnsn_object *sphere = dmnsn_new_sphere(); - if (!sphere) { - dmnsn_delete_scene(scene); - return NULL; - } - sphere->texture = dmnsn_new_texture(); - if (!sphere->texture) { - dmnsn_delete_scene(scene); - return NULL; - } - sphere->texture->pigment = dmnsn_new_solid_pigment(dmnsn_yellow); - if (!sphere->texture->pigment) { - dmnsn_delete_scene(scene); - return NULL; - } - sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.25, 1.25, 1.25)); dmnsn_object *cube = dmnsn_new_cube(); - if (!cube) { - dmnsn_delete_object(sphere); - dmnsn_delete_scene(scene); - return NULL; - } - cube->texture = dmnsn_new_texture(); - if (!cube->texture) { - dmnsn_delete_object(sphere); - dmnsn_delete_scene(scene); - return NULL; - } dmnsn_color cube_color = dmnsn_magenta; cube_color.filter = 0.25; cube_color.trans = 0.5; cube->texture->pigment = dmnsn_new_solid_pigment(cube_color); - if (!cube->texture->pigment) { - dmnsn_delete_object(sphere); - dmnsn_delete_scene(scene); - return NULL; - } dmnsn_color reflect = dmnsn_color_mul(0.5, dmnsn_white); cube->texture->finish = dmnsn_new_reflective_finish(reflect, reflect, 1.0); - if (!cube->texture->finish) { - dmnsn_delete_object(sphere); - dmnsn_delete_scene(scene); - return NULL; - } cube->interior = dmnsn_new_interior(); - if (!cube->interior) { - dmnsn_delete_object(sphere); - dmnsn_delete_scene(scene); - return NULL; - } cube->interior->ior = 1.1; cube->trans = dmnsn_rotation_matrix(dmnsn_new_vector(0.75, 0.0, 0.0)); dmnsn_object *csg = dmnsn_new_csg_difference(cube, sphere); - if (!csg) { - dmnsn_delete_scene(scene); - return NULL; - } dmnsn_array_push(scene->objects, &csg); /* Now make a light */ @@ -156,10 +96,6 @@ dmnsn_new_default_scene() dmnsn_new_vector(-15.0, 20.0, 10.0), dmnsn_cyan ); - if (!light) { - dmnsn_delete_scene(scene); - return NULL; - } dmnsn_array_push(scene->lights, &light); return scene; @@ -186,10 +122,7 @@ dmnsn_new_display(const dmnsn_canvas *canvas) XSetWindowAttributes swa; dmnsn_display *display; - display = malloc(sizeof(dmnsn_display)); - if (!display) { - return NULL; - } + display = dmnsn_malloc(sizeof(dmnsn_display)); display->dpy = NULL; display->win = 0; -- cgit v1.2.3