iscc: add lattice_width operation
[barvinok.git] / isl_obj_list.c
blobdcb204cfb41b2cb8a8c66defc3f10f4696e640b4
1 #include <isl_obj_list.h>
3 struct isl_list *isl_list_alloc(struct isl_ctx *ctx, int n)
5 int i;
6 struct isl_list *list;
8 isl_assert(ctx, n >= 0, return NULL);
9 list = isl_alloc(ctx, struct isl_list,
10 sizeof(struct isl_list) + n * sizeof(struct isl_obj));
11 if (!list)
12 return NULL;
14 list->ctx = ctx;
15 isl_ctx_ref(list->ctx);
16 list->ref = 1;
17 list->n = n;
19 for (i = 0; i < n; ++i) {
20 list->obj[i].type = isl_obj_none;
21 list->obj[i].v = NULL;
24 return list;
27 struct isl_list *isl_list_copy(struct isl_list *list)
29 if (!list)
30 return NULL;
32 list->ref++;
33 return list;
36 void isl_list_free(struct isl_list *list)
38 int i;
40 if (!list)
41 return;
43 if (--list->ref > 0)
44 return;
46 for (i = 0; i < list->n; ++i)
47 list->obj[i].type->free(list->obj[i].v);
48 isl_ctx_deref(list->ctx);
49 free(list);
52 static void *isl_obj_list_copy(void *v)
54 return isl_list_copy((struct isl_list *)v);
57 static void isl_obj_list_free(void *v)
59 isl_list_free((struct isl_list *)v);
62 static __isl_give isl_printer *isl_obj_list_print(__isl_take isl_printer *p,
63 void *v)
65 struct isl_list *list = (struct isl_list *)v;
66 int i;
68 p = isl_printer_print_str(p, "(");
69 for (i = 0; i < list->n; ++i) {
70 if (i)
71 p = isl_printer_print_str(p, ", ");
72 p = list->obj[i].type->print(p, list->obj[i].v);
74 p = isl_printer_print_str(p, ")");
75 return p;
78 __isl_give isl_list *isl_list_concat(__isl_take isl_list *list1,
79 __isl_take isl_list *list2)
81 int i;
82 struct isl_list *list;
84 if (!list1 || !list2)
85 goto error;
87 list = isl_list_alloc(list1->ctx, list1->n + list2->n);
88 if (!list)
89 goto error;
91 for (i = 0; i < list1->n; ++i) {
92 list->obj[i].type = list1->obj[i].type;
93 list->obj[i].v = list1->obj[i].type->copy(list1->obj[i].v);
96 for (i = 0; i < list2->n; ++i) {
97 list->obj[list1->n + i].type = list2->obj[i].type;
98 list->obj[list1->n + i].v = list2->obj[i].type->copy(list2->obj[i].v);
101 isl_list_free(list1);
102 isl_list_free(list2);
104 return list;
105 error:
106 isl_list_free(list1);
107 isl_list_free(list2);
108 return NULL;
111 __isl_give isl_list *isl_list_add_obj(__isl_take isl_list *list,
112 __isl_take struct isl_obj obj)
114 struct isl_list *new_list;
116 if (!list || !obj.v)
117 goto error;
119 new_list = isl_realloc(list->ctx, list, struct isl_list,
120 sizeof(struct isl_list) +
121 (list->n + 1) * sizeof(struct isl_obj));
122 if (!new_list)
123 goto error;
125 list = new_list;
126 list->obj[list->n] = obj;
127 list->n++;
129 return list;
130 error:
131 obj.type->free(obj.v);
132 isl_list_free(list);
133 return NULL;
136 static void *isl_obj_list_add(void *v1, void *v2)
138 struct isl_list *list1 = (struct isl_list *)v1;
139 struct isl_list *list2 = (struct isl_list *)v2;
141 return isl_list_concat(list1, list2);
144 struct isl_obj_vtable isl_obj_list_vtable = {
145 isl_obj_list_copy,
146 isl_obj_list_add,
147 isl_obj_list_print,
148 isl_obj_list_free