OpenScop  0.9.1
extbody.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** extensions/extbody.c **
6  **-----------------------------------------------------------------**
7  ** First version: 07/12/2010 **
8  **-----------------------------------------------------------------**
9 
10 
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  *****************************************************************************/
62 
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <ctype.h>
67 
68 #include <osl/macros.h>
69 #include <osl/util.h>
70 #include <osl/body.h>
71 #include <osl/extensions/extbody.h>
72 
73 
74 /*+***************************************************************************
75  * Structure display function *
76  *****************************************************************************/
77 
78 
89 void osl_extbody_idump(FILE * file, osl_extbody_p ebody, int level) {
90  size_t i;
91  int j;
92 
93  // Go to the right level.
94  for (j = 0; j < level; j++)
95  fprintf(file, "|\t");
96 
97  if (ebody != NULL)
98  fprintf(file, "+-- osl_extbody_t\n");
99  else
100  fprintf(file, "+-- NULL extbody\n");
101 
102  if (ebody != NULL) {
103  // Go to the right level.
104  for(j = 0; j <= level; j++)
105  fprintf(file, "|\t");
106 
107  // Display the number of ebody.
108  fprintf(file, "nb_access: %zu\n", ebody->nb_access);
109 
110  // Display the coordinates.
111  for(i = 0; i < ebody->nb_access; i++) {
112  // Go to the right level.
113  for(j = 0; j <= level; j++)
114  fprintf(file, "|\t");
115 
116  fprintf(file, "start: %d, length: %d\n",
117  ebody->start[i], ebody->length[i]);
118  }
119 
120  // Display the body
121  osl_body_idump(file, ebody->body, level);
122  }
123 
124  // The last line.
125  for (j = 0; j <= level; j++)
126  fprintf(file, "|\t");
127  fprintf(file, "\n");
128 }
129 
130 
138 void osl_extbody_dump(FILE * file, osl_extbody_p ebody) {
139  osl_extbody_idump(file, ebody, 0);
140 }
141 
142 
151  size_t i;
152  size_t high_water_mark = OSL_MAX_STRING;
153  char * string = NULL, * body_string = NULL;
154  char buffer[OSL_MAX_STRING];
155 
156  if (ebody != NULL) {
157  OSL_malloc(string, char *, high_water_mark * sizeof(char));
158  string[0] = '\0';
159 
160  sprintf(buffer, "# Number of accesses\n");
161  osl_util_safe_strcat(&string, buffer, &high_water_mark);
162 
163  sprintf(buffer, "%zu\n", ebody->nb_access);
164  osl_util_safe_strcat(&string, buffer, &high_water_mark);
165 
166  if (ebody->nb_access) {
167  sprintf(buffer, "# Access coordinates (start/length)\n");
168  osl_util_safe_strcat(&string, buffer, &high_water_mark);
169  }
170  for (i = 0; i < ebody->nb_access; i++) {
171  sprintf(buffer, "%d %d\n", ebody->start[i], ebody->length[i]);
172  osl_util_safe_strcat(&string, buffer, &high_water_mark);
173  }
174 
175  body_string = osl_body_sprint(ebody->body);
176  osl_util_safe_strcat(&string, body_string, &high_water_mark);
177  free(body_string);
178  }
179 
180  return string;
181 }
182 
183 
184 /*****************************************************************************
185  * Reading function *
186  *****************************************************************************/
187 
188 
203  size_t k, nb_access_unsigned;
204  int nb_access;
205  osl_extbody_p ebody;
206 
207  if (input == NULL) {
208  OSL_debug("no extbody optional tag");
209  return NULL;
210  }
211 
212  // Find the number of ebody provided.
213  nb_access = osl_util_read_int(NULL, input);
214  if (nb_access < 0) {
215  OSL_error("negative number of access");
216  }
217  nb_access_unsigned = (size_t) nb_access;
218 
219  // Allocate the array of start and length.
220  ebody = osl_extbody_malloc();
221  OSL_malloc(ebody->start, int *, nb_access_unsigned * sizeof(int));
222  OSL_malloc(ebody->length, int *, nb_access_unsigned * sizeof(int));
223  ebody->nb_access = nb_access_unsigned;
224 
225  // Get each array start/length.
226  for (k = 0; k < nb_access_unsigned; k++) {
227  ebody->start[k] = osl_util_read_int(NULL, input);
228  ebody->length[k] = osl_util_read_int(NULL, input);
229  }
230 
231  // Read simple body.
232  ebody->body = osl_body_sread(input);
233 
234  return ebody;
235 }
236 
237 
238 /*+***************************************************************************
239  * Memory allocation/deallocation function *
240  *****************************************************************************/
241 
242 
252  osl_extbody_p ebody;
253  OSL_malloc(ebody, osl_extbody_p, sizeof(osl_extbody_t));
254 
255  ebody->nb_access = 0;
256  ebody->start = NULL;
257  ebody->length = NULL;
258  ebody->body = NULL;
259 
260  return ebody;
261 }
262 
263 
270  if (ebody != NULL) {
271  free(ebody->start);
272  free(ebody->length);
273  osl_body_free(ebody->body);
274  free(ebody);
275  }
276 }
277 
278 
279 /*+***************************************************************************
280  * Processing functions *
281  *****************************************************************************/
282 
283 
292  size_t i;
293  osl_extbody_p clone;
294 
295  if (ebody == NULL)
296  return NULL;
297 
298  clone = osl_extbody_malloc();
299  clone->nb_access = ebody->nb_access;
300  OSL_malloc(clone->start, int *, ebody->nb_access * sizeof(int));
301  OSL_malloc(clone->length, int *, ebody->nb_access * sizeof(int));
302 
303  for (i = 0; i < ebody->nb_access; i++) {
304  clone->start[i] = ebody->start[i];
305  clone->length[i] = ebody->length[i];
306  }
307 
308  clone->body = osl_body_clone(ebody->body);
309 
310  return clone;
311 }
312 
313 
325  size_t i, j, found;
326 
327  if (e1 == e2)
328  return 1;
329 
330  if (((e1 == NULL) && (e2 != NULL)) || ((e1 != NULL) && (e2 == NULL))) {
331  OSL_info("extbody are not the same");
332  return 0;
333  }
334 
335  // Check whether the number of ebody is the same.
336  if (e1->nb_access != e2->nb_access) {
337  OSL_info("extbody are not the same");
338  return 0;
339  }
340 
341  // We accept a different order of the start/length, as long as the
342  // identifiers are the same.
343  for (i = 0; i < e1->nb_access; i++) {
344  found = 0;
345  for (j = 0; j < e2->nb_access; j++) {
346  if ((e1->start[i] == e2->start[j]) &&
347  (e1->length[i] == e2->length[j])) {
348  found = 1;
349  break;
350  }
351  }
352  if (found != 1) {
353  OSL_info("extbody are not the same");
354  return 0;
355  }
356  }
357 
358  return osl_body_equal(e1->body, e2->body);
359 }
360 
361 
370 
371  OSL_strdup(interface->URI, OSL_URI_EXTBODY);
372  interface->idump = (osl_idump_f)osl_extbody_idump;
373  interface->sprint = (osl_sprint_f)osl_extbody_sprint;
374  interface->sread = (osl_sread_f)osl_extbody_sread;
375  interface->malloc = (osl_malloc_f)osl_extbody_malloc;
376  interface->free = (osl_free_f)osl_extbody_free;
377  interface->clone = (osl_clone_f)osl_extbody_clone;
378  interface->equal = (osl_equal_f)osl_extbody_equal;
379 
380  return interface;
381 }
382 
383 
388 void osl_extbody_add(osl_extbody_p ebody, int start, int length) {
389  ebody->nb_access++;
390 
391  OSL_realloc(ebody->start, int*, sizeof(int) * ebody->nb_access);
392  OSL_realloc(ebody->length, int*, sizeof(int) * ebody->nb_access);
393 
394  ebody->start[ebody->nb_access-1] = start;
395  ebody->length[ebody->nb_access-1] = length;
396 }
void *(* osl_clone_f)(void *)
Definition: interface.h:80
#define OSL_error(msg)
Definition: macros.h:149
void *(* osl_sread_f)(char **)
Definition: interface.h:77
void osl_extbody_add(osl_extbody_p ebody, int start, int length)
Definition: extbody.c:388
osl_body_p osl_body_clone(osl_body_p body)
Definition: body.c:337
int osl_util_read_int(FILE *file, char **str)
Definition: util.c:140
size_t nb_access
Definition: extbody.h:87
void(* osl_idump_f)(FILE *, void *, int)
Definition: interface.h:75
osl_interface_p osl_interface_malloc(void)
Definition: interface.c:212
osl_extbody_p osl_extbody_malloc(void)
Definition: extbody.c:251
char * osl_extbody_sprint(osl_extbody_p ebody)
Definition: extbody.c:150
#define OSL_info(msg)
Definition: macros.h:139
void osl_util_safe_strcat(char **dst, char *src, size_t *hwm)
Definition: util.c:482
char *(* osl_sprint_f)(void *)
Definition: interface.h:76
osl_body_p body
Definition: extbody.h:86
#define OSL_debug(msg)
Definition: macros.h:133
int osl_extbody_equal(osl_extbody_p e1, osl_extbody_p e2)
Definition: extbody.c:324
osl_interface_p osl_extbody_interface(void)
Definition: extbody.c:368
osl_body_p osl_body_sread(char **input)
Definition: body.c:255
void osl_extbody_free(osl_extbody_p ebody)
Definition: extbody.c:269
#define OSL_strdup(destination, source)
Definition: macros.h:169
void *(* osl_malloc_f)(void)
Definition: interface.h:78
int * length
Definition: extbody.h:89
#define OSL_realloc(ptr, type, size)
Definition: macros.h:163
char * osl_body_sprint(osl_body_p body)
Definition: body.c:199
osl_extbody_p osl_extbody_sread(char **input)
Definition: extbody.c:202
int(* osl_equal_f)(void *, void *)
Definition: interface.h:81
int * start
Definition: extbody.h:88
#define OSL_MAX_STRING
Definition: macros.h:94
void(* osl_free_f)(void *)
Definition: interface.h:79
osl_extbody_p osl_extbody_clone(osl_extbody_p ebody)
Definition: extbody.c:291
void osl_body_idump(FILE *file, osl_body_p body, int level)
Definition: body.c:90
int osl_body_equal(osl_body_p b1, osl_body_p b2)
Definition: body.c:359
#define OSL_URI_EXTBODY
Definition: extbody.h:78
void osl_extbody_idump(FILE *file, osl_extbody_p ebody, int level)
Definition: extbody.c:89
void osl_extbody_dump(FILE *file, osl_extbody_p ebody)
Definition: extbody.c:138
void osl_body_free(osl_body_p body)
Definition: body.c:314
#define OSL_malloc(ptr, type, size)
Definition: macros.h:157