add isl_id_set_free_user
authorSven Verdoolaege <skimo@kotnet.org>
Mon, 10 Sep 2012 12:23:40 +0000 (14:23 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Tue, 18 Sep 2012 13:08:22 +0000 (15:08 +0200)
Allow the user to specify a callback to be called on the user pointer
when the last reference to the isl_id disappears.  This is convenient
for attaching dynamically allocated data structures to isl_ids,
especially in annotations to AST nodes.

Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
doc/user.pod
include/isl/id.h
isl_id.c
isl_id_private.h

index c9c0b39..737f6de 100644 (file)
@@ -598,6 +598,9 @@ using the following functions.
        #include <isl/id.h>
        __isl_give isl_id *isl_id_alloc(isl_ctx *ctx,
                __isl_keep const char *name, void *user);
+       __isl_give isl_id *isl_id_set_free_user(
+               __isl_take isl_id *id,
+               __isl_give void (*free_user)(void *user));
        __isl_give isl_id *isl_id_copy(isl_id *id);
        void *isl_id_free(__isl_take isl_id *id);
 
@@ -608,6 +611,8 @@ using the following functions.
        __isl_give isl_printer *isl_printer_print_id(
                __isl_take isl_printer *p, __isl_keep isl_id *id);
 
+The callback set by C<isl_id_set_free_user> is called on the user
+pointer when the last reference to the C<isl_id> is freed.
 Note that C<isl_id_get_name> returns a pointer to some internal
 data structure, so the result can only be used while the
 corresponding C<isl_id> is alive.
index 86232f7..d237b23 100644 (file)
@@ -21,6 +21,9 @@ void *isl_id_free(__isl_take isl_id *id);
 void *isl_id_get_user(__isl_keep isl_id *id);
 __isl_keep const char *isl_id_get_name(__isl_keep isl_id *id);
 
+__isl_give isl_id *isl_id_set_free_user(__isl_take isl_id *id,
+       __isl_give void (*free_user)(void *user));
+
 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
        __isl_keep isl_id *id);
 void isl_id_dump(__isl_keep isl_id *id);
index fd3f68d..26f19c6 100644 (file)
--- a/isl_id.c
+++ b/isl_id.c
@@ -44,7 +44,7 @@ static __isl_give isl_id *id_alloc(isl_ctx *ctx, const char *name, void *user)
 
        if (name && !copy)
                return NULL;
-       id = isl_alloc_type(ctx, struct isl_id);
+       id = isl_calloc_type(ctx, struct isl_id);
        if (!id)
                goto error;
 
@@ -135,6 +135,19 @@ uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
        return hash;
 }
 
+/* Replace the free_user callback by "free_user".
+ */
+__isl_give isl_id *isl_id_set_free_user(__isl_take isl_id *id,
+       __isl_give void (*free_user)(void *user))
+{
+       if (!id)
+               return NULL;
+
+       id->free_user = free_user;
+
+       return id;
+}
+
 /* If the id has a negative refcount, then it is a static isl_id
  * and should not be freed.
  */
@@ -159,6 +172,9 @@ void *isl_id_free(__isl_take isl_id *id)
        else
                isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
 
+       if (id->free_user)
+               id->free_user(id->user);
+
        free((char *)id->name);
        isl_ctx_deref(id->ctx);
        free(id);
index c89773e..c583b31 100644 (file)
 
 #include <isl/id.h>
 
+/* Represent a name and/or user pointer.
+ *
+ * If "free_user" is set, then it will be called on "user" when
+ * the last instance of the isl_id is freed.
+ */
 struct isl_id {
        int ref;
        isl_ctx *ctx;
@@ -19,6 +24,8 @@ struct isl_id {
        const char *name;
        void *user;
        uint32_t hash;
+
+       __isl_give void (*free_user)(void *user);
 };
 
 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id);