diff options
-rw-r--r-- | libdimension/ambient.c | 4 | ||||
-rw-r--r-- | libdimension/bvst.c | 37 | ||||
-rw-r--r-- | libdimension/camera.c | 3 | ||||
-rw-r--r-- | libdimension/canvas.c | 4 | ||||
-rw-r--r-- | libdimension/diffuse.c | 2 | ||||
-rw-r--r-- | libdimension/finish_combination.c | 2 | ||||
-rw-r--r-- | libdimension/gl-stubs.c | 8 | ||||
-rw-r--r-- | libdimension/gl.c | 8 | ||||
-rw-r--r-- | libdimension/interior.c | 3 | ||||
-rw-r--r-- | libdimension/light.c | 3 | ||||
-rw-r--r-- | libdimension/object.c | 3 | ||||
-rw-r--r-- | libdimension/perspective.c | 2 | ||||
-rw-r--r-- | libdimension/phong.c | 2 | ||||
-rw-r--r-- | libdimension/png-stubs.c | 10 | ||||
-rw-r--r-- | libdimension/png.c | 26 | ||||
-rw-r--r-- | libdimension/point_light.c | 2 | ||||
-rw-r--r-- | libdimension/progress.c | 8 | ||||
-rw-r--r-- | libdimension/raytrace.c | 14 | ||||
-rw-r--r-- | libdimension/reflective.c | 2 | ||||
-rw-r--r-- | libdimension/scene.c | 4 | ||||
-rw-r--r-- | libdimension/solid_pigment.c | 2 | ||||
-rw-r--r-- | libdimension/texture.c | 7 |
22 files changed, 117 insertions, 39 deletions
diff --git a/libdimension/ambient.c b/libdimension/ambient.c index 309a9db..66fabe0 100644 --- a/libdimension/ambient.c +++ b/libdimension/ambient.c @@ -19,8 +19,9 @@ *************************************************************************/ #include "dimension.h" -#include <stdlib.h> /* For malloc */ +#include <errno.h> #include <math.h> +#include <stdlib.h> /* For malloc */ /* * Ambient finish @@ -44,6 +45,7 @@ dmnsn_new_ambient_finish(dmnsn_color ambient) dmnsn_color *param = malloc(sizeof(dmnsn_color)); if (!param) { dmnsn_delete_finish(finish); + errno = ENOMEM; return NULL; } diff --git a/libdimension/bvst.c b/libdimension/bvst.c index 462d1f0..c3c8d9d 100644 --- a/libdimension/bvst.c +++ b/libdimension/bvst.c @@ -21,9 +21,6 @@ #include "dimension_impl.h" #include <stdlib.h> -static dmnsn_bvst_node *dmnsn_new_bvst_node(); -static void dmnsn_delete_bvst_node(dmnsn_bvst_node *node); - /* Return an empty tree */ dmnsn_bvst * dmnsn_new_bvst() @@ -37,6 +34,16 @@ dmnsn_new_bvst() 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."); + } + return node; +} + /* Recursively copy the nodes of a BVST */ static dmnsn_bvst_node * dmnsn_bvst_copy_recursive(dmnsn_bvst_node *root) @@ -66,8 +73,14 @@ dmnsn_copy_bvst(dmnsn_bvst *tree) return copy; } +static void +dmnsn_delete_bvst_node(dmnsn_bvst_node *node) +{ + free(node); +} + /* Recursively free a BVST */ -void +static void dmnsn_delete_bvst_recursive(dmnsn_bvst_node *node) { if (node) { @@ -443,19 +456,3 @@ dmnsn_ray_box_intersection(dmnsn_line line, dmnsn_vector min, dmnsn_vector max, return 0; } - -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."); - } - return node; -} - -static void -dmnsn_delete_bvst_node(dmnsn_bvst_node *node) -{ - free(node); -} diff --git a/libdimension/camera.c b/libdimension/camera.c index 6a0ee80..eeb399c 100644 --- a/libdimension/camera.c +++ b/libdimension/camera.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ /* Allocate a new dummy camera */ @@ -28,6 +29,8 @@ dmnsn_new_camera() dmnsn_camera *camera = malloc(sizeof(dmnsn_camera)); if (camera) { camera->free_fn = NULL; + } else { + errno = ENOMEM; } return camera; } diff --git a/libdimension/canvas.c b/libdimension/canvas.c index 14bc156..d18d0b7 100644 --- a/libdimension/canvas.c +++ b/libdimension/canvas.c @@ -20,6 +20,7 @@ #include "dimension.h" #include <pthread.h> +#include <errno.h> #include <stdlib.h> /* For malloc(), free() */ /* Allocate a new canvas, of width x and height y */ @@ -41,8 +42,11 @@ dmnsn_new_canvas(unsigned int x, unsigned int y) canvas->pixels = malloc(sizeof(dmnsn_color)*x*y); if (!canvas->pixels) { dmnsn_delete_canvas(canvas); + errno = ENOMEM; return NULL; } + } else { + errno = ENOMEM; } return canvas; diff --git a/libdimension/diffuse.c b/libdimension/diffuse.c index 910e23f..d6ecf37 100644 --- a/libdimension/diffuse.c +++ b/libdimension/diffuse.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ #include <math.h> @@ -48,6 +49,7 @@ dmnsn_new_diffuse_finish(double diffuse) double *param = malloc(sizeof(double)); if (!param) { dmnsn_delete_finish(finish); + errno = ENOMEM; return NULL; } diff --git a/libdimension/finish_combination.c b/libdimension/finish_combination.c index 4cc0c9e..472dc23 100644 --- a/libdimension/finish_combination.c +++ b/libdimension/finish_combination.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ #include <math.h> @@ -126,6 +127,7 @@ dmnsn_new_finish_combination(dmnsn_finish *f1, dmnsn_finish *f2) dmnsn_delete_finish(finish); dmnsn_delete_finish(f2); dmnsn_delete_finish(f1); + errno = ENOMEM; return NULL; } diff --git a/libdimension/gl-stubs.c b/libdimension/gl-stubs.c index adcde7c..4c0d8b4 100644 --- a/libdimension/gl-stubs.c +++ b/libdimension/gl-stubs.c @@ -19,24 +19,28 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> /* Stubs for GL functions when compiled with --disable-gl */ int dmnsn_gl_optimize_canvas(dmnsn_canvas *canvas) { - return 1; + errno = ENOTSUP; + return -1; } int dmnsn_gl_write_canvas(const dmnsn_canvas *canvas) { - return 1; + errno = ENOTSUP; + return -1; } dmnsn_canvas * dmnsn_gl_read_canvas(unsigned int x0, unsigned int y0, unsigned int width, unsigned int height) { + errno = ENOTSUP; return NULL; } diff --git a/libdimension/gl.c b/libdimension/gl.c index 8b9fecc..0136ac3 100644 --- a/libdimension/gl.c +++ b/libdimension/gl.c @@ -20,6 +20,7 @@ #include "dimension.h" #include <GL/gl.h> +#include <errno.h> #include <stdlib.h> #include <stdint.h> @@ -49,7 +50,8 @@ dmnsn_gl_optimize_canvas(dmnsn_canvas *canvas) /* Allocate a buffer to hold RGB values */ optimizer.ptr = malloc(4*canvas->x*canvas->y*sizeof(GLushort)); if (!optimizer.ptr) { - return 1; + errno = ENOTSUP; + return -1; } /* Set a new optimizer */ @@ -83,7 +85,8 @@ 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) { - return 1; + errno = ENOMEM; + return -1; } for (y = 0; y < height; ++y) { @@ -150,6 +153,7 @@ dmnsn_gl_read_canvas(unsigned int x0, unsigned int y0, pixels = malloc(4*width*height*sizeof(GLushort)); if (!pixels) { dmnsn_delete_canvas(canvas); + errno = ENOMEM; return NULL; } diff --git a/libdimension/interior.c b/libdimension/interior.c index a643f70..546a420 100644 --- a/libdimension/interior.c +++ b/libdimension/interior.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ /* Allocate an interior */ @@ -29,6 +30,8 @@ dmnsn_new_interior() if (interior) { interior->ior = 1.0; interior->free_fn = NULL; + } else { + errno = ENOMEM; } return interior; } diff --git a/libdimension/light.c b/libdimension/light.c index d561526..cc2c961 100644 --- a/libdimension/light.c +++ b/libdimension/light.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ /* Allocate a new dummy light */ @@ -30,6 +31,8 @@ dmnsn_new_light() light->light_fn = NULL; light->free_fn = NULL; light->ptr = NULL; + } else { + errno = ENOMEM; } return light; } diff --git a/libdimension/object.c b/libdimension/object.c index 7cad7f7..9f17971 100644 --- a/libdimension/object.c +++ b/libdimension/object.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ /* Allocate an intersection - cannot fail */ @@ -49,6 +50,8 @@ dmnsn_new_object() object->interior = NULL; object->trans = dmnsn_identity_matrix(); object->free_fn = NULL; + } else { + errno = ENOMEM; } return object; } diff --git a/libdimension/perspective.c b/libdimension/perspective.c index 024934a..df679ae 100644 --- a/libdimension/perspective.c +++ b/libdimension/perspective.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> /* * Perspective camera @@ -41,6 +42,7 @@ dmnsn_new_perspective_camera() ptr = malloc(sizeof(dmnsn_matrix)); if (!ptr) { dmnsn_delete_camera(camera); + errno = ENOMEM; return NULL; } *ptr = dmnsn_identity_matrix(); diff --git a/libdimension/phong.c b/libdimension/phong.c index 2bad239..8188488 100644 --- a/libdimension/phong.c +++ b/libdimension/phong.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ #include <math.h> @@ -58,6 +59,7 @@ dmnsn_new_phong_finish(double specular, double exp) double *params = malloc(2*sizeof(double)); if (!params) { dmnsn_delete_finish(finish); + errno = ENOMEM; return NULL; } diff --git a/libdimension/png-stubs.c b/libdimension/png-stubs.c index eca7697..db4a19d 100644 --- a/libdimension/png-stubs.c +++ b/libdimension/png-stubs.c @@ -19,35 +19,41 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> /* Stubs for PNG functions when compiled with --disable-png */ int dmnsn_png_optimize_canvas(dmnsn_canvas *canvas) { - return 1; + errno = ENOTSUP; + return -1; } int dmnsn_png_write_canvas(const dmnsn_canvas *canvas, FILE *file) { - return 1; + errno = ENOTSUP; + return -1; } dmnsn_progress * dmnsn_png_write_canvas_async(const dmnsn_canvas *canvas, FILE *file) { + errno = ENOTSUP; return NULL; } dmnsn_canvas * dmnsn_png_read_canvas(FILE *file) { + errno = ENOTSUP; return NULL; } dmnsn_progress * dmnsn_png_read_canvas_async(dmnsn_canvas **canvas, FILE *file) { + errno = ENOTSUP; return NULL; } diff --git a/libdimension/png.c b/libdimension/png.c index 9609e43..4f622dc 100644 --- a/libdimension/png.c +++ b/libdimension/png.c @@ -21,8 +21,9 @@ #include "dimension.h" #include <pthread.h> #include <png.h> -#include <setjmp.h> #include <arpa/inet.h> +#include <errno.h> +#include <setjmp.h> #include <stdlib.h> #include <stdint.h> @@ -51,7 +52,8 @@ dmnsn_png_optimize_canvas(dmnsn_canvas *canvas) optimizer.ptr = malloc(4*canvas->x*canvas->y*sizeof(uint16_t)); if (!optimizer.ptr) { - return 1; + errno = ENOMEM; + return -1; } dmnsn_optimize_canvas(canvas, optimizer); @@ -144,6 +146,7 @@ dmnsn_png_write_canvas_async(const dmnsn_canvas *canvas, FILE *file) payload = malloc(sizeof(dmnsn_png_write_payload)); if (!payload) { dmnsn_delete_progress(progress); + errno = ENOMEM; return NULL; } @@ -185,6 +188,7 @@ dmnsn_png_read_canvas_async(dmnsn_canvas **canvas, FILE *file) payload = malloc(sizeof(dmnsn_png_write_payload)); if (!payload) { dmnsn_delete_progress(progress); + errno = ENOMEM; return NULL; } @@ -235,7 +239,7 @@ dmnsn_png_read_canvas_thread(void *ptr) if (retval) { *payload->canvas = dmnsn_png_read_canvas_impl(payload->progress, payload->file); - *retval = *payload->canvas ? 0 : 1; /* Fail if it returned NULL */ + *retval = *payload->canvas ? 0 : -1; /* Fail if it returned NULL */ } dmnsn_done_progress(payload->progress); free(payload); @@ -258,7 +262,8 @@ dmnsn_png_write_canvas_impl(dmnsn_progress *progress, if (!file) { /* file was NULL */ - return 1; + errno = EINVAL; + return -1; } width = canvas->x; @@ -269,14 +274,14 @@ dmnsn_png_write_canvas_impl(dmnsn_progress *progress, png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { /* Couldn't create libpng write struct */ - return 1; + return -1; } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { /* Couldn't create libpng info struct */ png_destroy_write_struct(&png_ptr, NULL); - return 1; + return -1; } /* libpng will longjmp here if it encounters an error from here on */ @@ -284,7 +289,7 @@ dmnsn_png_write_canvas_impl(dmnsn_progress *progress, /* libpng error */ free(row); png_destroy_write_struct(&png_ptr, &info_ptr); - return 1; + return -1; } /* Associate file with the libpng write struct */ @@ -329,7 +334,8 @@ dmnsn_png_write_canvas_impl(dmnsn_progress *progress, row = malloc(4*sizeof(uint16_t)*width); if (!row) { png_destroy_write_struct(&png_ptr, &info_ptr); - return 1; + errno = ENOMEM; + return -1; } /* Write the pixels */ @@ -443,12 +449,14 @@ dmnsn_png_read_canvas_impl(dmnsn_progress *progress, FILE *file) if (!file) { /* file was NULL */ + errno = EINVAL; return NULL; } fread(header, 1, 8, file); if (png_sig_cmp(header, 0, 8)) { /* file is not a PNG file, or the read failed */ + errno = EINVAL; return NULL; } @@ -519,6 +527,7 @@ dmnsn_png_read_canvas_impl(dmnsn_progress *progress, FILE *file) image = malloc(rowbytes*height); if (!image) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); + errno = ENOMEM; return NULL; } @@ -528,6 +537,7 @@ dmnsn_png_read_canvas_impl(dmnsn_progress *progress, FILE *file) if (!row_pointers) { free(image); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); + errno = ENOMEM; return NULL; } diff --git a/libdimension/point_light.c b/libdimension/point_light.c index cc0e47a..1ca3c96 100644 --- a/libdimension/point_light.c +++ b/libdimension/point_light.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> /* * Point light source @@ -39,6 +40,7 @@ dmnsn_new_point_light(dmnsn_vector x0, dmnsn_color color) dmnsn_color *ptr = malloc(sizeof(dmnsn_color)); if (!ptr) { dmnsn_delete_light(light); + errno = ENOMEM; return NULL; } *ptr = color; diff --git a/libdimension/progress.c b/libdimension/progress.c index 8608522..998af8e 100644 --- a/libdimension/progress.c +++ b/libdimension/progress.c @@ -20,6 +20,7 @@ #include "dimension.h" #include <pthread.h> +#include <errno.h> #include <stdlib.h> /* For malloc */ /* For thread synchronization */ @@ -46,6 +47,7 @@ dmnsn_new_progress() 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) { @@ -56,6 +58,7 @@ dmnsn_new_progress() 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) { @@ -66,12 +69,15 @@ dmnsn_new_progress() 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; } return progress; @@ -105,7 +111,7 @@ dmnsn_delete_progress(dmnsn_progress *progress) int dmnsn_finish_progress(dmnsn_progress *progress) { void *ptr; - int retval = 1; + int retval = -1; if (progress) { if (pthread_join(progress->thread, &ptr) != 0) { diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c index 61ebd08..840f6f2 100644 --- a/libdimension/raytrace.c +++ b/libdimension/raytrace.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension_impl.h" +#include <errno.h> /* * Boilerplate for multithreading @@ -59,6 +60,7 @@ dmnsn_raytrace_scene_async(dmnsn_scene *scene) payload = malloc(sizeof(dmnsn_raytrace_payload)); if (!payload) { dmnsn_delete_progress(progress); + errno = ENOMEM; return NULL; } @@ -95,6 +97,8 @@ dmnsn_raytrace_scene_thread(void *ptr) int *retval = malloc(sizeof(int)); if (retval) { *retval = dmnsn_raytrace_scene_multithread(payload); + } else { + errno = ENOMEM; } dmnsn_done_progress(payload->progress); free(payload); @@ -121,13 +125,15 @@ dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload) payloads = malloc(nthreads*sizeof(dmnsn_raytrace_payload)); if (!payloads) { - return 1; + errno = ENOMEM; + return -1; } threads = malloc(nthreads*sizeof(pthread_t)); if (!threads) { free(payloads); - return 1; + errno = ENOMEM; + return -1; } /* Set up the progress object */ @@ -164,7 +170,7 @@ dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload) } free(payloads); - return 1; + return -1; } } @@ -202,6 +208,8 @@ dmnsn_raytrace_scene_multithread_thread(void *ptr) *retval = dmnsn_raytrace_scene_impl(payload->progress, payload->scene, payload->bvst, payload->index, payload->threads); + } else { + errno = ENOMEM; } return retval; } diff --git a/libdimension/reflective.c b/libdimension/reflective.c index 726bf8d..2e90b8f 100644 --- a/libdimension/reflective.c +++ b/libdimension/reflective.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ #include <math.h> @@ -53,6 +54,7 @@ dmnsn_new_reflective_finish(dmnsn_color min, dmnsn_color max, double falloff) dmnsn_reflection_params *params = malloc(sizeof(dmnsn_reflection_params)); if (!params) { dmnsn_delete_finish(finish); + errno = ENOMEM; return NULL; } diff --git a/libdimension/scene.c b/libdimension/scene.c index 8ebf706..4595afd 100644 --- a/libdimension/scene.c +++ b/libdimension/scene.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ #include <unistd.h> /* For sysconf */ @@ -31,6 +32,7 @@ dmnsn_new_scene() scene->default_texture = dmnsn_new_texture(); if (!scene->default_texture) { dmnsn_delete_scene(scene); + errno = ENOMEM; return NULL; } @@ -46,6 +48,8 @@ dmnsn_new_scene() 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 811b240..a69e25a 100644 --- a/libdimension/solid_pigment.c +++ b/libdimension/solid_pigment.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ /* Solid color pigment callback */ @@ -35,6 +36,7 @@ dmnsn_new_solid_pigment(dmnsn_color color) solid = malloc(sizeof(dmnsn_color)); if (!solid) { dmnsn_delete_pigment(pigment); + errno = ENOMEM; return NULL; } *solid = color; diff --git a/libdimension/texture.c b/libdimension/texture.c index 24ff759..28f5033 100644 --- a/libdimension/texture.c +++ b/libdimension/texture.c @@ -19,6 +19,7 @@ *************************************************************************/ #include "dimension.h" +#include <errno.h> #include <stdlib.h> /* For malloc */ /* Allocate a dummy pigment */ @@ -28,6 +29,8 @@ dmnsn_new_pigment() dmnsn_pigment *pigment = malloc(sizeof(dmnsn_pigment)); if (pigment) { pigment->free_fn = NULL; + } else { + errno = ENOMEM; } return pigment; } @@ -55,6 +58,8 @@ dmnsn_new_finish() finish->ambient_fn = NULL; finish->reflection_fn = NULL; finish->free_fn = NULL; + } else { + errno = ENOMEM; } return finish; } @@ -79,6 +84,8 @@ dmnsn_new_texture() if (texture) { texture->pigment = NULL; texture->finish = NULL; + } else { + errno = ENOMEM; } return texture; } |