PIPS
alloc.c
Go to the documentation of this file.
1 /*
2 
3  $Id: alloc.c 1641 2016-03-02 08:20:19Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of Linear/C3 Library.
8 
9  Linear/C3 Library is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  Linear/C3 Library is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with Linear/C3 Library. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 
25  /*
26  * CREATION, COPIE ET DESTRUCTION D'UN VECTEUR
27  */
28 
29 /*LINTLIBRARY*/
30 #ifdef HAVE_CONFIG_H
31  #include "config.h"
32 #endif
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 
38 #include "boolean.h"
39 #include "arithmetique.h"
40 #include "vecteur.h"
41 
42 #define MALLOC(s,t,f) malloc(s)
43 #define FREE(p,t,f) free(p)
44 
45 /* Pvecteur vect_dup(Pvecteur v_in): duplication du vecteur v_in; allocation de
46  * et copie dans v_out;
47  *
48  * allocate v_out;
49  * v_out := v_in;
50  */
52 Pvecteur v_in;
53 {
54  Pvecteur v_out;
55  Pvecteur v;
56 
57  v_out = NULL;
58  for(v=v_in; v!=NULL; v=v->succ) {
59  v_out = vect_chain(v_out,var_of(v),val_of(v));
60  }
61 
62  return v_out;
63 }
64 
65 /* void vect_rm(Pvecteur v): desallocation des couples de v;
66  *
67  * Attention! La procedure appelante doit penser a modifier la
68  * valeur de v apres l'appel pour ne pas pointer dans le vide:
69  * vect_rm(v);
70  * v = NULL;
71  *
72  * Il vaudrait mieux que vect_rm retourne NULL et qu'on puisse
73  * ecrire:
74  * v = vect_rm(v);
75  * ou que vect_rm prenne un Pvecteur * en argument:
76  * vect_rm(&v);
77  */
78 void vect_rm(v)
79 Pvecteur v;
80 {
81  Pvecteur elem = NULL;
82  Pvecteur nelem = NULL;
83 
84  for(elem = v; elem!=NULL; elem = nelem) {
85  nelem = elem->succ;
86  free((char *)elem);
87  }
88  /*
89  while (v != NULL) {
90  Pvecteur nv = v->succ;
91  free((char *) v);
92  v = nv;
93  }
94  */
95 }
96 
97 /* Pvecteur vect_new(Variable var,Value coeff):
98  * allocation d'un vecteur colineaire
99  * au vecteur de base var et de coefficient coeff (i.e. creation
100  * d'un nouveau vecteur ne comportant qu'un seul couple (var,coeff))
101  *
102  * -->
103  * coeff var
104  *
105  * Pourrait etre remplace par un vect_chain(NULL,,)
106  *
107  * Modifications:
108  * - a 0 coeff generates a null vector; Francois Irigoin, 26 March 1991
109  */
111 {
112  Pvecteur v;
113 
114  if(coeff!=0) {
115  v = (Pvecteur) MALLOC(sizeof(Svecteur),VECTEUR,"vect_new");
116  if (v == NULL) {
117  (void) fprintf(stderr,"vect_new: Out of memory space\n");
118  /* fprintf(stderr, "%10.3f MB",
119  (sbrk(0) - etext)/(double)(1 << 20)); // not portable */
120  abort();
121  /*exit(-1);*/
122  }
123  var_of(v) = var;
124  val_of(v) = coeff;
125  v->succ = NULL;
126  }
127  else
128  v = NULL;
129 
130  return v;
131 }
132 
133 /* void dbg_vect_rm(Pvecteur v, char * f): desallocation d'un vecteur
134  * avec marquage de la fonction provoquant la desallocation
135  *
136  * Apparemment obsolete.
137  */
138 /*ARGSUSED*/
140  char __attribute__((unused)) *f)
141 {
142  Pvecteur v1,v2;
143  v1 = v;
144  while (v1!=NULL) {
145  v2 = v1->succ;
146  FREE((char *)v1,VECTEUR,f);
147  v1 = v2;
148  }
149 }
150 
151 /* Pvecteur vect_make(v, [var, val,]* 0, val)
152  * Pvecteur v; // may be NULL, use assigne anyway
153  * Variable var;
154  * Value val;
155  *
156  * Builds a vector from the list of arguments, by successive additions.
157  * ends when a 0 Variable (that is TCST!) is encountered.
158  *
159  * Because of the var val order, this function cannot be called directly
160  * with a va_list, but (va_list, 0) should be used, since the val argument
161  * is expected, read and used anyway.
162  *
163  * CAUTION: the initial vector is modified by the process!
164  */
166 {
167  va_list the_args;
168 
169  // handle fist argument
170  vect_add_elem(&v, var, val);
171 
172  // get others
173  va_start(the_args, val);
174 
175  while (var != (Variable) 0)
176  {
177  var = va_arg(the_args, Variable);
178  val = va_arg(the_args, Value);
179  vect_add_elem(&v, var, val);
180  }
181 
182  va_end (the_args);
183  return v;
184 }
185 
186 /* Allocate a new vector v whose coefficient are given by the list of
187  values ad whose dimension is given by b. The number of constant
188  values passed as argument is supposed to be equal to the dimension
189  of b.
190 
191  Note: 0 is a normal value. I see no way to mark the last argument.
192 
193  FI: I add this function to check under gdb that a given point
194  belongs to a constraint system. The manual verification is tedious
195  and error prone. This is done for debugging in Linear/C3 Library the
196  linked_regions bug.
197  */
199 {
200  va_list the_args;
201  Pvecteur v = VECTEUR_NUL;
202  Variable var;
203  int dim = (int) base_dimension(b);
204  int i = 0; // current dimension
205 
206  /* handle first argument - the first element of a basis has rank 1 */
207  var = variable_of_rank(b,1);
208  vect_add_elem(&v, var, val);
209 
210  /* get others */
211  va_start (the_args, val);
212 
213  for(i=2;i<=dim;i++) // i <= dim because of the way dimensions are counted
214  {
215  var = variable_of_rank(b,i);
216  val = va_arg(the_args, Value);
217  vect_add_elem(&v, var, val);
218  }
219 
220  va_end (the_args);
221 
222  return v;
223 }
224 
225 /* Generate a sparse vector a x + b TCST */
227 {
228  Pvecteur v = vect_new(x, a);
229  vect_add_elem(&v, TCST, b);
230  return v;
231 }
232 
233 /* direct duplication.
234  * vect_dup() and vect_reversal() do the same thing :
235  * duplicate the vector with the reversal order.
236  * vect_copy duplicate the vector with the same order.
237  * in use of sc_copy. (DN,24/6/02)
238  * Does not change parameter b (DN,28/06/02)
239  */
241 {
242  Pvecteur n = VECTEUR_NUL, p = VECTEUR_NUL, r = VECTEUR_NUL, tmp = b;
243 
244  for (; tmp!=VECTEUR_NUL; tmp=tmp->succ)
245  {
246  n = (Pvecteur) MALLOC(sizeof(Svecteur),VECTEUR,"vect_copy");
247  if (n == NULL) {
248  fprintf(stderr,"[vect_copy] out of memory space\n");
249  abort();
250  }
251  var_of(n) = var_of(tmp);
252  val_of(n) = val_of(tmp);
253  n->succ = NULL;
254  if (r==VECTEUR_NUL) r = n;
255  if (p!=VECTEUR_NUL) p->succ = n;
256  p = n;
257  }
258 
259  return r;
260 }
261 
262 /* Pbase base_dup(Pbase b)
263  * Note: this function changes the value of the pointer.
264  * Use base_copy instead. Should become a link, not a function.
265  * For the moment, it's a function, because of the sc.h cannot be updated
266  * without installation, due to decision of integration of Janus or not? DN 12/5/03
267  */
269 {
270  /* return base_copy(b);*/
271 
272  Pbase n = BASE_NULLE, p = BASE_NULLE, r = BASE_NULLE;
273 
274  for (; b!=BASE_NULLE; b=b->succ)
275  {
276  n = (Pvecteur) MALLOC(sizeof(Svecteur),VECTEUR,"base_dup");
277  if (n == NULL) {
278  fprintf(stderr,"[base_dup] out of memory space\n");
279  abort();
280  }
281  var_of(n) = var_of(b);
282  val_of(n) = VALUE_ONE;
283  n->succ = NULL;
284  if (r==BASE_NULLE) r = n;
285  if (p!=BASE_NULLE) p->succ = n;
286  p = n;
287  }
288  return r;
289 }
290 
291 
292 
293 /* Direct duplication. The initial Pbase is assumed to be valid.
294  * Absolutely the same with base_dup, but base_up is the only function
295  * that maintains the old order.
296  * So recopy here for use with copy version including
297  * vect_copy, contrainte_copy, contraintes_copy, sc_copy (DN,24/6/02)
298  * Does not change the parameter. Did have a look at all copy version (DN,1/7/2002)
299  */
301 {
302  Pbase n = BASE_NULLE, p = BASE_NULLE, r = BASE_NULLE, tmp = b;
303 
304  for (; tmp!=BASE_NULLE; tmp=tmp->succ)
305  {
306  n = (Pvecteur) MALLOC(sizeof(Svecteur),VECTEUR,"base_copy");
307  if (n == NULL) {
308  fprintf(stderr,"[base_copy] out of memory space\n");
309  abort();
310  }
311  var_of(n) = var_of(tmp);
312  val_of(n) = VALUE_ONE;
313  n->succ = NULL;
314  if (r==BASE_NULLE) r = n;
315  if (p!=BASE_NULLE) p->succ = n;
316  p = n;
317  }
318 
319  return r;
320 }
321 
float a2sf[2] __attribute__((aligned(16)))
USER generates a user error (i.e., non fatal) by printing the given MSG according to the FMT.
Definition: 3dnow.h:3
void const char const char const int
int Value
#define VALUE_ONE
void free(void *)
#define abort()
Definition: misc-local.h:53
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
Pvecteur vect_chain(Pvecteur v_in, Variable var, Value coeff)
package vecteur routines internes au package
Definition: private.c:69
Variable variable_of_rank()
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
static char * x
Definition: split_file.c:159
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
struct Svecteur * succ
Definition: vecteur-local.h:92
#define TCST
VARIABLE REPRESENTANT LE TERME CONSTANT.
#define val_of(varval)
#define VECTEUR_NUL
DEFINITION DU VECTEUR NUL.
struct Svecteur * Pvecteur
void * Variable
arithmetique is a requirement for vecteur, but I do not want to inforce it in all pips files....
Definition: vecteur-local.h:60
#define var_of(varval)
#define base_dimension(b)
#define BASE_NULLE
MACROS SUR LES BASES.
#define VECTEUR
package sur les vecteurs creux et les bases
Definition: vecteur-local.h:51
#define MALLOC(s, t, f)
INTLIBRARY.
Definition: alloc.c:42
Pvecteur vect_make(Pvecteur v, Variable var, Value val,...)
Pvecteur vect_make(v, [var, val,]* 0, val) Pvecteur v; // may be NULL, use assigne anyway Variable va...
Definition: alloc.c:165
Pbase vect_copy(Pvecteur b)
direct duplication.
Definition: alloc.c:240
Pvecteur vect_dup(Pvecteur v_in)
Pvecteur vect_dup(Pvecteur v_in): duplication du vecteur v_in; allocation de et copie dans v_out;.
Definition: alloc.c:51
Pbase base_dup(Pbase b)
Pbase base_dup(Pbase b) Note: this function changes the value of the pointer.
Definition: alloc.c:268
#define FREE(p, t, f)
Definition: alloc.c:43
void dbg_vect_rm(Pvecteur v, char __attribute__((unused)) *f)
void dbg_vect_rm(Pvecteur v, char * f): desallocation d'un vecteur avec marquage de la fonction provo...
Definition: alloc.c:139
Pvecteur vect_make_1D(Value a, Variable x, Value b)
Generate a sparse vector a x + b TCST.
Definition: alloc.c:226
Pvecteur vect_new(Variable var, Value coeff)
Pvecteur vect_new(Variable var,Value coeff): allocation d'un vecteur colineaire au vecteur de base va...
Definition: alloc.c:110
Pvecteur vect_make_dense(Pbase b, Value val,...)
Allocate a new vector v whose coefficient are given by the list of values ad whose dimension is given...
Definition: alloc.c:198
Pbase base_copy(Pbase b)
Direct duplication.
Definition: alloc.c:300
void vect_rm(Pvecteur v)
void vect_rm(Pvecteur v): desallocation des couples de v;
Definition: alloc.c:78
void vect_add_elem(Pvecteur *pvect, Variable var, Value val)
void vect_add_elem(Pvecteur * pvect, Variable var, Value val): addition d'un vecteur colineaire au ve...
Definition: unaires.c:72