1 //#define SHOWALLOC
2 #ifndef NCHECKPTR
3 #define NCHECKPTR // Bug with MPI ??? FH
4 #endif
5 /*
6 #if __cplusplus >= 201103L
7 #define PARAM_THROW(i) (i)
8 #define PARAM_NOTHROW(i) (i,nothrow) noexcept
9 #define DCLPARAM_NOTHROW(i) (i,const std::nothrow_t& nothrow_constant) noexcept
10 #define DLDCLPARAM_NOTHROW(i) (i,const std::nothrow_t& nothrow_constant) noexcept
11 #define DCLPARAM_THROW(i) (i) noexcept
12 #else
13 #define PARAM_THROW(i) (i) throw(std::bad_alloc);
14 #define PARAM_NOTHROW(i) (i,nothrow) throw()
15 #define DCLPARAM_NOTHROW(i) (i,const std::nothrow_t& nothrow_constant) throw()
16 #define DLDCLPARAM_NOTHROW(i) (i,const std::nothrow_t& nothrow_constant) throw()
17 #define DCLPARAM_THROW(i) (i) throw(std::bad_alloc)
18 #endif
19  98: // nothrow
20  void* operator new (std::size_t size) throw (std::bad_alloc);
21  void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw();
22  void operator delete[] (void* ptr) throw();
23  void operator delete[] (void* ptr, const std::nothrow_t& nothrow_constant) throw();
24 
25    11:
26  void* operator new (std::size_t size);
27  void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
28  p
29  */
30 
31 #include <cstdlib>
32 
33 static long verbosity;
34 
35 
StorageUsed()36 static long StorageUsed()
37 {
38 #if MALLOC_ZONE_SPECIFIC_FLAGS
39     struct mstats mem1;
40     mem1 = mstats();
41     return mem1.bytes_used;
42 #elif M_MMAP_THRESHOLD
43     struct mallinfo mem1;
44     mem1=mallinfo();
45     return mem1.uordblks;
46 #else
47     return 0;
48 #endif
49 
50 }
51 #ifndef NCHECKPTR
52 #define DEBUGUNALLOC 1
53 // -*- Mode : c++ -*-
54 //
55 // SUMMARY  :
56 // USAGE    :
57 // ORG      :
58 // AUTHOR   : Frederic Hecht
59 // E-MAIL   : hecht@ann.jussieu.fr
60 //
61 
62 /*
63 
64  This file is part of Freefem++
65 
66  Freefem++ is free software; you can redistribute it and/or modify
67  it under the terms of the GNU Lesser General Public License as published by
68  the Free Software Foundation; either version 2.1 of the License, or
69  (at your option) any later version.
70 
71  Freefem++  is distributed in the hope that it will be useful,
72  but WITHOUT ANY WARRANTY; without even the implied warranty of
73  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
74  GNU Lesser General Public License for more details.
75 
76  You should have received a copy of the GNU Lesser General Public License
77  along with Freefem++; if not, write to the Free Software
78  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
79  */
80 #include <cstdlib>
81 #include <cerrno>
82 #include <cstdio>
83 #include <new>
84 
debugalloc()85 void debugalloc()
86 { }
87 
debugunalloc()88 void debugunalloc()
89 { static long count=0;
90   //  debugalloc();
91  count++;}
92 
93 
exitalloc(int i)94 void exitalloc(int i)
95 { static long count=0;
96  count++;
97  exit(1);
98 }
99 
100 // Modif:         Juin 2001  for debuging  missing  delete point
101 //  --  THE MACRO
102 // TO SHOW ALL ALLOCATION
103 // #define SHOWALLOC
104 //  TO DEBUG ALL UN DELETE POINETUR
105 
106 
107 int UnShowAlloc =1;
108 
109 int ShowAlloc(const char *s,size_t & lg);
110 
111 //inline void *operator new(size_t, void *place) { return place; }
112 
113 static int kerr=0;
mymalloc(size_t l)114 void * mymalloc(size_t l)
115 {
116   char *p = (char*)malloc(l+16);
117   if (!p) return p;
118   for( int i = 0; i < 8 ; i++)
119     p[i] = 'a'+i,p[i+l+8] = 'z'-i; // put a marque before
120   return (void*) (p+8);
121 }
myfree(char * p,size_t l=0,int nordre=0)122 void myfree(char *p,size_t l=0,int nordre=0)
123 {
124   if(p) {
125     p -= 8;
126     int k =0;
127     for( int i = 0; i < 8 ; i++)
128       {
129 	if (p[i] != 'a' +i)     k++;
130 	if(l && (p[i+l+8] != 'z' -i)) k++;
131       }
132     for (size_t i=0;i<l;i++)
133       p[i+8]=127;
134     if(!k) free(p);
135    else {
136      debugalloc();
137      if (kerr++<20)
138        printf("@@@@@@@@@@@@@@@@@ Erreur myfree p= %p   l=%ul n=%d\n",p,(unsigned int) l,nordre);
139 
140    }
141   }
142 }
143 /*
144 void* operator new(std::size_t ) throw(std::bad_alloc) ;
145 void* operator new[](std::size_t ) throw(std::bad_alloc) ;
146 void* operator new(std::size_t ,const std::nothrow_t& nothrow_constant) throw() ;
147 void* operator new[](std::size_t ,const std::nothrow_t& nothrow_constant) throw() ;
148 
149 void operator delete[](void * ) throw();
150 void operator delete(void * ) throw();
151 void operator delete(void * ,const std::nothrow_t& nothrow_constant) throw();
152 void operator delete[](void * ,const std::nothrow_t& nothrow_constant) throw();
153 */
154 const int N100 = 10000;
155 class  AllocData;
156 
157 class AllocExtern {
158   public:
159 class OneAlloc {public:
160   void * p;
161   size_t l;
162   long n;
163   bool is_array ;
operator <(const OneAlloc & a) const164   bool operator<(const OneAlloc & a) const { return n<a.n;}
165 };
166 
HeapSort(OneAlloc ** c,long n)167   void  HeapSort(OneAlloc **c,long n)
168   {
169     long l,j,r,i;
170     OneAlloc* crit;
171     c--; // on decale de 1 pour que le tableau commence a 1
172     if( n <= 1) return;
173     l = n/2 + 1;
174     r = n;
175     while (1) { // label 2
176       if(l <= 1 ) { // label 20
177 	crit = c[r];
178 	c[r--] = c[1];
179 	if ( r == 1 ) { c[1]=crit; return;}
180       } else  crit = c[--l];
181       j=l;
182       while (1) {// label 4
183       i=j;
184       j=2*j;
185       if  (j>r) {c[i]=crit;break;} // L8 -> G2
186       if ((j<r) && (*c[j] < *c[j+1])) j++; // L5
187       if ( *crit < *c[j]) c[i]=c[j]; // L6+1 G4
188       else {c[i]=crit;break;} //L8 -> G2
189       }
190     }
191   }
192 
193   class AllocData {public:
194     OneAlloc *a;
195     AllocData * next;
196     AllocData();
197     ~AllocData();
198     private:
199     AllocData(const AllocExtern::AllocData&);
200     void operator=(const AllocExtern::AllocData&);
201   };
202 
203 private:
204 
205   static const long Maxundelptr = 2048;
206     static size_t StorageUsage;
207   static size_t AllocSize ;
208   static size_t MaxUsedSize;
209   static AllocData * AllocHead ;
210   static long NbAlloc;
211   static long NbAllocShow;
212   static long NbPtr;
213   static void * NextFree;
214   static long NbuDelPtr;
215   static long uDelPtr[Maxundelptr];
216   static bool after_end;
217   static char filename[128];
218   AllocData * NewAllocData();
219   OneAlloc *Alloc();
220   static   const int nblastalloc=20;
221     static OneAlloc *  LastAlloc[nblastalloc];
222 public:
223   static  bool CheckAlloc ;
224 
225   void * MyNewOperator(size_t ll,bool is_array );
226   void MyDeleteOperator(void * pp,bool is_array);
227   AllocExtern();
228   ~AllocExtern();
229   void init();
230   int ShowAlloc( const char *s,size_t & lg);
IsUnDelPtr(long nn)231   bool IsUnDelPtr(long nn) { // dichotomic find
232     long i=0;
233     long j=NbuDelPtr-1;
234     while (i<=j) {
235       long k = (i+j)/2, kn=uDelPtr[k];
236       if ( nn<kn) j=k-1;
237       else if ( nn > kn) i = k+1;
238       else
239         return  true;}
240     return false;
241   }
242 };
243 
244 static AllocExtern AllocExternData;
245 size_t AllocExtern::StorageUsage=0;
246 size_t AllocExtern::AllocSize =0;
247 size_t AllocExtern::MaxUsedSize =0;
248 AllocExtern::AllocData * AllocExtern::AllocHead =0;
249 long AllocExtern::NbAlloc =0;
250 long AllocExtern::NbAllocShow=0;
251 long AllocExtern::NbPtr =0;
252 void * AllocExtern::NextFree =0;
253 long AllocExtern::NbuDelPtr =0;
254 long AllocExtern::uDelPtr[Maxundelptr];
255 bool AllocExtern::after_end =false;
256 bool AllocExtern::CheckAlloc = true;
257 char AllocExtern::filename[128] ="ListOfUnAllocPtr.bin";
258 AllocExtern::OneAlloc * AllocExtern::LastAlloc[nblastalloc];
NewAllocData()259 AllocExtern::AllocData * AllocExtern::NewAllocData()
260 {
261 
262   AllocExtern::AllocData * ad = (AllocData *) mymalloc(sizeof(AllocData));
263   ad->a = (OneAlloc*) mymalloc(sizeof(OneAlloc)*N100);
264   for (int i=0;i<N100;i++)
265     ad->a[i].l=0,ad->a[i].p=NextFree,NextFree = & ad->a[i];
266   ad->next = AllocHead;
267   AllocHead = ad;
268 #ifdef SHOWALLOC
269   printf("\t\tCheckPtr: OneAlloc[100] %lx\n",this);
270 #endif
271   return ad;
272 }
273 
274 
275 
Alloc()276 AllocExtern::OneAlloc * AllocExtern::Alloc()
277 {
278   OneAlloc * f =  (OneAlloc *) NextFree;
279   if (!f)
280     AllocHead = NewAllocData();
281   f =(OneAlloc *) NextFree;
282   if (!f) exitalloc(1);
283   NextFree =   f->p;
284   return f;
285 }
286 
287 
MyNewOperator(size_t ll,bool is_array)288 void * AllocExtern::MyNewOperator(size_t ll,bool is_array)
289 {
290   if(after_end || !CheckAlloc)  return malloc(ll);
291   init();
292   AllocExtern::OneAlloc * a = Alloc();
293   a->p = mymalloc(ll);
294   a->l = ll+1; // pour les allocation null
295   a->n = ++NbAlloc;
296   a->is_array = is_array;
297   LastAlloc[NbPtr%nblastalloc] = a;
298 
299   NbPtr++;
300   AllocSize += ll;
301 #ifdef DEBUGUNALLOC
302   if ( (IsUnDelPtr(a->n) && (a->n >= DEBUGUNALLOC) ))
303     debugunalloc();
304 #endif
305 #ifdef SHOWALLOC
306   printf( "\t%d\tCheckPtr: New Alloc %ld %lx when %ld\n ",a->n, ll, a->p, a->n);
307 #endif
308   MaxUsedSize = AllocSize < MaxUsedSize ? MaxUsedSize :  AllocSize;
309   if( !ll &&  !a->p)
310     {
311         if(verbosity>2) {
312       printf("\t\tCheckPtrMem Full Exit(10) New Alloc %ld %p when %ld\n ", ll, a->p, a->n);
313       printf ("\t\tCheckPtr:Max Memory used %10.3f kbytes " ,  MaxUsedSize/1024. );
314       printf (" Memory undelete %ld \n" , AllocSize);
315         }
316       exitalloc(1);
317     }
318   return (void*) ((char*)a->p);
319 }
320 
MyDeleteOperator(void * pp,bool is_array)321 void AllocExtern::MyDeleteOperator(void * pp,bool is_array)
322 {
323   if(after_end) { /*free(pp)*/; return;}
324   if( !AllocExtern::CheckAlloc) {free(pp); return;}
325 
326   init();
327     for( int i=0; i< nblastalloc; ++i)
328     {
329         if( (NbPtr-i) <0) break;
330         int k =(NbPtr-i)%nblastalloc;
331          if (LastAlloc[k]  && LastAlloc[k]->l>0 && LastAlloc[k]->p ==pp)
332          {
333              AllocExtern::OneAlloc * pa=LastAlloc[k];
334              LastAlloc[k]=0;
335              size_t ll = pa->l-1;
336              for (size_t kkk=0;kkk<ll;kkk++)
337                  ((char *) pp)[kkk]=18;
338 
339              myfree((char*)pp,ll,pa->n);
340 #ifdef SHOWALLOC
341              printf("\t%d\tCheckPtr: fast delete  Alloc %ld %lx when %ld \n",pa->n,pa->l-1,  pa->p, pa->n);
342 #endif
343 
344              AllocSize -= ll;
345              NbPtr--;
346              pa->l=0;
347              pa->p = NextFree;
348              pa->n =0;
349              if (pa->is_array != is_array)
350                  printf("\t\tCheckPtr:  erreur delete [] \n");
351              //if( pa->n < NbAllocShow )		  debugalloc();
352              NextFree = & pa->p;
353              return;
354          }
355     }
356   if (AllocHead)
357     {
358       AllocExtern::AllocData *p = AllocHead;
359         int kloop=0;
360       while (p)
361 	{
362 	  for (int i=0;i<N100;i++)
363 	    if((p->a[i].l > 0) && (p->a[i].p == pp))
364 	      {
365 #ifdef SHOWALLOC
366 		printf("\t%d\tCheckPtr: delete  Alloc %ld %lx when %ld \n",p->a[i].n,p->a[i].l-1,  p->a[i].p, p->a[i].n);
367 #endif
368 		size_t ll = p->a[i].l-1;
369 		for (size_t kkk=0;kkk<ll;kkk++)
370 		  ((char *) pp)[kkk]=18;
371 
372 		myfree((char*)pp,ll,p->a[i].n);
373 
374 		AllocSize -= ll;
375 		NbPtr--;
376 		p->a[i].l=0;
377 		p->a[i].p = NextFree;
378 		p->a[i].n =0;
379 		if (p->a[i].is_array != is_array)
380 		  printf("\t\tCheckPtr:  erreur delete [] \n");
381 		//if( p->a[i].n < NbAllocShow )		  debugalloc();
382 		NextFree = & p->a[i].p;
383 		return;}
384                 if(p == p->next ||  kloop++ > 10000)
385                   {
386                    printf("\n\n ****CheckPTR Big BUG ??????? loop %d\n",kloop);
387                    break;
388                   }
389 
390 	  p = p->next;
391 	}
392       if(pp)
393 	{
394 	  printf( "\t\tCheckPtr: delete of bad pointer %p  -----------\n",pp);
395 	  debugalloc();
396 	}
397 
398     } else
399       myfree((char*)pp);
400 }
init()401 void AllocExtern::init()
402 {
403    static int count=0;
404    if(0== (count++))
405     {
406       sprintf(filename,"ListOfAllocPtr-%d.bin",(int) sizeof(void*));
407       StorageUsage=0;
408       AllocSize =0;
409       MaxUsedSize =0;
410       AllocHead =0;
411       NbAlloc =0;
412       NbPtr =0;
413       NextFree =0;
414       NbuDelPtr =0;
415       NbuDelPtr = 0;
416 
417       after_end = false;
418       for(int i=0; i<nblastalloc; i++)
419           LastAlloc[i]=0;
420       FILE *file=fopen(filename,"rb");
421 
422       if (file)
423 	{
424 	  fread(&NbuDelPtr,sizeof(long),1,file);
425 	  fread(uDelPtr,sizeof(long),NbuDelPtr,file);
426 	  if(NbuDelPtr> 100000000 && NbuDelPtr <0)
427 	    {
428 	      printf("Fatal error in the file %s is wrong (please remove)",filename);
429 	      exit(1);
430 	    }
431 	  fclose(file);
432 	}
433       else
434 	{ // printf("fopen ListOfUnAllocPtr errno = %d\n",errno);
435 	}
436     }
437 }
AllocExtern()438 AllocExtern::AllocExtern()
439 {
440   init();
441 
442 }
443 
~AllocExtern()444 AllocExtern::~AllocExtern()
445 {
446     if(UnShowAlloc==0) return;
447      OneAlloc *  list[Maxundelptr];
448 
449      AllocData * p=AllocHead;
450      int k=0,kk=0;
451      int lln=0;
452 
453      while (p) {int i=N100;
454      while(i--)
455        if (p->a[i].l >0  )
456 	 {
457 	   if ( p->a[i].n >=  p->a[i].n) lln = p->a[i].n;
458 	   if ( p->a[i].n <= NbAllocShow )
459 	     k++;
460 	   else
461 	     if (kk<Maxundelptr)
462 	       list[kk++]=p->a+i;
463 
464 	 }
465       p = p->next;
466      }
467      k+=kk;
468     kk=kk < Maxundelptr ? kk : Maxundelptr;
469     HeapSort(list,kk);
470     if(verbosity > 2)
471     for (int i= kk-10<0 ? 0 : kk-10 ;i<kk;i++)
472       {
473         printf ("\t\tCheckPtr:Undelete pointer  %p size %ld  when %ld\n", list[i]->p,list[i]->l,list[i]->n);
474       }
475     if (kk)
476       {
477 	FILE *file=fopen(filename,"wb");
478 	if (file)
479 	  {
480 	    NbuDelPtr=kk;
481 	    for (int i=0;i<kk;i++)
482 	      uDelPtr[i]=list[i]->n;
483 	    fwrite(&NbuDelPtr,sizeof(long),1,file);
484 	    fwrite(uDelPtr,sizeof(long),NbuDelPtr,file);
485 	    fclose(file);
486 	  }
487       }
488 
489       if(verbosity>2) {
490     if(k)  printf ("\t\tCheckPtr:Nb of undelete pointer is %d last %d\n",k,lln);
491     printf ("\t\tCheckPtr:Max Memory used %10.3f kbytes " ,  MaxUsedSize/1024. );
492     printf (" Memory undelete %ld \n" , AllocSize);
493       }
494 
495     //   clean store pointer
496     p=AllocHead;
497     while (p)
498       {
499 	myfree((char*)p->a);
500 	AllocData * pold = p;
501 	p = p->next;
502 	myfree((char*)pold);
503       }
504     AllocHead=0;
505     after_end=true;
506 }
507 // ------------------
508 
509 
operator new(std::size_t ll,const std::nothrow_t & nothrow_constant)510 void * operator new(std::size_t ll,const std::nothrow_t& nothrow_constant) throw()
511 { void * p =  AllocExternData.MyNewOperator(ll,false);
512  if (ll && !p) { printf("EXIT BECAUSE MEMORY FULL \n");
513  exitalloc(1); };
514  return p;}
515 
operator new[](std::size_t ll,const std::nothrow_t & nothrow_constant)516 void * operator new[](std::size_t ll,const std::nothrow_t& nothrow_constant) throw()
517 { void * p =  AllocExternData.MyNewOperator(ll,true);
518  if (ll && !p) { printf("EXIT BECAUSE MEMORY FULL \n");
519  exitalloc(1); };
520  return p;}
521 
operator new(std::size_t ll)522 void *operator new(std::size_t ll) throw(std::bad_alloc)
523 { void * p =  AllocExternData.MyNewOperator(ll,false);
524     if (ll && !p) { printf("THROW BECAUSE MEMORY FULL \n");
525         std::bad_alloc exception;
526         throw exception; };
527     return p;}
528 
operator new[](std::size_t ll)529 void *operator new[] (std::size_t ll) throw(std::bad_alloc)
530 { void * p =  AllocExternData.MyNewOperator(ll,true);
531     if (ll && !p) {
532         printf("THROW BECAUSE MEMORY FULL \n");
533         std::bad_alloc exception;
534         throw exception; };
535     return p;}
536 
537 
operator delete(void * pp,const std::nothrow_t & nothrow_constant)538 void operator delete(void * pp,const std::nothrow_t& nothrow_constant) throw()
539 {  AllocExternData.MyDeleteOperator(pp,false);}
operator delete[](void * pp,const std::nothrow_t & nothrow_constant)540 void operator delete[] (void * pp,const std::nothrow_t& nothrow_constant) throw()
541 {  AllocExternData.MyDeleteOperator(pp,true);}
operator delete(void * pp)542 void operator delete(void * pp) throw()
543 {  AllocExternData.MyDeleteOperator(pp,false);}
operator delete[](void * pp)544 void operator delete[](void * pp) throw()
545 {  AllocExternData.MyDeleteOperator(pp,true);}
546 
ShowAlloc(const char * s,size_t & lg)547 int AllocExtern::ShowAlloc(const char *s,size_t & lg) {
548     size_t m =StorageUsage;
549     StorageUsage =StorageUsed();
550     if (!NbAllocShow) {NbAllocShow=NbAlloc;}
551     if(verbosity > 2)
552   printf ("----------CheckPtr:-----%s------ NbUndelPtr  %ld  Alloc: %ld  NbPtr %ld  Mem Usage: %zu diff: %ld\n",s,NbPtr,AllocSize,NbAlloc,StorageUsage,(long)(StorageUsage-m));
553   lg = AllocSize;
554   return NbPtr;
555 }
ShowAlloc(const char * s,size_t & lg)556 int ShowAlloc(const char *s,size_t & lg)
557 {  return  AllocExternData.ShowAlloc(s,lg);}
WithoutCheckPtr()558 void WithoutCheckPtr(){AllocExtern::CheckAlloc=false;}
WithCheckPtr()559 void WithCheckPtr(){AllocExtern::CheckAlloc=true;}
560 
561 #else
562 #define XXXX
563 #ifdef XXXX
564 #include <cstdlib>
565 #include <cerrno>
566 #include <cstdio>
567 #include <new>
568 #include <iostream>
569 
570 long  CheckPtr___nbptr=0;
571 size_t CheckPtr___memoryusage =0;
572 
operator new(std::size_t size,const std::nothrow_t & nothrow_constant)573 void * operator new(std::size_t size,const std::nothrow_t& nothrow_constant) throw()
574 {
575     CheckPtr___nbptr++;
576     void *p = malloc( size );
577     if(verbosity > 1000000 )
578         std::cout << " CheckPtr: new nothrow" << CheckPtr___nbptr << " " << size
579                   << " p =" << p <<std::endl;
580 
581     return p;
582 }
583 
operator new[](std::size_t size,const std::nothrow_t & nothrow_constant)584 void * operator new[](std::size_t size,const std::nothrow_t& nothrow_constant) throw()
585 {
586     void *p = malloc(size);
587      CheckPtr___nbptr++;
588     if(verbosity > 1000000 )
589         std::cout << " CheckPtr: new[] nothrow" << CheckPtr___nbptr << " " << size << " p =" << p <<std::endl;
590     return p;
591 }
592 
operator new(std::size_t size)593 void *operator new(std::size_t size) throw(std::bad_alloc)
594 {
595     CheckPtr___nbptr++;
596     void *p = malloc( size );
597     if(verbosity > 1000000 )
598         std::cout << " CheckPtr: new throw " << CheckPtr___nbptr << " " << size
599         << " p =" << p <<std::endl;
600 
601     return p;
602 }
603 
operator new[](std::size_t size)604 void *operator new[](std::size_t size) throw(std::bad_alloc)
605 {
606     void *p = malloc(size);
607     CheckPtr___nbptr++;
608     if(verbosity > 1000000 )
609         std::cout << " CheckPtr: new[]  throw" << CheckPtr___nbptr << " " << size << " p =" << p <<std::endl;
610     return p;
611 }
612 
operator delete(void * p,const std::nothrow_t & nothrow_constant)613 void operator delete(void * p,const std::nothrow_t& nothrow_constant) throw()
614 {
615     if(verbosity > 1000000 )
616         std::cout << " CheckPtr: free nothrow " << CheckPtr___nbptr-1 <<  " p =" << p <<std::endl;
617 
618     free(p);
619 
620     CheckPtr___nbptr--;
621 
622 }
623 
operator delete[](void * p,const std::nothrow_t & nothrow_constant)624 void operator delete[](void * p,const std::nothrow_t& nothrow_constant) throw()
625 {
626     if(verbosity > 1000000 )
627         std::cout << " CheckPtr: free[] thow" << CheckPtr___nbptr-1 <<  " p =" << p <<std::endl;
628     free(p);
629     CheckPtr___nbptr--;
630 
631 }
operator delete(void * p)632 void operator delete(void * p) throw()
633 {
634     if(verbosity > 1000000 )
635         std::cout << " CheckPtr: free throw " << CheckPtr___nbptr-1 <<  " p =" << p <<std::endl;
636 
637     free(p);
638 
639     CheckPtr___nbptr--;
640 
641 }
642 
operator delete[](void * p)643 void operator delete[](void * p) throw()
644 {
645     if(verbosity > 1000000 )
646         std::cout << " CheckPtr: free[] throw " << CheckPtr___nbptr-1 <<  " p =" << p <<std::endl;
647     free(p);
648     CheckPtr___nbptr--;
649 
650 }
651 
ShowAlloc(const char * s,size_t & lg)652 int ShowAlloc(const char *s,size_t & lg)
653 {
654     size_t m =StorageUsed();
655     long diff = m-CheckPtr___memoryusage;
656     if(verbosity > 0 && CheckPtr___memoryusage!=0 && m != CheckPtr___memoryusage)
657         printf("CheckPtr:  Warning memory leak with malloc = %ld \n ",diff);
658     CheckPtr___memoryusage=m;
659     lg = 0; return CheckPtr___nbptr;}
WithoutCheckPtr()660 void WithoutCheckPtr(){}
WithCheckPtr()661 void WithCheckPtr(){}
662 
663 int UnShowAlloc =0;
664 #else
665 #include <cstdlib>
666 
ShowAlloc(const char * s,size_t & lg)667 int ShowAlloc(const char *s,size_t & lg)
668 {lg=0; return 0;}
669 int UnShowAlloc =0;
WithoutCheckPtr()670         void WithoutCheckPtr(){}
WithCheckPtr()671         void WithCheckPtr(){}
672 
673 #endif
674 #endif
675