/usr/include/freefem++/ffstack.hpp is in libfreefem++-dev 3.47+dfsg1-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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | /// \file
// -*- Mode : c++ -*-
//
// SUMMARY :
// USAGE :
// ORG :
// AUTHOR : Frederic Hecht
// E-MAIL : hecht@ann.jussieu.fr
//
/*
This file is part of Freefem++
Freefem++ 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.
Freefem++ 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 Freefem++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//-----------------------------------
// to manage the freefem stack
#include "throwassert.hpp"
// in the stack we save all the variable
// a adresse 0 we have the MeshPointStack to defineP,N, ....
// a adresse sizeof(void *)
//
//
// Offset in (void *)
const int MeshPointStackOffset =0;
const int ParamPtrOffset = 1;
const int ElemMatPtrOffset = 2;
const int ExprPtrs = 4;
const int NbPtrs = 5;
const int BeginOffset = 6;
// 0 : MeshPoint pointeur
// 1 : ParamPtrOffset
// 2 : Truc les matrice elementaire
#define NEWFFSTACKxxx
#ifndef NEWFFSTACK
typedef void StackType;
/// <<Stack>>
typedef void *Stack;
const Stack NullStack=0;
//typedef StackType& Stack;
inline Stack pvoid2Stack(void * pv) { return pv;}
template<class T>
T * Stack_offset (Stack stack,size_t offset)
{ //cout << "Stack_offset" << stack << " " << offset << endl;
return (T *) (void *) (((char *) stack)+offset);}
template<class T>
T * & Stack_Ptr (Stack stack,size_t offset)
{return (T * &) (((void **) stack)[offset]);}
void ShowType(ostream & f);
struct VOIDPtrType {
virtual ~VOIDPtrType();
};
template<class T>
struct PtrType: public VOIDPtrType {
T * p;
PtrType(T* pp):p(pp) {}
~PtrType() {delete p;}
};
template<class T>
struct PtrArrayType: public VOIDPtrType {
T * p;
PtrArrayType(T* pp):p(pp) {}
PtrArrayType() {delete [] p;}
};
#else
struct StackType;
//typedef void *Stack;
/// Stack used by CListOfInst::eval()
typedef StackType & Stack;
struct StackType {
size_t lg;
char * stack;
char * MeshPointStack;
operator char *() { return stack;}
operator void *() { return stack;}
operator long *() { return (long *)(void *)stack;}
operator void **() {return (void **) (void *) stack;}
operator StackType *() { return this;}
template<class T>
T * Offset(size_t offset){ return (T*) (void*) (stack+offset);}
template<class T>
T *& ptr(size_t offset){ return (T* &) ((void**) (void *) stack)[offset];}
StackType(size_t ll) :lg(ll),stack(new char[ll]),MeshPointStack(new char[1000])
{
long * p= ptr<long>(0);
long l4=lg/sizeof(long);
for (int i = 0;i< l4;i++) p[i]=0;
ptr<char>(MeshPointStackOffset)=MeshPointStack;
}
void clean() { delete []stack; delete [] MeshPointStack; }
};
inline Stack pvoid2Stack(void * pv) { return *static_cast<StackType *>(pv) ;}
static StackType * NullStackPtr= 0;
static StackType & NullStack(*NullStackPtr);
//typedef StackType& Stack;
template<class T>
T * Stack_offset (Stack stack,size_t offset)
{return stack.Offset<T>(offset);}
template<class T>
T * & Stack_Ptr (Stack stack,size_t offset)
{return (T * &) (((void **) (void *) stack.stack)[offset]);}
void ShowType(ostream & f);
struct VOIDPtrType {
virtual ~VOIDPtrType();
};
template<class T>
struct PtrType: public VOIDPtrType {
T * p;
PtrType(T* pp):p(pp) {}
~PtrType() {delete p;}
};
template<class T>
struct PtrArrayType: public VOIDPtrType {
T * p;
PtrArrayType(T* pp):p(pp) {}
PtrArrayType() {delete [] p;}
};
#endif
//------------------------------------
// Add FH mars 2006
// clean pointeur ceated by the language
// ----
struct BaseNewInStack {
virtual ~BaseNewInStack() {};
};
struct BaseNewInStack ;
struct StackOfPtr2Free;
inline StackOfPtr2Free * & WhereStackOfPtr2Free(Stack s) { return Stack_Ptr<StackOfPtr2Free>(s,ExprPtrs) ;} // fait
struct StackOfPtr2Free {
typedef vector<BaseNewInStack *>::iterator iterator;
typedef vector<BaseNewInStack *>::reverse_iterator reverse_iterator;
StackOfPtr2Free ** where; // where is store the ptr to the stack
StackOfPtr2Free *prev; // previous stack
vector<BaseNewInStack *> stackptr;
static const int sizeofmemory4tmp=1024;
int topmemory4tmp;
char *memory4tmp ; //
void add(BaseNewInStack *p) {
// cout << "\n\t\t ### ptr/lg add " << p << " at " << stackptr.size() << " \n";
stackptr.push_back(p);}
public:
StackOfPtr2Free(Stack s):
where(&WhereStackOfPtr2Free(s)),
prev(*where),
topmemory4tmp(0), // add FH oct 2008 of tmp allocation clean after each instruction
memory4tmp(new char[sizeofmemory4tmp]) // add FH oct 2008 of tmp allocation clean after each instruction
{
stackptr.reserve(20);
if(prev) Add2StackOfPtr2Free(s,this);
}
bool clean()
{
topmemory4tmp=0;// clean the tmp allocation
bool ret= !stackptr.empty();
if(ret)
{
if(stackptr.size()>=20 && verbosity>2)
// FFCS: nothing on following line for tests/compare
cout << "\n\t\t ### big?? ptr/lg clean " << stackptr.size() << " ptr's\n";
for (reverse_iterator i=stackptr.rbegin(); i != stackptr.rend();++i)
{
delete (* (i) );
// cout << "StackOfPtr2Free: clean " << (* (i) ) << " " << endl;
}
stackptr.resize(0);// clean the
}
return ret;
}
void * alloc(int lg)
{
int lg8=lg%8;
if(lg8) lg += 8-lg8;
if(topmemory4tmp + lg>= sizeofmemory4tmp) {
ErrorExec(" Fatal Error: too much temporary alloction, your expression is too long, or a bug in ff++, FH; april,2015",1);
ffassert(0);}
void * p=static_cast<void*> (memory4tmp+topmemory4tmp);// correct FH Oct 2009
topmemory4tmp+= lg;
return p;
}
~StackOfPtr2Free() {clean();delete [] memory4tmp; *where=prev;} // restore the previous stack
private:// no copy ....
StackOfPtr2Free(const StackOfPtr2Free&);
void operator =(const StackOfPtr2Free&);
template<class T>
friend T * NewTmp(Stack s);
template<class T>
friend T * Add2StackOfPtr2Free(Stack s,T * p);
};
inline void * NewAllocTmp(Stack s,size_t l)
{
return WhereStackOfPtr2Free(s)->alloc(l);
}
template<class T>
struct NewInStack: public BaseNewInStack {
T * p;
bool array;
~NewInStack() {
// cout << "~NewInStack " << typeid(T).name() << " " << p << " array " << array << " " << this << "\n";
if(p)
delete p;}
private:
NewInStack(T * pp,bool aa=false) : p(pp),array(aa) {
// cout << "NewInStack " << typeid(T).name() << " " << p << " array " << aa << " " << this << "\n";
}
template<class TT>
friend TT * Add2StackOfPtr2FreeA(Stack s,TT * p);
template<class TT>
friend TT * Add2StackOfPtr2Free(Stack s,TT * p);
};
// ajout of 2 class NewRefCountInStack and NewArrayInStack
// for clean of meshes
template<class T>
struct NewRefCountInStack: public BaseNewInStack {
T * p;
bool array;
~NewRefCountInStack() {
// cout << "~NewInStack " << typeid(T).name() << " " << p << " array " << array << " " << this << "\n";
if(p) p->destroy();}
private:
NewRefCountInStack(T * pp,bool aa=false) : p(pp),array(aa) {
// cout << "NewInStack " << typeid(T).name() << " " << p << " array " << aa << " " << this << "\n";
}
template<class TT>
friend TT * Add2StackOfPtr2FreeRC(Stack s,TT * p);
};
template<class T>
struct NewArrayInStack: public BaseNewInStack {
T * p;
bool array;
~NewArrayInStack() {
// cout << "~NewInStack " << typeid(T).name() << " " << p << " array " << array << " " << this << "\n";
if(p) delete [] p;
}
private:
NewArrayInStack(T * pp,bool aa=false) : p(pp),array(aa) {
// cout << "NewInStack " << typeid(T).name() << " " << p << " array " << aa << " " << this << "\n";
}
template<class TT>
friend TT * Add2StackOfPtr2FreeA(Stack s,TT * p);
template<class TT>
friend TT * Add2StackOfPtr2Free(Stack s,TT * p);
};
template<class T>
T * Add2StackOfPtr2FreeRC(Stack s,T * p)
{
if(p)
WhereStackOfPtr2Free(s)->add(new NewRefCountInStack<T>(p));
return p;
}
template<class T>
T * Add2StackOfPtr2Free(Stack s,T * p)
{
if(p)
WhereStackOfPtr2Free(s)->add(new NewInStack<T>(p));
return p;
}
template<class T>
T * Add2StackOfPtr2FreeA(Stack s,T * p)
{
if(p)
WhereStackOfPtr2Free(s)->add(new NewArrayInStack<T>(p));
return p;
}
// fin modif gestion of allocation of Ptr in Language
// ---------------------------------------------------
#ifndef NEWFFSTACK
/// <<newStack>>
inline Stack newStack(size_t l)
{
char * mps;
Stack thestack = new char[l];
for (size_t i = 0;i< l/sizeof(long);i++) ((long*) thestack)[i]=0;
((char **) thestack)[MeshPointStackOffset] = mps = new char [1000];
for(int i=0;i<1000;++i) mps[i]=0;
WhereStackOfPtr2Free(thestack)=new StackOfPtr2Free(thestack);
return thestack;
// return *new StackType(l);}
}
inline void deleteStack(Stack s)
{
delete WhereStackOfPtr2Free(s); // add gestion of the Ptr
delete [] (((char **) s)[MeshPointStackOffset]);
delete [] (char *) s;
// s.clean();
}
#else
// a faire ....
/// Called to create a new #Stack used to evaluate a FreeFem++ script in CListOfInst::eval()
inline Stack newStack(size_t l)
{
/* Stack thestack = new char[l];
for (int i = 0;i< l/sizeof(long);i++) ((long*) thestack)[i]=0;
((char **) thestack)[MeshPointStackOffset] = new char [1000];
return thestack;*/
return *new StackType(l);
}
inline void deleteStack(Stack s)
{
// delete [] (((char **) s)[MeshPointStackOffset]);
// delete [] (char *) s;
s.clean();
}
#endif
|