allow isl_ids with negative reference counts
[isl.git] / isl_id.c
blob8149e69147a05bf6e2f73a9a93128c6c3e82d002
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <string.h>
11 #include <isl_ctx_private.h>
12 #include <isl_id_private.h>
14 isl_ctx *isl_id_get_ctx(__isl_keep isl_id *id)
16 return id ? id->ctx : NULL;
19 void *isl_id_get_user(__isl_keep isl_id *id)
21 return id ? id->user : NULL;
24 const char *isl_id_get_name(__isl_keep isl_id *id)
26 return id ? id->name : NULL;
29 static __isl_give isl_id *id_alloc(isl_ctx *ctx, const char *name, void *user)
31 const char *copy = name ? strdup(name) : NULL;
32 isl_id *id;
34 if (name && !copy)
35 return NULL;
36 id = isl_alloc_type(ctx, struct isl_id);
37 if (!id)
38 goto error;
40 id->ctx = ctx;
41 isl_ctx_ref(id->ctx);
42 id->ref = 1;
43 id->name = copy;
44 id->user = user;
46 id->hash = isl_hash_init();
47 if (name)
48 id->hash = isl_hash_string(id->hash, name);
49 else
50 id->hash = isl_hash_builtin(id->hash, user);
52 return id;
53 error:
54 free((char *)copy);
55 return NULL;
58 static int isl_id_has_name(const void *entry, const void *val)
60 isl_id *id = (isl_id *)entry;
61 const char *s = (const char *)val;
63 return !strcmp(id->name, s);
66 __isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
68 struct isl_hash_table_entry *entry;
69 uint32_t id_hash;
71 id_hash = isl_hash_init();
72 if (name)
73 id_hash = isl_hash_string(id_hash, name);
74 else
75 id_hash = isl_hash_builtin(id_hash, user);
76 entry = isl_hash_table_find(ctx, &ctx->id_table, id_hash,
77 isl_id_has_name, name, 1);
78 if (!entry)
79 return NULL;
80 if (entry->data)
81 return isl_id_copy(entry->data);
82 entry->data = id_alloc(ctx, name, user);
83 if (!entry->data)
84 ctx->id_table.n--;
85 return entry->data;
88 /* If the id has a negative refcount, then it is a static isl_id
89 * which should not be changed.
91 __isl_give isl_id *isl_id_copy(isl_id *id)
93 if (!id)
94 return NULL;
96 if (id->ref < 0)
97 return id;
99 id->ref++;
100 return id;
103 static int isl_id_eq(const void *entry, const void *name)
105 return entry == name;
108 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
110 if (id)
111 isl_hash_hash(hash, id->hash);
113 return hash;
116 /* If the id has a negative refcount, then it is a static isl_id
117 * and should not be freed.
119 void *isl_id_free(__isl_take isl_id *id)
121 struct isl_hash_table_entry *entry;
123 if (!id)
124 return NULL;
126 if (id->ref < 0)
127 return NULL;
129 if (--id->ref > 0)
130 return NULL;
132 entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
133 isl_id_eq, id, 0);
134 if (!entry)
135 isl_die(id->ctx, isl_error_unknown,
136 "unable to find id", (void)0);
137 else
138 isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
140 free((char *)id->name);
141 isl_ctx_deref(id->ctx);
142 free(id);
144 return NULL;
147 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
148 __isl_keep isl_id *id)
150 if (!id)
151 goto error;
153 if (id->name)
154 p = isl_printer_print_str(p, id->name);
155 if (id->user) {
156 char buffer[50];
157 snprintf(buffer, sizeof(buffer), "@%p", id->user);
158 p = isl_printer_print_str(p, buffer);
160 return p;
161 error:
162 isl_printer_free(p);
163 return NULL;