~armagetronad-dev/armagetronad/trunk-armagetronad-fortress_ai

« back to all changes in this revision

Viewing changes to src/tools/tMemManager.cpp

  • Committer: Manuel Moos
  • Date: 2010-08-07 21:08:56 UTC
  • mfrom: (890.1.89 armagetronad)
  • Revision ID: z-man@users.sf.net-20100807210856-93ljrijn34c77p5v
MergingĀ fromĀ mainline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
106
106
static tMockMutex st_mutex;
107
107
#endif
108
108
 
 
109
// create an object of this class while calling external functions
 
110
// that are known to have (harmless!) leaks
 
111
static int st_knownExternalLeak = 0;
 
112
 
 
113
tKnownExternalLeak::tKnownExternalLeak()
 
114
{
 
115
    st_knownExternalLeak++;
 
116
}
 
117
 
 
118
tKnownExternalLeak::~tKnownExternalLeak()
 
119
{
 
120
    st_knownExternalLeak--;
 
121
}
 
122
 
109
123
class tBottleNeck
110
124
{
111
125
public:
171
185
    int checksum;
172
186
    int line;
173
187
    bool array;
 
188
    bool report;
174
189
#endif
175
190
 
176
191
    tAllocationInfo( bool 
177
192
#ifdef LEAKFINDER
178
193
                     array_ 
179
194
#endif
 
195
                     , bool 
 
196
#ifdef LEAKFINDER
 
197
                     alwaysReport 
 
198
#endif
 
199
                     = false
180
200
        )
181
201
#ifdef LEAKFINDER
182
 
    : filename( "XXX" ), classname( "XXX" ), checksum(-1), line(0), array( array_ )
 
202
    : filename( "XXX" ), classname( "XXX" ), checksum(-1), line(0), array( array_ ), report(alwaysReport || st_knownExternalLeak==0)
183
203
#endif
184
204
    {
185
205
    }
289
309
#ifdef LEAKFINDER
290
310
    int checksum;
291
311
    int counter;
292
 
    bool array;
 
312
    bool array:1;
 
313
    bool report:1;
293
314
#endif
294
315
#ifdef PROFILER
295
316
    size_t realSize;
421
442
            chunk(ret).realSize = size;
422
443
#endif
423
444
            chunk(ret).array=info.array;
 
445
            chunk(ret).report=info.report;
424
446
            chunk(ret).checksum=info.checksum;
425
447
            chunk(ret).counter =++counter[info.checksum];
426
448
 
455
477
        chunk(ret).realSize = size;
456
478
#endif
457
479
        chunk(ret).array=info.array;
 
480
        chunk(ret).report=info.report;
458
481
        chunk(ret).checksum=info.checksum;
459
482
#endif
460
483
 
577
600
    }
578
601
 
579
602
#ifdef LEAKFINDER
580
 
    void dumpleaks(std::ostream &s){
 
603
    bool dumpleaks(std::ostream &s){
 
604
        bool ret = false;
581
605
        for (int i=myman->blocksize-1;i>=0;i--)
582
 
            if (chunk(i).occupied && chunk(i).checksum >= 0 ){
 
606
        {
 
607
            if (chunk(i).occupied && chunk(i).report && chunk(i).checksum >= 0 )
 
608
            {
583
609
                s << chunk(i).checksum << " " << chunk(i).counter << '\n';
584
610
                leak();
 
611
                ret = true;
585
612
            }
 
613
        }
 
614
        return ret;
586
615
    }
587
616
#endif
588
617
};
614
643
 
615
644
tMemManager::~tMemManager(){
616
645
#ifdef LEAKFINDER
617
 
    bool warn = true;
 
646
    static bool warn = true;
618
647
#ifdef HAVE_LIBZTHREAD
619
648
    warn = false;
620
649
#endif
641
670
            memblock::destroy(blocks(i));
642
671
        else{
643
672
#ifdef LEAKFINDER
644
 
            if ( warn )
 
673
            std::ofstream l(leakname,std::ios::app);
 
674
            bool report = blocks(i)->dumpleaks(l);
 
675
            if ( report && warn )
645
676
            {
646
677
                std::cout << "Memmanager warning: leaving block untouched.\n";
647
678
                warn = false;
648
679
            }
649
 
            std::ofstream l(leakname,std::ios::app);
650
 
            blocks(i)->dumpleaks(l);
651
680
#endif
652
681
        }
653
682
    }
657
686
            memblock::destroy(full_blocks(i));
658
687
        else{
659
688
#ifdef LEAKFINDER
660
 
            if ( warn )
 
689
            std::ofstream l(leakname,std::ios::app);
 
690
            bool report = full_blocks(i)->dumpleaks(l);
 
691
            if ( report && warn )
661
692
            {
662
693
                std::cout << "Memmanager warning: leaving block untouched.\n";
663
694
                warn = false;
664
695
            }
665
 
            std::ofstream l(leakname,std::ios::app);
666
 
            full_blocks(i)->dumpleaks(l);
667
696
#endif
668
697
        }
669
698
    }
1092
1121
    void *ret=malloc(s+sizeof(chunkinfo));//,classname,fileName,line);
1093
1122
    ((chunkinfo *)ret)->checksum=info.checksum;
1094
1123
    ((chunkinfo *)ret)->array=info.array;
 
1124
    ((chunkinfo *)ret)->report=info.report;
1095
1125
#ifdef PROFILER
1096
1126
    ((chunkinfo *)ret)->realSize=s;
1097
1127
    if ( info.checksum >= 0 )
1305
1335
 
1306
1336
 
1307
1337
void* operator new      (size_t size,const char *classn,const char *file,int l) THROW_BADALLOC{
1308
 
    tAllocationInfo info( false );
 
1338
    tAllocationInfo info( false, true );
1309
1339
#ifdef LEAKFINDER
1310
1340
    info.filename=file;
1311
1341
    info.classname=classn;
1339
1369
}
1340
1370
 
1341
1371
void  operator delete   (void *ptr,const char *classname,const char *file,int line)  THROW_NOTHING{
1342
 
    tAllocationInfo info( false );
 
1372
    tAllocationInfo info( false, true );
1343
1373
 
1344
1374
    if (ptr) tMemMan::Dispose(info, ptr);
1345
1375
}
1364
1394
 
1365
1395
 
1366
1396
void* operator new[]    (size_t size,const char *classn,const char *file,int l)  THROW_BADALLOC{
1367
 
    tAllocationInfo info( true );
 
1397
    tAllocationInfo info( true, true );
1368
1398
#ifdef LEAKFINDER
1369
1399
    info.filename=file;
1370
1400
    info.classname=classn;
1398
1428
}
1399
1429
 
1400
1430
void  operator delete[]   (void *ptr,const char *classname,const char *file,int line)   THROW_NOTHING{
1401
 
    tAllocationInfo info( true );
 
1431
    tAllocationInfo info( true, true );
1402
1432
 
1403
1433
    if (ptr) tMemMan::Dispose(info, ptr);
1404
1434
}