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 library is free software; you can redistribute it and/or *
18 * modify it under the terms of the GNU Lesser General Public *
19 * License as published by the Free Software Foundation; either *
20 * version 2.1 of the License, or (at your option) any later version. *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
25 * Lesser General Public License for more details. *
27 * You should have received a copy of the GNU Lesser General Public *
28 * License along with this library; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
30 * Boston, MA 02110-1301 USA *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
35 ******************************************************************************/
36 /* CAUTION: the english used for comments is probably the worst you ever read,
37 * please feel free to correct and improve it !
43 # include "../include/cloog/cloog.h"
46 /******************************************************************************
47 * Memory leaks hunting *
48 ******************************************************************************/
52 * These functions and global variables are devoted to memory leaks hunting: we
53 * want to know at each moment how many CloogStatement structures had been
54 * allocated (cloog_statement_allocated) and how many had been freed
55 * (cloog_statement_freed). Each time a CloogStatement structure is allocated,
56 * a call to the function cloog_statement_leak_up() must be carried out, and
57 * respectively cloog_statement_leak_down() when a CloogStatement structure is
58 * freed. The special variable cloog_statement_max gives the maximal number of
59 * CloogStatement structures simultaneously alive (i.e. allocated and
60 * non-freed) in memory.
61 * - July 3rd->11th 2003: first version (memory leaks hunt and correction).
65 static void cloog_statement_leak_up(CloogState
*state
)
67 state
->statement_allocated
++;
68 if ((state
->statement_allocated
- state
->statement_freed
) > state
->statement_max
)
69 state
->statement_max
= state
->statement_allocated
- state
->statement_freed
;
73 static void cloog_statement_leak_down(CloogState
*state
)
75 state
->statement_freed
++;
79 /******************************************************************************
80 * Structure display function *
81 ******************************************************************************/
85 * cloog_domain_print_structure :
86 * this function is a human-friendly way to display the CloogDomain data
87 * structure, it includes an indentation level (level) in order to work with
88 * others print_structure functions.
89 * - June 16th 2005: first version.
91 void cloog_statement_print_structure(file
, statement
, level
)
93 CloogStatement
* statement
;
97 if (statement
!= NULL
)
98 { /* Go to the right level. */
99 for (i
=0; i
<level
; i
++)
100 fprintf(file
,"|\t") ;
101 fprintf(file
,"+-- CloogStatement %d \n",statement
->number
) ;
103 statement
= statement
->next
;
105 while (statement
!= NULL
)
106 { for (i
=0; i
<level
; i
++)
107 fprintf(file
,"|\t") ;
108 fprintf(file
,"| |\n");
109 for (i
=0; i
<level
; i
++)
110 fprintf(file
,"|\t") ;
111 fprintf(file
,"| V\n");
113 for (i
=0; i
<level
; i
++)
114 fprintf(file
,"|\t") ;
115 fprintf(file
,"| CloogStatement %d \n",statement
->number
) ;
116 statement
= statement
->next
;
120 { for (i
=0; i
<level
; i
++)
121 fprintf(file
,"|\t") ;
123 fprintf(file
,"+-- No CloogStatement\n") ;
129 * cloog_statement_print function:
130 * This function prints the content of a CloogStatement structure (statement)
131 * into a file (file, possibly stdout).
133 void cloog_statement_print(FILE * file
, CloogStatement
* statement
)
134 { cloog_statement_print_structure(file
,statement
,0) ;
138 /******************************************************************************
139 * Memory deallocation function *
140 ******************************************************************************/
144 * cloog_statement_free function:
145 * This function frees the allocated memory for a CloogStatement structure.
147 void cloog_statement_free(CloogStatement
* statement
)
148 { CloogStatement
* next
;
150 while (statement
!= NULL
) {
151 cloog_statement_leak_down(statement
->state
);
153 next
= statement
->next
;
154 /* free(statement->usr) ; Actually, this is user's job ! */
155 free(statement
->name
);
162 /******************************************************************************
163 * Processing functions *
164 ******************************************************************************/
168 * cloog_statement_malloc function:
169 * This function allocates the memory space for a CloogStatement structure and
170 * sets its fields with default values. Then it returns a pointer to the
172 * - November 21th 2005: first version.
174 CloogStatement
*cloog_statement_malloc(CloogState
*state
)
175 { CloogStatement
* statement
;
177 /* Memory allocation for the CloogStatement structure. */
178 statement
= (CloogStatement
*)malloc(sizeof(CloogStatement
)) ;
179 if (statement
== NULL
)
180 cloog_die("memory overflow.\n");
181 cloog_statement_leak_up(state
);
183 /* We set the various fields with default values. */
184 statement
->state
= state
;
185 statement
->number
= 0;
186 statement
->name
= NULL
;
187 statement
->usr
= NULL
; /* To fill it is actually user's job ! */
188 statement
->next
= NULL
;
195 * cloog_statement_alloc function:
196 * This function allocates the memory space for a CloogStatement structure and
197 * sets its fields with those given as input. Then it returns a pointer to the
199 * - number is the statement number.
201 * - September 9th 2002: first version.
202 * - March 17th 2003: fix for the usr field in CloogStatement structure.
203 * - April 16th 2005: adaptation to new CloogStatement structure (with
204 * number), cloog_statement_read becomes
205 * cloog_statement_alloc sincethere is nothing more to
207 * - November 21th 2005: use of cloog_statement_malloc.
209 CloogStatement
*cloog_statement_alloc(CloogState
*state
, int number
)
210 { CloogStatement
* statement
;
212 /* Memory allocation and initialization of the structure. */
213 statement
= cloog_statement_malloc(state
);
215 statement
->number
= number
;
222 * cloog_statement_copy function:
223 * This function returns a copy of the CloogStatement structure given as input.
224 * - October 28th 2001: first version (in loop.c).
225 * - March 17th 2003: fix for the usr field in CloogStatement structure.
226 * - April 16th 2005: adaptation to new CloogStatement struct (with number).
228 CloogStatement
* cloog_statement_copy(CloogStatement
* source
)
229 { CloogStatement
* statement
, * temp
, * now
= NULL
;
233 while (source
!= NULL
) {
234 cloog_statement_leak_up(source
->state
);
236 temp
= (CloogStatement
*)malloc(sizeof(CloogStatement
)) ;
238 cloog_die("memory overflow.\n");
240 temp
->state
= source
->state
;
241 temp
->number
= source
->number
;
242 temp
->name
= source
->name
? strdup(source
->name
) : NULL
;
243 temp
->usr
= source
->usr
;
246 if (statement
== NULL
)
254 source
= source
->next
;
261 * cloog_statement_add function:
262 * This function adds a CloogStatement structure (statement) at a given place
263 * (now) of a NULL terminated list of CloogStatement structures. The beginning
264 * of this list is (start). This function updates (now) to (loop), and
265 * updates (start) if the added element is the first one -that is when (start)
267 * - March 27th 2004: first version.
269 void cloog_statement_add(start
, now
, statement
)
270 CloogStatement
** start
, ** now
, * statement
;
271 { if (*start
== NULL
)
272 { *start
= statement
;
276 { (*now
)->next
= statement
;
277 *now
= (*now
)->next
;