diff options
author | Tavian Barnes <tavianator@gmail.com> | 2010-04-07 14:26:15 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2010-04-07 14:34:52 -0400 |
commit | 2b087cb45ae91f90492a935625570d7d42ee3ecb (patch) | |
tree | a464213b08d04c8c91c8879a84e534f895c84378 /dimension | |
parent | 7d6663eeb68bf9d0a3dff86128827c0c1d85df69 (diff) | |
download | dimension-2b087cb45ae91f90492a935625570d7d42ee3ecb.tar.xz |
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.
Diffstat (limited to 'dimension')
-rw-r--r-- | dimension/common.prologue | 6 | ||||
-rw-r--r-- | dimension/common.rules | 17 | ||||
-rw-r--r-- | dimension/lexer.l | 15 | ||||
-rw-r--r-- | dimension/main.c | 31 | ||||
-rw-r--r-- | dimension/parse.c | 50 | ||||
-rw-r--r-- | dimension/realize.c | 60 | ||||
-rw-r--r-- | dimension/tokenize.c | 18 |
7 files changed, 25 insertions, 172 deletions
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 = "<environment>", .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; } |