introduce identifiers
[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 __isl_give isl_id *isl_id_copy(isl_id *id)
89 {
90         if (!id)
91                 return NULL;
92
93         id->ref++;
94         return id;
95 }
96
97 static int isl_id_eq(const void *entry, const void *name)
98 {
99         return entry == name;
100 }
101
102 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
103 {
104         if (id)
105                 isl_hash_hash(hash, id->hash);
106
107         return hash;
108 }
109
110 void *isl_id_free(__isl_take isl_id *id)
111 {
112         struct isl_hash_table_entry *entry;
113
114         if (!id)
115                 return NULL;
116
117         if (--id->ref > 0)
118                 return NULL;
119
120         entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
121                                         isl_id_eq, id, 0);
122         if (!entry)
123                 isl_die(id->ctx, isl_error_unknown,
124                         "unable to find id", (void)0);
125         else
126                 isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
127
128         free((char *)id->name);
129         isl_ctx_deref(id->ctx);
130         free(id);
131
132         return NULL;
133 }
134
135 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
136         __isl_keep isl_id *id)
137 {
138         if (!id)
139                 goto error;
140
141         if (id->name)
142                 p = isl_printer_print_str(p, id->name);
143         if (id->user) {
144                 char buffer[50];
145                 snprintf(buffer, sizeof(buffer), "@%p", id->user);
146                 p = isl_printer_print_str(p, buffer);
147         }
148         return p;
149 error:
150         isl_printer_free(p);
151         return NULL;
152 }
153
154 void isl_id_dump(__isl_keep isl_id *id)
155 {
156         isl_printer *printer;
157
158         if (!id)
159                 return;
160
161         printer = isl_printer_to_file(isl_id_get_ctx(id), stderr);
162         printer = isl_printer_print_id(printer, id);
163         printer = isl_printer_end_line(printer);
164
165         isl_printer_free(printer);
166 }