Solve a memory leak for cloned scops
[openscop.git] / source / vector.c
blobecc2d54e41611dfbf803070a3e96cd1644442a55
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** vector.c **
6 **-----------------------------------------------------------------**
7 ** First version: 30/04/2008 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29 * *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31 * *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
35 * are met: *
36 * *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
44 * *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
55 * *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60 * *
61 *****************************************************************************/
64 # include <stdlib.h>
65 # include <stdio.h>
66 # include <ctype.h>
67 # include <openscop/vector.h>
70 /*+***************************************************************************
71 * Structure display function *
72 *****************************************************************************/
75 /**
76 * openscop_vector_idump function:
77 * Displays a openscop_vector_t structure (*vector) into a file (file, possibly
78 * stdout) in a way that trends to be understandable without falling in a deep
79 * depression or, for the lucky ones, getting a headache... It includes an
80 * indentation level (level) in order to work with others print_structure
81 * functions.
82 * \param[in] file File where informations are printed.
83 * \param[in] vector The vector whose information have to be printed.
84 * \param[in] level Number of spaces before printing, for each line.
86 void openscop_vector_idump(FILE * file, openscop_vector_p vector, int level) {
87 int j;
89 if (vector != NULL) {
90 // Go to the right level.
91 for (j = 0; j < level; j++)
92 fprintf(file,"|\t");
93 fprintf(file,"+-- openscop_vector_t (");
94 openscop_int_dump_precision(file, vector->precision);
95 fprintf(file, ")\n");
97 for (j = 0; j <= level; j++)
98 fprintf(file,"|\t");
99 fprintf(file,"%d\n", vector->size);
101 // Display the vector.
102 for (j = 0; j <= level; j++)
103 fprintf(file, "|\t");
105 fprintf(file, "[ ");
107 for (j = 0; j < vector->size; j++) {
108 openscop_int_print(file, vector->precision, vector->v, j);
109 fprintf(file, " ");
112 fprintf(file, "]\n");
114 else {
115 // Go to the right level.
116 for (j = 0; j < level; j++)
117 fprintf(file, "|\t");
118 fprintf(file, "+-- NULL vector\n");
121 // The last line.
122 for (j = 0; j <= level; j++)
123 fprintf(file, "|\t");
124 fprintf(file, "\n");
129 * openscop_vector_dump function:
130 * This function prints the content of a openscop_vector_t structure
131 * (*vector) into a file (file, possibly stdout).
132 * \param[in] file File where informations are printed.
133 * \param[in] vector The vector whose information have to be printed.
135 void openscop_vector_dump(FILE * file, openscop_vector_p vector) {
136 openscop_vector_idump(file, vector, 0);
140 /*+***************************************************************************
141 * Memory allocation/deallocation function *
142 *****************************************************************************/
146 * openscop_vector_pmalloc function:
147 * (precision malloc) this function allocates the memory space for an
148 * openscop_vector_t structure and sets its fields with default values. Then
149 * it returns a pointer to the allocated space.
150 * \param[in] precision The precision of the vector entries.
151 * \param[in] size The number of entries of the vector to allocate.
152 * \return A pointer to the newly allocated openscop_vector_t structure.
154 openscop_vector_p openscop_vector_pmalloc(int precision, int size) {
155 openscop_vector_p vector;
156 int i;
158 OPENSCOP_malloc(vector, openscop_vector_p, sizeof(openscop_vector_t));
159 vector->size = size;
160 vector->precision = precision;
161 if (size == 0) {
162 vector->v = NULL;
164 else {
165 OPENSCOP_malloc(vector->v, void *, size * openscop_int_sizeof(precision));
166 for (i = 0; i < size; i++)
167 openscop_int_init_set_si(precision, vector->v, i, 0);
169 return vector;
174 * openscop_vector_malloc function:
175 * This function allocates the memory space for a openscop_vector_t structure
176 * and sets its fields with default values. Then it returns a pointer to the
177 * allocated space. The precision of the vector elements corresponds to the
178 * precision environment variable or to the highest available precision if it
179 * is not defined.
180 * \param[in] size The number of entries of the vector to allocate.
181 * \return A pointer to the newly allocated openscop_vector_t structure.
183 openscop_vector_p openscop_vector_malloc(int size) {
184 int precision = openscop_util_get_precision();
185 return openscop_vector_pmalloc(precision, size);
190 * openscop_vector_free function:
191 * This function frees the allocated memory for a openscop_vector_t structure.
192 * \param[in] vector The pointer to the vector we want to free.
194 void openscop_vector_free(openscop_vector_p vector) {
195 int i;
197 if (vector != NULL) {
198 if (vector->v != NULL) {
199 for (i = 0; i < vector->size; i++)
200 openscop_int_clear(vector->precision, vector->v, i);
202 free(vector->v);
204 free(vector);
209 /*+***************************************************************************
210 * Processing functions *
211 *****************************************************************************/
215 * openscop_vector_add_scalar function:
216 * This function adds a scalar to the vector representation of an affine
217 * expression (this means we add the scalar only to the very last entry of the
218 * vector). It returns a new vector resulting from this addition.
219 * \param[in] vector The basis vector.
220 * \param[in] scalar The scalar to add to the vector.
221 * \return A pointer to a new vector, copy of the basis one plus the scalar.
223 openscop_vector_p openscop_vector_add_scalar(openscop_vector_p vector,
224 int scalar) {
225 int i, precision, last;
226 openscop_vector_p result;
228 if ((vector == NULL) || (vector->size < 2))
229 OPENSCOP_error("incompatible vector for addition");
231 precision = vector->precision;
232 last = vector->size - 1;
234 result = openscop_vector_pmalloc(precision, vector->size);
235 for (i = 0; i < vector->size; i++)
236 openscop_int_assign(precision, result->v, i, vector->v, i);
237 openscop_int_add_ui(precision, result->v, last, vector->v, last, scalar);
239 return result;
244 * openscop_vector_add function:
245 * This function achieves the addition of two vectors and returns the
246 * result as a new vector (the addition means the ith entry of the new vector
247 * is equal to the ith entry of vector v1 plus the ith entry of vector v2).
248 * \param v1 The first vector for the addition.
249 * \param v2 The second vector for the addition.
250 * \return A pointer to a new vector, corresponding to v1 + v2.
252 openscop_vector_p openscop_vector_add(openscop_vector_p v1,
253 openscop_vector_p v2) {
254 int i;
255 openscop_vector_p v3;
257 if ((v1 == NULL) || (v2 == NULL) ||
258 (v1->size != v2->size) || (v1->precision != v2->precision))
259 OPENSCOP_error("incompatible vectors for addition");
261 v3 = openscop_vector_pmalloc(v1->precision, v1->size);
262 for (i = 0; i < v1->size; i++)
263 openscop_int_add(v1->precision, v3->v, i, v1->v, i, v2->v, i);
265 return v3;
270 * openscop_vector_sub function:
271 * This function achieves the subtraction of two vectors and returns the
272 * result as a new vector (the addition means the ith entry of the new vector
273 * is equal to the ith entry of vector v1 minus the ith entry of vector v2).
274 * \param v1 The first vector for the subtraction.
275 * \param v2 The second vector for the subtraction (result is v1-v2).
276 * \return A pointer to a new vector, corresponding to v1 - v2.
278 openscop_vector_p openscop_vector_sub(openscop_vector_p v1,
279 openscop_vector_p v2) {
280 int i;
281 openscop_vector_p v3;
283 if ((v1 == NULL) || (v2 == NULL) ||
284 (v1->size != v2->size) || (v1->precision != v2->precision))
285 OPENSCOP_error("incompatible vectors for subtraction");
287 v3 = openscop_vector_pmalloc(v1->precision, v1->size);
288 for (i = 0; i < v1->size; i++)
289 openscop_int_sub(v1->precision, v3->v, i, v1->v, i, v2->v, i);
291 return v3;
296 * openscop_vector_tag_inequality function:
297 * This function tags a vector representation of a contraint as being an
298 * inequality >=0. This means in the PolyLib format, to set to 1 the very
299 * first entry of the vector. It modifies directly the vector provided as
300 * an argument.
301 * \param vector The vector to be tagged.
303 void openscop_vector_tag_inequality(openscop_vector_p vector) {
304 if ((vector == NULL) || (vector->size < 1))
305 OPENSCOP_error("vector cannot be tagged");
306 openscop_int_set_si(vector->precision, vector->v, 0, 1);
311 * openscop_vector_tag_equality function:
312 * This function tags a vector representation of a contraint as being an
313 * equality ==0. This means in the PolyLib format, to set to 0 the very
314 * first entry of the vector. It modifies directly the vector provided as
315 * an argument.
316 * \param vector The vector to be tagged.
318 void openscop_vector_tag_equality(openscop_vector_p vector) {
319 if ((vector == NULL) || (vector->size < 1))
320 OPENSCOP_error("vector cannot be tagged");
321 openscop_int_set_si(vector->precision, vector->v, 0, 0);
326 * openscop_vector_equal function:
327 * this function returns true if the two vectors are the same, false
328 * otherwise.
329 * \param v1 The first vector.
330 * \param v2 The second vector.
331 * \return 1 if v1 and v2 are the same (content-wise), 0 otherwise.
333 int openscop_vector_equal(openscop_vector_p v1, openscop_vector_p v2) {
334 int i;
336 if (v1 == v2)
337 return 1;
339 if ((v1->size != v2->size) || (v1->precision != v2->precision))
340 return 0;
342 for (i = 0; i < v1->size; i++)
343 if (openscop_int_ne(v1->precision, v1->v, i, v2->v, i))
344 return 0;
346 return 1;
351 * openscop_vector_mul_scalar function:
352 * this function returns a new vector corresponding to the one provided
353 * as parameter with each entry multiplied by a scalar.
354 * \param v The vector to multiply.
355 * \param scalar The scalar coefficient.
356 * \return A new vector corresponding to scalar * v.
358 openscop_vector_p openscop_vector_mul_scalar(openscop_vector_p v,
359 int scalar) {
360 int i;
361 openscop_vector_p result = openscop_vector_pmalloc(v->precision, v->size);
363 for(i = 0; i < v->size; i++)
364 openscop_int_mul_si(v->precision, result->v, i, v->v, i, scalar);
366 return result;