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 / cbps.c
index 2e59fae..cfbb4af 100644 (file)
@@ -8,8 +8,8 @@
 /* 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"
-GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(cbps,datadesc);
+#include "gras/DataDesc/datadesc_private.h"
+GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_cbps,datadesc,"callback persistant state");
 
 typedef struct {
   gras_datadesc_type_t *type;
@@ -28,31 +28,27 @@ struct s_gras_cbps {
 static void free_string(void *d);
 
 static void free_string(void *d){
-  free(*(void**)d);
+  gras_free(*(void**)d);
 }
 
-gras_error_t
-gras_cbps_new(gras_cbps_t **dst) {
+gras_cbps_t * gras_cbps_new(void) {
   gras_error_t errcode;
   gras_cbps_t *res;
 
-  if (!(res=malloc(sizeof(gras_cbps_t))))
-    RAISE_MALLOC;
-
-  TRY(gras_dynar_new(&(res->lints), sizeof(int), NULL));
+  res=gras_new(gras_cbps_t,1);
 
-  TRY(gras_dict_new(&(res->space)));
+  gras_dynar_new(&(res->lints), sizeof(int), NULL);
+  gras_dict_new(&(res->space));
   /* no leak, the content is freed manually on block_end */
-  TRY(gras_dynar_new(&(res->frames), sizeof(gras_dynar_t*), NULL));
-  TRY(gras_dynar_new(&(res->globals), sizeof(char*), NULL));
+  gras_dynar_new(&(res->frames), sizeof(gras_dynar_t*), NULL);
+  gras_dynar_new(&(res->globals), sizeof(char*), NULL);
 
   gras_cbps_block_begin(res);
-  *dst = res;
-  return no_error;
+
+  return res;
 }
 
