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 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. * 21 * * 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. * 26 * * 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 * 31 * * 32 * CLooG, the Chunky Loop Generator * 33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr * 34 * * 35 ******************************************************************************/ 36/* CAUTION: the english used for comments is probably the worst you ever read, 37 * please feel free to correct and improve it ! 38 */ 39 40# include <stdlib.h> 41# include <stdio.h> 42# include <string.h> 43# include "../include/cloog/cloog.h" 44 45 46/****************************************************************************** 47 * Memory leaks hunting * 48 ******************************************************************************/ 49 50 51/** 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). 62 */ 63 64 65static void cloog_statement_leak_up(CloogState *state) 66{ 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 ; 70} 71 72 73static void cloog_statement_leak_down(CloogState *state) 74{ 75 state->statement_freed++; 76} 77 78 79/****************************************************************************** 80 * Structure display function * 81 ******************************************************************************/ 82 83 84/** 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. 90 */ 91void cloog_statement_print_structure(file, statement, level) 92FILE * file ; 93CloogStatement * statement ; 94int level ; 95{ int i ; 96 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) ; 102 103 statement = statement->next ; 104 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"); 112 113 for (i=0; i<level; i++) 114 fprintf(file,"|\t") ; 115 fprintf(file,"| CloogStatement %d \n",statement->number) ; 116 statement = statement->next ; 117 } 118 } 119 else 120 { for (i=0; i<level; i++) 121 fprintf(file,"|\t") ; 122 123 fprintf(file,"+-- No CloogStatement\n") ; 124 } 125} 126 127 128/** 129 * cloog_statement_print function: 130 * This function prints the content of a CloogStatement structure (statement) 131 * into a file (file, possibly stdout). 132 */ 133void cloog_statement_print(FILE * file, CloogStatement * statement) 134{ cloog_statement_print_structure(file,statement,0) ; 135} 136 137 138/****************************************************************************** 139 * Memory deallocation function * 140 ******************************************************************************/ 141 142 143/** 144 * cloog_statement_free function: 145 * This function frees the allocated memory for a CloogStatement structure. 146 */ 147void cloog_statement_free(CloogStatement * statement) 148{ CloogStatement * next ; 149 150 while (statement != NULL) { 151 cloog_statement_leak_down(statement->state); 152 153 next = statement->next ; 154 /* free(statement->usr) ; Actually, this is user's job ! */ 155 free(statement->name); 156 free(statement) ; 157 statement = next ; 158 } 159} 160 161 162/****************************************************************************** 163 * Processing functions * 164 ******************************************************************************/ 165 166 167/** 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 171 * allocated space. 172 * - November 21th 2005: first version. 173 */ 174CloogStatement *cloog_statement_malloc(CloogState *state) 175{ CloogStatement * statement ; 176 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); 182 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 ; 189 190 return statement ; 191} 192 193 194/** 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 198 * allocated space. 199 * - number is the statement number. 200 ** 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 206 * read on a file. 207 * - November 21th 2005: use of cloog_statement_malloc. 208 */ 209CloogStatement *cloog_statement_alloc(CloogState *state, int number) 210{ CloogStatement * statement ; 211 212 /* Memory allocation and initialization of the structure. */ 213 statement = cloog_statement_malloc(state); 214 215 statement->number = number ; 216 217 return statement ; 218} 219 220 221/** 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). 227 */ 228CloogStatement * cloog_statement_copy(CloogStatement * source) 229{ CloogStatement * statement, * temp, * now = NULL ; 230 231 statement = NULL ; 232 233 while (source != NULL) { 234 cloog_statement_leak_up(source->state); 235 236 temp = (CloogStatement *)malloc(sizeof(CloogStatement)) ; 237 if (temp == NULL) 238 cloog_die("memory overflow.\n"); 239 240 temp->state = source->state; 241 temp->number = source->number ; 242 temp->name = source->name ? strdup(source->name) : NULL; 243 temp->usr = source->usr ; 244 temp->next = NULL ; 245 246 if (statement == NULL) 247 { statement = temp ; 248 now = statement ; 249 } 250 else 251 { now->next = temp ; 252 now = now->next ; 253 } 254 source = source->next ; 255 } 256 return(statement) ; 257} 258 259 260/** 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) 266 * is NULL-. 267 * - March 27th 2004: first version. 268 */ 269void cloog_statement_add(start, now, statement) 270CloogStatement ** start, ** now, * statement ; 271{ if (*start == NULL) 272 { *start = statement ; 273 *now = *start ; 274 } 275 else 276 { (*now)->next = statement ; 277 *now = (*now)->next ; 278 } 279} 280 281