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  /* package contrainte - allocations et desallocations */
26 
27 /*LINTLIBRARY*/
28 
29 #ifdef HAVE_CONFIG_H
30  #include "config.h"
31 #endif
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <stdarg.h>
36 
37 #include "boolean.h"
38 #include "arithmetique.h"
39 #include "vecteur.h"
40 #include "contrainte.h"
41 
42 /* Pcontrainte contrainte_new(): allocation et initialisation d'une contrainte
43  * vide
44  *
45  * Anciens noms: init_eq(), creer_eq
46  */
48 {
49  Pcontrainte c;
50 
51  /*c = (Pcontrainte)MALLOC(sizeof (Scontrainte),CONTRAINTE,
52  "contrainte_new");*/
53  c = (Pcontrainte) malloc(sizeof (Scontrainte));
54  if (c == NULL) {
55  (void) fprintf(stderr,"contrainte_new: Out of memory space\n");
56  exit(-1);
57  }
58  c->eq_sat = NULL;
59  c->s_sat = NULL;
60  c->r_sat = NULL;
61  c->vecteur = NULL;
62  c->succ = NULL;
63 
64  return c;
65 }
66 
67 /* Pcontrainte contrainte_make(Pvecteur pv): allocation et
68  * initialisation d'une contrainte avec un vecteur passe en parametre.
69  *
70  * Modifications:
71  * - le signe du terme constant n'est plus modifie (FI, 24/11/89)
72  */
74 {
76  contrainte_vecteur(c) = pv;
77  return(c);
78 }
79 
80 /* Generate a constraint a x <= b or a x >= b, according to less_p, or
81  * ax==b, regardless of less_p.
82  *
83  * Since equalities and inequalities are not distinguished, less_p is
84  * not relevant when equations are built.
85  */
87 {
89  if(less_p)
90  v = vect_make_1D(a, x, value_uminus(b));
91  else
92  v = vect_make_1D(value_uminus(a), x, b);
94  return c;
95 }
96 
97 
98 /* Convert a list of vectors into a list of constraints */
100 {
101  va_list the_args;
102 
104  Pcontrainte lc = c;
105  contrainte_vecteur(c) = pv;
106 
107  va_start(the_args, pv);
108  Pvecteur nv = pv;
109  while(nv!=VECTEUR_NUL) {
110  nv = va_arg(the_args, Pvecteur);
112  contrainte_vecteur(nc) = nv;
113  contrainte_succ(lc) = nc;
114  lc = nc;
115  }
116 
117  return(c);
118 }
119 
120 /* Pcontrainte contrainte_dup(Pcontrainte c_in): allocation d'une contrainte
121  * c_out prenant la valeur de la contrainte c_in (i.e. duplication
122  * d'une contrainte); les tableaux de saturations ne sont pas recopies
123  * car on n'en connait pas la dimension. Le lien vers le successeur est
124  * aussi ignore pour ne pas cree de sharing intempestif.
125  *
126  * allocate c_out;
127  * c_out := c_in;
128  * return c_out;
129  *
130  * Ancien nom: eq_dup() et cp_eq()
131  */
133 {
134  Pcontrainte c_out = NULL;
135 
136  if(c_in!=NULL) {
137  c_out = contrainte_new();
138  c_out->vecteur = vect_dup(c_in->vecteur);
139  }
140  return c_out;
141 }
142 
143 /* Pcontrainte contraintes_dup(Pcontrainte c_in)
144  * a list of constraints is copied
145  */
147 {
149  c_tmp = contrainte_dup(c_in),
150  c_out = c_tmp,
151  c = NULL;
152 
153  for (c=(c_in==NULL?NULL:c_in->succ);
154  c!=NULL;
155  c=c->succ)
156  c_tmp->succ = contrainte_dup(c),
157  c_tmp = c_tmp->succ;
158 
159  return c_out;
160 }
161 
162 
163 /* Pcontrainte contrainte_free(Pcontrainte c): liberation de l'espace memoire
164  * alloue a la contrainte c ainsi que de ses champs vecteur et saturations;
165  * seul le lien vers la contrainte suivante est ignore.
166  *
167  * Utilisation standard:
168  * c = contrainte_free(c);
169  *
170  * Autre utilisation possible:
171  * (void) contrainte_free(c);
172  * c = NULL;
173  *
174  * comme toujours, les champs pointeurs sont remis a NULL avant la
175  * desallocation pour detecter au plus tot les erreurs dues a l'allocation
176  * dynamique de memoire.
177  *
178  * Modification:
179  * - renvoi systematique de CONTRAINTE_NULLE comme valeur de la fonction;
180  * ca permet de diminuer la taille du code utilisateur et d'assurer
181  * plus facilement la mise a CONTRAINTE_NULLE de pointeurs referencant
182  * une zone desallouee (FI, 24/11/89)
183  */
185 {
186  // Cannot be used at the contrainte level
187  //ifscdebug(1)
188  // fprintf(stderr, "Constraint %p is going to be freed\n", c);
189  if (!CONTRAINTE_UNDEFINED_P(c))
190  {
191  if (c->eq_sat != NULL) {
192  free((char *)c->eq_sat);
193  c->eq_sat = NULL;
194  }
195 
196  if (c->r_sat != NULL) {
197  free((char *)c->r_sat);
198  c->r_sat = NULL;
199  }
200 
201  if (c->s_sat != NULL) {
202  free((char *)c->s_sat);
203  c->s_sat = NULL;
204  }
205 
206  if (c->vecteur != VECTEUR_UNDEFINED) {
207  vect_rm(c->vecteur);
208  c->vecteur = NULL;
209  }
210 
211  c->succ = NULL;
212 
213  free((char *)c);
214  }
215 
216  return CONTRAINTE_UNDEFINED;
217 }
218 
219 /* Pcontrainte contraintes_free(Pcontrainte pc): desallocation de toutes les
220  * contraintes de la liste pc.
221  *
222  * chaque contrainte est detruite par un appel a contrainte_free.
223  *
224  * Ancien nom: elim_tte_ineg()
225  */
227 {
228  while (!CONTRAINTE_UNDEFINED_P(pc)) {
229  Pcontrainte pcs = pc->succ;
230  (void) contrainte_free(pc);
231  pc = pcs;
232  }
233  return CONTRAINTE_UNDEFINED;
234 }
235 
236 /* void dbg_contrainte_rm(Pcontrainte c): version debug de contrainte rm;
237  * trace de la desallocation et impression de la contrainte sur stdout
238  */
240 {
241  (void) printf("destruction de EQ dans %s\n",f);
242  /*print_eq(c);*/
243  dbg_vect_rm(c->vecteur,f);
244  /*FREE((char *)c,CONTRAINTE,f);*/
245  free((char *)c);
246 }
247 
248 /* Have a look at contrainte_dup and contraintes_dup which reverse the
249  * order of the list
250  * This copy version (including vect_copy, sc_copy) maintains the order
251  * (DN,24/6/02)
252  */
253 
255 {
256  Pcontrainte c_out = NULL;
257 
258  if(c_in!=NULL) {
259  c_out = contrainte_new();
260  c_out->vecteur = vect_copy(c_in->vecteur);
261  }
262  return c_out;
263 }
264 
265 /* Pcontrainte contraintes_copy(Pcontrainte c_in)
266  * a list of constraints is copied with the same order
267  * In fact, here we only need to replace contrainte_dup by contrainte_copy
268  * Have a look at contrainte_copy (DN,24/6/02)
269  */
271 {
273  c_tmp = contrainte_copy(c_in),
274  c_out = c_tmp,
275  c = NULL;
276 
277  for (c=(c_in==NULL?NULL:c_in->succ);
278  c!=NULL;
279  c=c->succ)
280  c_tmp->succ = contrainte_copy(c),
281  c_tmp = c_tmp->succ;
282 
283  return c_out;
284 }
#define value_uminus(val)
unary operators on values
int Value
FILE * c_in
Definition: c_syntax.h:291
FILE * c_out
#define CONTRAINTE_UNDEFINED_P(c)
#define contrainte_succ(c)
#define contrainte_vecteur(c)
passage au champ vecteur d'une contrainte "a la Newgen"
#define CONTRAINTE_UNDEFINED
struct Scontrainte * Pcontrainte
Pcontrainte contraintes_make(Pvecteur pv,...)
Convert a list of vectors into a list of constraints.
Definition: alloc.c:99
Pcontrainte contraintes_free(Pcontrainte pc)
Pcontrainte contraintes_free(Pcontrainte pc): desallocation de toutes les contraintes de la liste pc.
Definition: alloc.c:226
Pcontrainte contrainte_make(Pvecteur pv)
Pcontrainte contrainte_make(Pvecteur pv): allocation et initialisation d'une contrainte avec un vecte...
Definition: alloc.c:73
Pcontrainte contraintes_copy(Pcontrainte c_in)
Pcontrainte contraintes_copy(Pcontrainte c_in) a list of constraints is copied with the same order In...
Definition: alloc.c:270
Pcontrainte contrainte_free(Pcontrainte c)
Pcontrainte contrainte_free(Pcontrainte c): liberation de l'espace memoire alloue a la contrainte c a...
Definition: alloc.c:184
Pcontrainte contrainte_dup(Pcontrainte c_in)
Pcontrainte contrainte_dup(Pcontrainte c_in): allocation d'une contrainte c_out prenant la valeur de ...
Definition: alloc.c:132
Pcontrainte contrainte_copy(Pcontrainte c_in)
Have a look at contrainte_dup and contraintes_dup which reverse the order of the list This copy versi...
Definition: alloc.c:254
void dbg_contrainte_rm(Pcontrainte c, char *f)
void dbg_contrainte_rm(Pcontrainte c): version debug de contrainte rm; trace de la desallocation et i...
Definition: alloc.c:239
Pcontrainte contrainte_new(void)
package contrainte - allocations et desallocations
Definition: alloc.c:47
Pcontrainte contraintes_dup(Pcontrainte c_in)
Pcontrainte contraintes_dup(Pcontrainte c_in) a list of constraints is copied.
Definition: alloc.c:146
Pcontrainte contrainte_make_1D(Value a, Variable x, Value b, bool less_p)
Generate a constraint a x <= b or a x >= b, according to less_p, or ax==b, regardless of less_p.
Definition: alloc.c:86
void * malloc(YYSIZE_T)
void free(void *)
#define exit(code)
Definition: misc-local.h:54
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
int printf()
static char * x
Definition: split_file.c:159
Pvecteur vecteur
struct Scontrainte * succ
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
#define VECTEUR_NUL
DEFINITION DU VECTEUR NUL.
#define VECTEUR_UNDEFINED
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
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
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
void vect_rm(Pvecteur v)
void vect_rm(Pvecteur v): desallocation des couples de v;
Definition: alloc.c:78