Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Interface revolution: do not try to survive to malloc failure
[simgrid.git] / src / gras / DataDesc / ddt_create.c
index a4bb66b..757b6cf 100644 (file)
@@ -8,9 +8,9 @@
 /* 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. */
 
-#include "DataDesc/datadesc_private.h"
+#include "gras/DataDesc/datadesc_private.h"
 
-GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc);
+GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc,"Creating new datadescriptions");
 
 /**
  * gras_ddt_freev:
@@ -25,26 +25,22 @@ void gras_ddt_freev(void *ddt) {
   }
 }
 
-static gras_error_t
+static void
 gras_ddt_new(const char            *name,
             gras_datadesc_type_t **dst) {
   gras_error_t errcode;
   gras_datadesc_type_t *res;
 
   GRAS_IN1("(%s)",name);
-  res=malloc(sizeof(gras_datadesc_type_t));
-  if (!res) 
-    RAISE_MALLOC;
+  res=gras_new0(gras_datadesc_type_t,1);
 
-  memset(res, 0, sizeof(gras_datadesc_type_t));
-  res->name = strdup(name);
+  res->name = (char*)strdup(name);
   res->name_len = strlen(name);
       
   *dst=res;
-  TRY(gras_set_add(gras_datadesc_set_local,
-                  (gras_set_elm_t*)res,&gras_ddt_freev));
+  gras_set_add(gras_datadesc_set_local,
+              (gras_set_elm_t*)res,&gras_ddt_freev);
   GRAS_OUT;
-  return no_error;
 }
 
 /**
@@ -84,13 +80,11 @@ gras_error_t gras_datadesc_by_id(long int               code,
  *
  * Create a new scalar and give a pointer to it 
  */
