Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Data description]
[simgrid.git] / src / gras / DataDesc / ddt_create.c
index 1d871ce..d8a4e76 100644 (file)
 
 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(new,DataDesc);
 
+/**
+ * gras_ddt_freev:
+ *
+ * gime that memory back, dude. I mean it.
+ */
+static void gras_ddt_freev(void *ddt) {
+  gras_datadesc_type_t *type= (gras_datadesc_type_t *)ddt;
+  
+  if (type) {
+    gras_ddt_free(&type);
+  }
+}
+
 static gras_error_t
 gras_ddt_new(const char            *name,
             gras_datadesc_type_t **dst) {
 
+  gras_error_t errcode;
   gras_datadesc_type_t *res=malloc(sizeof(gras_datadesc_type_t));
   if (!res) 
     RAISE_MALLOC;
 
-  memset(res, 0, sizeof(res));
+  memset(res, 0, sizeof(gras_datadesc_type_t));
   res->name = strdup(name);
   res->name_len = strlen(name);
-  res->pre = NULL;
-  res->post = NULL;
   
+  TRY(gras_set_add(gras_datadesc_set_local,
+                  (gras_set_elm_t*)res,&gras_ddt_freev));
+
   *dst=res;
   return no_error;
 }
 
 /**
- * gras_ddt_new_scalar:
+ * gras_datadesc_by_name:
+ *
+ * Retrieve a type from its name
+ */
+gras_datadesc_type_t *gras_datadesc_by_name(const char *name) {
+
+  gras_datadesc_type_t *type;
+
+  if (gras_set_get_by_name(gras_datadesc_set_local,
+                          name,(gras_set_elm_t**)&type) == no_error) {
+    return type;
+  } else { 
+    return NULL;
+  }
+}
+
+/**
+ * gras_datadesc_by_id:
+ *
+ * Retrieve a type from its code
+ */
+gras_error_t gras_datadesc_by_id(long int               code,
+                                gras_datadesc_type_t **type) {
+  return gras_set_get_by_id(gras_datadesc_set_local,
+                           code,(gras_set_elm_t**)type);
+}
+
+/**
+ * gras_datadesc_declare_scalar:
  *
  * Create a new scalar and give a pointer to it 
  */
 gras_error_t 
