Fix memory leaks.
[cloog-ppl.git] / source / block.c
blob37f6f12e206e3197fa2cd507cb68bfa7db101e28
2 /**-------------------------------------------------------------------**
3 ** CLooG **
4 **-------------------------------------------------------------------**
5 ** block.c **
6 **-------------------------------------------------------------------**
7 ** First version: june 11th 2005 **
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 "../include/cloog/cloog.h"
44 /******************************************************************************
45 * Memory leaks hunting *
46 ******************************************************************************/
49 /**
50 * These functions and global variables are devoted to memory leaks hunting: we
51 * want to know at each moment how many CloogBlock structures had been allocated
52 * (cloog_block_allocated) and how many had been freed (cloog_block_freed).
53 * Each time a CloogBlock structure is allocated, a call to the function
54 * cloog_block_leak_up() must be carried out, and respectively
55 * cloog_block_leak_down() when a CloogBlock structure is freed. The special
56 * variable cloog_block_max gives the maximal number of CloogBlock structures
57 * simultaneously alive (i.e. allocated and non-freed) in memory.
58 * - June 11th 2005: first version.
62 int cloog_block_allocated = 0 ;
63 int cloog_block_freed = 0 ;
64 int cloog_block_max = 0 ;
67 static void cloog_block_leak_up (void)
68 { cloog_block_allocated ++ ;
69 if ((cloog_block_allocated - cloog_block_freed) > cloog_block_max)
70 cloog_block_max = cloog_block_allocated - cloog_block_freed ;
74 static void cloog_block_leak_down (void)
75 { cloog_block_freed ++ ;
78 static inline int cloog_block_references (CloogBlock *b)
80 return b->_references;
83 static inline void cloog_block_init_references (CloogBlock *b)
85 b->_references = 1;
88 static inline void cloog_block_inc_references (CloogBlock *b)
90 b->_references++;
93 static inline void cloog_block_dec_references (CloogBlock *b)
95 b->_references--;
101 /******************************************************************************
102 * Structure display function *
103 ******************************************************************************/
107 * cloog_domain_print_structure :
108 * this function is a human-friendly way to display the CloogDomain data
109 * structure, it includes an indentation level (level) in order to work with
110 * others print_structure functions.
111 * - June 16th 2005: first version.
113 void cloog_block_print_structure(FILE * file, CloogBlock * block, int level)
114 { int i ;
116 /* Go to the right level. */
117 for (i=0; i<level; i++)
118 fprintf(file,"|\t") ;
120 if (block != NULL)
121 { fprintf(file,"+-- CloogBlock\n") ;
123 /* A blank line. */
124 for (i=0; i<level+2; i++)
125 fprintf(file,"|\t") ;
126 fprintf(file,"\n") ;
128 /* Print statement list. */
129 cloog_statement_print_structure(file,cloog_block_stmt (block),level+1) ;
131 /* A blank line. */
132 for (i=0; i<level+2; i++)
133 fprintf(file,"|\t") ;
134 fprintf(file,"\n") ;
136 /* A blank line. */
137 for (i=0; i<level+2; i++)
138 fprintf(file,"|\t") ;
139 fprintf(file,"\n") ;
141 /* Print scalar dimensions. */
142 for (i=0; i<level+1; i++)
143 fprintf(file,"|\t") ;
145 if (cloog_block_nb_scaldims (block) == 0)
146 fprintf(file,"No scalar dimensions\n") ;
147 else
149 fprintf (file, "Scalar dimensions (%d):", cloog_block_nb_scaldims (block));
150 for (i = 0; i < cloog_block_nb_scaldims (block); i++)
151 value_print (file, " "VALUE_FMT, block->scaldims[i]);
152 fprintf (file, "\n");
155 /* A blank line. */
156 for (i=0; i<level+2; i++)
157 fprintf(file,"|\t") ;
158 fprintf(file,"\n") ;
160 /* Print depth. */
161 for (i=0; i<level+1; i++)
162 fprintf(file,"|\t") ;
163 fprintf (file, "Depth: %d\n", cloog_block_depth (block));
165 /* A blank line. */
166 for (i=0; i<level+1; i++)
167 fprintf(file,"|\t") ;
168 fprintf(file,"\n") ;
170 else
171 fprintf(file,"+-- Null CloogBlock\n") ;
176 * cloog_block_print function:
177 * This function prints the content of a CloogBlock structure (block) into a
178 * file (file, possibly stdout).
179 * - June 11th 2005: first version.
180 * - June 16th 2005: now just a call to cloog_block_print_structure.
182 void cloog_block_print(FILE * file, CloogBlock * block)
183 { cloog_block_print_structure(file,block,0) ;
188 * cloog_block_list_print function:
189 * This function prints the content of a CloogBlock structure (block) into a
190 * file (file, possibly stdout).
191 * - June 16th 2005: first version.
193 void cloog_block_list_print(FILE * file, CloogBlockList * blocklist)
194 { int i=0 ;
196 while (blocklist != NULL)
198 fprintf(file,"+-- CloogBlockList node %d\n",i) ;
199 cloog_block_print_structure (file, cloog_block_list_block (blocklist), 1);
200 blocklist = cloog_block_list_next (blocklist);
201 i++ ;
206 /******************************************************************************
207 * Memory deallocation function *
208 ******************************************************************************/
212 * cloog_block_free function:
213 * This function frees the allocated memory for a CloogStatement structure.
214 * - June 11th 2005: first version.
215 * - June 30th 2005: scaldims field management.
217 void cloog_block_free(CloogBlock * block)
218 { int i ;
220 if (block != NULL)
222 cloog_block_dec_references (block);
224 if (cloog_block_references (block) == 0)
225 { cloog_block_leak_down() ;
226 if (cloog_block_scaldims (block))
228 for (i = 0; i < cloog_block_nb_scaldims (block); i++)
229 value_clear_c (block->scaldims[i]);
231 free (cloog_block_scaldims (block)) ;
233 cloog_statement_free(cloog_block_stmt (block)) ;
234 free(block) ;
241 * cloog_block_list_free function:
242 * This function frees the allocated memory for a CloogBlockList structure.
243 * - June 11th 2005: first version.
245 void cloog_block_list_free(CloogBlockList * blocklist)
246 { CloogBlockList * temp ;
248 while (blocklist != NULL)
250 temp = cloog_block_list_next (blocklist);
251 cloog_block_free (cloog_block_list_block (blocklist));
252 free(blocklist) ;
253 blocklist = temp ;
258 /******************************************************************************
259 * Processing functions *
260 ******************************************************************************/
263 * cloog_block_malloc function:
264 * This function allocates the memory space for a CloogBlock structure and
265 * sets its fields with default values. Then it returns a pointer to the
266 * allocated space.
267 * - November 21th 2005: first version.
269 CloogBlock * cloog_block_malloc (void)
270 { CloogBlock * block ;
272 /* Memory allocation for the CloogBlock structure. */
273 block = (CloogBlock *)malloc(sizeof(CloogBlock)) ;
274 if (block == NULL)
275 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
276 exit(1) ;
278 cloog_block_leak_up() ;
280 /* We set the various fields with default values. */
281 cloog_block_set_stmt (block, NULL);
282 cloog_block_set_nb_scaldims (block, 0);
283 cloog_block_set_scaldims (block, NULL);
284 cloog_block_set_depth (block, 0);
285 cloog_block_init_references (block);
286 cloog_block_set_usr (block, NULL);
288 return block ;
293 * cloog_block_alloc function:
294 * This function allocates the memory space for a CloogBlock structure and
295 * sets its fields with those given as input. Then it returns a pointer to the
296 * allocated space. The two parameters nb_scaldims and scaldims are for internal
297 * service, put to respectively 0 and NULL if you don't know what they are
298 * useful for !
299 * - statement is the statement list of the block,
300 * - nb_scaldims is the number of scalar dimensions (0 if unsure !),
301 * - scaldims is the array with the scalar dimensions values (NULL if unsure !),
302 * - depth is the original block depth (the number of outer loops).
304 * - June 11th 2005: first version.
305 * - June 30th 2005: addition of the nb_scaldims and scaldims parameters.
306 * - November 21th 2005: use of cloog_block_malloc.
308 CloogBlock * cloog_block_alloc(CloogStatement * statement, int nb_scaldims,
309 Value * scaldims, int depth)
311 CloogBlock * block ;
313 /* Block allocation. */
314 block = cloog_block_malloc() ;
316 cloog_block_set_stmt (block, statement);
317 cloog_block_set_nb_scaldims (block, nb_scaldims);
318 cloog_block_set_scaldims (block, scaldims);
319 cloog_block_set_depth (block, depth);
320 cloog_block_init_references (block);
322 return block ;
327 * cloog_block_list_malloc function:
328 * This function allocates the memory space for a CloogBlockList structure and
329 * sets its fields with default values. Then it returns a pointer to the
330 * allocated space.
331 * - November 21th 2005: first version.
333 CloogBlockList * cloog_block_list_malloc (void)
335 CloogBlockList * blocklist ;
337 /* Memory allocation for the CloogBlock structure. */
338 blocklist = (CloogBlockList *)malloc(sizeof(CloogBlockList)) ;
339 if (blocklist == NULL)
340 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
341 exit(1) ;
344 /* We set the various fields with default values. */
345 cloog_block_list_set_block (blocklist, NULL);
346 cloog_block_list_set_next (blocklist, NULL);
348 return blocklist ;
353 * cloog_block_list_alloc function:
354 * This function allocates the memory space for a CloogBlockList structure and
355 * sets its fields with those given as input. Then it returns a pointer to the
356 * allocated space.
357 * - block is the block element of the list node,
359 * - June 11th 2005: first version.
360 * - November 21th 2005: use of cloog_block_list_malloc.
362 CloogBlockList * cloog_block_list_alloc(CloogBlock * block)
363 { CloogBlockList * blocklist ;
365 /* Block list node allocation. */
366 blocklist = cloog_block_list_malloc() ;
368 cloog_block_list_set_block (blocklist, block);
369 cloog_block_inc_references (cloog_block_list_block (blocklist)); /* The block has a new reference to it. */
370 cloog_block_list_set_next (blocklist, NULL);
372 return blocklist ;
377 * cloog_block_copy function:
378 * This function returns a copy of a CloogBlock structure 'block'. To save
379 * memory this is not a memory copy but we increment a counter of active
380 * references inside the structure, then return a pointer to that structure.
382 CloogBlock * cloog_block_copy(CloogBlock * block)
383 { if (block == NULL)
384 return NULL ;
386 cloog_block_inc_references (block);
387 return block ;
392 * cloog_block_merge function:
393 * this function adds at the end of the statement list of the block 'block',
394 * the statement list of the block 'merged'. Then the CloogBlock structure
395 * of 'merged' is freed (obviously not its statement list that is now
396 * included in 'block').
397 * - June 11th 2005: first version.
399 void cloog_block_merge(CloogBlock * block, CloogBlock * merged)
400 { CloogStatement * statement ;
402 if ((block == NULL) || (merged == NULL))
403 return ;
405 if (cloog_block_stmt (block))
407 statement = cloog_block_stmt (block) ;
409 while (cloog_statement_next (statement))
410 statement = cloog_statement_next (statement) ;
412 cloog_statement_set_next (statement, cloog_block_stmt (merged));
414 else
415 cloog_block_set_stmt (block, cloog_block_stmt (merged));
417 cloog_block_leak_down() ;
418 free(merged) ;