-gras_error_t 
-gras_datadesc_scalar(const char                      *name,
-                    gras_ddt_scalar_type_t           type,
-                    enum e_gras_dd_scalar_encoding   encoding,
-                    gras_datadesc_type_t           **dst) {
+gras_datadesc_type_t *
+  gras_datadesc_scalar(const char                      *name,
+                      gras_ddt_scalar_type_t           type,
+                      enum e_gras_dd_scalar_encoding   encoding) {
 
-  gras_error_t errcode;
   gras_datadesc_type_t *res;
   long int arch;
 
@@ -104,17 +98,18 @@ gras_datadesc_scalar(const char                      *name,
     gras_assert1(res->category.scalar_data.type == type,
                 "Redefinition of type %s does not match", name);
     VERB1("Discarding redefinition of %s",name);
-    *dst=res;
-    return no_error;
+    return res;
   }
-  TRY(gras_ddt_new(name,dst));
-  res=*dst;
+  gras_ddt_new(name,&res);
 
   for (arch = 0; arch < gras_arch_count; arch ++) {
     long int sz;
     long int mask;
-    res->size[arch] = gras_arches[arch].sizeof_scalars[type];
-
+    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;
     
@@ -143,13 +138,15 @@ gras_datadesc_scalar(const char                      *name,
       res->alignment[arch]       = res->size[arch];
       res->aligned_size[arch]    = res->size[arch];
     }
+#endif     
   }
 
   res->category_code                 = e_gras_datadesc_type_cat_scalar;
   res->category.scalar_data.encoding = encoding;
   res->category.scalar_data.type     = type;
   GRAS_OUT;
-  return no_error;
+  
+  return res;
 }
 
 
@@ -163,8 +160,8 @@ void gras_dd_cat_field_free(void *f) {
   GRAS_IN;
   if (field) {
     if (field->name) 
-      free(field->name);
-    free(field);
+      gras_free(field->name);
+    gras_free(field);
   }
   GRAS_OUT;
 }
@@ -174,9 +171,8 @@ void gras_dd_cat_field_free(void *f) {
  *
  * Create a new struct and give a pointer to it 
  */
-gras_error_t 
-gras_datadesc_struct(const char            *name,
-                    gras_datadesc_type_t **dst) {
+gras_datadesc_type_t *
+  gras_datadesc_struct(const char            *name) {
 
   gras_error_t errcode;
   gras_datadesc_type_t *res;
@@ -189,11 +185,9 @@ gras_datadesc_struct(const char            *name,
     gras_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
                 "Redefinition of type %s does not match", name);
     VERB1("Discarding redefinition of %s",name);
-    *dst=res;
-    return no_error;
+    return res;
   }
-  TRY(gras_ddt_new(name,dst));
-  res=*dst;
+  gras_ddt_new(name,&res);
 
   for (arch=0; arch<gras_arch_count; arch ++) {
     res->size[arch] = 0;
@@ -201,12 +195,12 @@ gras_datadesc_struct(const char            *name,
     res->aligned_size[arch] = 0;
   }
   res->category_code = e_gras_datadesc_type_cat_struct;
-  TRY(gras_dynar_new(&(res->category.struct_data.fields),
-                    sizeof(gras_dd_cat_field_t*),
-                    &gras_dd_cat_field_free));
+  gras_dynar_new(&(res->category.struct_data.fields),
+                sizeof(gras_dd_cat_field_t*),
+                &gras_dd_cat_field_free);
 
   GRAS_OUT;
-  return no_error;
+  return res;
 }
 
 /**
@@ -214,7 +208,7 @@ gras_datadesc_struct(const char            *name,
  *
  * Append a field to the struct
  */
-gras_error_t 
+void
 gras_datadesc_struct_append(gras_datadesc_type_t  *struct_type,
                            const char            *name,
                            gras_datadesc_type_t  *field_type) {
@@ -227,31 +221,26 @@ gras_datadesc_struct_append(gras_datadesc_type_t  *struct_type,
   if (struct_type->category.struct_data.closed) {
     VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
          struct_type->name);
-    return no_error;
+    return;
   }
 
-  gras_assert1(field_type->size >= 0,
+  gras_assert1(field_type->size != 0,
               "Cannot add a dynamically sized field in structure %s",
               struct_type->name);
     
-  field=malloc(sizeof(gras_dd_cat_field_t));
-  if (!field)
-    RAISE_MALLOC;
-
-  field->name   = strdup(name);
+  field=gras_new(gras_dd_cat_field_t,1);
+  field->name   = (char*)strdup(name);
 
   DEBUG0("----------------");
-  DEBUG4("PRE s={size=%ld,align=%ld,asize=%ld} struct_boundary=%d",
+  DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
         struct_type->size[GRAS_THISARCH], 
         struct_type->alignment[GRAS_THISARCH], 
-        struct_type->aligned_size[GRAS_THISARCH],
-        gras_arches[GRAS_THISARCH].struct_boundary);
+        struct_type->aligned_size[GRAS_THISARCH]);
      
      
   for (arch=0; arch<gras_arch_count; arch ++) {
     field->offset[arch] = aligned(struct_type->size[arch],
-                                 min(field_type->alignment[arch],
-                                     gras_arches[arch].struct_boundary));
+                                 field_type->alignment[arch]);
 
     struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
     struct_type->alignment[arch] = max(struct_type->alignment[arch],
@@ -263,7 +252,7 @@ gras_datadesc_struct_append(gras_datadesc_type_t  *struct_type,
   field->pre    = NULL;
   field->post   = NULL;
   
-  TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
+  gras_dynar_push(struct_type->category.struct_data.fields, &field);
 
   DEBUG3("Push a %s into %s at offset %ld.",
         field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
@@ -276,8 +265,8 @@ gras_datadesc_struct_append(gras_datadesc_type_t  *struct_type,
         struct_type->alignment[GRAS_THISARCH], 
         struct_type->aligned_size[GRAS_THISARCH]);
   GRAS_OUT;
-  return no_error;
 }
+
 void
 gras_datadesc_struct_close(gras_datadesc_type_t  *struct_type) {
   GRAS_IN;
@@ -289,10 +278,9 @@ gras_datadesc_struct_close(gras_datadesc_type_t  *struct_type) {
  *
  * Create a new union and give a pointer to it 
  */
-gras_error_t 
+gras_datadesc_type_t *
 gras_datadesc_union(const char                   *name,
-                   gras_datadesc_type_cb_int_t   selector,
-                   gras_datadesc_type_t        **dst) {
+                   gras_datadesc_type_cb_int_t   selector) {
 
   gras_error_t errcode;
   gras_datadesc_type_t *res;
@@ -310,12 +298,10 @@ gras_datadesc_union(const char                   *name,
     gras_assert1(res->category.union_data.selector == selector,
                 "Redefinition of type %s does not match", name);
     VERB1("Discarding redefinition of %s",name);
-    *dst=res;
-    return no_error;
+    return res;
   }
 
-  TRY(gras_ddt_new(name,dst));
-  res=*dst;
+  gras_ddt_new(name,&res);
 
   for (arch=0; arch<gras_arch_count; arch ++) {
      res->size[arch] = 0;
@@ -324,12 +310,12 @@ gras_datadesc_union(const char                   *name,
   }
 
   res->category_code           = e_gras_datadesc_type_cat_union;
-  TRY(gras_dynar_new(&(res->category.union_data.fields),
-                    sizeof(gras_dd_cat_field_t*),
-                    &gras_dd_cat_field_free));
+  gras_dynar_new(&(res->category.union_data.fields),
+                sizeof(gras_dd_cat_field_t*),
+                &gras_dd_cat_field_free);
   res->category.union_data.selector = selector;
 
-  return no_error;
+  return res;
 }
 
 /**
@@ -337,39 +323,32 @@ gras_datadesc_union(const char                   *name,
  *
  * Append a field to the union
  */
-gras_error_t 
+void
 gras_datadesc_union_append(gras_datadesc_type_t  *union_type,
                           const char            *name,
                           gras_datadesc_type_t  *field_type) {
 
-  gras_error_t errcode;
   gras_dd_cat_field_t *field;
   int arch;
 
   GRAS_IN3("(%s %s.%s;)",field_type->name,union_type->name,name);
-  gras_assert1(field_type->size >= 0,
+  gras_assert1(field_type->size != 0,
               "Cannot add a dynamically sized field in union %s",
               union_type->name);
 
   if (union_type->category.union_data.closed) {
     VERB1("Ignoring request to add field to union %s (closed)",
           union_type->name);
-    return no_error;
+    return;
   }
     
-  field=malloc(sizeof(gras_dd_cat_field_t));
-  if (!field)
-    RAISE_MALLOC;
+  field=gras_new0(gras_dd_cat_field_t,1);
 
-  field->name   = strdup(name);
-  for (arch=0; arch<gras_arch_count; arch ++) {
-    field->offset[arch] = 0; /* that's the purpose of union ;) */
-  }
+  field->name   = (char*)strdup(name);
   field->type   = field_type;
-  field->pre    = NULL;
-  field->post   = NULL;
+  /* All offset are left to 0 in an union */
   
-  TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
+  gras_dynar_push(union_type->category.union_data.fields, &field);
 
   for (arch=0; arch<gras_arch_count; arch ++) {
     union_type->size[arch] = max(union_type->size[arch],
@@ -379,7 +358,6 @@ gras_datadesc_union_append(gras_datadesc_type_t  *union_type,
     union_type->aligned_size[arch] = aligned(union_type->size[arch],
                                             union_type->alignment[arch]);
   }
-  return no_error;
 }
 
 void
@@ -391,10 +369,9 @@ gras_datadesc_union_close(gras_datadesc_type_t  *union_type) {
  *
  * Create a new ref to a fixed type and give a pointer to it 
  */
-gras_error_t 
-gras_datadesc_ref(const char             *name,
-                 gras_datadesc_type_t   *referenced_type,
-                 gras_datadesc_type_t  **dst) {
+gras_datadesc_type_t *
+  gras_datadesc_ref(const char             *name,
+                   gras_datadesc_type_t   *referenced_type) {
 
   gras_error_t errcode;
   gras_datadesc_type_t *res;
@@ -411,12 +388,10 @@ gras_datadesc_ref(const char             *name,
     gras_assert1(res->category.ref_data.selector == NULL,
                 "Redefinition of %s does not match",name);
     VERB1("Discarding redefinition of %s",name);
-    *dst=res;
-    return no_error;
+    return res;
   }
 
-  TRY(gras_ddt_new(name,dst));
-  res=*dst;
+  gras_ddt_new(name,&res);
 
   gras_assert0(pointer_type, "Cannot get the description of data pointer");
       
@@ -430,19 +405,17 @@ gras_datadesc_ref(const char             *name,
   res->category.ref_data.type     = referenced_type;
   res->category.ref_data.selector = NULL;
 
-  return no_error;
+  return res;
 }
 /**
  * gras_datadesc_ref_generic:
  *
  * Create a new ref to a type given at use time, and give a pointer to it 
  */
-gras_error_t 
-gras_datadesc_ref_generic(const char                *name,
-                         gras_datadesc_selector_t   selector,
-                         gras_datadesc_type_t     **dst) {
+gras_datadesc_type_t *
+  gras_datadesc_ref_generic(const char                *name,
+                           gras_datadesc_selector_t   selector) {
 
-  gras_error_t errcode;
   gras_datadesc_type_t *res;
   gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
   int arch;
@@ -457,11 +430,9 @@ gras_datadesc_ref_generic(const char                *name,
     gras_assert1(res->category.ref_data.selector == selector,
                 "Redefinition of type %s does not match", name);
     VERB1("Discarding redefinition of %s",name);
-    *dst=res;
-    return no_error;
+    return res;
   }
-  TRY(gras_ddt_new(name,dst));
-  res=*dst;
+  gras_ddt_new(name,&res);
 
   gras_assert0(pointer_type, "Cannot get the description of data pointer");
       
@@ -476,7 +447,7 @@ gras_datadesc_ref_generic(const char                *name,
   res->category.ref_data.type     = NULL;
   res->category.ref_data.selector = selector;
 
-  return no_error;
+  return res;
 }
 
 /**
@@ -484,13 +455,11 @@ gras_datadesc_ref_generic(const char                *name,
  *
  * Create a new array and give a pointer to it 
  */
-gras_error_t 
-gras_datadesc_array_fixed(const char              *name,
-                         gras_datadesc_type_t    *element_type,
-                         long int                 fixed_size,
-                         gras_datadesc_type_t   **dst) {
+gras_datadesc_type_t *
+  gras_datadesc_array_fixed(const char              *name,
+                           gras_datadesc_type_t    *element_type,
+                           long int                 fixed_size) {
 
-  gras_error_t errcode;
   gras_datadesc_type_t *res;
   int arch;
 
@@ -507,13 +476,11 @@ gras_datadesc_array_fixed(const char              *name,
                 "Redefinition of type %s does not match", name);
     VERB1("Discarding redefinition of %s",name);
 
-    *dst=res;
-    return no_error;
+    return res;
   }
-  TRY(gras_ddt_new(name,dst));
-  res=*dst;
+  gras_ddt_new(name,&res);
 
-  gras_assert1(fixed_size > 0, "'%s' is a array of negative fixed size",name);
+  gras_assert1(fixed_size > 0, "'%s' is a array of null fixed size",name);
   for (arch=0; arch<gras_arch_count; arch ++) {
     res->size[arch] = fixed_size * element_type->aligned_size[arch];
     res->alignment[arch] = element_type->alignment[arch];
@@ -526,20 +493,18 @@ gras_datadesc_array_fixed(const char              *name,
   res->category.array_data.fixed_size   = fixed_size;
   res->category.array_data.dynamic_size = NULL;
 
-  return no_error;
+  return res;
 }
 /**
  * gras_datadesc_array_dyn:
  *
  * Create a new array and give a pointer to it 
  */
-gras_error_t 
-gras_datadesc_array_dyn(const char                      *name,
-                       gras_datadesc_type_t            *element_type,
-                       gras_datadesc_type_cb_int_t      dynamic_size,
-                       gras_datadesc_type_t           **dst) {
+gras_datadesc_type_t *
+  gras_datadesc_array_dyn(const char                      *name,
+                         gras_datadesc_type_t            *element_type,
+                         gras_datadesc_type_cb_int_t      dynamic_size) {
 
-  gras_error_t errcode;
   gras_datadesc_type_t *res;
   int arch;
 
@@ -554,32 +519,30 @@ gras_datadesc_array_dyn(const char                      *name,
                 "Redefinition of type %s does not match", name);
     gras_assert1(res->category.array_data.type == element_type,
                 "Redefinition of type %s does not match", name);
-    gras_assert1(res->category.array_data.fixed_size == -1,
+    gras_assert1(res->category.array_data.fixed_size == 0,
                 "Redefinition of type %s does not match", name);
     gras_assert1(res->category.array_data.dynamic_size == dynamic_size,
                 "Redefinition of type %s does not match", name);
     VERB1("Discarding redefinition of %s",name);
 
-    *dst=res;
-    return no_error;
+    return res;
   }
 
-  TRY(gras_ddt_new(name,dst));
-  res=*dst;
+  gras_ddt_new(name,&res);
 
   for (arch=0; arch<gras_arch_count; arch ++) {
-    res->size[arch] = -1; /* make sure it indicates "dynamic" */
+    res->size[arch] = 0; /* make sure it indicates "dynamic" */
     res->alignment[arch] = element_type->alignment[arch];
-    res->aligned_size[arch] = -1; /*FIXME: That was so in GS, but looks stupid*/
+    res->aligned_size[arch] = 0; /*FIXME: That was so in GS, but looks stupid*/
   }
 
   res->category_code           = e_gras_datadesc_type_cat_array;
 
   res->category.array_data.type         = element_type;
-  res->category.array_data.fixed_size   = -1;
+  res->category.array_data.fixed_size   = 0;
   res->category.array_data.dynamic_size = dynamic_size;
 
-  return no_error;
+  return res;
 }
 
 /**
@@ -605,23 +568,23 @@ gras_datadesc_array_dyn(const char                      *name,
  * list when the first field gets transfered.
  *
  */
-gras_error_t 
-gras_datadesc_ref_pop_arr(gras_datadesc_type_t  *element_type,
-                         gras_datadesc_type_t **dst) {
-  gras_error_t errcode;
-  char *name=malloc(strlen(element_type->name) + 4);
+gras_datadesc_type_t *
+  gras_datadesc_ref_pop_arr(gras_datadesc_type_t  *element_type) {
+
+  gras_datadesc_type_t *res;
+  char *name=(char*)gras_malloc(strlen(element_type->name) + 4);
 
   sprintf(name,"%s[]",element_type->name);
 
-  TRY(gras_datadesc_array_dyn(name,element_type,
-                                     gras_datadesc_cb_pop, dst));
+  res = gras_datadesc_array_dyn(name,element_type,
+                               gras_datadesc_cb_pop);
 
   sprintf(name,"%s[]*",element_type->name);
-  TRY(gras_datadesc_ref(name,*dst,dst));
+  res = gras_datadesc_ref(name,res);
 
-  free(name);
+  gras_free(name);
 
-  return no_error;
+  return res;
 }
 gras_error_t
 gras_datadesc_import_nws(const char           *name,
@@ -634,7 +597,7 @@ gras_datadesc_import_nws(const char           *name,
 /**
  * gras_datadesc_cb_send:
  *
- * Add a pre-send callback to this datadexc.
+ * Add a pre-send callback to this datadesc.
  * (useful to push the sizes of the upcoming arrays, for example)
  */
 void gras_datadesc_cb_send (gras_datadesc_type_t         *type,
@@ -651,12 +614,94 @@ void gras_datadesc_cb_recv(gras_datadesc_type_t         *type,
                           gras_datadesc_type_cb_void_t  recv) {
   type->recv = recv;
 }
+/**
+ * gras_dd_find_field:
+ * 
+ * Returns the type descriptor of the given field. Abort on error.
+ */
+static gras_datadesc_type_t *
+  gras_dd_find_field(gras_datadesc_type_t         *type,
+                    const char                   *field_name) {
+   gras_datadesc_type_t *sub_type=NULL;
+   gras_dynar_t         *field_array;
+   
+   gras_dd_cat_field_t  *field=NULL;
+   int                   field_num;
+   
+   if (type->category_code == e_gras_datadesc_type_cat_union) {
+      field_array = type->category.union_data.fields;
+   } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
+      field_array = type->category.struct_data.fields;
+   } else {
+      ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,(void*)type);
+      gras_abort();
+   }
+   gras_dynar_foreach(field_array,field_num,field) {
+      if (!strcmp(field_name,field->name)) {
+        return field->type;
+      }
+   }
+   ERROR2("No field nammed %s in %s",field_name,type->name);
+   gras_abort();
+
+}
+                                 
+/**
+ * gras_datadesc_cb_field_send:
+ *
+ * Add a pre-send callback to the given field of the datadesc (which must be a struct or union).
+ * (useful to push the sizes of the upcoming arrays, for example)
+ */
+void gras_datadesc_cb_field_send (gras_datadesc_type_t         *type,
+                                 const char                   *field_name,
+                                 gras_datadesc_type_cb_void_t  send) {
+   
+   gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);   
+   sub_type->send = send;
+}
+
+/**
+ * gras_datadesc_cb_field_push:
+ *
+ * Add a pre-send callback to the given field resulting in its value to be pushed to
+ * the stack of sizes. It must be a int, unsigned int, long int or unsigned long int.
+ */
+void gras_datadesc_cb_field_push (gras_datadesc_type_t         *type,
+                                 const char                   *field_name) {
+   
+   gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
+   if (!strcmp("int",sub_type->name)) {
+      sub_type->send = gras_datadesc_cb_push_int;
+   } else if (!strcmp("unsigned int",sub_type->name)) {
+      sub_type->send = gras_datadesc_cb_push_uint;
+   } else if (!strcmp("long int",sub_type->name)) {
+      sub_type->send = gras_datadesc_cb_push_lint;
+   } else if (!strcmp("unsigned long int",sub_type->name)) {
+      sub_type->send = gras_datadesc_cb_push_ulint;
+   } else {
+      ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
+            sub_type->name);
+      gras_abort();
+   }
+}
+/**
+ * gras_datadesc_cb_field_recv:
+ *
+ * Add a post-receive callback to the given field of the datadesc (which must be a struct or union).
+ * (useful to put the function pointers to the right value, for example)
+ */
+void gras_datadesc_cb_field_recv(gras_datadesc_type_t         *type,
+                                const char                   *field_name,
+                                gras_datadesc_type_cb_void_t  recv) {
+   
+   gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);   
+   sub_type->recv = recv;
+}
 
 /**
- * gras_datadesc_unref:
+ * gras_datadesc_free:
  *
- * Removes a reference to the datastruct. 
- * ddt will be freed only when the refcount becomes 0.
+ * Free a datadesc. Should only be called at gras_exit. 
  */
 void gras_datadesc_free(gras_datadesc_type_t *type) {
 
@@ -688,6 +733,6 @@ void gras_datadesc_free(gras_datadesc_type_t *type) {
     /* datadesc was invalid. Killing it is like euthanasy, I guess */
     break;
   }
-  free(type->name);
-  free(type);
+  gras_free(type->name);
+  gras_free(type);
 }