isl_union_map_params: plug memory leak in case of empty input
[isl.git] / isl_obj.c
blob7eba67139913a0e18d89ed7203a1f0c084406f16
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <isl/set.h>
12 #include <isl/map.h>
13 #include <isl/polynomial.h>
14 #include <isl/obj.h>
16 struct isl_int_obj {
17 int ref;
18 isl_ctx *ctx;
19 isl_int v;
22 __isl_give isl_int_obj *isl_int_obj_alloc(isl_ctx *ctx, isl_int v)
24 isl_int_obj *i;
26 i = isl_alloc_type(ctx, isl_int_obj);
27 if (!i)
28 return NULL;
30 i->ctx = ctx;
31 isl_ctx_ref(ctx);
32 i->ref = 1;
33 isl_int_init(i->v);
34 isl_int_set(i->v, v);
36 return i;
39 __isl_give isl_int_obj *isl_int_obj_copy(__isl_keep isl_int_obj *i)
41 if (!i)
42 return NULL;
44 i->ref++;
45 return i;
48 __isl_give isl_int_obj *isl_int_obj_dup(__isl_keep isl_int_obj *i)
50 if (!i)
51 return NULL;
53 return isl_int_obj_alloc(i->ctx, i->v);
56 __isl_give isl_int_obj *isl_int_obj_cow(__isl_take isl_int_obj *i)
58 if (!i)
59 return NULL;
61 if (i->ref == 1)
62 return i;
63 i->ref--;
64 return isl_int_obj_dup(i);
67 void isl_int_obj_free(__isl_take isl_int_obj *i)
69 if (!i)
70 return;
72 if (--i->ref > 0)
73 return;
75 isl_ctx_deref(i->ctx);
76 isl_int_clear(i->v);
77 free(i);
80 __isl_give isl_int_obj *isl_int_obj_add(__isl_take isl_int_obj *i1,
81 __isl_take isl_int_obj *i2)
83 i1 = isl_int_obj_cow(i1);
84 if (!i1 || !i2)
85 goto error;
87 isl_int_add(i1->v, i1->v, i2->v);
89 isl_int_obj_free(i2);
90 return i1;
91 error:
92 isl_int_obj_free(i1);
93 isl_int_obj_free(i2);
94 return NULL;
97 __isl_give isl_int_obj *isl_int_obj_sub(__isl_take isl_int_obj *i1,
98 __isl_take isl_int_obj *i2)
100 i1 = isl_int_obj_cow(i1);
101 if (!i1 || !i2)
102 goto error;
104 isl_int_sub(i1->v, i1->v, i2->v);
106 isl_int_obj_free(i2);
107 return i1;
108 error:
109 isl_int_obj_free(i1);
110 isl_int_obj_free(i2);
111 return NULL;
114 __isl_give isl_int_obj *isl_int_obj_mul(__isl_take isl_int_obj *i1,
115 __isl_take isl_int_obj *i2)
117 i1 = isl_int_obj_cow(i1);
118 if (!i1 || !i2)
119 goto error;
121 isl_int_mul(i1->v, i1->v, i2->v);
123 isl_int_obj_free(i2);
124 return i1;
125 error:
126 isl_int_obj_free(i1);
127 isl_int_obj_free(i2);
128 return NULL;
131 void isl_int_obj_get_int(__isl_keep isl_int_obj *i, isl_int *v)
133 if (!i)
134 return;
135 isl_int_set(*v, i->v);
138 static void *isl_obj_int_copy(void *v)
140 return isl_int_obj_copy((isl_int_obj *)v);
143 static void isl_obj_int_free(void *v)
145 isl_int_obj_free((isl_int_obj *)v);
148 static __isl_give isl_printer *isl_obj_int_print(__isl_take isl_printer *p,
149 void *v)
151 isl_int_obj *i = v;
152 return isl_printer_print_isl_int(p, i->v);
155 static void *isl_obj_int_add(void *v1, void *v2)
157 return isl_int_obj_add((isl_int_obj *)v1, (isl_int_obj *)v2);
160 struct isl_obj_vtable isl_obj_int_vtable = {
161 isl_obj_int_copy,
162 isl_obj_int_add,
163 isl_obj_int_print,
164 isl_obj_int_free
167 static void *isl_obj_val_copy(void *v)
169 return isl_val_copy((isl_val *)v);
172 static void isl_obj_val_free(void *v)
174 isl_val_free((isl_val *)v);
177 static __isl_give isl_printer *isl_obj_val_print(__isl_take isl_printer *p,
178 void *v)
180 return isl_printer_print_val(p, (isl_val *)v);
183 static void *isl_obj_val_add(void *v1, void *v2)
185 return isl_val_add((isl_val *) v1, (isl_val *) v2);
188 struct isl_obj_vtable isl_obj_val_vtable = {
189 isl_obj_val_copy,
190 isl_obj_val_add,
191 isl_obj_val_print,
192 isl_obj_val_free
195 static void *isl_obj_map_copy(void *v)
197 return isl_map_copy((struct isl_map *)v);
200 static void isl_obj_map_free(void *v)
202 isl_map_free((struct isl_map *)v);
205 static __isl_give isl_printer *isl_obj_map_print(__isl_take isl_printer *p,
206 void *v)
208 return isl_printer_print_map(p, (struct isl_map *)v);
211 static void *isl_obj_map_add(void *v1, void *v2)
213 return isl_map_union((struct isl_map *)v1, (struct isl_map *)v2);
216 struct isl_obj_vtable isl_obj_map_vtable = {
217 isl_obj_map_copy,
218 isl_obj_map_add,
219 isl_obj_map_print,
220 isl_obj_map_free
223 static void *isl_obj_union_map_copy(void *v)
225 return isl_union_map_copy((isl_union_map *)v);
228 static void isl_obj_union_map_free(void *v)
230 isl_union_map_free((isl_union_map *)v);
233 static __isl_give isl_printer *isl_obj_union_map_print(__isl_take isl_printer *p,
234 void *v)
236 return isl_printer_print_union_map(p, (isl_union_map *)v);
239 static void *isl_obj_union_map_add(void *v1, void *v2)
241 return isl_union_map_union((isl_union_map *)v1, (isl_union_map *)v2);
244 struct isl_obj_vtable isl_obj_union_map_vtable = {
245 isl_obj_union_map_copy,
246 isl_obj_union_map_add,
247 isl_obj_union_map_print,
248 isl_obj_union_map_free
251 static void *isl_obj_set_copy(void *v)
253 return isl_set_copy((struct isl_set *)v);
256 static void isl_obj_set_free(void *v)
258 isl_set_free((struct isl_set *)v);
261 static __isl_give isl_printer *isl_obj_set_print(__isl_take isl_printer *p,
262 void *v)
264 return isl_printer_print_set(p, (struct isl_set *)v);
267 static void *isl_obj_set_add(void *v1, void *v2)
269 return isl_set_union((struct isl_set *)v1, (struct isl_set *)v2);
272 struct isl_obj_vtable isl_obj_set_vtable = {
273 isl_obj_set_copy,
274 isl_obj_set_add,
275 isl_obj_set_print,
276 isl_obj_set_free
279 static void *isl_obj_union_set_copy(void *v)
281 return isl_union_set_copy((isl_union_set *)v);
284 static void isl_obj_union_set_free(void *v)
286 isl_union_set_free((isl_union_set *)v);
289 static __isl_give isl_printer *isl_obj_union_set_print(__isl_take isl_printer *p,
290 void *v)
292 return isl_printer_print_union_set(p, (isl_union_set *)v);
295 static void *isl_obj_union_set_add(void *v1, void *v2)
297 return isl_union_set_union((isl_union_set *)v1, (isl_union_set *)v2);
300 struct isl_obj_vtable isl_obj_union_set_vtable = {
301 isl_obj_union_set_copy,
302 isl_obj_union_set_add,
303 isl_obj_union_set_print,
304 isl_obj_union_set_free
307 static void *isl_obj_none_copy(void *v)
309 return v;
312 static void isl_obj_none_free(void *v)
316 static __isl_give isl_printer *isl_obj_none_print(__isl_take isl_printer *p,
317 void *v)
319 return p;
322 static void *isl_obj_none_add(void *v1, void *v2)
324 return NULL;
327 struct isl_obj_vtable isl_obj_none_vtable = {
328 isl_obj_none_copy,
329 isl_obj_none_add,
330 isl_obj_none_print,
331 isl_obj_none_free
334 static void *isl_obj_pw_qp_copy(void *v)
336 return isl_pw_qpolynomial_copy((struct isl_pw_qpolynomial *)v);
339 static void isl_obj_pw_qp_free(void *v)
341 isl_pw_qpolynomial_free((struct isl_pw_qpolynomial *)v);
344 static __isl_give isl_printer *isl_obj_pw_qp_print(__isl_take isl_printer *p,
345 void *v)
347 return isl_printer_print_pw_qpolynomial(p,
348 (struct isl_pw_qpolynomial *)v);
351 static void *isl_obj_pw_qp_add(void *v1, void *v2)
353 return isl_pw_qpolynomial_add((struct isl_pw_qpolynomial *)v1,
354 (struct isl_pw_qpolynomial *)v2);
357 struct isl_obj_vtable isl_obj_pw_qpolynomial_vtable = {
358 isl_obj_pw_qp_copy,
359 isl_obj_pw_qp_add,
360 isl_obj_pw_qp_print,
361 isl_obj_pw_qp_free
364 static void *isl_obj_union_pw_qp_copy(void *v)
366 return isl_union_pw_qpolynomial_copy((struct isl_union_pw_qpolynomial *)v);
369 static void isl_obj_union_pw_qp_free(void *v)
371 isl_union_pw_qpolynomial_free((struct isl_union_pw_qpolynomial *)v);
374 static __isl_give isl_printer *isl_obj_union_pw_qp_print(
375 __isl_take isl_printer *p, void *v)
377 return isl_printer_print_union_pw_qpolynomial(p,
378 (struct isl_union_pw_qpolynomial *)v);
381 static void *isl_obj_union_pw_qp_add(void *v1, void *v2)
383 return isl_union_pw_qpolynomial_add(
384 (struct isl_union_pw_qpolynomial *)v1,
385 (struct isl_union_pw_qpolynomial *)v2);
388 struct isl_obj_vtable isl_obj_union_pw_qpolynomial_vtable = {
389 isl_obj_union_pw_qp_copy,
390 isl_obj_union_pw_qp_add,
391 isl_obj_union_pw_qp_print,
392 isl_obj_union_pw_qp_free
395 static void *isl_obj_pw_qpf_copy(void *v)
397 return isl_pw_qpolynomial_fold_copy((struct isl_pw_qpolynomial_fold *)v);
400 static void isl_obj_pw_qpf_free(void *v)
402 isl_pw_qpolynomial_fold_free((struct isl_pw_qpolynomial_fold *)v);
405 static __isl_give isl_printer *isl_obj_pw_qpf_print(__isl_take isl_printer *p,
406 void *v)
408 return isl_printer_print_pw_qpolynomial_fold(p,
409 (struct isl_pw_qpolynomial_fold *)v);
412 static void *isl_obj_pw_qpf_add(void *v1, void *v2)
414 return isl_pw_qpolynomial_fold_fold((struct isl_pw_qpolynomial_fold *)v1,
415 (struct isl_pw_qpolynomial_fold *)v2);
418 struct isl_obj_vtable isl_obj_pw_qpolynomial_fold_vtable = {
419 isl_obj_pw_qpf_copy,
420 isl_obj_pw_qpf_add,
421 isl_obj_pw_qpf_print,
422 isl_obj_pw_qpf_free
425 static void *isl_obj_union_pw_qpf_copy(void *v)
427 return isl_union_pw_qpolynomial_fold_copy((struct isl_union_pw_qpolynomial_fold *)v);
430 static void isl_obj_union_pw_qpf_free(void *v)
432 isl_union_pw_qpolynomial_fold_free((struct isl_union_pw_qpolynomial_fold *)v);
435 static __isl_give isl_printer *isl_obj_union_pw_qpf_print(
436 __isl_take isl_printer *p, void *v)
438 return isl_printer_print_union_pw_qpolynomial_fold(p,
439 (struct isl_union_pw_qpolynomial_fold *)v);
442 static void *isl_obj_union_pw_qpf_add(void *v1, void *v2)
444 return isl_union_pw_qpolynomial_fold_fold(
445 (struct isl_union_pw_qpolynomial_fold *)v1,
446 (struct isl_union_pw_qpolynomial_fold *)v2);
449 struct isl_obj_vtable isl_obj_union_pw_qpolynomial_fold_vtable = {
450 isl_obj_union_pw_qpf_copy,
451 isl_obj_union_pw_qpf_add,
452 isl_obj_union_pw_qpf_print,
453 isl_obj_union_pw_qpf_free