allow isl_ids with negative reference counts
[platform/upstream/isl.git] / isl_id.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include <string.h>
11 #include <isl_ctx_private.h>
12 #include <isl_id_private.h>
13
14 isl_ctx *isl_id_get_ctx(__isl_keep isl_id *id)
15 {
16         return id ? id->ctx : NULL;
17 }
18
19 void *isl_id_get_user(__isl_keep isl_id *id)
20 {
21         return id ? id->user : NULL;
22 }
23
24 const char *isl_id_get_name(__isl_keep isl_id *id)
25 {
26         return id ? id->name : NULL;
27 }
28
29 static __isl_give isl_id *id_alloc(isl_ctx *ctx, const char *name, void *user)
30 {
31         const char *copy = name ? strdup(name) : NULL;
32         isl_id *id;
33
34         if (name && !copy)
35                 return NULL;
36         id = isl_alloc_type(ctx, struct isl_id);
37         if (!id)
38                 goto error;
39
40         id->ctx = ctx;
41         isl_ctx_ref(id->ctx);
42         id->ref = 1;
43         id->name = copy;
44         id->user = user;
45
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);
51
52         return id;
53 error:
54         free((char *)copy);
55         return NULL;
56 }
57
58 static int isl_id_has_name(const void *entry, const void *val)
59 {
60         isl_id *id = (isl_id *)entry;
61         const char *s = (const char *)val;
62
63         return !strcmp(id->name, s);
64 }
65
66 __isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
67 {
68         struct isl_hash_table_entry *entry;
69         uint32_t id_hash;
70
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;
86 }
87
88 /* If the id has a negative refcount, then it is a static isl_id
89  * which should not be changed.
90  */
91 __isl_give isl_id *isl_id_copy(isl_id *id)
92 {
93         if (!id)
94                 return NULL;
95
96         if (id->ref < 0)
97                 return id;
98
99         id->ref++;
100         return id;
101 }
102
103 static int isl_id_eq(const void *entry, const void *name)
104 {
105         return entry == name;
106 }
107
108 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
109 {
110         if (id)
111                 isl_hash_hash(hash, id->hash);
112
113         return hash;
114 }
115
116 /* If the id has a negative refcount, then it is a static isl_id
117  * and should not be freed.
118  */
119 void *isl_id_free(__isl_take isl_id *id)
120 {
121         struct isl_hash_table_entry *entry;
122
123         if (!id)
124                 return NULL;
125
126         if (id->ref < 0)
127                 return NULL;
128
129         if (--id->ref > 0)
130                 return NULL;
131
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);
139
140         free((char *)id->name);
141         isl_ctx_deref(id->ctx);
142         free(id);
143
144         return NULL;
145 }
146
147 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
148         __isl_keep isl_id *id)
149 {
150         if (!id)
151                 goto error;
152
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);
159         }
160         return p;
161 error:
162         isl_printer_free(p);
163         return NULL;
164 }