OpenScop  0.9.0
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  int i, j;
91 
92  // Go to the right level.
93  for (j = 0; j < level; j++)
94  fprintf(file, "|\t");
95 
96  if (ebody != NULL)
97  fprintf(file, "+-- osl_extbody_t\n");
98  else
99  fprintf(file, "+-- NULL extbody\n");
100 
101  if (ebody != NULL) {
102  // Go to the right level.
103  for(j = 0; j <= level; j++)
104  fprintf(file, "|\t");
105 
106  // Display the number of ebody.
107  fprintf(file, "nb_access: %d\n", ebody->nb_access);
108 
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");
114 
115  fprintf(file, "start: %d, length: %d\n",
116  ebody->start[i], ebody->length[i]);
117  }
118 
119  // Display the body
120  osl_body_idump(file, ebody->body, level);
121  }
122 
123  // The last line.
124  for (j = 0; j <= level; j++)
125  fprintf(file, "|\t");
126  fprintf(file, "\n");
127 }
128 
129 
137 void osl_extbody_dump(FILE * file, osl_extbody_p ebody) {
138  osl_extbody_idump(file, ebody, 0);
139 }
140 
141 
150  int i;
151  int high_water_mark = OSL_MAX_STRING;
152  char * string = NULL, * body_string = NULL;
153  char buffer[OSL_MAX_STRING];
154 
155  if (ebody != NULL) {
156  OSL_malloc(string, char *, high_water_mark * sizeof(char));
157  string[0] = '\0';
158 
159  sprintf(buffer, "# Number of accesses\n");
160  osl_util_safe_strcat(&string, buffer, &high_water_mark);
161 
162  sprintf(buffer, "%d\n", ebody->nb_access);
163  osl_util_safe_strcat(&string, buffer, &high_water_mark);
164 
165  if (ebody->nb_access) {
166  sprintf(buffer, "# Access coordinates (start/length)\n");
167  osl_util_safe_strcat(&string, buffer, &high_water_mark);
168  }
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);
172  }
173 
174  body_string = osl_body_sprint(ebody->body);
175  osl_util_safe_strcat(&string, body_string, &high_water_mark);
176  free(body_string);
177  }
178 
179  return string;
180 }
181 
182 
183 /*****************************************************************************
184  * Reading function *
185  *****************************************************************************/
186 
187 
202  int k;
203  int nb_access;
204  osl_extbody_p ebody;
205 
206  if (input == NULL) {
207  OSL_debug("no extbody optional tag");
208  return NULL;
209  }
210 
211  // Find the number of ebody provided.
212  nb_access = osl_util_read_int(NULL, input);
213 
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;
219 
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);
224  }
225 
226  // Read simple body.
227  ebody->body = osl_body_sread(input);
228 
229  return ebody;
230 }
231 
232 
233 /*+***************************************************************************
234  * Memory allocation/deallocation function *
235  *****************************************************************************/
236 
237 
247  osl_extbody_p ebody;
248  OSL_malloc(ebody, osl_extbody_p, sizeof(osl_extbody_t));
249 
250  ebody->nb_access = 0;
251  ebody->start = NULL;
252  ebody->length = NULL;
253  ebody->body = NULL;
254 
255  return ebody;
256 }
257 
258 
265  if (ebody != NULL) {
266  free(ebody->start);
267  free(ebody->length);
268  osl_body_free(ebody->body);
269  free(ebody);
270  }
271 }
272 
273 
274 /*+***************************************************************************
275  * Processing functions *
276  *****************************************************************************/
277 
278 
287  int i;
288  osl_extbody_p clone;
289 
290  if (ebody == NULL)
291  return NULL;
292 
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));
297 
298  for (i = 0; i < ebody->nb_access; i++) {
299  clone->start[i] = ebody->start[i];
300  clone->length[i] = ebody->length[i];
301  }
302 
303  clone->body = osl_body_clone(ebody->body);
304 
305  return clone;
306 }
307 
308 
320  int i, j, found;
321 
322  if (e1 == e2)
323  return 1;
324 
325  if (((e1 == NULL) && (e2 != NULL)) || ((e1 != NULL) && (e2 == NULL))) {
326  OSL_info("extbody are not the same");
327  return 0;
328  }
329 
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;
334  }
335 
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;
345  }
346  }
347  if (found != 1) {
348  OSL_info("extbody are not the same");
349  return 0;
350  }
351  }
352 
353  return osl_body_equal(e1->body, e2->body);
354 }
355 
356 
365 
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;
374 
375  return interface;
376 }
377 
378 
383 void osl_extbody_add(osl_extbody_p ebody, int start, int length) {
384  ebody->nb_access++;
385 
386  OSL_realloc(ebody->start, int*, sizeof(int) * ebody->nb_access);
387  OSL_realloc(ebody->length, int*, sizeof(int) * ebody->nb_access);
388 
389  ebody->start[ebody->nb_access-1] = start;
390  ebody->length[ebody->nb_access-1] = length;
391 }
osl_extbody_p osl_extbody_clone(osl_extbody_p ebody)
Definition: extbody.c:286
void *(* osl_clone_f)(void *)
Definition: interface.h:80
void osl_extbody_dump(FILE *file, osl_extbody_p ebody)
Definition: extbody.c:137
char * osl_body_sprint(osl_body_p body)
Definition: body.c:199
osl_interface_p osl_interface_malloc()
Definition: interface.c:212
void *(* osl_sread_f)(char **)
Definition: interface.h:77
void osl_util_safe_strcat(char **dst, char *src, int *hwm)
Definition: util.c:483
osl_body_p body
Definition: extbody.h:86
osl_extbody_p osl_extbody_malloc()
Definition: extbody.c:246
void(* osl_idump_f)(FILE *, void *, int)
Definition: interface.h:75
int nb_access
Definition: extbody.h:87
void osl_extbody_free(osl_extbody_p ebody)
Definition: extbody.c:264
osl_body_p osl_body_sread(char **input)
Definition: body.c:255
void osl_body_idump(FILE *file, osl_body_p body, int level)
Definition: body.c:90
int * length
Definition: extbody.h:89
int osl_util_read_int(FILE *file, char **str)
Definition: util.c:140
char *(* osl_sprint_f)(void *)
Definition: interface.h:76
char * osl_extbody_sprint(osl_extbody_p ebody)
Definition: extbody.c:149
void osl_extbody_idump(FILE *file, osl_extbody_p ebody, int level)
Definition: extbody.c:89
int * start
Definition: extbody.h:88
osl_extbody_p osl_extbody_sread(char **input)
Definition: extbody.c:201
void *(* osl_malloc_f)()
Definition: interface.h:78
osl_interface_p osl_extbody_interface()
Definition: extbody.c:363
int osl_body_equal(osl_body_p b1, osl_body_p b2)
Definition: body.c:359
int osl_extbody_equal(osl_extbody_p e1, osl_extbody_p e2)
Definition: extbody.c:319
int(* osl_equal_f)(void *, void *)
Definition: interface.h:81
void osl_extbody_add(osl_extbody_p ebody, int start, int length)
Definition: extbody.c:383
osl_body_p osl_body_clone(osl_body_p body)
Definition: body.c:337
void osl_body_free(osl_body_p body)
Definition: body.c:314
void(* osl_free_f)(void *)
Definition: interface.h:79