/usr/include/getfem/dal_static_stored_objects.h is in libgetfem++-dev 4.2.1~beta1~svn4482~dfsg-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | /* -*- c++ -*- (enables emacs c++ mode) */
/*===========================================================================
Copyright (C) 2002-2012 Yves Renard
This file is a part of GETFEM++
Getfem++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version along with the GCC Runtime Library
Exception either version 3.1 or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License and GCC Runtime Library Exception for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, you may use this file as it is a part of a free
software library without restriction. Specifically, if other files
instantiate templates or use macros or inline functions from this file,
or you compile this file and link it with other files to produce an
executable, this file does not by itself cause the resulting executable
to be covered by the GNU Lesser General Public License. This exception
does not however invalidate any other reasons why the executable file
might be covered by the GNU Lesser General Public License.
===========================================================================*/
/** @file dal_static_stored_objects.h
@author Yves Renard <Yves.Renard@insa-lyon.fr>
@date February 19, 2005
@brief Stores interdependent getfem objects.
Stored object :
A type of object to be stored should derive from
dal::static_stored_object and a key should inherit from
static_stored_object_key with an overloaded "compare" method.
To store a new object, you have to test if the object is not
already stored and then call dal::add_stored_object:
@code
if (!search_stored_object(your_object_key(parameters))) {
add_stored_object(new your_object_key(parameters),
new your_object(parameters));
}
@endcode
You can add a dependency of your new object with
@code
add_dependency(pointer_on_your_object,
pointer_on_the_object_object_from_which_it_depends);
@endcode
and then your object will be automatically deleted if the second object is
deleted.
The dependency can be added within the add_stored_object call:
@code
add_stored_object(new your_object_key(parameters),
new your_object(parameters),
dependency);
@endcode
Boost intrusive_ptr are used.
*/
#ifndef DAL_STATIC_STORED_OBJECTS_H__
#define DAL_STATIC_STORED_OBJECTS_H__
#include "dal_config.h"
#include "getfem_omp.h"
#include <algorithm>
#include "dal_singleton.h"
#include <set>
#include "getfem/getfem_arch_config.h"
#ifdef GETFEM_HAVE_BOOST
# include <boost/intrusive_ptr.hpp>
#else
# include <getfem_boost/intrusive_ptr.hpp>
#endif
#include <memory>
namespace dal {
enum permanence { PERMANENT_STATIC_OBJECT = 0, // not deletable object
STRONG_STATIC_OBJECT = 1, // preferable not to delete it
STANDARD_STATIC_OBJECT = 2, // standard
WEAK_STATIC_OBJECT = 3, // delete it if necessary
AUTODELETE_STATIC_OBJECT = 4 // automatically deleted
// when the last dependent object is deleted
};
class static_stored_object_key {
protected :
virtual bool compare(const static_stored_object_key &) const {
GMM_ASSERT1(false, "This method should not be called");
}
public :
bool operator < (const static_stored_object_key &o) const {
// comparaison des noms d'objet
if (typeid(*this).before(typeid(o))) return true;
if (typeid(o).before(typeid(*this))) return false;
return compare(o);
}
virtual ~static_stored_object_key() {}
};
template <typename var_type>
class simple_key : virtual public static_stored_object_key {
var_type a;
public :
virtual bool compare(const static_stored_object_key &oo) const {
const simple_key &o = dynamic_cast<const simple_key &>(oo);
if (a < o.a) return true; return false;
}
simple_key(var_type aa) : a(aa) {}
};
#define DAL_SIMPLE_KEY(class_name, var_type) \
struct class_name : public dal::simple_key<var_type> { \
class_name(var_type aa) : dal::simple_key<var_type>(aa) {} \
}
#define DAL_DOUBLE_KEY(class_name, var_type1, var_type2) \
struct class_name : \
public dal::simple_key<std::pair<var_type1,var_type2> > { \
class_name(var_type1 aa, var_type2 bb) : \
dal::simple_key<std::pair<var_type1,var_type2> > \
(std::make_pair(aa,bb)) {} \
}
#define DAL_TRIPLE_KEY(class_name, var_type1, var_type2, var_type3) \
struct class_name : \
public dal::simple_key<std::pair<var_type1, \
std::pair<var_type2,var_type3> > > { \
class_name(var_type1 aa, var_type2 bb, var_type3 cc) : \
dal::simple_key<std::pair<var_type1, \
std::pair<var_type2, var_type3> > > \
(std::make_pair(aa,std::make_pair(bb,cc))) {} \
}
#define DAL_FOUR_KEY(class_name,var_type1,var_type2,var_type3,var_type4)\
struct class_name : public \
dal::simple_key<std::pair \
<var_type1, std::pair<var_type2, std::pair \
<var_type3,var_type4> > > > { \
class_name(var_type1 aa, var_type2 bb, var_type3 cc,var_type4 dd) : \
dal::simple_key<std::pair \
<var_type1, std::pair<var_type2, \
std::pair<var_type3, \
var_type4> > > > \
(std::make_pair(aa,std::make_pair(bb,std::make_pair(cc, dd)))) {} \
}
typedef const static_stored_object_key* pstatic_stored_object_key;
class static_stored_object;
struct static_stored_objects_garbage{
std::set<const static_stored_object*> garbage_pointers;
public:
static_stored_objects_garbage(){}
inline void add_to_garbage_bin(const static_stored_object* p)
{garbage_pointers.insert(p);}
};
/**
base class for reference-counted getfem objects (via
boost::intrusive_ptr).
The reference-counting is thread safe, but the garbage
is removed after parallel region
@see dal_static_stored_objects.h
*/
class static_stored_object {
mutable getfem::omp_distribute<long> pointer_ref_count_;
public :
static_stored_object(void) : pointer_ref_count_(0) {}
virtual ~static_stored_object() { }
friend void intrusive_ptr_add_ref(const static_stored_object *o);
friend void intrusive_ptr_release(const static_stored_object *o);
inline long ref_sum() const {
long sum=0;
for(size_t i=0;i<getfem::num_threads();i++)
sum+=pointer_ref_count_(i);
return sum;
}
};
typedef boost::intrusive_ptr<const static_stored_object>
pstatic_stored_object;
template<class T> boost::intrusive_ptr<const T>
stored_cast(pstatic_stored_object o) {
return boost::intrusive_ptr<const T>(dynamic_cast<const T *>(o.get()));
}
inline void intrusive_ptr_add_ref(const static_stored_object *o)
{
o->pointer_ref_count_++;
}
inline void intrusive_ptr_release(const static_stored_object *o)
{
if (--(o->pointer_ref_count_) > 0) return;
if (getfem::me_is_multithreaded_now() ) {
//GMM_ASSERT3(getfem::open_mp_is_running_properly::is_it(),
// "Open MP parallel region was initialized without"
// " open_mp_loop_runner !! This will lead to memory leaks");
static_stored_objects_garbage& garbage =
dal::singleton<static_stored_objects_garbage>::instance();
garbage.add_to_garbage_bin(o);
} else {
if (o->ref_sum()==0)
delete o;
}
}
/** Gives a pointer to an object from a key pointer. */
pstatic_stored_object search_stored_object(pstatic_stored_object_key k);
/** Gives a pointer to an object from a key reference. */
inline pstatic_stored_object
search_stored_object(const static_stored_object_key &k)
{ return search_stored_object(&k); }
/** Test if an object is stored in local thread storage. */
bool exists_stored_object(pstatic_stored_object o);
/** Test if an object is stored in storage of all threads. */
bool exists_stored_object_all_threads(pstatic_stored_object o);
/** Add a dependency, object o1 will depend on object o2. */
void add_dependency(pstatic_stored_object o1, pstatic_stored_object o2);
/** remove a dependency. Return true if o2 has no more dependent object. */
bool del_dependency(pstatic_stored_object o1, pstatic_stored_object o2);
/** Add an object with two optional dependencies. */
void add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o,
permanence perm = STANDARD_STATIC_OBJECT);
inline void
add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o,
pstatic_stored_object dep1,
permanence perm = STANDARD_STATIC_OBJECT) {
add_stored_object(k, o, perm);
add_dependency(o, dep1);
}
inline void
add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o,
pstatic_stored_object dep1, pstatic_stored_object dep2,
permanence perm = STANDARD_STATIC_OBJECT) {
add_stored_object(k, o, perm);
add_dependency(o, dep1);
add_dependency(o, dep2);
}
inline void
add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o,
pstatic_stored_object dep1, pstatic_stored_object dep2,
pstatic_stored_object dep3,
permanence perm = STANDARD_STATIC_OBJECT) {
add_stored_object(k, o, perm);
add_dependency(o, dep1);
add_dependency(o, dep2);
add_dependency(o, dep3);
}
inline void
add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o,
pstatic_stored_object dep1, pstatic_stored_object dep2,
pstatic_stored_object dep3, pstatic_stored_object dep4,
permanence perm = STANDARD_STATIC_OBJECT) {
add_stored_object(k, o, perm);
add_dependency(o, dep1);
add_dependency(o, dep2);
add_dependency(o, dep3);
add_dependency(o, dep4);
}
/** does the actual deletion of the objects after the parallel OpenMP section.
The list of the objects for deletion was populated by the following
del_stored_object(s) functions*/
void flush_deleted_objects();
/** Delete an object and the object which depend on it. */
void del_stored_object(pstatic_stored_object o, bool ignore_unstored=false);
/** Delete all the object whose permanence is greater or equal to perm. */
void del_stored_objects(int perm);
/** Gives a pointer to a key of an object from its pointer. */
pstatic_stored_object_key key_of_stored_object(pstatic_stored_object o);
/** Show a list of stored objects (for debugging purpose). */
void list_stored_objects(std::ostream &ost);
/** Return the number of stored objects (for debugging purpose). */
size_t nb_stored_objects(void);
}
#endif /* DAL_STATIC_STORED_OBJECTS_H__ */
|