From 4479d25609e26253c4e5fcfc78b093c0b45cefb8 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 28 May 2011 18:16:04 -0600 Subject: Don't allocate reference counts on the heap. --- libdimension/Makefile.am | 1 - libdimension/camera.c | 3 +-- libdimension/canvas.c | 4 +--- libdimension/dimension/camera.h | 2 +- libdimension/dimension/canvas.h | 2 +- libdimension/dimension/interior.h | 2 +- libdimension/dimension/object.h | 2 +- libdimension/dimension/refcount.h | 29 ++++++------------------ libdimension/dimension/texture.h | 4 ++-- libdimension/interior.c | 3 +-- libdimension/object.c | 3 +-- libdimension/refcount.c | 46 --------------------------------------- libdimension/texture.c | 3 +-- 13 files changed, 18 insertions(+), 86 deletions(-) delete mode 100644 libdimension/refcount.c diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am index e824308..83e6f62 100644 --- a/libdimension/Makefile.am +++ b/libdimension/Makefile.am @@ -103,7 +103,6 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \ prtree.c \ prtree.h \ raytrace.c \ - refcount.c \ reflective.c \ scene.c \ sky_sphere.c \ diff --git a/libdimension/camera.c b/libdimension/camera.c index e76200a..935a129 100644 --- a/libdimension/camera.c +++ b/libdimension/camera.c @@ -33,7 +33,7 @@ dmnsn_new_camera(void) dmnsn_camera *camera = dmnsn_malloc(sizeof(dmnsn_camera)); camera->free_fn = NULL; camera->trans = dmnsn_identity_matrix(); - camera->refcount = dmnsn_new_refcount(); + camera->refcount = 0; return camera; } @@ -42,7 +42,6 @@ void dmnsn_delete_camera(dmnsn_camera *camera) { if (camera && DMNSN_DECREF(camera)) { - dmnsn_delete_refcount(camera->refcount); if (camera->free_fn) { camera->free_fn(camera->ptr); } diff --git a/libdimension/canvas.c b/libdimension/canvas.c index b1b65e1..6df5dd3 100644 --- a/libdimension/canvas.c +++ b/libdimension/canvas.c @@ -36,7 +36,7 @@ dmnsn_new_canvas(size_t width, size_t height) canvas->height = height; canvas->optimizers = dmnsn_new_array(sizeof(dmnsn_canvas_optimizer)); canvas->pixels = dmnsn_malloc(sizeof(dmnsn_color)*width*height); - canvas->refcount = dmnsn_new_refcount(); + canvas->refcount = 0; return canvas; } @@ -46,8 +46,6 @@ void dmnsn_delete_canvas(dmnsn_canvas *canvas) { if (canvas && DMNSN_DECREF(canvas)) { - dmnsn_delete_refcount(canvas->refcount); - /* Free the optimizers */ DMNSN_ARRAY_FOREACH (dmnsn_canvas_optimizer *, i, canvas->optimizers) { if (i->free_fn) { diff --git a/libdimension/dimension/camera.h b/libdimension/dimension/camera.h index 67fffc0..75d86a9 100644 --- a/libdimension/dimension/camera.h +++ b/libdimension/dimension/camera.h @@ -46,7 +46,7 @@ struct dmnsn_camera { void *ptr; /**< @internal Generic pointer for camera info. */ - dmnsn_refcount *refcount; /**< @internal reference count. */ + dmnsn_refcount refcount; /**< @internal reference count. */ }; /** diff --git a/libdimension/dimension/canvas.h b/libdimension/dimension/canvas.h index f33fc88..864e2d6 100644 --- a/libdimension/dimension/canvas.h +++ b/libdimension/dimension/canvas.h @@ -40,7 +40,7 @@ typedef struct { */ dmnsn_color *pixels; - dmnsn_refcount *refcount; /**< @internal Reference count. */ + dmnsn_refcount refcount; /**< @internal Reference count. */ } dmnsn_canvas; /* Forward-declare dmnsn_canvas_optimizer */ diff --git a/libdimension/dimension/interior.h b/libdimension/dimension/interior.h index 76f0a32..22a9091 100644 --- a/libdimension/dimension/interior.h +++ b/libdimension/dimension/interior.h @@ -33,7 +33,7 @@ typedef struct dmnsn_interior { void *ptr; /** @internal Reference count. */ - dmnsn_refcount *refcount; + dmnsn_refcount refcount; } dmnsn_interior; /** diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h index efe2323..02daaba 100644 --- a/libdimension/dimension/object.h +++ b/libdimension/dimension/object.h @@ -92,7 +92,7 @@ struct dmnsn_object { void *ptr; /** @internal Reference count. */ - dmnsn_refcount *refcount; + dmnsn_refcount refcount; }; /** diff --git a/libdimension/dimension/refcount.h b/libdimension/dimension/refcount.h index 9d2c304..4431cbc 100644 --- a/libdimension/dimension/refcount.h +++ b/libdimension/dimension/refcount.h @@ -23,36 +23,21 @@ * Generic reference count implementation. */ -/** - * Increment a reference count. - * @param[in,out] object The reference-counted object to acquire. - */ -#define DMNSN_INCREF(obj) ((void)++(*(obj)->refcount)) - -/** - * @internal - * Decrement a reference count. - * @param[in,out] object The reference-counted object to release. - * @return Whether the object is now garbage. - */ -#define DMNSN_DECREF(obj) (*(obj)->refcount == 0 || --(*(obj)->refcount) == 0) - /** * Reference counter. */ typedef unsigned int dmnsn_refcount; /** - * @internal - * Create a reference count. - * @return A new reference counter, initialized to zero (a "borrowed" reference, - * which will be garbage-collected the first time it is deleted). + * Increment a reference count. + * @param[in,out] object The reference-counted object to acquire. */ -dmnsn_refcount *dmnsn_new_refcount(void); +#define DMNSN_INCREF(obj) ((void)++(obj)->refcount) /** * @internal - * Delete a reference count. Raises an error if the reference count wasn't - * zero. + * Decrement a reference count. + * @param[in,out] object The reference-counted object to release. + * @return Whether the object is now garbage. */ -void dmnsn_delete_refcount(dmnsn_refcount *refcount); +#define DMNSN_DECREF(obj) ((obj)->refcount == 0 || --(obj)->refcount == 0) diff --git a/libdimension/dimension/texture.h b/libdimension/dimension/texture.h index 8051c02..7edcabc 100644 --- a/libdimension/dimension/texture.h +++ b/libdimension/dimension/texture.h @@ -31,8 +31,8 @@ typedef struct { dmnsn_matrix trans; /**< Transformation matrix. */ dmnsn_matrix trans_inv; /**< The inverse of the transformation matrix. */ - dmnsn_refcount *refcount; /**< @internal Reference count. */ - bool should_init; /**< @internal Whether to initialize the texture. */ + dmnsn_refcount refcount; /**< @internal Reference count. */ + bool should_init; /**< @internal Whether to initialize the texture. */ } dmnsn_texture; /** diff --git a/libdimension/interior.c b/libdimension/interior.c index 87b5059..0594e06 100644 --- a/libdimension/interior.c +++ b/libdimension/interior.c @@ -33,7 +33,7 @@ dmnsn_new_interior(void) dmnsn_interior *interior = dmnsn_malloc(sizeof(dmnsn_interior)); interior->ior = 1.0; interior->free_fn = NULL; - interior->refcount = dmnsn_new_refcount(); + interior->refcount = 0; return interior; } @@ -42,7 +42,6 @@ void dmnsn_delete_interior(dmnsn_interior *interior) { if (interior && DMNSN_DECREF(interior)) { - dmnsn_delete_refcount(interior->refcount); if (interior->free_fn) { interior->free_fn(interior->ptr); } diff --git a/libdimension/object.c b/libdimension/object.c index f73ea02..fecfdbe 100644 --- a/libdimension/object.c +++ b/libdimension/object.c @@ -39,7 +39,7 @@ dmnsn_new_object(void) object->inside_fn = NULL; object->initialize_fn = NULL; object->free_fn = NULL; - object->refcount = dmnsn_new_refcount(); + object->refcount = 0; return object; } @@ -48,7 +48,6 @@ void dmnsn_delete_object(dmnsn_object *object) { if (object && DMNSN_DECREF(object)) { - dmnsn_delete_refcount(object->refcount); DMNSN_ARRAY_FOREACH (dmnsn_object **, child, object->children) { dmnsn_delete_object(*child); } diff --git a/libdimension/refcount.c b/libdimension/refcount.c deleted file mode 100644 index 66bc010..0000000 --- a/libdimension/refcount.c +++ /dev/null @@ -1,46 +0,0 @@ -/************************************************************************* - * Copyright (C) 2011 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 * - * . * - *************************************************************************/ - -/** - * @file - * Reference counts. - */ - -#include "dimension.h" - -dmnsn_refcount * -dmnsn_new_refcount(void) -{ - dmnsn_refcount *refcount = dmnsn_malloc(sizeof(dmnsn_refcount)); - *refcount = 0; - return refcount; -} - -void -dmnsn_delete_refcount(dmnsn_refcount *refcount) -{ - if (refcount) { - if (*refcount != 0) { - dmnsn_error("Attempt to delete non-zero reference count."); - } - - dmnsn_free(refcount); - } -} diff --git a/libdimension/texture.c b/libdimension/texture.c index 2199103..c65c913 100644 --- a/libdimension/texture.c +++ b/libdimension/texture.c @@ -33,7 +33,7 @@ dmnsn_new_texture(void) texture->pigment = NULL; texture->finish = NULL; texture->trans = dmnsn_identity_matrix(); - texture->refcount = dmnsn_new_refcount(); + texture->refcount = 0; texture->should_init = true; return texture; } @@ -43,7 +43,6 @@ void dmnsn_delete_texture(dmnsn_texture *texture) { if (texture && DMNSN_DECREF(texture)) { - dmnsn_delete_refcount(texture->refcount); dmnsn_delete_finish(texture->finish); dmnsn_delete_pigment(texture->pigment); dmnsn_free(texture); -- cgit v1.2.3