Add overflow detection
[openscop.git] / source / extensions / extbody.c
blob5a5410307cde2f25e4ef43caeefe3f3b44b85f39
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** extensions/extbody.c **
6 **-----------------------------------------------------------------**
7 ** First version: 07/12/2010 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29 * *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31 * *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
35 * are met: *
36 * *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
44 * *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
55 * *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60 * *
61 *****************************************************************************/
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <ctype.h>
68 #include <osl/macros.h>
69 #include <osl/util.h>
70 #include <osl/body.h>
71 #include <osl/extensions/extbody.h>
74 /*+***************************************************************************
75 * Structure display function *
76 *****************************************************************************/
79 /**
80 * osl_extbody_idump function:
81 * this function displays an osl_extbody_t structure (*extbody) into a
82 * file (file, possibly stdout) in a way that trends to be understandable. It
83 * includes an indentation level (level) in order to work with others idump
84 * functions.
85 * \param[in] file The file where the information has to be printed.
86 * \param[in] ebody The ebody structure to print.
87 * \param[in] level Number of spaces before printing, for each line.
89 void osl_extbody_idump(FILE * file, osl_extbody_p ebody, int level) {
90 int i, j;
92 // Go to the right level.
93 for (j = 0; j < level; j++)
94 fprintf(file, "|\t");
96 if (ebody != NULL)
97 fprintf(file, "+-- osl_extbody_t\n");
98 else
99 fprintf(file, "+-- NULL extbody\n");
101 if (ebody != NULL) {
102 // Go to the right level.
103 for(j = 0; j <= level; j++)
104 fprintf(file, "|\t");
106 // Display the number of ebody.
107 fprintf(file, "nb_access: %d\n", ebody->nb_access);
109 // Display the coordinates.
110 for(i = 0; i < ebody->nb_access; i++) {
111 // Go to the right level.
112 for(j = 0; j <= level; j++)
113 fprintf(file, "|\t");
115 fprintf(file, "start: %d, length: %d\n",
116 ebody->start[i], ebody->length[i]);
119 // Display the body
120 osl_body_idump(file, ebody->body, level);
123 // The last line.
124 for (j = 0; j <= level; j++)
125 fprintf(file, "|\t");
126 fprintf(file, "\n");
131 * osl_extbody_dump function:
132 * this function prints the content of an osl_extbody_t structure
133 * (*ebody) into a file (file, possibly stdout).
134 * \param[in] file The file where the information has to be printed.
135 * \param[in] ebody The ebody structure to print.
137 void osl_extbody_dump(FILE * file, osl_extbody_p ebody) {
138 osl_extbody_idump(file, ebody, 0);
143 * osl_extbody_sprint function:
144 * this function prints the content of an osl_extbody_t structure
145 * (*ebody) into a string (returned) in the OpenScop textual format.
146 * \param[in] ebody The ebody structure to print.
147 * \return A string containing the OpenScop dump of the ebodystructure.
149 char * osl_extbody_sprint(osl_extbody_p ebody) {
150 int i;
151 int high_water_mark = OSL_MAX_STRING;
152 char * string = NULL, * body_string = NULL;
153 char buffer[OSL_MAX_STRING];
155 if (ebody != NULL) {
156 OSL_malloc(string, char *, high_water_mark * sizeof(char));
157 string[0] = '\0';
159 sprintf(buffer, "# Number of accesses\n");
160 osl_util_safe_strcat(&string, buffer, &high_water_mark);
162 sprintf(buffer, "%d\n", ebody->nb_access);
163 osl_util_safe_strcat(&string, buffer, &high_water_mark);
165 if (ebody->nb_access) {
166 sprintf(buffer, "# Access coordinates (start/length)\n");
167 osl_util_safe_strcat(&string, buffer, &high_water_mark);
169 for (i = 0; i < ebody->nb_access; i++) {
170 sprintf(buffer, "%d %d\n", ebody->start[i], ebody->length[i]);
171 osl_util_safe_strcat(&string, buffer, &high_water_mark);
174 body_string = osl_body_sprint(ebody->body);
175 osl_util_safe_strcat(&string, body_string, &high_water_mark);
176 free(body_string);
179 return string;
183 /*****************************************************************************
184 * Reading function *
185 *****************************************************************************/
189 * osl_extbody_sread function:
190 * this function reads an extbody structure from a string complying
191 * to the OpenScop textual format and returns a pointer to this
192 * extbody structure. The string should contain only one textual
193 * format of an extbody structure. The input parameter is updated to
194 * the position in the input string this function reach right after reading the
195 * comment structure.
196 * \param[in,out] input The input string where to find an extbody
197 * structure.
198 * Updated to the position after what has been read.
199 * \return A pointer to the extbody structure that has been read.
201 osl_extbody_p osl_extbody_sread(char ** input) {
202 int k;
203 int nb_access;
204 osl_extbody_p ebody;
206 if (input == NULL) {
207 OSL_debug("no extbody optional tag");
208 return NULL;
211 // Find the number of ebody provided.
212 nb_access = osl_util_read_int(NULL, input);
214 // Allocate the array of start and length.
215 ebody = osl_extbody_malloc();
216 OSL_malloc(ebody->start, int *, nb_access * sizeof(int));
217 OSL_malloc(ebody->length, int *, nb_access * sizeof(int));
218 ebody->nb_access = nb_access;
220 // Get each array start/length.
221 for (k = 0; k < nb_access; k++) {
222 ebody->start[k] = osl_util_read_int(NULL, input);
223 ebody->length[k] = osl_util_read_int(NULL, input);
226 // Read simple body.
227 ebody->body = osl_body_sread(input);
229 return ebody;
233 /*+***************************************************************************
234 * Memory allocation/deallocation function *
235 *****************************************************************************/
239 * osl_extbody_malloc function:
240 * this function allocates the memory space for an osl_extbody_t
241 * structure and sets its fields with default values. Then it returns a
242 * pointer to the allocated space.
243 * \return A pointer to an empty extbody structure with fields set to
244 * default values.
246 osl_extbody_p osl_extbody_malloc() {
247 osl_extbody_p ebody;
248 OSL_malloc(ebody, osl_extbody_p, sizeof(osl_extbody_t));
250 ebody->nb_access = 0;
251 ebody->start = NULL;
252 ebody->length = NULL;
253 ebody->body = NULL;
255 return ebody;
260 * osl_extbody_free function:
261 * this function frees the allocated memory for an ebody structure.
262 * \param[in,out] ebody The pointer to the extbody structure we want to free.
264 void osl_extbody_free(osl_extbody_p ebody) {
265 if (ebody != NULL) {
266 free(ebody->start);
267 free(ebody->length);
268 osl_body_free(ebody->body);
269 free(ebody);
274 /*+***************************************************************************
275 * Processing functions *
276 *****************************************************************************/
280 * osl_extbody_clone function:
281 * this function builds and returns a "hard copy" (not a pointer copy) of an
282 * osl_extbody_t data structure.
283 * \param[in] ebody The pointer to the extbody structure to clone.
284 * \return A pointer to the clone of the extbody structure.
286 osl_extbody_p osl_extbody_clone(osl_extbody_p ebody) {
287 int i;
288 osl_extbody_p clone;
290 if (ebody == NULL)
291 return NULL;
293 clone = osl_extbody_malloc();
294 clone->nb_access = ebody->nb_access;
295 OSL_malloc(clone->start, int *, ebody->nb_access * sizeof(int));
296 OSL_malloc(clone->length, int *, ebody->nb_access * sizeof(int));
298 for (i = 0; i < ebody->nb_access; i++) {
299 clone->start[i] = ebody->start[i];
300 clone->length[i] = ebody->length[i];
303 clone->body = osl_body_clone(ebody->body);
305 return clone;
310 * osl_extbody_equal function:
311 * this function returns true if the two extbody structures are the
312 * same (content-wise), false otherwise. This functions considers two
313 * extbody structures as equal if the order of the array start/length
314 * differ, however the identifiers, start and length must be the same.
315 * \param[in] e1 The first extbody structure.
316 * \param[in] e2 The second extbody structure.
317 * \return 1 if e1 and e2 are the same (content-wise), 0 otherwise.
319 int osl_extbody_equal(osl_extbody_p e1, osl_extbody_p e2) {
320 int i, j, found;
322 if (e1 == e2)
323 return 1;
325 if (((e1 == NULL) && (e2 != NULL)) || ((e1 != NULL) && (e2 == NULL))) {
326 OSL_info("extbody are not the same");
327 return 0;
330 // Check whether the number of ebody is the same.
331 if (e1->nb_access != e2->nb_access) {
332 OSL_info("extbody are not the same");
333 return 0;
336 // We accept a different order of the start/length, as long as the
337 // identifiers are the same.
338 for (i = 0; i < e1->nb_access; i++) {
339 found = 0;
340 for (j = 0; j < e2->nb_access; j++) {
341 if ((e1->start[i] == e2->start[j]) &&
342 (e1->length[i] == e2->length[j])) {
343 found = 1;
344 break;
347 if (found != 1) {
348 OSL_info("extbody are not the same");
349 return 0;
353 return osl_body_equal(e1->body, e2->body);
358 * osl_extbody_interface function:
359 * this function creates an interface structure corresponding to the
360 * extbody extension and returns it.
361 * \return An interface structure for the extbody extension.
363 osl_interface_p osl_extbody_interface() {
364 osl_interface_p interface = osl_interface_malloc();
366 OSL_strdup(interface->URI, OSL_URI_EXTBODY);
367 interface->idump = (osl_idump_f)osl_extbody_idump;
368 interface->sprint = (osl_sprint_f)osl_extbody_sprint;
369 interface->sread = (osl_sread_f)osl_extbody_sread;
370 interface->malloc = (osl_malloc_f)osl_extbody_malloc;
371 interface->free = (osl_free_f)osl_extbody_free;
372 interface->clone = (osl_clone_f)osl_extbody_clone;
373 interface->equal = (osl_equal_f)osl_extbody_equal;
375 return interface;
380 * osl_extbody_add function:
381 * This function add an entry in the list of coordinates
383 void osl_extbody_add(osl_extbody_p ebody, int start, int length) {
384 ebody->nb_access++;
386 OSL_realloc(ebody->start, int*, sizeof(int) * ebody->nb_access);
387 OSL_realloc(ebody->length, int*, sizeof(int) * ebody->nb_access);
389 ebody->start[ebody->nb_access-1] = start;
390 ebody->length[ebody->nb_access-1] = length;