-void
-gras_cbps_free(gras_cbps_t **state) {
+void gras_cbps_free(gras_cbps_t **state) {
 
   gras_dynar_free(    (*state)->lints   );
 
@@ -61,7 +57,7 @@ gras_cbps_free(gras_cbps_t **state) {
   gras_dynar_free(    (*state)->frames    );
   gras_dynar_free(    (*state)->globals   );
 
-  free(*state);
+  gras_free(*state);
   *state = NULL;
 }
 
@@ -74,16 +70,16 @@ gras_cbps_free(gras_cbps_t **state) {
  */
 gras_error_t
 gras_cbps_v_push(gras_cbps_t        *ps,
-                   const char            *name,
-                   void                  *data,
-                   gras_datadesc_type_t  *ddt) {
+                const char            *name,
+                void                  *data,
+                gras_datadesc_type_t  *ddt) {
 
   gras_dynar_t            *varstack,*frame;
   gras_cbps_elm_t      *p_var;
   gras_error_t errcode;
-  char *varname = strdup(name);
+  char *varname = (char*)strdup(name);
 
-  DEBUG2("push(%s,%p)",name,data);
+  DEBUG2("push(%s,%p)",name,(void*)data);
   errcode = gras_dict_get(ps->space, name, (void **)&varstack);
  
   if (errcode == mismatch_error) {
@@ -95,14 +91,14 @@ gras_cbps_v_push(gras_cbps_t        *ps,
     return errcode;
   }
  
-  p_var       = calloc(1, sizeof(gras_cbps_elm_t));
+  p_var       = gras_new0(gras_cbps_elm_t,1);
   p_var->type = ddt;
   p_var->data = data;
   
   gras_dynar_push(varstack, &p_var);
   
   gras_dynar_pop(ps->frames, &frame);
-  DEBUG4("Push %s (%p @%p) into frame %p",varname,varname,&varname,frame);
+  DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(void*)&varname,(void*)frame);
   gras_dynar_push(frame, &varname);
   gras_dynar_push(ps->frames, &frame); 
   return no_error;
@@ -144,7 +140,7 @@ gras_cbps_v_pop (gras_cbps_t        *ps,
     *ddt = var->type;  
   data    = var->data;
   
-  free(var);
+  gras_free(var);
   
   gras_dynar_pop(ps->frames, &frame);
   {
@@ -156,7 +152,7 @@ gras_cbps_v_pop (gras_cbps_t        *ps,
       gras_dynar_get(frame, l, &_name);
       if (!strcmp(name, _name)) {
        gras_dynar_remove_at(frame, l, &_name);
-       free(_name);
+       gras_free(_name);
        break;
       }
     }
@@ -195,7 +191,7 @@ gras_cbps_v_set (gras_cbps_t        *ps,
     gras_dynar_new(&p_dynar, sizeof (gras_cbps_elm_t *), NULL);
     gras_dict_set(ps->space, name, (void **)p_dynar, NULL);
     
-    p_elm   = calloc(1, sizeof(gras_cbps_elm_t));
+    p_elm   = gras_new0(gras_cbps_elm_t,1);
     gras_dynar_push(ps->globals, &name);
   } else {
     gras_dynar_pop(p_dynar, &p_elm);
@@ -284,7 +280,7 @@ gras_cbps_block_end(gras_cbps_t *ps) {
     gras_dynar_t            *varstack    = NULL;
     gras_cbps_elm_t      *var         = NULL;
  
-    DEBUG2("Get ride of %s (%p)",name,name);
+    DEBUG2("Get ride of %s (%p)",name,(void*)name);
     gras_dict_get(ps->space, name, (void **)&varstack);
     gras_dynar_pop(varstack, &var);
  
@@ -293,9 +289,9 @@ gras_cbps_block_end(gras_cbps_t *ps) {
       gras_dynar_free_container(varstack); /*already empty, save a test ;) */
     }
     
-    if (var->data) free(var->data);
-    free(var);
-    free(name);
+    if (var->data) gras_free(var->data);
+    gras_free(var);
+    gras_free(name);
   }
   gras_dynar_free_container(frame);/* we just emptied it */
   DEBUG0("<<< Block end");
@@ -309,9 +305,10 @@ gras_cbps_block_end(gras_cbps_t *ps) {
  */
 void
 gras_cbps_i_push(gras_cbps_t        *ps, 
-                   int val) {
+                int val) {
   gras_error_t errcode;
-  TRYFAIL(gras_dynar_push(ps->lints,&val));
+  DEBUG1("push %d as a size",val);
+  gras_dynar_push(ps->lints,&val);
 }
 /**
  * gras_cbps_i_pop:
@@ -325,14 +322,54 @@ gras_cbps_i_pop(gras_cbps_t        *ps) {
   gras_assert0(gras_dynar_length(ps->lints) > 0,
               "gras_cbps_i_pop: no value to pop");
   gras_dynar_pop(ps->lints, &ret);
+  DEBUG1("pop %d as a size",ret);
   return ret;
 }
 
 /**
- * gras_dd_cb_pop:
+ * gras_datadesc_cb_pop:
  *
  * Generic cb returning the lastly pushed value
  */
 int gras_datadesc_cb_pop(gras_cbps_t *vars, void *data) {
   return gras_cbps_i_pop(vars);
 }
+
+/**
+ * gras_datadesc_cb_push_int:
+ * 
+ * Cb to push an integer. Must be attached to the field you want to push
+ */
+void gras_datadesc_cb_push_int(gras_cbps_t *vars, void *data) {
+   int *i = (int*)data;
+   gras_cbps_i_push(vars, (int) *i);
+}
+
+/**
+ * gras_datadesc_cb_push_uint:
+ * 
+ * Cb to push an unsigned integer. Must be attached to the field you want to push
+ */
+void gras_datadesc_cb_push_uint(gras_cbps_t *vars, void *data) {
+   unsigned int *i = (unsigned int*)data;
+   gras_cbps_i_push(vars, (int) *i);
+}
+
+/**
+ * gras_datadesc_cb_push_lint:
+ * 
+ * Cb to push an long integer. Must be attached to the field you want to push
+ */
+void gras_datadesc_cb_push_lint(gras_cbps_t *vars, void *data) {
+   long int *i = (long int*)data;
+   gras_cbps_i_push(vars, (int) *i);
+}
+/**
+ * gras_datadesc_cb_push_ulint:
+ * 
+ * Cb to push an long integer. Must be attached to the field you want to push
+ */
+void gras_datadesc_cb_push_ulint(gras_cbps_t *vars, void *data) {
+   unsigned long int *i = (unsigned long int*)data;
+   gras_cbps_i_push(vars, (int) *i);
+}