Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove dead code
[simgrid.git] / src / gras / DataDesc / ddt_create.c
index 3a91cb5..408ef38 100644 (file)
@@ -2,7 +2,9 @@
 
 /* ddt_new - creation/deletion of datatypes structs (private to this module)*/
 
-/* Copyright (c) 2004 Olivier Aumage, Martin Quinson. All rights reserved.  */
+/* Copyright (c) 2003 Olivier Aumage.                                       */
+/* Copyright (c) 2003, 2004 Martin Quinson.                                 */
+/* All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -101,44 +103,9 @@ gras_datadesc_type_t
   res = gras_ddt_new(name);
 
   for (arch = 0; arch < gras_arch_count; arch ++) {
-#if 0
-    long int sz;
-    long int mask;
-#endif
     res->size[arch]         = gras_arches[arch].sizeofs[type];
     res->alignment[arch]    = gras_arches[arch].boundaries[type];
     res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
-/* FIXME: kill the following after discution with Oli */
-#if 0
-    sz = res->size[arch];
-    mask = sz;
-    
-    /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
-       and a size is always>=0 */
-    
-    /* make sure mask have all the bits under the biggest one of size set to 1
-       Example: size=000100101 => mask=0000111111 */
-    while ((sz >>= 1)) {
-      mask |= sz;
-    } 
-    
-    if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
-      /* size is not a power of 2 */
-      /* alignment= next power of 2 after size */
-      res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
-      xbt_assert0(res->alignment[arch] != 0,
-                  "scalar type too large");
-      
-      res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
-      xbt_assert0 (res->aligned_size[arch] >= 0,
-                   "scalar type too large");
-      
-    } else {
-      /* size is a power of 2, life is great */
-      res->alignment[arch]       = res->size[arch];
-      res->aligned_size[arch]    = res->size[arch];
-    }
-#endif     
   }
 
   res->category_code                 = e_gras_datadesc_type_cat_scalar;
