isl_map_subtract.c: tab_add_constraints: avoid NULL pointer dereference
[isl.git] / isl_vec.c
blobd3c112619bf2598a668c0fc1b08c8a38fb26ea92
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 "isl_seq.h"
11 #include "isl_vec.h"
13 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
15 struct isl_vec *vec;
17 vec = isl_alloc_type(ctx, struct isl_vec);
18 if (!vec)
19 return NULL;
21 vec->block = isl_blk_alloc(ctx, size);
22 if (isl_blk_is_error(vec->block))
23 goto error;
25 vec->ctx = ctx;
26 isl_ctx_ref(ctx);
27 vec->ref = 1;
28 vec->size = size;
29 vec->el = vec->block.data;
31 return vec;
32 error:
33 isl_blk_free(ctx, vec->block);
34 return NULL;
37 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
39 if (!vec)
40 return NULL;
42 vec->ref++;
43 return vec;
46 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
48 struct isl_vec *vec2;
50 if (!vec)
51 return NULL;
52 vec2 = isl_vec_alloc(vec->ctx, vec->size);
53 isl_seq_cpy(vec2->el, vec->el, vec->size);
54 return vec2;
57 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
59 struct isl_vec *vec2;
60 if (!vec)
61 return NULL;
63 if (vec->ref == 1)
64 return vec;
66 vec2 = isl_vec_dup(vec);
67 isl_vec_free(vec);
68 return vec2;
71 void isl_vec_free(struct isl_vec *vec)
73 if (!vec)
74 return;
76 if (--vec->ref > 0)
77 return;
79 isl_ctx_deref(vec->ctx);
80 isl_blk_free(vec->ctx, vec->block);
81 free(vec);
84 void isl_vec_dump(struct isl_vec *vec, FILE *out, int indent)
86 int i;
88 if (!vec) {
89 fprintf(out, "%*snull vec\n", indent, "");
90 return;
93 fprintf(out, "%*s[", indent, "");
94 for (i = 0; i < vec->size; ++i) {
95 if (i)
96 fprintf(out, ",");
97 isl_int_print(out, vec->el[i], 0);
99 fprintf(out, "]\n");
102 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
104 isl_seq_lcm(vec->block.data, vec->size, lcm);
107 /* Given a rational vector, with the denominator in the first element
108 * of the vector, round up all coordinates.
110 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
112 vec = isl_vec_cow(vec);
113 if (!vec)
114 return NULL;
116 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
118 isl_int_set_si(vec->el[0], 1);
120 return vec;
123 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
125 if (!vec)
126 return NULL;
127 isl_seq_normalize(vec->ctx, vec->el, vec->size);
128 return vec;
131 __isl_give isl_vec *isl_vec_scale(__isl_take isl_vec *vec, isl_int m)
133 if (isl_int_is_one(m))
134 return vec;
135 vec = isl_vec_cow(vec);
136 if (!vec)
137 return NULL;
138 isl_seq_scale(vec->el, vec->el, m, vec->size);
139 return vec;
142 __isl_give isl_vec *isl_vec_add(__isl_take isl_vec *vec1,
143 __isl_take isl_vec *vec2)
145 vec1 = isl_vec_cow(vec1);
146 if (!vec1 || !vec2)
147 goto error;
149 isl_assert(vec1->ctx, vec1->size == vec2->size, goto error);
151 isl_seq_combine(vec1->el, vec1->ctx->one, vec1->el,
152 vec1->ctx->one, vec2->el, vec1->size);
154 isl_vec_free(vec2);
155 return vec1;
156 error:
157 isl_vec_free(vec1);
158 isl_vec_free(vec2);
159 return NULL;