Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not use non-letter chars in type name so that it's usable from this poor old parsi...
[simgrid.git] / src / gras / DataDesc / ddt_create.c
index 4b85d08..ee432e1 100644 (file)
@@ -48,20 +48,37 @@ static gras_datadesc_type_t gras_ddt_new(const char *name) {
   return res;
 }
 
+/** @brief retrieve an existing message type from its name (or NULL if it does not exist). */
+gras_datadesc_type_t gras_datadesc_by_name_or_null (const char *name) {
+  xbt_ex_t e;
+  gras_datadesc_type_t res = NULL;
+   
+  TRY {
+     res = gras_datadesc_by_name(name);
+  } CATCH(e) {
+     res = NULL;
+     xbt_ex_free(e);
+  }
+  return res;
+}
 /**
- * This returns NULL no type of this name can be found instead of throwing exceptions which would complicate the GRAS_DEFINE_TYPE macro
+ * Search the given datadesc (or raises an exception if it can't be found)
  */
 gras_datadesc_type_t gras_datadesc_by_name(const char *name) {
   xbt_ex_t e;
   gras_datadesc_type_t res = NULL;
+  volatile int found = 0;
   TRY {
     res = (gras_datadesc_type_t)xbt_set_get_by_name(gras_datadesc_set_local,name);
+    found = 1;
   } CATCH(e) {
     if (e.category != not_found_error)
       RETHROW;
     xbt_ex_free(e);
-    res = NULL;
   }
+  if (!found)
+    THROW1(not_found_error,0,"No registred datatype of that name: %s",name);     
+   
   return res;
 }
 
@@ -94,7 +111,7 @@ gras_datadesc_type_t
   long int arch;
 
   XBT_IN;
-  res = gras_datadesc_by_name(name);
+  res = gras_datadesc_by_name_or_null(name);
   if (res) {
     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_scalar,
                 "Redefinition of type %s does not match", name);
@@ -142,7 +159,7 @@ gras_datadesc_type_t
   long int arch;
   
   XBT_IN1("(%s)",name);
-  res = gras_datadesc_by_name(name);
+  res = gras_datadesc_by_name_or_null(name);
   if (res) {
     /* FIXME: Check that field redefinition matches */
     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
@@ -176,7 +193,7 @@ gras_datadesc_struct_append(gras_datadesc_type_t struct_type,
   int arch;
 
   xbt_assert2(field_type,
-              "Cannot add the field '%s' into struct '%s': its type is NULL. Typo in get_by_name?",
+              "Cannot add the field '%s' into struct '%s': its type is NULL",
               name,struct_type->name);
   XBT_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
   if (struct_type->category.struct_data.closed) {
@@ -281,7 +298,7 @@ gras_datadesc_type_t
   xbt_assert0(selector,
               "Attempt to creat an union without field_count function");
 
-  res = gras_datadesc_by_name(name);
+  res = gras_datadesc_by_name_or_null(name);
   if (res) {
     /* FIXME: Check that field redefinition matches */
     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_union,
@@ -356,6 +373,22 @@ gras_datadesc_union_close(gras_datadesc_type_t union_type) {
    union_type->category.union_data.closed = 1;
 }
 
+/** \brief Copy a type under another name
+ * 
+ * This may reveal useful to circumvent parsing macro limitations
+ */
+gras_datadesc_type_t 
+  gras_datadesc_copy(const char           *name,
+                    gras_datadesc_type_t  copied) {
+
+     gras_datadesc_type_t res = gras_ddt_new(name);
+     char *name_cpy = res->name;
+     
+     memcpy(res,copied,sizeof(s_gras_datadesc_type_t));
+     res->name = name_cpy;
+     return res;
+  }
+
 /** \brief Declare a new type being a reference to the one passed in arg */
 gras_datadesc_type_t 
   gras_datadesc_ref(const char           *name,
@@ -366,7 +399,7 @@ gras_datadesc_type_t
   int arch;
 
   XBT_IN1("(%s)",name);
-  res = gras_datadesc_by_name(name);
+  res = gras_datadesc_by_name_or_null(name);
   if (res) {
     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
                 "Redefinition of %s does not match",name);
@@ -409,7 +442,7 @@ gras_datadesc_type_t
   int arch;
 
   XBT_IN1("(%s)",name);
-  res = gras_datadesc_by_name(name);
+  res = gras_datadesc_by_name_or_null(name);
 
   if (res) {
     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
@@ -449,7 +482,7 @@ gras_datadesc_type_t
   int arch;
 
   XBT_IN1("(%s)",name);
-  res = gras_datadesc_by_name(name);
+  res = gras_datadesc_by_name_or_null(name);
   if (res) {
     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
                 "Redefinition of type %s does not match", name);
@@ -499,7 +532,7 @@ gras_datadesc_type_t gras_datadesc_array_dyn(const char                 *name,
               "'%s' is a dynamic array without size discriminant",
               name);
 
-  res = gras_datadesc_by_name(name);
+  res = gras_datadesc_by_name_or_null(name);
   if (res) {
     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
                 "Redefinition of type %s does not match", name);
@@ -596,6 +629,7 @@ static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc, gras_cbps_t va
 
   dynar->elmsize = subtype->size[GRAS_THISARCH];
   dynar->size = dynar->used;
+  dynar->mutex = NULL;
 }
 
 /** \brief Declare a new type being a dynar in which each elements are of the given type
@@ -614,7 +648,7 @@ gras_datadesc_dynar(gras_datadesc_type_t elm_t,
   char *buffname;
   gras_datadesc_type_t res;
   
-  asprintf(&buffname,"dynar(%s)_s",elm_t->name);
+  asprintf(&buffname,"s_xbt_dynar_of_%s",elm_t->name);
    
   res = gras_datadesc_struct(buffname);
   
@@ -634,6 +668,9 @@ gras_datadesc_dynar(gras_datadesc_type_t elm_t,
                              gras_datadesc_by_name("function pointer"));
   memcpy(res->extra,&free_func,sizeof(free_func));
       
+  gras_datadesc_struct_append(res, "mutex",
+                             gras_datadesc_by_name("data pointer"));
+   
   gras_datadesc_struct_close(res);
    
   gras_datadesc_cb_field_push(res, "used");
@@ -641,7 +678,7 @@ gras_datadesc_dynar(gras_datadesc_type_t elm_t,
 
   /* build a ref to it */
   free(buffname);
-  asprintf(&buffname,"dynar(%s)",elm_t->name);
+  asprintf(&buffname,"xbt_dynar_of_%s",elm_t->name);
   res=gras_datadesc_ref(buffname,res);
   free(buffname);
   return res;
@@ -691,7 +728,7 @@ gras_datadesc_matrix(gras_datadesc_type_t elm_t,
   gras_datadesc_cb_field_push(res, "lines");
   gras_datadesc_cb_field_push_multiplier(res, "rows");
 
-  gras_datadesc_cb_recv(res,  &gras_datadesc_dynar_cb);
+  gras_datadesc_cb_recv(res,  &gras_datadesc_matrix_cb);
   memcpy(res->extra,&free_f,sizeof(free_f));
 
   /* build a ref to it */
@@ -749,7 +786,7 @@ static gras_dd_cat_field_t
         return field;
       }
    }
-   ERROR2("No field nammed %s in %s",field_name,type->name);
+   ERROR2("No field named '%s' in '%s'",field_name,type->name);
    xbt_abort();
 
 }