resetting manifest requested domain to floor
[platform/upstream/cloog.git] / source / statement.c
1
2    /**-------------------------------------------------------------------**
3     **                              CLooG                                **
4     **-------------------------------------------------------------------**
5     **                           statement.c                             **
6     **-------------------------------------------------------------------**
7     **                 First version: november 4th 2001                  **
8     **-------------------------------------------------------------------**/
9
10
11 /******************************************************************************
12  *               CLooG : the Chunky Loop Generator (experimental)             *
13  ******************************************************************************
14  *                                                                            *
15  * Copyright (C) 2001-2005 Cedric Bastoul                                     *
16  *                                                                            *
17  * This is free software; you can redistribute it and/or modify it under the  *
18  * terms of the GNU General Public License as published by the Free Software  *
19  * Foundation; either version 2 of the License, or (at your option) any later *
20  * version.                                                                   *
21  *                                                                            *
22  * This software is distributed in the hope that it will be useful, but       *
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
24  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   *
25  * for more details.                                                          *
26  *                                                                            *
27  * You should have received a copy of the GNU General Public License along    *
28  * with software; if not, write to the Free Software Foundation, Inc.,        *
29  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA                     *
30  *                                                                            *
31  * CLooG, the Chunky Loop Generator                                           *
32  * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr                         *
33  *                                                                            *
34  ******************************************************************************/
35 /* CAUTION: the english used for comments is probably the worst you ever read,
36  *          please feel free to correct and improve it !
37  */
38
39 # include <stdlib.h>
40 # include <stdio.h>
41 # include <string.h>
42 # include "../include/cloog/cloog.h"
43
44
45 /******************************************************************************
46  *                             Memory leaks hunting                           *
47  ******************************************************************************/
48
49
50 /**
51  * These functions and global variables are devoted to memory leaks hunting: we
52  * want to know at each moment how many CloogStatement structures had been
53  * allocated (cloog_statement_allocated) and how many had been freed
54  * (cloog_statement_freed). Each time a CloogStatement structure is allocated,
55  * a call to the function cloog_statement_leak_up() must be carried out, and
56  * respectively cloog_statement_leak_down() when a CloogStatement structure is
57  * freed. The special variable cloog_statement_max gives the maximal number of
58  * CloogStatement structures simultaneously alive (i.e. allocated and
59  * non-freed) in memory.
60  * - July 3rd->11th 2003: first version (memory leaks hunt and correction).
61  */
62
63
64 int cloog_statement_allocated = 0 ;
65 int cloog_statement_freed = 0 ;
66 int cloog_statement_max = 0 ;
67
68
69 static void cloog_statement_leak_up (void)
70 { cloog_statement_allocated ++ ;
71   if ((cloog_statement_allocated-cloog_statement_freed) > cloog_statement_max)
72   cloog_statement_max = cloog_statement_allocated - cloog_statement_freed ;
73 }
74
75
76 static void cloog_statement_leak_down (void)
77 { cloog_statement_freed ++ ;
78 }
79
80
81 /******************************************************************************
82  *                          Structure display function                        *
83  ******************************************************************************/
84
85
86 /**
87  * cloog_domain_print_structure :
88  * this function is a human-friendly way to display the CloogDomain data
89  * structure, it includes an indentation level (level) in order to work with
90  * others print_structure functions.
91  * - June  16th 2005: first version.
92  */
93 void cloog_statement_print_structure(FILE *file, CloogStatement *statement, int level)
94 { int i ;
95       
96   if (statement != NULL)
97   { /* Go to the right level. */
98     for (i=0; i<level; i++)
99     fprintf(file,"|\t") ;
100     fprintf (file, "+-- CloogStatement %d \n", cloog_statement_number (statement));
101     
102     statement = cloog_statement_next (statement);
103  
104     while (statement != NULL)
105     { for (i=0; i<level; i++)
106       fprintf(file,"|\t") ;
107       fprintf(file,"|          |\n");
108       for (i=0; i<level; i++)
109       fprintf(file,"|\t") ;
110       fprintf(file,"|          V\n");
111       
112       for (i=0; i<level; i++)
113       fprintf(file,"|\t") ;
114       fprintf (file, "|   CloogStatement %d \n", cloog_statement_number (statement));
115       statement = cloog_statement_next (statement) ;
116     }
117   }
118   else
119   { for (i=0; i<level; i++)
120     fprintf(file,"|\t") ;
121     
122     fprintf(file,"+-- No CloogStatement\n") ;
123   }  
124 }
125
126
127 /**
128  * cloog_statement_print function:
129  * This function prints the content of a CloogStatement structure (statement)
130  * into a file (file, possibly stdout).
131  */
132 void cloog_statement_print(FILE * file, CloogStatement * statement)
133 { cloog_statement_print_structure(file,statement,0) ;
134 }
135
136
137 /******************************************************************************
138  *                         Memory deallocation function                       *
139  ******************************************************************************/
140
141
142 /**
143  * cloog_statement_free function:
144  * This function frees the allocated memory for a CloogStatement structure.
145  */
146 void cloog_statement_free(CloogStatement * statement)
147 { CloogStatement * next ;
148
149   while (statement != NULL)
150   { cloog_statement_leak_down() ;
151     
152     next = cloog_statement_next (statement) ;
153     free(statement) ;
154     statement = next ;
155   }
156 }
157
158
159 /******************************************************************************
160  *                            Processing functions                            *
161  ******************************************************************************/
162
163
164 /**
165  * cloog_statement_malloc function:
166  * This function allocates the memory space for a CloogStatement structure and
167  * sets its fields with default values. Then it returns a pointer to the
168  * allocated space.
169  * - November 21th 2005: first version.
170  */
171 CloogStatement * cloog_statement_malloc (void)
172 { CloogStatement * statement ;
173   
174   /* Memory allocation for the CloogStatement structure. */
175   statement = (CloogStatement *)malloc(sizeof(CloogStatement)) ;
176   if (statement == NULL) 
177   { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
178     exit(1) ;
179   }
180   cloog_statement_leak_up() ;
181   
182   /* We set the various fields with default values. */
183   cloog_statement_set_number (statement, 0);
184   cloog_statement_set_usr (statement, NULL);
185   cloog_statement_set_next (statement, NULL);
186   
187   return statement ;
188 }  
189
190
191 /**
192  * cloog_statement_alloc function:
193  * This function allocates the memory space for a CloogStatement structure and
194  * sets its fields with those given as input. Then it returns a pointer to the
195  * allocated space.
196  * - number is the statement number.
197  **
198  * - September 9th 2002: first version.
199  * - March    17th 2003: fix for the usr field in CloogStatement structure.
200  * - April    16th 2005: adaptation to new CloogStatement structure (with
201  *                       number), cloog_statement_read becomes
202  *                       cloog_statement_alloc sincethere is nothing more to
203  *                       read on a file.
204  * - November 21th 2005: use of cloog_statement_malloc.
205  */
206 CloogStatement * cloog_statement_alloc(int number)
207 { CloogStatement * statement ;
208     
209   /* Memory allocation and initialization of the structure. */
210   statement = cloog_statement_malloc() ;
211
212   cloog_statement_set_number (statement, number);
213   
214   return statement ;
215 }
216
217
218 /**
219  * cloog_statement_copy function:
220  * This function returns a copy of the CloogStatement structure given as input.
221  * - October 28th 2001: first version (in loop.c). 
222  * - March   17th 2003: fix for the usr field in CloogStatement structure.
223  * - April   16th 2005: adaptation to new CloogStatement struct (with number). 
224  */ 
225 CloogStatement * cloog_statement_copy(CloogStatement * source)
226 { CloogStatement * statement, * temp, * now = NULL ;
227   
228   statement = NULL ;
229
230   while (source != NULL)
231   { cloog_statement_leak_up() ;
232
233     temp = (CloogStatement *)malloc(sizeof(CloogStatement)) ;
234     if (temp == NULL)
235     { fprintf(stderr, "Memory Overflow.\n") ;
236       exit(1) ;
237     }
238     
239     cloog_statement_set_number (temp, cloog_statement_number (source));
240     cloog_statement_set_usr (temp, cloog_statement_usr (source));
241     cloog_statement_set_next (temp, NULL);
242     
243     if (statement == NULL)
244     { statement = temp ;
245       now = statement ;
246     }
247     else
248       { cloog_statement_set_next (now, temp);
249         now = cloog_statement_next (now) ;
250     }
251     source = cloog_statement_next (source) ;
252   }
253   return(statement) ;
254 }
255
256
257 /** 
258  * cloog_statement_add function:
259  * This function adds a CloogStatement structure (statement) at a given place
260  * (now) of a NULL terminated list of CloogStatement structures. The beginning
261  * of this list is (start). This function updates (now) to (loop), and
262  * updates (start) if the added element is the first one -that is when (start)
263  * is NULL-.
264  * - March 27th 2004: first version. 
265  */ 
266 void cloog_statement_add(CloogStatement **start, CloogStatement **now, CloogStatement *statement)
267 { if (*start == NULL)
268   { *start = statement ;
269     *now = *start ;
270   }
271   else
272     { cloog_statement_set_next (*now, statement);
273       *now = cloog_statement_next (*now);
274   }
275 }
276