Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Remove useless commented lines of code.
[simgrid.git] / src / smpi / include / smpi_keyvals.hpp
index c0ee395..a23be10 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2019. The SimGrid Team. 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. */
@@ -7,29 +7,35 @@
 #define SMPI_KEYVALS_HPP_INCLUDED
 
 #include "smpi/smpi.h"
-#include "xbt/ex.hpp"
 
 #include <unordered_map>
 
-typedef struct smpi_delete_fn{
+struct smpi_delete_fn {
   MPI_Comm_delete_attr_function          *comm_delete_fn;
   MPI_Type_delete_attr_function          *type_delete_fn;
   MPI_Win_delete_attr_function           *win_delete_fn;
-} smpi_delete_fn;
+  MPI_Comm_delete_attr_function_fort     *comm_delete_fn_fort;
+  MPI_Type_delete_attr_function_fort     *type_delete_fn_fort;
+  MPI_Win_delete_attr_function_fort      *win_delete_fn_fort;
+};
 
-typedef struct smpi_copy_fn{
+struct smpi_copy_fn {
   MPI_Comm_copy_attr_function          *comm_copy_fn;
   MPI_Type_copy_attr_function          *type_copy_fn;
   MPI_Win_copy_attr_function           *win_copy_fn;
-} smpi_copy_fn;
+  MPI_Comm_copy_attr_function_fort     *comm_copy_fn_fort;
+  MPI_Type_copy_attr_function_fort     *type_copy_fn_fort;
+  MPI_Win_copy_attr_function_fort      *win_copy_fn_fort;
+};
 
-typedef struct s_smpi_key_elem {
+struct s_smpi_key_elem_t {
   smpi_copy_fn copy_fn;
   smpi_delete_fn delete_fn;
+  void* extra_state;
   int refcount;
-} s_smpi_mpi_key_elem_t;
+};
 
-typedef struct s_smpi_key_elem *smpi_key_elem;
+typedef s_smpi_key_elem_t* smpi_key_elem;
 
 namespace simgrid{
 namespace smpi{
@@ -43,7 +49,9 @@ class Keyval{
 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
 //    static std::unordered_map<int, smpi_key_elem> keyvals_;
 //    static int keyval_id_;
-    template <typename T> static int keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_statee);
+    template <typename T>
+    static int keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval,
+                             void* extra_state);
     template <typename T> static int keyval_free(int* keyval);
     template <typename T> int attr_delete(int keyval);
     template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
@@ -52,12 +60,14 @@ class Keyval{
     template <typename T> void cleanup_attr();
 };
 
-template <typename T> int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_state){
-
-  smpi_key_elem value = (smpi_key_elem) xbt_new0(s_smpi_mpi_key_elem_t,1);
+template <typename T>
+int Keyval::keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval, void* extra_state)
+{
+  smpi_key_elem value = new s_smpi_key_elem_t;
 
   value->copy_fn=copy_fn;
   value->delete_fn=delete_fn;
+  value->extra_state=extra_state;
   value->refcount=1;
 
   *keyval = T::keyval_id_;
@@ -75,7 +85,7 @@ template <typename T> int Keyval::keyval_free(int* keyval){
   }
   if(elem->refcount==1){
     T::keyvals_.erase(*keyval);
-    xbt_free(elem);
+    delete elem;
   }else{
     elem->refcount--;
   }
@@ -125,15 +135,15 @@ template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
   if(elem==nullptr)
     return MPI_ERR_ARG;
   elem->refcount++;
-  void * value = nullptr;
   int flag=0;
-  this->attr_get<T>(keyval, &value, &flag);
-  if(flag!=0){
-    int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
+  auto p = attributes()->insert({keyval, attr_value});
+  if (!p.second) {
+    int ret = call_deleter<T>((T*)this, elem, keyval,p.first->second,&flag);
+    // overwrite previous value
+    p.first->second = attr_value;
     if(ret!=MPI_SUCCESS)
-        return ret;
+      return ret;
   }
-  attributes()->insert({keyval, attr_value});
   return MPI_SUCCESS;
 }
 
@@ -148,7 +158,7 @@ template <typename T> void Keyval::cleanup_attr(){
           call_deleter<T>((T*)this, elem, it.first,it.second,&flag);
         }
       } else {
-        //already deleted, not a problem;
+        // already deleted, not a problem
         flag=0;
       }
     }