@@ -272,6 +239,11 @@ void
 gras_datadesc_struct_close(gras_datadesc_type_t struct_type) {
   XBT_IN;
   struct_type->category.struct_data.closed = 1;
+  DEBUG4("structure %s closed. size=%ld,align=%ld,asize=%ld",
+        struct_type->name,
+        struct_type->size[GRAS_THISARCH], 
+        struct_type->alignment[GRAS_THISARCH], 
+        struct_type->aligned_size[GRAS_THISARCH]);
 }
 
 /**
@@ -760,3 +732,160 @@ void gras_datadesc_free(gras_datadesc_type_t *type) {
   xbt_free(*type);
   type=NULL;
 }
+
+/**
+ * gras_datadesc_type_cmp:
+ *
+ * Compares two datadesc types with the same semantic than strcmp.
+ *
+ * This comparison does not take the set headers into account (name and ID), 
+ * but only the payload (actual type description).
+ */
+int gras_datadesc_type_cmp(const gras_datadesc_type_t d1,
+                          const gras_datadesc_type_t d2) {
+  int ret,cpt;
+  gras_dd_cat_field_t field1,field2;
+  gras_datadesc_type_t field_desc_1,field_desc_2;
+
+  if (d1 == d2) return 0; /* easy optimization */
+
+  if (!d1 && d2) {
+    DEBUG0("ddt_cmp: !d1 && d2 => 1");
+    return 1;
+  }
+  if (!d1 && !d2) {
+    DEBUG0("ddt_cmp: !d1 && !d2 => 0");
+    return 0;
+  }
+  if ( d1 && !d2) {
+    DEBUG0("ddt_cmp: d1 && !d2 => -1");
+    return -1;
+  }
+
+  for (cpt=0; cpt<gras_arch_count; cpt++) {
+    if (d1->size[cpt] != d2->size[cpt]) {
+      DEBUG5("ddt_cmp: %s->size=%ld  !=  %s->size=%ld (on %s)",
+            d1->name,d1->size[cpt],d2->name,d2->size[cpt],
+            gras_arches[cpt].name);
+      return d1->size[cpt] >  d2->size[cpt] ? 1 : -1;
+    }
+
+    if (d1->alignment[cpt] != d2->alignment[cpt]) {
+      DEBUG5("ddt_cmp: %s->alignment=%ld  !=  %s->alignment=%ld (on %s)",
+            d1->name,d1->alignment[cpt],d2->name,d2->alignment[cpt],
+            gras_arches[cpt].name);
+      return d1->alignment[cpt] > d2->alignment[cpt] ? 1 : -1;
+    }
+
+    if (d1->aligned_size[cpt] != d2->aligned_size[cpt]) {
+      DEBUG5("ddt_cmp: %s->aligned_size=%ld  !=  %s->aligned_size=%ld (on %s)",
+            d1->name,d1->aligned_size[cpt],d2->name,d2->aligned_size[cpt],
+            gras_arches[cpt].name);
+      return d1->aligned_size[cpt] > d2->aligned_size[cpt] ? 1 : -1;
+    }
+  }
+
+  if (d1->category_code != d2->category_code) {
+    DEBUG4("ddt_cmp: %s->cat=%s  !=  %s->cat=%s",
+          d1->name,gras_datadesc_cat_names[d1->category_code],
+          d2->name,gras_datadesc_cat_names[d2->category_code]);
+    return d1->category_code > d2->category_code ? 1 : -1;
+  }
+
+  if (d1->send != d2->send) {
+    DEBUG4("ddt_cmp: %s->send=%p  !=  %s->send=%p",
+          d1->name,(void*)d1->send, d2->name,(void*)d2->send);
+    return 1; /* ISO C forbids ordered comparisons of pointers to functions */
+  }
+
+  if (d1->recv != d2->recv) {
+    DEBUG4("ddt_cmp: %s->recv=%p  !=  %s->recv=%p",
+          d1->name,(void*)d1->recv, d2->name,(void*)d2->recv);
+    return 1; /* ISO C forbids ordered comparisons of pointers to functions */
+  }
+
+  switch (d1->category_code) {
+  case e_gras_datadesc_type_cat_scalar:
+    if (d1->category.scalar_data.encoding != d2->category.scalar_data.encoding)
+      return d1->category.scalar_data.encoding > d2->category.scalar_data.encoding ? 1 : -1 ;
+    break;
+    
+  case e_gras_datadesc_type_cat_struct:    
+    if (xbt_dynar_length(d1->category.struct_data.fields) != 
+       xbt_dynar_length(d2->category.struct_data.fields)) {
+      DEBUG4("ddt_cmp: %s (having %lu fields) !=  %s (having %lu fields)",
+            d1->name, xbt_dynar_length(d1->category.struct_data.fields),
+            d2->name, xbt_dynar_length(d2->category.struct_data.fields));
+      
+      return xbt_dynar_length(d1->category.struct_data.fields) >
+       xbt_dynar_length(d2->category.struct_data.fields) ?
+       1 : -1;
+    }
+    xbt_dynar_foreach(d1->category.struct_data.fields, cpt, field1) {
+      
+      field2 = xbt_dynar_get_as(d2->category.struct_data.fields, cpt, gras_dd_cat_field_t);
+      field_desc_1 = field1->type;
+      field_desc_2 = field2->type;
+      ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2);
+      if (ret) {
+       DEBUG6("%s->field[%d]=%s != %s->field[%d]=%s",
+              d1->name,cpt,field1->name,              
+              d2->name,cpt,field2->name);
+       return ret;
+      }
+      
+    }
+    break;
+    
+  case e_gras_datadesc_type_cat_union:
+    if (d1->category.union_data.selector != d2->category.union_data.selector) 
+      return 1;  /* ISO C forbids ordered comparisons of pointers to functions */
+    
+    if (xbt_dynar_length(d1->category.union_data.fields) != 
+       xbt_dynar_length(d2->category.union_data.fields))
+      return xbt_dynar_length(d1->category.union_data.fields) >
+            xbt_dynar_length(d2->category.union_data.fields) ?
+       1 : -1;
+    
+    xbt_dynar_foreach(d1->category.union_data.fields, cpt, field1) {
+      
+      field2 = xbt_dynar_get_as(d2->category.union_data.fields, cpt, gras_dd_cat_field_t);
+      field_desc_1 = field1->type;
+      field_desc_2 = field2->type;
+      ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2);
+      if (ret)
+       return ret;
+      
+    }
+    break;
+    
+    
+  case e_gras_datadesc_type_cat_ref:
+    if (d1->category.ref_data.selector != d2->category.ref_data.selector) 
+      return 1; /* ISO C forbids ordered comparisons of pointers to functions */
+    
+    if (d1->category.ref_data.type != d2->category.ref_data.type) 
+      return d1->category.ref_data.type > d2->category.ref_data.type ? 1 : -1;
+    break;
+    
+  case e_gras_datadesc_type_cat_array:
+    if (d1->category.array_data.type != d2->category.array_data.type) 
+      return d1->category.array_data.type > d2->category.array_data.type ? 1 : -1;
+    
+    if (d1->category.array_data.fixed_size != d2->category.array_data.fixed_size) 
+      return d1->category.array_data.fixed_size > d2->category.array_data.fixed_size ? 1 : -1;
+    
+    if (d1->category.array_data.dynamic_size != d2->category.array_data.dynamic_size) 
+      return 1; /* ISO C forbids ordered comparisons of pointers to functions */
+    
+    break;
+    
+  case e_gras_datadesc_type_cat_ignored:
+    /* That's ignored... */
+  default:
+    /* two stupidly created ddt are equally stupid ;) */
+    break;
+  }
+  return 0;
+  
+}