Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
set keyval to invalid after free
[simgrid.git] / src / smpi / include / smpi_keyvals.hpp
1 /* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SMPI_KEYVALS_HPP_INCLUDED
7 #define SMPI_KEYVALS_HPP_INCLUDED
8
9 #include "smpi/smpi.h"
10
11 #include <unordered_map>
12
13 struct smpi_delete_fn {
14   MPI_Comm_delete_attr_function          *comm_delete_fn;
15   MPI_Type_delete_attr_function          *type_delete_fn;
16   MPI_Win_delete_attr_function           *win_delete_fn;
17   MPI_Comm_delete_attr_function_fort     *comm_delete_fn_fort;
18   MPI_Type_delete_attr_function_fort     *type_delete_fn_fort;
19   MPI_Win_delete_attr_function_fort      *win_delete_fn_fort;
20 };
21
22 struct smpi_copy_fn {
23   MPI_Comm_copy_attr_function          *comm_copy_fn;
24   MPI_Type_copy_attr_function          *type_copy_fn;
25   MPI_Win_copy_attr_function           *win_copy_fn;
26   MPI_Comm_copy_attr_function_fort     *comm_copy_fn_fort;
27   MPI_Type_copy_attr_function_fort     *type_copy_fn_fort;
28   MPI_Win_copy_attr_function_fort      *win_copy_fn_fort;
29 };
30
31 struct s_smpi_key_elem_t {
32   smpi_copy_fn copy_fn;
33   smpi_delete_fn delete_fn;
34   void* extra_state;
35   int refcount;
36 };
37
38 using smpi_key_elem = s_smpi_key_elem_t*;
39
40 namespace simgrid{
41 namespace smpi{
42
43 class Keyval{
44   private:
45     std::unordered_map<int, void*> attributes_;
46   protected:
47     std::unordered_map<int, void*>* attributes();
48   public:
49 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
50 //    static std::unordered_map<int, smpi_key_elem> keyvals_;
51 //    static int keyval_id_;
52     template <typename T>
53     static int keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval,
54                              void* extra_state);
55     template <typename T> static int keyval_free(int* keyval);
56     template <typename T> int attr_delete(int keyval);
57     template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
58     template <typename T> int attr_put(int keyval, void* attr_value);
59     template <typename T>
60     static int call_deleter(T* obj, const s_smpi_key_elem_t* elem, int keyval, void* value, int* flag);
61     template <typename T> void cleanup_attr();
62 };
63
64 template <typename T>
65 int Keyval::keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval, void* extra_state)
66 {
67   auto* value = new s_smpi_key_elem_t;
68
69   value->copy_fn=copy_fn;
70   value->delete_fn=delete_fn;
71   value->extra_state=extra_state;
72   value->refcount=1;
73
74   *keyval = T::keyval_id_;
75   T::keyvals_.insert({*keyval, value});
76   T::keyval_id_++;
77   return MPI_SUCCESS;
78 }
79
80 template <typename T> int Keyval::keyval_free(int* keyval){
81 /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it
82          * is in use in an attribute */
83   smpi_key_elem elem = T::keyvals_.at(*keyval);
84   if (elem == nullptr) {
85     return MPI_ERR_ARG;
86   }
87   if(elem->refcount==1){
88     T::keyvals_.erase(*keyval);
89     delete elem;
90   }else{
91     elem->refcount--;
92   }
93   *keyval = MPI_KEYVAL_INVALID;
94   return MPI_SUCCESS;
95 }
96
97 template <typename T> int Keyval::attr_delete(int keyval){
98   smpi_key_elem elem = T::keyvals_.at(keyval);
99   if(elem==nullptr)
100     return MPI_ERR_ARG;
101   elem->refcount--;
102   void * value = nullptr;
103   int flag=0;
104   if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
105     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
106     if(ret!=MPI_SUCCESS)
107         return ret;
108   }
109   if(attributes()->empty())
110     return MPI_ERR_ARG;
111   attributes()->erase(keyval);
112   return MPI_SUCCESS;
113 }
114
115
116 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
117   const s_smpi_key_elem_t* elem = T::keyvals_.at(keyval);
118   if(elem==nullptr)
119     return MPI_ERR_ARG;
120   if(attributes()->empty()){
121     *flag=0;
122     return MPI_SUCCESS;
123   }
124   const auto& attribs = attributes();
125   auto attr           = attribs->find(keyval);
126   if (attr != attribs->end()) {
127     *static_cast<void**>(attr_value) = attr->second;
128     *flag=1;
129   } else {
130     *flag=0;
131   }
132   return MPI_SUCCESS;
133 }
134
135 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
136   smpi_key_elem elem = T::keyvals_.at(keyval);
137   if(elem==nullptr)
138     return MPI_ERR_ARG;
139   elem->refcount++;
140   int flag=0;
141   auto p = attributes()->insert({keyval, attr_value});
142   if (!p.second) {
143     int ret = call_deleter<T>((T*)this, elem, keyval,p.first->second,&flag);
144     // overwrite previous value
145     p.first->second = attr_value;
146     if(ret!=MPI_SUCCESS)
147       return ret;
148   }
149   return MPI_SUCCESS;
150 }
151
152 template <typename T> void Keyval::cleanup_attr(){
153   if (not attributes()->empty()) {
154     int flag=0;
155     for (auto const& it : attributes_) {
156       auto elm = T::keyvals_.find(it.first);
157       if (elm != T::keyvals_.end()) {
158         smpi_key_elem elem = elm->second;
159         if(elem != nullptr){
160           call_deleter<T>((T*)this, elem, it.first,it.second,&flag);
161         }
162       } else {
163         // already deleted, not a problem
164         flag=0;
165       }
166     }
167   }
168 }
169
170 }
171 }
172
173 #endif