add isl_printer_indent
[platform/upstream/isl.git] / isl_name.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_name.h"
13
14 struct isl_name *isl_name_alloc(struct isl_ctx *ctx, const char *s)
15 {
16         const char *copy = strdup(s);
17         struct isl_name *name;
18
19         if (!copy)
20                 return NULL;
21         name = isl_alloc_type(ctx, struct isl_name);
22         if (!name)
23                 goto error;
24
25         name->ref = 1;
26         name->name = copy;
27
28         name->hash = isl_hash_init();
29         name->hash = isl_hash_string(name->hash, s);
30
31         return name;
32 error:
33         free((char *)copy);
34         return NULL;
35 }
36
37 static int isl_name_has_name(const void *entry, const void *val)
38 {
39         struct isl_name *name = (struct isl_name *)entry;
40         const char *s = (const char *)val;
41
42         return !strcmp(name->name, s);
43 }
44
45 struct isl_name *isl_name_get(struct isl_ctx *ctx, const char *name)
46 {
47         struct isl_hash_table_entry *entry;
48         uint32_t name_hash;
49
50         name_hash = isl_hash_string(isl_hash_init(), name);
51         entry = isl_hash_table_find(ctx, &ctx->name_hash, name_hash,
52                                         isl_name_has_name, name, 1);
53         if (!entry)
54                 return NULL;
55         if (entry->data)
56                 return isl_name_copy(ctx, entry->data);
57         entry->data = isl_name_alloc(ctx, name);
58         if (!entry->data)
59                 ctx->name_hash.n--;
60         return entry->data;
61 }
62
63 struct isl_name *isl_name_copy(struct isl_ctx *ctx, struct isl_name *name)
64 {
65         if (!name)
66                 return NULL;
67
68         name->ref++;
69         return name;
70 }
71
72 static int isl_name_eq(const void *entry, const void *name)
73 {
74         return entry == name;
75 }
76
77 uint32_t isl_hash_name(uint32_t hash, struct isl_name *name)
78 {
79         if (name)
80                 isl_hash_hash(hash, name->hash);
81
82         return hash;
83 }
84
85 void isl_name_free(struct isl_ctx *ctx, struct isl_name *name)
86 {
87         uint32_t name_hash;
88         struct isl_hash_table_entry *entry;
89
90         if (!name)
91                 return;
92
93         if (--name->ref > 0)
94                 return;
95
96         name_hash = isl_hash_string(isl_hash_init(), name->name);
97         entry = isl_hash_table_find(ctx, &ctx->name_hash, name_hash,
98                                         isl_name_eq, name, 0);
99         isl_assert(ctx, entry, return);
100         isl_hash_table_remove(ctx, &ctx->name_hash, entry);
101
102         free((char *)name->name);
103         free(name);
104 }