-gras_ddt_new_scalar(const char                      *name,
-                   gras_ddt_scalar_type_t           type,
-                   enum e_gras_dd_scalar_encoding   encoding,
-                   gras_datadesc_type_cb_void_t     cb,
-                   gras_datadesc_type_t           **dst) {
+gras_datadesc_declare_scalar(const char                      *name,
+                       gras_ddt_scalar_type_t           type,
+                       enum e_gras_dd_scalar_encoding   encoding,
+                       gras_datadesc_type_cb_void_t     cb,
+                       gras_datadesc_type_t           **dst) {
 
   gras_error_t errcode;
   gras_datadesc_type_t *res;
@@ -114,15 +157,13 @@ void gras_dd_cat_field_free(void *f) {
 }
 
 /**
- * gras_ddt_new_struct:
+ * gras_datadesc_declare_struct:
  *
  * Create a new struct and give a pointer to it 
  */
 gras_error_t 
-gras_ddt_new_struct(const char                      *name,
-                   gras_datadesc_type_cb_void_t     pre,
-                   gras_datadesc_type_cb_void_t     post,
-                   gras_datadesc_type_t           **dst) {
+gras_datadesc_declare_struct(const char                      *name,
+                       gras_datadesc_type_t           **dst) {
 
   gras_error_t errcode;
   gras_datadesc_type_t *res;
@@ -140,23 +181,19 @@ gras_ddt_new_struct(const char                      *name,
   TRY(gras_dynar_new(&(res->category.struct_data.fields),
                     sizeof(gras_dd_cat_field_t*),
                     &gras_dd_cat_field_free));
-  res->pre                     = pre;
-  res->post                    = post;
 
   return no_error;
 }
 
 /**
- * gras_ddt_new_struct_append:
+ * gras_datadesc_declare_struct_append:
  *
  * Append a field to the struct
  */
 gras_error_t 
-gras_ddt_new_struct_append(gras_datadesc_type_t            *struct_type,
-                          const char                      *name,
-                          gras_datadesc_type_t            *field_type,
-                          gras_datadesc_type_cb_void_t     pre,
-                          gras_datadesc_type_cb_void_t     post) {
+gras_datadesc_declare_struct_append(gras_datadesc_type_t            *struct_type,
+                              const char                      *name,
+                              gras_datadesc_type_t            *field_type) {
 
   gras_error_t errcode;
   gras_dd_cat_field_t *field;
@@ -175,8 +212,8 @@ gras_ddt_new_struct_append(gras_datadesc_type_t            *struct_type,
     field->offset[arch] = aligned(struct_type->size[arch], field_type->alignment[arch]);
   }
   field->code   = field_type->code;
-  field->pre    = pre;
-  field->post   = post;
+  field->pre    = NULL;
+  field->post   = NULL;
   
   TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
 
@@ -190,14 +227,13 @@ gras_ddt_new_struct_append(gras_datadesc_type_t            *struct_type,
 }
 
 /**
- * gras_ddt_new_union:
+ * gras_datadesc_declare_union:
  *
  * Create a new union and give a pointer to it 
  */
 gras_error_t 
-gras_ddt_new_union(const char                      *name,
+gras_datadesc_declare_union(const char                      *name,
                   gras_datadesc_type_cb_int_t      selector,
-                  gras_datadesc_type_cb_void_t     post,
                   gras_datadesc_type_t           **dst) {
 
   gras_error_t errcode;
@@ -220,23 +256,19 @@ gras_ddt_new_union(const char                      *name,
                     sizeof(gras_dd_cat_field_t*),
                     &gras_dd_cat_field_free));
   res->category.union_data.selector = selector;
-  res->pre                     = NULL;
-  res->post                    = post;
 
   return no_error;
 }
 
 /**
- * gras_ddt_new_union_append:
+ * gras_datadesc_declare_union_append:
  *
  * Append a field to the union
  */
 gras_error_t 
-gras_ddt_new_union_append(gras_datadesc_type_t            *union_type,
+gras_datadesc_declare_union_append(gras_datadesc_type_t            *union_type,
                          const char                      *name,
-                         gras_datadesc_type_t            *field_type,
-                         gras_datadesc_type_cb_void_t     pre,
-                         gras_datadesc_type_cb_void_t     post) {
+                         gras_datadesc_type_t            *field_type) {
 
   gras_error_t errcode;
   gras_dd_cat_field_t *field;
@@ -254,8 +286,8 @@ gras_ddt_new_union_append(gras_datadesc_type_t            *union_type,
     field->offset[arch] = 0; /* that's the purpose of union ;) */
   }
   field->code   = field_type->code;
-  field->pre    = pre;
-  field->post   = post;
+  field->pre    = NULL;
+  field->post   = NULL;
   
   TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
 
@@ -269,29 +301,57 @@ gras_ddt_new_union_append(gras_datadesc_type_t            *union_type,
 }
 
 /**
- * gras_ddt_new_ref:
+ * gras_datadesc_declare_ref:
  *
- * Create a new ref and give a pointer to it 
+ * Create a new ref to a fixed type and give a pointer to it 
  */
 gras_error_t 
-gras_ddt_new_ref(const char                      *name,
-                gras_datadesc_type_t            *referenced_type,
-                gras_datadesc_type_cb_int_t      selector,
-                gras_datadesc_type_cb_void_t     post,
-                gras_datadesc_type_t           **dst) {
+gras_datadesc_declare_ref(const char             *name,
+                         gras_datadesc_type_t   *referenced_type,
+                         gras_datadesc_type_t  **dst) {
 
   gras_error_t errcode;
   gras_datadesc_type_t *res;
-  gras_datadesc_type_t *pointer_type;
+  gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
   int arch;
 
-  gras_assert0(selector || referenced_type,
-              "Attempt to create a generic reference without selector");
+  TRY(gras_ddt_new(name,dst));
+  res=*dst;
+
+  gras_assert0(pointer_type, "Cannot get the description of data pointer");
+      
+  for (arch=0; arch<gras_arch_count; arch ++) {
+    res->size[arch]                    = pointer_type->size[arch];
+    res->alignment[arch]               = pointer_type->alignment[arch];
+    res->aligned_size[arch]            = pointer_type->aligned_size[arch];
+  }
+
+  res->category_code           = e_gras_datadesc_type_cat_ref;
+
+  res->category.ref_data.code     = referenced_type->code;
+  res->category.ref_data.selector = NULL;
+
+  return no_error;
+}
+/**
+ * gras_datadesc_declare_ref_generic:
+ *
+ * Create a new ref to a type given at use time, and give a pointer to it 
+ */
+gras_error_t 
+gras_datadesc_declare_ref_generic(const char                      *name,
+                        gras_datadesc_type_cb_int_t      selector,
+                        gras_datadesc_type_t           **dst) {
+
+  gras_error_t errcode;
+  gras_datadesc_type_t *res;
+  gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
+  int arch;
 
   TRY(gras_ddt_new(name,dst));
   res=*dst;
 
-  TRY(gras_datadesc_by_name("data pointer", &pointer_type));
+  gras_assert0(pointer_type, "Cannot get the description of data pointer");
       
   for (arch=0; arch<gras_arch_count; arch ++) {
     res->size[arch]                    = pointer_type->size[arch];
@@ -301,43 +361,33 @@ gras_ddt_new_ref(const char                      *name,
 
   res->category_code           = e_gras_datadesc_type_cat_ref;
 
-  res->category.ref_data.code         = referenced_type ? referenced_type->code : -1;
+  res->category.ref_data.code     = -1;
   res->category.ref_data.selector = selector;
-  res->pre                     = NULL;
-  res->post                    = post;
 
   return no_error;
 }
 
 /**
- * gras_ddt_new_array:
+ * gras_datadesc_declare_array_fixed:
  *
  * Create a new array and give a pointer to it 
  */
 gras_error_t 
-gras_ddt_new_array(const char                      *name,
-                  gras_datadesc_type_t            *element_type,
-                  long int                         fixed_size,
-                  gras_datadesc_type_cb_int_t      dynamic_size,
-                  gras_datadesc_type_cb_void_t     post,
-                  gras_datadesc_type_t           **dst) {
+gras_datadesc_declare_array_fixed(const char                      *name,
+                        gras_datadesc_type_t            *element_type,
+                        long int                         fixed_size,
+                        gras_datadesc_type_t           **dst) {
 
   gras_error_t errcode;
   gras_datadesc_type_t *res;
   int arch;
 
-  gras_assert0(dynamic_size || fixed_size>0,
-              "Attempt to create a dynamic array without size discriminant");
-
   TRY(gras_ddt_new(name,dst));
   res=*dst;
 
+  gras_assert1(fixed_size > 0, "'%s' is a array of negative fixed size",name);
   for (arch=0; arch<gras_arch_count; arch ++) {
-    if (fixed_size <= 0) {
-      res->size[arch] = fixed_size; /* make sure it indicates "dynamic" */
-    } else {
-      res->size[arch] = fixed_size * element_type->aligned_size[arch];
-    }
+    res->size[arch] = fixed_size * element_type->aligned_size[arch];
     res->alignment[arch]       = element_type->alignment[arch];
     res->aligned_size[arch]    = fixed_size; /*FIXME: That was so in GS, but looks stupid*/
   }
@@ -345,84 +395,64 @@ gras_ddt_new_array(const char                      *name,
 
   res->category.array_data.code         = element_type->code;
   res->category.array_data.fixed_size   = fixed_size;
-  res->category.array_data.dynamic_size = dynamic_size;
-
-  res->pre                     = NULL;
-  res->post                    = post;
+  res->category.array_data.dynamic_size = NULL;
 
   return no_error;
 }
-
 /**
- * gras_ddt_new_ignored:
+ * gras_datadesc_declare_array_dyn:
  *
- * Create a new ignored field and give a pointer to it. 
- *
- * If you give a default value, it will be copied away so that you can free your copy.
+ * Create a new array and give a pointer to it 
  */
 gras_error_t 
-gras_ddt_new_ignored(const char *name,
-                    void *default_value,
-                    void_f_pvoid_t   *free_func,
-                    long int                       size,
-                    long int                       alignment,
-                    gras_datadesc_type_cb_void_t     post,
-                    gras_datadesc_type_t           **dst) {
-  RAISE_UNIMPLEMENTED;
-  /*
+gras_datadesc_declare_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_error_t errcode;
   gras_datadesc_type_t *res;
+  int arch;
+
+  gras_assert1(dynamic_size,
+              "'%s' is a dynamic array without size discriminant",
+              name);
 
   TRY(gras_ddt_new(name,dst));
   res=*dst;
 
-  res->size            = size > 0?size:0;
-  res->alignment       = alignment;
-
-  if (size > 0) {
-    res->aligned_size  = aligned(size, alignment);
-  } else {
-    res->aligned_size  = 0;
-  }
-
-  if (default_value && res->size) {
-    res->category.ignored_data.default_value = malloc((size_t)size);
-    if (! (res->category.ignored_data.default_value) ) 
-      RAISE_MALLOC;
-    memcpy(res->category.ignored_data.default_value,
-          default_value, (size_t)size);
+  for (arch=0; arch<gras_arch_count; arch ++) {
+    res->size[arch] = -1; /* 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->category_code           = e_gras_datadesc_type_cat_array;
 
-  res->category_code = e_gras_datadesc_type_cat_ignored;
-  res->category.ignored_data.free_func = free_func;
-
-  res->post = post;
-
-
-  res->size = size;
+  res->category.array_data.code         = element_type->code;
+  res->category.array_data.fixed_size   = -1;
+  res->category.array_data.dynamic_size = dynamic_size;
 
   return no_error;
-  */
 }
 
 /**
- * gras_ddt_new_parse:
+ * gras_datadesc_declare_parse:
  *
  * Create a datadescription from the result of parsing the C type description
  */
 gras_error_t
-gras_ddt_new_parse(const char            *name,
-                  const char            *C_statement,
-                  gras_datadesc_type_t **dst) {
+gras_datadesc_parse(const char            *name,
+                   const char            *C_statement,
+                   gras_datadesc_type_t **dst) {
   RAISE_UNIMPLEMENTED;
 }
 
 
 gras_error_t
-gras_ddt_new_from_nws(const char           *name,
-                     const DataDescriptor *desc,
-                     size_t                howmany,
-                     gras_datadesc_type_t **dst) {
+gras_datadesc_import_nws(const char           *name,
+                        const DataDescriptor *desc,
+                        size_t                howmany,
+                        gras_datadesc_type_t **dst) {
   RAISE_UNIMPLEMENTED;
 }
 
@@ -465,3 +495,22 @@ void gras_ddt_free(gras_datadesc_type_t **type) {
     }
   }
 }
+
+/**
+ * gras_datadesc_cb_set_pre:
+ *
+ * Add a pre-send callback to this datadexc
+ */
+void gras_datadesc_cb_set_pre (gras_datadesc_type_t         *type,
+                              gras_datadesc_type_cb_void_t  pre) {
+  type->pre = pre;
+}
+/**
+ * gras_datadesc_cb_set_post:
+ *
+ * Add a post-send callback to this datadexc
+ */
+void gras_datadesc_cb_set_post(gras_datadesc_type_t         *type,
+                              gras_datadesc_type_cb_void_t  post) {
+  type->post = post;
+}