only print execution time if sys/resource.h is available
[cloog.git] / source / statement.c
blobadd735c5ab970e21e5316e2546f532d66bd3d81f
2 /**-------------------------------------------------------------------**
3 ** CLooG **
4 **-------------------------------------------------------------------**
5 ** statement.c **
6 **-------------------------------------------------------------------**
7 ** First version: november 4th 2001 **
8 **-------------------------------------------------------------------**/
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 !
39 # include <stdlib.h>
40 # include <stdio.h>
41 # include <string.h>
42 # include "../include/cloog/cloog.h"
45 /******************************************************************************
46 * Memory leaks hunting *
47 ******************************************************************************/
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).
64 int cloog_statement_allocated = 0 ;
65 int cloog_statement_freed = 0 ;
66 int cloog_statement_max = 0 ;
69 static void cloog_statement_leak_up()
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 ;
76 static void cloog_statement_leak_down()
77 { cloog_statement_freed ++ ;
81 /******************************************************************************
82 * Structure display function *
83 ******************************************************************************/
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.
93 void cloog_statement_print_structure(file, statement, level)
94 FILE * file ;
95 CloogStatement * statement ;
96 int level ;
97 { int i ;
99 if (statement != NULL)
100 { /* Go to the right level. */
101 for (i=0; i<level; i++)
102 fprintf(file,"|\t") ;
103 fprintf(file,"+-- CloogStatement %d \n",statement->number) ;
105 statement = statement->next ;
107 while (statement != NULL)
108 { for (i=0; i<level; i++)
109 fprintf(file,"|\t") ;
110 fprintf(file,"| |\n");
111 for (i=0; i<level; i++)
112 fprintf(file,"|\t") ;
113 fprintf(file,"| V\n");
115 for (i=0; i<level; i++)
116 fprintf(file,"|\t") ;
117 fprintf(file,"| CloogStatement %d \n",statement->number) ;
118 statement = statement->next ;
121 else
122 { for (i=0; i<level; i++)
123 fprintf(file,"|\t") ;
125 fprintf(file,"+-- No CloogStatement\n") ;
131 * cloog_statement_print function:
132 * This function prints the content of a CloogStatement structure (statement)
133 * into a file (file, possibly stdout).
135 void cloog_statement_print(FILE * file, CloogStatement * statement)
136 { cloog_statement_print_structure(file,statement,0) ;
140 /******************************************************************************
141 * Memory deallocation function *
142 ******************************************************************************/
146 * cloog_statement_free function:
147 * This function frees the allocated memory for a CloogStatement structure.
149 void cloog_statement_free(CloogStatement * statement)
150 { CloogStatement * next ;
152 while (statement != NULL)
153 { cloog_statement_leak_down() ;
155 next = statement->next ;
156 /* free(statement->usr) ; Actually, this is user's job ! */
157 free(statement) ;
158 statement = next ;
163 /******************************************************************************
164 * Processing functions *
165 ******************************************************************************/
169 * cloog_statement_malloc function:
170 * This function allocates the memory space for a CloogStatement structure and
171 * sets its fields with default values. Then it returns a pointer to the
172 * allocated space.
173 * - November 21th 2005: first version.
175 CloogStatement * cloog_statement_malloc()
176 { CloogStatement * statement ;
178 /* Memory allocation for the CloogStatement structure. */
179 statement = (CloogStatement *)malloc(sizeof(CloogStatement)) ;
180 if (statement == NULL)
181 cloog_die("memory overflow.\n");
182 cloog_statement_leak_up() ;
184 /* We set the various fields with default values. */
185 statement->number = 0;
186 statement->usr = NULL ; /* To fill it is actually user's job ! */
187 statement->next = NULL ;
189 return statement ;
194 * cloog_statement_alloc function:
195 * This function allocates the memory space for a CloogStatement structure and
196 * sets its fields with those given as input. Then it returns a pointer to the
197 * allocated space.
198 * - number is the statement number.
200 * - September 9th 2002: first version.
201 * - March 17th 2003: fix for the usr field in CloogStatement structure.
202 * - April 16th 2005: adaptation to new CloogStatement structure (with
203 * number), cloog_statement_read becomes
204 * cloog_statement_alloc sincethere is nothing more to
205 * read on a file.
206 * - November 21th 2005: use of cloog_statement_malloc.
208 CloogStatement * cloog_statement_alloc(int number)
209 { CloogStatement * statement ;
211 /* Memory allocation and initialization of the structure. */
212 statement = cloog_statement_malloc() ;
214 statement->number = number ;
216 return statement ;
221 * cloog_statement_copy function:
222 * This function returns a copy of the CloogStatement structure given as input.
223 * - October 28th 2001: first version (in loop.c).
224 * - March 17th 2003: fix for the usr field in CloogStatement structure.
225 * - April 16th 2005: adaptation to new CloogStatement struct (with number).
227 CloogStatement * cloog_statement_copy(CloogStatement * source)
228 { CloogStatement * statement, * temp, * now = NULL ;
230 statement = NULL ;
232 while (source != NULL)
233 { cloog_statement_leak_up() ;
235 temp = (CloogStatement *)malloc(sizeof(CloogStatement)) ;
236 if (temp == NULL)
237 cloog_die("memory overflow.\n");
239 temp->number = source->number ;
240 temp->usr = source->usr ;
241 temp->next = NULL ;
243 if (statement == NULL)
244 { statement = temp ;
245 now = statement ;
247 else
248 { now->next = temp ;
249 now = now->next ;
251 source = source->next ;
253 return(statement) ;
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.
266 void cloog_statement_add(start, now, statement)
267 CloogStatement ** start, ** now, * statement ;
268 { if (*start == NULL)
269 { *start = statement ;
270 *now = *start ;
272 else
273 { (*now)->next = statement ;
274 *now = (*now)->next ;