Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Prefer using emplace() to insert() for smpi keyvals/attributes.
[simgrid.git] / src / smpi / include / smpi_keyvals.hpp
index c6a200f1d33d02e617ab2513ed3f36a04b0701d9..d1c647d582bd59b5b540e97569f55570b3fb40e6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2021. 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. */
@@ -35,7 +35,7 @@ struct s_smpi_key_elem_t {
   int refcount;
 };
 
-typedef s_smpi_key_elem_t* smpi_key_elem;
+using smpi_key_elem = s_smpi_key_elem_t*;
 
 namespace simgrid{
 namespace smpi{
@@ -44,23 +44,28 @@ class Keyval{
   private:
     std::unordered_map<int, void*> attributes_;
   protected:
-    std::unordered_map<int, void*>* attributes();
+    std::unordered_map<int, void*>& attributes() { return attributes_; }
+
   public:
 // 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);
     template <typename T> int attr_put(int keyval, void* attr_value);
-    template <typename T> static int call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag);
+    template <typename T>
+    static int call_deleter(T* obj, const s_smpi_key_elem_t* elem, int keyval, void* value, int* flag);
     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 = new s_smpi_key_elem_t;
+template <typename T>
+int Keyval::keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval, void* extra_state)
+{
+  auto* value = new s_smpi_key_elem_t;
 
   value->copy_fn=copy_fn;
   value->delete_fn=delete_fn;
@@ -68,7 +73,7 @@ template <typename T> int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delet
   value->refcount=1;
 
   *keyval = T::keyval_id_;
-  T::keyvals_.insert({*keyval, value});
+  T::keyvals_.emplace(*keyval, value);
   T::keyval_id_++;
   return MPI_SUCCESS;
 }
@@ -77,7 +82,7 @@ template <typename T> int Keyval::keyval_free(int* keyval){
 /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it
          * is in use in an attribute */
   smpi_key_elem elem = T::keyvals_.at(*keyval);
-  if(elem==0){
+  if (elem == nullptr) {
     return MPI_ERR_ARG;
   }
   if(elem->refcount==1){
@@ -86,6 +91,7 @@ template <typename T> int Keyval::keyval_free(int* keyval){
   }else{
     elem->refcount--;
   }
+  *keyval = MPI_KEYVAL_INVALID;
   return MPI_SUCCESS;
 }
 
@@ -101,24 +107,20 @@ template <typename T> int Keyval::attr_delete(int keyval){
     if(ret!=MPI_SUCCESS)
         return ret;
   }
-  if(attributes()->empty())
+  if (attributes().empty())
     return MPI_ERR_ARG;
-  attributes()->erase(keyval);
+  attributes().erase(keyval);
   return MPI_SUCCESS;
 }
 
 
 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
-  smpi_key_elem elem = T::keyvals_.at(keyval);
+  const s_smpi_key_elem_t* elem = T::keyvals_.at(keyval);
   if(elem==nullptr)
     return MPI_ERR_ARG;
-  if(attributes()->empty()){
-    *flag=0;
-    return MPI_SUCCESS;
-  }
-  const auto& attribs = attributes();
-  auto attr           = attribs->find(keyval);
-  if (attr != attribs->end()) {
+
+  auto attr = attributes().find(keyval);
+  if (attr != attributes().end()) {
     *static_cast<void**>(attr_value) = attr->second;
     *flag=1;
   } else {
@@ -133,8 +135,8 @@ template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
     return MPI_ERR_ARG;
   elem->refcount++;
   int flag=0;
-  auto p = attributes()->insert({keyval, attr_value});
-  if (!p.second) {
+  auto p  = attributes().emplace(keyval, attr_value);
+  if (not p.second) {
     int ret = call_deleter<T>((T*)this, elem, keyval,p.first->second,&flag);
     // overwrite previous value
     p.first->second = attr_value;
@@ -145,19 +147,17 @@ template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
 }
 
 template <typename T> void Keyval::cleanup_attr(){
-  if (not attributes()->empty()) {
-    int flag=0;
-    for (auto const& it : attributes_) {
-      auto elm = T::keyvals_.find(it.first);
-      if (elm != T::keyvals_.end()) {
-        smpi_key_elem elem = elm->second;
-        if(elem != nullptr){
-          call_deleter<T>((T*)this, elem, it.first,it.second,&flag);
-        }
-      } else {
-        //already deleted, not a problem;
-        flag=0;
+  int flag = 0;
+  for (auto const& it : attributes()) {
+    auto elm = T::keyvals_.find(it.first);
+    if (elm != T::keyvals_.end()) {
+      smpi_key_elem elem = elm->second;
+      if (elem != nullptr) {
+        call_deleter<T>((T*)this, elem, it.first, it.second, &flag);
       }
+    } else {
+      // already deleted, not a problem
+      flag = 0;
     }
   }
 }