From a8181915a4aac7a37bdd62705e2eac1067eb1904 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 24 Dec 2011 13:19:38 -0500 Subject: Use macros to initialize refcounts. --- libdimension/camera.c | 6 +++--- libdimension/canvas.c | 4 +--- libdimension/checker.c | 4 ++-- libdimension/dimension/refcount.h | 6 +++--- libdimension/finish.c | 20 ++++++++++---------- libdimension/interior.c | 6 +++--- libdimension/leopard.c | 4 ++-- libdimension/light.c | 2 +- libdimension/object.c | 2 +- libdimension/pattern.c | 4 ++-- libdimension/pigment.c | 2 +- libdimension/refcount-internal.h | 15 +++++++++++++-- libdimension/texture.c | 2 +- 13 files changed, 43 insertions(+), 34 deletions(-) diff --git a/libdimension/camera.c b/libdimension/camera.c index 13b91f0..0a007ea 100644 --- a/libdimension/camera.c +++ b/libdimension/camera.c @@ -31,9 +31,9 @@ dmnsn_camera * dmnsn_new_camera(void) { dmnsn_camera *camera = dmnsn_malloc(sizeof(dmnsn_camera)); - camera->free_fn = NULL; - camera->trans = dmnsn_identity_matrix(); - camera->refcount = 1; + camera->free_fn = NULL; + camera->trans = dmnsn_identity_matrix(); + DMNSN_REFCOUNT_INIT(camera); return camera; } diff --git a/libdimension/canvas.c b/libdimension/canvas.c index eeeb66b..2bcc502 100644 --- a/libdimension/canvas.c +++ b/libdimension/canvas.c @@ -31,13 +31,11 @@ dmnsn_canvas * dmnsn_new_canvas(size_t width, size_t height) { dmnsn_canvas *canvas = dmnsn_malloc(sizeof(dmnsn_canvas)); - canvas->width = width; canvas->height = height; canvas->optimizers = dmnsn_new_array(sizeof(dmnsn_canvas_optimizer)); canvas->pixels = dmnsn_malloc(sizeof(dmnsn_tcolor)*width*height); - canvas->refcount = 1; - + DMNSN_REFCOUNT_INIT(canvas); return canvas; } diff --git a/libdimension/checker.c b/libdimension/checker.c index aa663ed..2971a82 100644 --- a/libdimension/checker.c +++ b/libdimension/checker.c @@ -23,7 +23,7 @@ * Checker pattern. */ -#include "dimension.h" +#include "dimension-internal.h" /** Checker pattern callback. */ static double @@ -54,7 +54,7 @@ dmnsn_checker_pattern_fn(const dmnsn_pattern *checker, dmnsn_vector v) /** The singleton instance. */ static dmnsn_pattern dmnsn_checker_instance = { .pattern_fn = dmnsn_checker_pattern_fn, - .refcount = 1, + DMNSN_REFCOUNT_INITIALIZER, }; dmnsn_pattern * diff --git a/libdimension/dimension/refcount.h b/libdimension/dimension/refcount.h index 9f6d8b0..f94815c 100644 --- a/libdimension/dimension/refcount.h +++ b/libdimension/dimension/refcount.h @@ -24,10 +24,10 @@ */ /** @internal The name of the reference count field in all structs. */ -#define DMNSN_REFCOUNT_NAME refcount +#define DMNSN_REFCOUNT_FIELD refcount /** @internal Declare a reference count field in a struct. */ -#define DMNSN_REFCOUNT unsigned int DMNSN_REFCOUNT_NAME +#define DMNSN_REFCOUNT unsigned int DMNSN_REFCOUNT_FIELD /** * Increment a reference count. @@ -38,6 +38,6 @@ /* Suppress "address will always evaluate to true" warning */ \ void *testptr = (object); \ if (testptr) { \ - ++(object)->DMNSN_REFCOUNT_NAME; \ + ++(object)->DMNSN_REFCOUNT_FIELD; \ } \ } while (0) diff --git a/libdimension/finish.c b/libdimension/finish.c index cd922ff..e5ba88e 100644 --- a/libdimension/finish.c +++ b/libdimension/finish.c @@ -30,7 +30,7 @@ dmnsn_new_ambient(dmnsn_color ambient_light) { dmnsn_ambient *ambient = dmnsn_malloc(sizeof(dmnsn_ambient)); ambient->ambient = ambient_light; - ambient->refcount = 1; + DMNSN_REFCOUNT_INIT(ambient); return ambient; } @@ -46,9 +46,9 @@ dmnsn_diffuse * dmnsn_new_diffuse(void) { dmnsn_diffuse *diffuse = dmnsn_malloc(sizeof(dmnsn_diffuse)); - diffuse->free_fn = NULL; - diffuse->ptr = NULL; - diffuse->refcount = 1; + diffuse->free_fn = NULL; + diffuse->ptr = NULL; + DMNSN_REFCOUNT_INIT(diffuse); return diffuse; } @@ -67,9 +67,9 @@ dmnsn_specular * dmnsn_new_specular(void) { dmnsn_specular *specular = dmnsn_malloc(sizeof(dmnsn_specular)); - specular->free_fn = NULL; - specular->ptr = NULL; - specular->refcount = 1; + specular->free_fn = NULL; + specular->ptr = NULL; + DMNSN_REFCOUNT_INIT(specular); return specular; } @@ -88,9 +88,9 @@ dmnsn_reflection * dmnsn_new_reflection(void) { dmnsn_reflection *reflection = dmnsn_malloc(sizeof(dmnsn_reflection)); - reflection->free_fn = NULL; - reflection->ptr = NULL; - reflection->refcount = 1; + reflection->free_fn = NULL; + reflection->ptr = NULL; + DMNSN_REFCOUNT_INIT(reflection); return reflection; } diff --git a/libdimension/interior.c b/libdimension/interior.c index a8e328c..24797a6 100644 --- a/libdimension/interior.c +++ b/libdimension/interior.c @@ -31,9 +31,9 @@ dmnsn_interior * dmnsn_new_interior(void) { dmnsn_interior *interior = dmnsn_malloc(sizeof(dmnsn_interior)); - interior->ior = 1.0; - interior->free_fn = NULL; - interior->refcount = 1; + interior->ior = 1.0; + interior->free_fn = NULL; + DMNSN_REFCOUNT_INIT(interior); return interior; } diff --git a/libdimension/leopard.c b/libdimension/leopard.c index 48986b4..072f032 100644 --- a/libdimension/leopard.c +++ b/libdimension/leopard.c @@ -23,7 +23,7 @@ * Leopard pattern. */ -#include "dimension.h" +#include "dimension-internal.h" #include /** Leopard pattern callback. */ @@ -37,7 +37,7 @@ dmnsn_leopard_pattern_fn(const dmnsn_pattern *leopard, dmnsn_vector v) /** The singleton instance. */ static dmnsn_pattern dmnsn_leopard_instance = { .pattern_fn = dmnsn_leopard_pattern_fn, - .refcount = 1, + DMNSN_REFCOUNT_INITIALIZER, }; dmnsn_pattern * diff --git a/libdimension/light.c b/libdimension/light.c index 358ba2d..d64b2a2 100644 --- a/libdimension/light.c +++ b/libdimension/light.c @@ -36,7 +36,7 @@ dmnsn_new_light(void) light->shadow_fn = NULL; light->free_fn = NULL; light->ptr = NULL; - light->refcount = 1; + DMNSN_REFCOUNT_INIT(light); return light; } diff --git a/libdimension/object.c b/libdimension/object.c index b1b5fe3..9792926 100644 --- a/libdimension/object.c +++ b/libdimension/object.c @@ -41,8 +41,8 @@ dmnsn_new_object(void) object->inside_fn = NULL; object->initialize_fn = NULL; object->free_fn = NULL; - object->refcount = 1; object->initialized = false; + DMNSN_REFCOUNT_INIT(object); return object; } diff --git a/libdimension/pattern.c b/libdimension/pattern.c index 6a2e286..f32671c 100644 --- a/libdimension/pattern.c +++ b/libdimension/pattern.c @@ -30,8 +30,8 @@ dmnsn_pattern * dmnsn_new_pattern(void) { dmnsn_pattern *pattern = dmnsn_malloc(sizeof(dmnsn_pattern)); - pattern->free_fn = NULL; - pattern->refcount = 1; + pattern->free_fn = NULL; + DMNSN_REFCOUNT_INIT(pattern); return pattern; } diff --git a/libdimension/pigment.c b/libdimension/pigment.c index c518005..bc09ec7 100644 --- a/libdimension/pigment.c +++ b/libdimension/pigment.c @@ -35,8 +35,8 @@ dmnsn_new_pigment(void) pigment->free_fn = NULL; pigment->trans = dmnsn_identity_matrix(); pigment->quick_color = DMNSN_TCOLOR(dmnsn_black); - pigment->refcount = 1; pigment->initialized = false; + DMNSN_REFCOUNT_INIT(pigment); return pigment; } diff --git a/libdimension/refcount-internal.h b/libdimension/refcount-internal.h index 5ae5d58..fb6d3c6 100644 --- a/libdimension/refcount-internal.h +++ b/libdimension/refcount-internal.h @@ -23,11 +23,22 @@ * Reference count internal API. */ +/** + * Initialize a reference count. + * @param[in,out] object The reference-counted object. + */ +#define DMNSN_REFCOUNT_INIT(object) (void)((object)->DMNSN_REFCOUNT_FIELD = 1) + +/** + * Initialize a reference count in a statically-allocated object. + */ +#define DMNSN_REFCOUNT_INITIALIZER .DMNSN_REFCOUNT_FIELD = 1 + /** * Decrement a reference count. * @param[in,out] object The reference-counted object to release. * @return Whether the object is now garbage. */ #define DMNSN_DECREF(object) \ - ((object) && ((object)->DMNSN_REFCOUNT_NAME == 0 \ - || --(object)->DMNSN_REFCOUNT_NAME == 0)) + ((object) && ((object)->DMNSN_REFCOUNT_FIELD == 0 \ + || --(object)->DMNSN_REFCOUNT_FIELD == 0)) diff --git a/libdimension/texture.c b/libdimension/texture.c index 3018bcd..24ff9af 100644 --- a/libdimension/texture.c +++ b/libdimension/texture.c @@ -33,8 +33,8 @@ dmnsn_new_texture(void) texture->pigment = NULL; texture->finish = dmnsn_new_finish(); texture->trans = dmnsn_identity_matrix(); - texture->refcount = 1; texture->initialized = false; + DMNSN_REFCOUNT_INIT(texture); return texture; } -- cgit v1.2.3