diff options
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/dimension/interior.h | 3 | ||||
-rw-r--r-- | libdimension/dimension/texture.h | 3 | ||||
-rw-r--r-- | libdimension/interior.c | 17 | ||||
-rw-r--r-- | libdimension/texture.c | 19 |
4 files changed, 31 insertions, 11 deletions
diff --git a/libdimension/dimension/interior.h b/libdimension/dimension/interior.h index 0ab4a30..69a0fcb 100644 --- a/libdimension/dimension/interior.h +++ b/libdimension/dimension/interior.h @@ -34,6 +34,9 @@ typedef struct dmnsn_interior { /* Generic pointer */ void *ptr; + + /* Reference count */ + unsigned int *refcount; } dmnsn_interior; dmnsn_interior *dmnsn_new_interior(); diff --git a/libdimension/dimension/texture.h b/libdimension/dimension/texture.h index ce15742..9c5fae1 100644 --- a/libdimension/dimension/texture.h +++ b/libdimension/dimension/texture.h @@ -102,6 +102,9 @@ typedef struct { /* Transformation matrix */ dmnsn_matrix trans, trans_inv; + + /* Reference count */ + unsigned int *refcount; } dmnsn_texture; dmnsn_texture *dmnsn_new_texture(); diff --git a/libdimension/interior.c b/libdimension/interior.c index 9f9201d..0accb79 100644 --- a/libdimension/interior.c +++ b/libdimension/interior.c @@ -26,8 +26,10 @@ dmnsn_interior * dmnsn_new_interior() { dmnsn_interior *interior = dmnsn_malloc(sizeof(dmnsn_interior)); - interior->ior = 1.0; - interior->free_fn = NULL; + interior->ior = 1.0; + interior->free_fn = NULL; + interior->refcount = dmnsn_malloc(sizeof(unsigned int)); + *interior->refcount = 1; return interior; } @@ -36,9 +38,14 @@ void dmnsn_delete_interior(dmnsn_interior *interior) { if (interior) { - if (interior->free_fn) { - (*interior->free_fn)(interior->ptr); + if (*interior->refcount <= 1) { + if (interior->free_fn) { + (*interior->free_fn)(interior->ptr); + } + free(interior->refcount); + free(interior); + } else { + --*interior->refcount; } - free(interior); } } diff --git a/libdimension/texture.c b/libdimension/texture.c index 218b7f6..93279bb 100644 --- a/libdimension/texture.c +++ b/libdimension/texture.c @@ -80,9 +80,11 @@ dmnsn_texture * dmnsn_new_texture() { dmnsn_texture *texture = dmnsn_malloc(sizeof(dmnsn_texture)); - texture->pigment = NULL; - texture->finish = NULL; - texture->trans = dmnsn_identity_matrix(); + texture->pigment = NULL; + texture->finish = NULL; + texture->trans = dmnsn_identity_matrix(); + texture->refcount = dmnsn_malloc(sizeof(unsigned int)); + *texture->refcount = 1; return texture; } @@ -91,9 +93,14 @@ void dmnsn_delete_texture(dmnsn_texture *texture) { if (texture) { - dmnsn_delete_finish(texture->finish); - dmnsn_delete_pigment(texture->pigment); - free(texture); + if (*texture->refcount <= 1) { + dmnsn_delete_finish(texture->finish); + dmnsn_delete_pigment(texture->pigment); + free(texture->refcount); + free(texture); + } else { + --*texture->refcount; + } } } |