OpenScop  0.9.0
extbody.c
Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                      extensions/extbody.c                       **
00006      **-----------------------------------------------------------------**
00007      **                   First version: 07/12/2010                     **
00008      **-----------------------------------------------------------------**
00009 
00010 
00011  *****************************************************************************
00012  * OpenScop: Structures and formats for polyhedral tools to talk together    *
00013  *****************************************************************************
00014  *    ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__,                *
00015  *    /   / /  //  //  //  // /   / /  //  //   / /  // /  /|,_,             *
00016  *   /   / /  //  //  //  // /   / /  //  //   / /  // /  / / /\             *
00017  *  |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/  \            *
00018  *  | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\  \ /\           *
00019  *  | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\          *
00020  *  | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \  \         *
00021  *  | P |n| l | = | s | t |=| = |d| = | = | = | |   |=| o | | \# \  \        *
00022  *  | H | | y |   | e | o | | = |l|   |   | = | |   | | G | |  \  \  \       *
00023  *  | I | |   |   | e |   | |   | |   |   |   | |   | |   | |   \  \  \      *
00024  *  | T | |   |   |   |   | |   | |   |   |   | |   | |   | |    \  \  \     *
00025  *  | E | |   |   |   |   | |   | |   |   |   | |   | |   | |     \  \  \    *
00026  *  | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | /      \* \  \   *
00027  *  | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/        \  \ /   *
00028  *  '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---'          '--'    *
00029  *                                                                           *
00030  * Copyright (C) 2008 University Paris-Sud 11 and INRIA                      *
00031  *                                                                           *
00032  * (3-clause BSD license)                                                    *
00033  * Redistribution and use in source  and binary forms, with or without       *
00034  * modification, are permitted provided that the following conditions        *
00035  * are met:                                                                  *
00036  *                                                                           *
00037  * 1. Redistributions of source code must retain the above copyright notice, *
00038  *    this list of conditions and the following disclaimer.                  *
00039  * 2. Redistributions in binary form must reproduce the above copyright      *
00040  *    notice, this list of conditions and the following disclaimer in the    *
00041  *    documentation and/or other materials provided with the distribution.   *
00042  * 3. The name of the author may not be used to endorse or promote products  *
00043  *    derived from this software without specific prior written permission.  *
00044  *                                                                           *
00045  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      *
00046  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
00047  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   *
00048  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          *
00049  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  *
00050  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
00051  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     *
00052  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       *
00053  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  *
00054  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         *
00055  *                                                                           *
00056  * OpenScop Library, a library to manipulate OpenScop formats and data       *
00057  * structures. Written by:                                                   *
00058  * Cedric Bastoul     <Cedric.Bastoul@u-psud.fr> and                         *
00059  * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr>                          *
00060  *                                                                           *
00061  *****************************************************************************/
00062 
00063 #include <stdlib.h>
00064 #include <stdio.h>
00065 #include <string.h>
00066 #include <ctype.h>
00067 
00068 #include <osl/macros.h>
00069 #include <osl/util.h>
00070 #include <osl/body.h>
00071 #include <osl/extensions/extbody.h>
00072 
00073 
00074 /*+***************************************************************************
00075  *                          Structure display function                       *
00076  *****************************************************************************/
00077 
00078 
00089 void osl_extbody_idump(FILE * file, osl_extbody_p ebody, int level) {
00090   int i, j;
00091 
00092   // Go to the right level.
00093   for (j = 0; j < level; j++)
00094     fprintf(file, "|\t");
00095 
00096   if (ebody != NULL)
00097     fprintf(file, "+-- osl_extbody_t\n");
00098   else
00099     fprintf(file, "+-- NULL extbody\n");
00100 
00101   if (ebody != NULL) {
00102     // Go to the right level.
00103     for(j = 0; j <= level; j++)
00104       fprintf(file, "|\t");
00105 
00106     // Display the number of ebody.
00107     fprintf(file, "nb_access: %d\n", ebody->nb_access);
00108 
00109     // Display the coordinates.
00110     for(i = 0; i < ebody->nb_access; i++) {
00111       // Go to the right level.
00112       for(j = 0; j <= level; j++)
00113         fprintf(file, "|\t");
00114 
00115       fprintf(file, "start: %d, length: %d\n",
00116               ebody->start[i], ebody->length[i]);
00117     }
00118 
00119     // Display the body
00120     osl_body_idump(file, ebody->body, level);
00121   }
00122 
00123   // The last line.
00124   for (j = 0; j <= level; j++)
00125     fprintf(file, "|\t");
00126   fprintf(file, "\n");
00127 }
00128 
00129 
00137 void osl_extbody_dump(FILE * file, osl_extbody_p ebody) {
00138   osl_extbody_idump(file, ebody, 0);
00139 }
00140 
00141 
00149 char * osl_extbody_sprint(osl_extbody_p ebody) {
00150   int i;
00151   int high_water_mark = OSL_MAX_STRING;
00152   char * string = NULL, * body_string = NULL;
00153   char buffer[OSL_MAX_STRING];
00154 
00155   if (ebody != NULL) {
00156     OSL_malloc(string, char *, high_water_mark * sizeof(char));
00157     string[0] = '\0';
00158 
00159     sprintf(buffer, "# Number of accesses\n");
00160     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00161 
00162     sprintf(buffer, "%d\n", ebody->nb_access);
00163     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00164 
00165     if (ebody->nb_access) {
00166       sprintf(buffer, "# Access coordinates (start/length)\n");
00167       osl_util_safe_strcat(&string, buffer, &high_water_mark);
00168     }
00169     for (i = 0; i < ebody->nb_access; i++) {
00170       sprintf(buffer, "%d %d\n", ebody->start[i], ebody->length[i]);
00171       osl_util_safe_strcat(&string, buffer, &high_water_mark);
00172     }
00173 
00174     body_string = osl_body_sprint(ebody->body);
00175     osl_util_safe_strcat(&string, body_string, &high_water_mark);
00176     free(body_string);
00177   }
00178 
00179   return string;
00180 }
00181 
00182 
00183 /*****************************************************************************
00184  *                               Reading function                            *
00185  *****************************************************************************/
00186 
00187 
00201 osl_extbody_p osl_extbody_sread(char ** input) {
00202   int k;
00203   int nb_access;
00204   osl_extbody_p ebody;
00205 
00206   if (input == NULL) {
00207     OSL_debug("no extbody optional tag");
00208     return NULL;
00209   }
00210 
00211   // Find the number of ebody provided.
00212   nb_access = osl_util_read_int(NULL, input);
00213 
00214   // Allocate the array of start and length.
00215   ebody = osl_extbody_malloc();
00216   OSL_malloc(ebody->start, int *, nb_access * sizeof(int));
00217   OSL_malloc(ebody->length, int *, nb_access * sizeof(int));
00218   ebody->nb_access = nb_access;
00219 
00220   // Get each array start/length.
00221   for (k = 0; k < nb_access; k++) {
00222     ebody->start[k] = osl_util_read_int(NULL, input);
00223     ebody->length[k] = osl_util_read_int(NULL, input);
00224   }
00225 
00226   // Read simple body.
00227   ebody->body = osl_body_sread(input);
00228 
00229   return ebody;
00230 }
00231 
00232 
00233 /*+***************************************************************************
00234  *                    Memory allocation/deallocation function                *
00235  *****************************************************************************/
00236 
00237 
00246 osl_extbody_p osl_extbody_malloc() {
00247   osl_extbody_p ebody;
00248   OSL_malloc(ebody, osl_extbody_p, sizeof(osl_extbody_t));
00249 
00250   ebody->nb_access = 0;
00251   ebody->start     = NULL;
00252   ebody->length    = NULL;
00253   ebody->body      = NULL;
00254 
00255   return ebody;
00256 }
00257 
00258 
00264 void osl_extbody_free(osl_extbody_p ebody) {
00265   if (ebody != NULL) {
00266     free(ebody->start);
00267     free(ebody->length);
00268     osl_body_free(ebody->body);
00269     free(ebody);
00270   }
00271 }
00272 
00273 
00274 /*+***************************************************************************
00275  *                            Processing functions                           *
00276  *****************************************************************************/
00277 
00278 
00286 osl_extbody_p osl_extbody_clone(osl_extbody_p ebody) {
00287   int i;
00288   osl_extbody_p clone;
00289 
00290   if (ebody == NULL)
00291     return NULL;
00292 
00293   clone = osl_extbody_malloc();
00294   clone->nb_access = ebody->nb_access;
00295   OSL_malloc(clone->start, int *, ebody->nb_access * sizeof(int));
00296   OSL_malloc(clone->length, int *, ebody->nb_access * sizeof(int));
00297 
00298   for (i = 0; i < ebody->nb_access; i++) {
00299     clone->start[i]  = ebody->start[i];
00300     clone->length[i] = ebody->length[i];
00301   }
00302 
00303   clone->body = osl_body_clone(ebody->body);
00304 
00305   return clone;
00306 }
00307 
00308 
00319 int osl_extbody_equal(osl_extbody_p e1, osl_extbody_p e2) {
00320   int i, j, found;
00321 
00322   if (e1 == e2)
00323     return 1;
00324 
00325   if (((e1 == NULL) && (e2 != NULL)) || ((e1 != NULL) && (e2 == NULL))) {
00326     OSL_info("extbody are not the same");
00327     return 0;
00328   }
00329 
00330   // Check whether the number of ebody is the same.
00331   if (e1->nb_access != e2->nb_access) {
00332     OSL_info("extbody are not the same");
00333     return 0;
00334   }
00335 
00336   // We accept a different order of the start/length, as long as the
00337   // identifiers are the same.
00338   for (i = 0; i < e1->nb_access; i++) {
00339     found = 0;
00340     for (j = 0; j < e2->nb_access; j++) {
00341       if ((e1->start[i] == e2->start[j]) &&
00342           (e1->length[i] == e2->length[j])) {
00343         found = 1;
00344         break;
00345       }
00346     }
00347     if (found != 1) {
00348       OSL_info("extbody are not the same");
00349       return 0;
00350     }
00351   }
00352 
00353   return osl_body_equal(e1->body, e2->body);
00354 }
00355 
00356 
00363 osl_interface_p osl_extbody_interface() {
00364   osl_interface_p interface = osl_interface_malloc();
00365 
00366   OSL_strdup(interface->URI, OSL_URI_EXTBODY);
00367   interface->idump  = (osl_idump_f)osl_extbody_idump;
00368   interface->sprint = (osl_sprint_f)osl_extbody_sprint;
00369   interface->sread  = (osl_sread_f)osl_extbody_sread;
00370   interface->malloc = (osl_malloc_f)osl_extbody_malloc;
00371   interface->free   = (osl_free_f)osl_extbody_free;
00372   interface->clone  = (osl_clone_f)osl_extbody_clone;
00373   interface->equal  = (osl_equal_f)osl_extbody_equal;
00374 
00375   return interface;
00376 }
00377 
00378 
00383 void osl_extbody_add(osl_extbody_p ebody, int start, int length) {
00384   ebody->nb_access++;
00385 
00386   OSL_realloc(ebody->start, int*, sizeof(int) * ebody->nb_access);
00387   OSL_realloc(ebody->length, int*, sizeof(int) * ebody->nb_access);
00388 
00389   ebody->start[ebody->nb_access-1] = start;
00390   ebody->length[ebody->nb_access-1] = length;
00391 }