isl_seq_normalize: use pre-allocated temporary variable in isl_ctx
[isl.git] / isl_name.c
blob2ef6cbe9cd9d3d79fc9b8b836c9023eb900d5bb4
1 #include <string.h>
2 #include "isl_name.h"
4 struct isl_name *isl_name_alloc(struct isl_ctx *ctx, const char *s)
6 const char *copy = strdup(s);
7 struct isl_name *name;
9 if (!copy)
10 return NULL;
11 name = isl_alloc_type(ctx, struct isl_name);
12 if (!name)
13 return NULL;
15 name->ref = 1;
16 name->name = copy;
18 return name;
21 static int isl_name_has_name(const void *entry, const void *val)
23 struct isl_name *name = (struct isl_name *)entry;
24 const char *s = (const char *)val;
26 return !strcmp(name->name, s);
29 struct isl_name *isl_name_get(struct isl_ctx *ctx, const char *name)
31 struct isl_hash_table_entry *entry;
32 uint32_t name_hash;
34 name_hash = isl_hash_string(isl_hash_init(), name);
35 entry = isl_hash_table_find(ctx, &ctx->name_hash, name_hash,
36 isl_name_has_name, name, 1);
37 if (!entry)
38 return NULL;
39 if (entry->data)
40 return isl_name_copy(ctx, entry->data);
41 entry->data = isl_name_alloc(ctx, name);
42 if (!entry->data)
43 ctx->name_hash.n--;
44 return entry->data;
47 struct isl_name *isl_name_copy(struct isl_ctx *ctx, struct isl_name *name)
49 if (!name)
50 return NULL;
52 name->ref++;
53 return name;
56 static int isl_name_eq(const void *entry, const void *name)
58 return entry == name;
61 void isl_name_free(struct isl_ctx *ctx, struct isl_name *name)
63 uint32_t name_hash;
64 struct isl_hash_table_entry *entry;
66 if (!name)
67 return;
69 if (--name->ref > 0)
70 return;
72 name_hash = isl_hash_string(isl_hash_init(), name->name);
73 entry = isl_hash_table_find(ctx, &ctx->name_hash, name_hash,
74 isl_name_eq, name, 0);
75 isl_assert(ctx, entry, return);
76 isl_hash_table_remove(ctx, &ctx->name_hash, entry);
78 free((char *)name->name);
79 free(name);