2 /**-------------------------------------------------------------------**
4 **-------------------------------------------------------------------**
6 **-------------------------------------------------------------------**
7 ** First version: november 4th 2001 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
15 * Copyright (C) 2001-2005 Cedric Bastoul *
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 *
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 *
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 *
31 * CLooG, the Chunky Loop Generator *
32 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 ******************************************************************************/
35 /* CAUTION: the english used for comments is probably the worst you ever read,
36 * please feel free to correct and improve it !
42 # include "../include/cloog/cloog.h"
45 /******************************************************************************
46 * Memory leaks hunting *
47 ******************************************************************************/
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 (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
;
76 static void cloog_statement_leak_down (void)
77 { cloog_statement_freed
++ ;
81 /******************************************************************************
82 * Structure display function *
83 ******************************************************************************/
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 *file
, CloogStatement
*statement
, int level
)
96 if (statement
!= NULL
)
97 { /* Go to the right level. */
98 for (i
=0; i
<level
; i
++)
100 fprintf (file
, "+-- CloogStatement %d \n", cloog_statement_number (statement
));
102 statement
= cloog_statement_next (statement
);
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");
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
) ;
119 { for (i
=0; i
<level
; i
++)
120 fprintf(file
,"|\t") ;
122 fprintf(file
,"+-- No CloogStatement\n") ;
128 * cloog_statement_print function:
129 * This function prints the content of a CloogStatement structure (statement)
130 * into a file (file, possibly stdout).
132 void cloog_statement_print(FILE * file
, CloogStatement
* statement
)
133 { cloog_statement_print_structure(file
,statement
,0) ;
137 /******************************************************************************
138 * Memory deallocation function *
139 ******************************************************************************/
143 * cloog_statement_free function:
144 * This function frees the allocated memory for a CloogStatement structure.
146 void cloog_statement_free(CloogStatement
* statement
)
147 { CloogStatement
* next
;
149 while (statement
!= NULL
)
150 { cloog_statement_leak_down() ;
152 next
= cloog_statement_next (statement
) ;
159 /******************************************************************************
160 * Processing functions *
161 ******************************************************************************/
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
169 * - November 21th 2005: first version.
171 CloogStatement
* cloog_statement_malloc (void)
172 { CloogStatement
* statement
;
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") ;
180 cloog_statement_leak_up() ;
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
);
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
196 * - number is the statement number.
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
204 * - November 21th 2005: use of cloog_statement_malloc.
206 CloogStatement
* cloog_statement_alloc(int number
)
207 { CloogStatement
* statement
;
209 /* Memory allocation and initialization of the structure. */
210 statement
= cloog_statement_malloc() ;
212 cloog_statement_set_number (statement
, number
);
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).
225 CloogStatement
* cloog_statement_copy(CloogStatement
* source
)
226 { CloogStatement
* statement
, * temp
, * now
= NULL
;
230 while (source
!= NULL
)
231 { cloog_statement_leak_up() ;
233 temp
= (CloogStatement
*)malloc(sizeof(CloogStatement
)) ;
235 { fprintf(stderr
, "Memory Overflow.\n") ;
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
);
243 if (statement
== NULL
)
248 { cloog_statement_set_next (now
, temp
);
249 now
= cloog_statement_next (now
) ;
251 source
= cloog_statement_next (source
) ;
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)
264 * - March 27th 2004: first version.
266 void cloog_statement_add(CloogStatement
**start
, CloogStatement
**now
, CloogStatement
*statement
)
267 { if (*start
== NULL
)
268 { *start
= statement
;
272 { cloog_statement_set_next (*now
, statement
);
273 *now
= cloog_statement_next (*now
);