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
|
/* lb-garbage.C
* Copyright (C) 2005 Pascal Giorgi
*
* Written by Pascal Giorgi <pgiorgi@uwaterloo.ca>
*
* ========LICENCE========
* This file is part of the library LinBox.
*
* LinBox 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ========LICENCE========
*/
#ifndef __LINBOX_lb_garbage_C
#define __LINBOX_lb_garbage_C
#include <lb-garbage.h>
#include <lb-domain-function.h>
#include <lb-element-data.h>
#include <lb-blackbox-function.h>
#include <lb-vector-function.h>
#define __LINBOX_NO_GC_EXCEPTION
/***************************************
* API to delete a domain from ist key *
***************************************/
class DeleteElementFunctor{
protected:
EltAbstract *elt;
public:
DeleteElementFunctor(EltAbstract *e) : elt(e) {}
template<class Domain>
void operator() (void *, Domain *D) const {
if (EltEnvelope<typename Domain::Element> *ptr = dynamic_cast<EltEnvelope<typename Domain::Element>*>(elt))
delete ptr;
else
throw lb_runtime_error("LinBox ERROR: incompatible domain and element type (freeing element impossible)");
}
};
void deleteElement(const EltKey &key){
EltTable::iterator it = element_hashtable.find(key);
if ( it == element_hashtable.end()){
#ifndef __LINBOX_NO_GC_EXCEPTION
throw lb_runtime_error("LinBox ERROR: invalid element (freeing impossible)");
#endif
}
else {
DeleteElementFunctor Fct(it->second);
#ifdef __LINBOX_NO_GC_EXCEPTION
try {
#endif
DomainFunction::call(it->second->getDomainKey(), Fct);
#ifdef __LINBOX_NO_GC_EXCEPTION
} catch(lb_runtime_error &t){std::cout<<"LinBox exception catched\n"; exit(0);}
#endif
element_hashtable.erase(it);
}
}
/*****************************
* API to collect all domain *
*****************************/
void collectElement(){
EltTable::iterator it = element_hashtable.begin();
for (;it != element_hashtable.end(); ++it){
delete it->second;
element_hashtable.erase(it);
}
}
/***************************************
* API to delete a domain from its key *
***************************************/
void deleteDomain(const DomainKey &key) {
if (key.free()){
DomainTable::iterator it= domain_hashtable.find(key);
delete it->second;
domain_hashtable.erase(it);
}
else{
key.dispose();
}
}
/*****************************
* API to collect all domain *
*****************************/
void collectDomain(){
DomainTable::iterator it= domain_hashtable.begin();
for (; it != domain_hashtable.end(); ++it){
delete it->second;
domain_hashtable.erase(it);
}
}
/*****************************************
* API to delete a blackbox from its key *
*****************************************/
class DeleteBlackboxFunctor{
public:
template<class Blackbox>
void operator() (void *, Blackbox *B) const {
delete B;
}
};
void deleteBlackbox (const BlackboxKey &key){
BlackboxTable::iterator it = blackbox_hashtable.find(key);
if ( it == blackbox_hashtable.end()){
#ifndef __LINBOX_NO_GC_EXCEPTION
throw lb_runtime_error("LinBox ERROR: invalid blackbox (freeing impossible)\n");
#else
std::cout<<"LinBox ERROR: invalid blackbox (freeing impossible)\n";
#endif
}
else {
DeleteBlackboxFunctor Fct;
#ifdef __LINBOX_NO_GC_EXCEPTION
try {
#endif
BlackboxFunction::call(key, Fct);
#ifdef __LINBOX_NO_GC_EXCEPTION
} catch (lb_runtime_error &t) {std::cout<<"LinBox exception catched: "<<t<<"\n"; exit(0);}
#endif
delete it->second;
blackbox_hashtable.erase(it);
}
}
/*******************************
* API to collect all blackbox *
*******************************/
void collectBlackbox(){
DeleteBlackboxFunctor Fct;
BlackboxTable::iterator it= blackbox_hashtable.begin();
for (; it != blackbox_hashtable.end(); ++it){std::cout<<"bb to delete: "<<it->second->info()<<"\n";
#ifdef __LINBOX_NO_GC_EXCEPTION
try{
#endif
BlackboxFunction::call(*it, Fct);
#ifdef __LINBOX_NO_GC_EXCEPTION
} catch (lb_runtime_error &t) {std::cout<<"LinBox exception catched\n"; exit(0);}
#endif
delete it->second;
blackbox_hashtable.erase(it);
}
}
/***************************************
* API to delete a vector from its key *
***************************************/
class DeleteVectorFunctor{
public:
template<class Vector>
void operator() (void*, Vector *V) const {
delete V;
}
};
void deleteVector (const VectorKey &key){
VectorTable::iterator it = vector_hashtable.find(key);
if ( it == vector_hashtable.end()){
#ifndef __LINBOX_NO_GC_EXCEPTION
throw lb_runtime_error("LinBox ERROR: invalid vector (freeing impossible)");
#endif
}
else {
DeleteVectorFunctor Fct;
#ifdef __LINBOX_NO_GC_EXCEPTION
try{
#endif
VectorFunction::call(key, Fct);
#ifdef __LINBOX_NO_GC_EXCEPTION
} catch(lb_runtime_error &t) {exit(0);}
#endif
delete it->second;
vector_hashtable.erase(it);
}
}
/*****************************
* API to collect all vector *
*****************************/
void collectVector(){
DeleteVectorFunctor Fct;
VectorTable::iterator it= vector_hashtable.begin();
for (; it != vector_hashtable.end(); ++it){
#ifdef __LINBOX_NO_GC_EXCEPTION
try{
#endif
VectorFunction::call(*it, Fct);
#ifdef __LINBOX_NO_GC_EXCEPTION
} catch (lb_runtime_error &t) {std::cout<<"LinBox exception catched\n"; exit(0);}
#endif
delete it->second;
vector_hashtable.erase(it);
}
}
/***********************************************
* API to collect all data allocated by LinBox *
**********************************************/
void LinBoxCollect(){
collectVector();
collectBlackbox();
collectElement();
collectDomain();
}
#endif
// Local Variables:
// mode: C++
// tab-width: 4
// indent-tabs-mode: nil
// c-basic-offset: 4
// End:
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
|