add transitive closure example from Kelly's 1996 paper
[isl.git] / isl_name.c
blob21b7ff7edc56059c21d8820e395e841f0af2ed6b
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_name.h"
13 struct isl_name *isl_name_alloc(struct isl_ctx *ctx, const char *s)
15 const char *copy = strdup(s);
16 struct isl_name *name;
18 if (!copy)
19 return NULL;
20 name = isl_alloc_type(ctx, struct isl_name);
21 if (!name)
22 return NULL;
24 name->ref = 1;
25 name->name = copy;
27 return name;
30 static int isl_name_has_name(const void *entry, const void *val)
32 struct isl_name *name = (struct isl_name *)entry;
33 const char *s = (const char *)val;
35 return !strcmp(name->name, s);
38 struct isl_name *isl_name_get(struct isl_ctx *ctx, const char *name)
40 struct isl_hash_table_entry *entry;
41 uint32_t name_hash;
43 name_hash = isl_hash_string(isl_hash_init(), name);
44 entry = isl_hash_table_find(ctx, &ctx->name_hash, name_hash,
45 isl_name_has_name, name, 1);
46 if (!entry)
47 return NULL;
48 if (entry->data)
49 return isl_name_copy(ctx, entry->data);
50 entry->data = isl_name_alloc(ctx, name);
51 if (!entry->data)
52 ctx->name_hash.n--;
53 return entry->data;
56 struct isl_name *isl_name_copy(struct isl_ctx *ctx, struct isl_name *name)
58 if (!name)
59 return NULL;
61 name->ref++;
62 return name;
65 static int isl_name_eq(const void *entry, const void *name)
67 return entry == name;
70 void isl_name_free(struct isl_ctx *ctx, struct isl_name *name)
72 uint32_t name_hash;
73 struct isl_hash_table_entry *entry;
75 if (!name)
76 return;
78 if (--name->ref > 0)
79 return;
81 name_hash = isl_hash_string(isl_hash_init(), name->name);
82 entry = isl_hash_table_find(ctx, &ctx->name_hash, name_hash,
83 isl_name_eq, name, 0);
84 isl_assert(ctx, entry, return);
85 isl_hash_table_remove(ctx, &ctx->name_hash, entry);
87 free((char *)name->name);
88 free(name);