tMemManager Class Reference

Collaboration diagram for tMemManager:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 tMemManager (int size, int blocksize)
 tMemManager (int size)
 ~tMemManager ()
void * Alloc (tAllocationInfo const &info)
void complete_Dispose (memblock *m)
void Check ()

Static Public Member Functions

static void * AllocDefault (tAllocationInfo const &info, size_t size)
static void Dispose (tAllocationInfo const &info, void *p, bool keep=false)

Private Member Functions

int Lower (int i)
bool SwapIf (int i, int j)
void SwapDown (int j)
void SwapUp (int i)
void CheckHeap ()
void Insert (memblock *b)
void Remove (memblock *b)
memblockRemove (int i)
void Replace (int i)
void Replace (memblock *e)

Static Private Member Functions

static int UpperL (int i)
static int UpperR (int i)

Private Attributes

int size
int blocksize
tList< memblock, true > blocks
tList< memblock, true > full_blocks
int semaphore

Friends

class memblock


Detailed Description

Definition at line 194 of file tMemManager.cpp.


Constructor & Destructor Documentation

tMemManager::tMemManager ( int  size,
int  blocksize 
)

Definition at line 662 of file tMemManager.cpp.

References blocksize, and semaphore.

00662                                     :size(s),blocksize(s){
00663     // std::cout << sizeof(chunkinfo);
00664     if (blocksize>255)
00665         blocksize=255;
00666     semaphore = 1;
00667 
00668     tBottleNeck neck;
00669 
00670 #ifdef WIN32
00671     if (!inited)
00672         InitializeCriticalSection(&mutex);
00673 #endif
00674 
00675     inited = true;
00676 }

tMemManager::tMemManager ( int  size  ) 

Definition at line 678 of file tMemManager.cpp.

References blocks, blocksize, cs, full_blocks, SAFETYBYTES, semaphore, tArray< T, MALLOC >::SetLen(), and size.

00678                              :size(s){//,blocks(1000),full_blocks(1000){
00679     tBottleNeck neck;
00680 
00681     inited = false;
00682 
00683     blocks.SetLen(0);
00684     full_blocks.SetLen(0);
00685     blocksize=16000/(s+sizeof(chunkinfo)+SAFETYBYTES);
00686     if (blocksize>252)
00687         blocksize=252;
00688     //  std::cout << sizeof(chunkinfo) << ',' << s << ',' << blocksize << '\n';
00689     semaphore = 1;
00690 
00691 #ifdef LEAKFINDER
00692     if (size == 0){
00693         for ( int i = MAXCHECKSUM-1; i>=0; --i )
00694         {
00695             leaks[i] = 0;
00696             counter[i] = 0;
00697 #ifdef PROFILER
00698             checksum_filename[i] = 0;
00699             checksum_classname[i] = 0;
00700             checksum_line[i] = 0;
00701             checksum_blocks[i] = 0;
00702             checksum_bytes[i] = 0;
00703 #endif
00704         }
00705 
00706         std::ifstream l(leakname);
00707         while (l.good() && !l.eof()){
00708             int cs,ln;
00709             l >> cs >> ln;
00710 
00711             if (cs>=0 && cs < MAXCHECKSUM && (ln < leaks[cs] || leaks[cs] == 0))
00712                 leaks[cs]=ln;
00713         }
00714     }
00715     if (!inited){
00716         // delete it
00717         std::ofstream lw(leakname);
00718         lw << "\n\n";
00719     }
00720 #endif
00721 
00722 #ifdef WIN32
00723     if (!inited)
00724         InitializeCriticalSection(&mutex);
00725 #endif
00726 
00727     inited = true;
00728 }

Here is the call graph for this function:

tMemManager::~tMemManager (  ) 

Definition at line 604 of file tMemManager.cpp.

References blocks, blocksize, memblock::destroy(), full_blocks, GrowingArrayBase::Len(), and tMemManBase::Profile().

00604                          {
00605 #ifdef LEAKFINDER
00606     bool warn = true;
00607 #ifdef HAVE_LIBZTHREAD
00608     warn = false;
00609 #endif
00610 
00611     if (inited){
00612         // l???sche das ding
00613         std::ofstream lw(leakname);
00614         lw << "\n\n";
00615         tMemMan::Profile();
00616     }
00617 #endif
00618 
00619 #ifdef WIN32
00620     if (inited)
00621         DeleteCriticalSection(&mutex);
00622 #endif
00623 
00624     inited = false;
00625 
00626     //tASSERT (full_blocks.Len() == 0);
00627     int i;
00628     for (i=blocks.Len()-1;i>=0;i--){
00629         if (blocks(i)->value==blocksize)
00630             memblock::destroy(blocks(i));
00631         else{
00632 #ifdef LEAKFINDER
00633             if ( warn )
00634             {
00635                 std::cout << "Memmanager warning: leaving block untouched.\n";
00636                 warn = false;
00637             }
00638             std::ofstream l(leakname,std::ios::app);
00639             blocks(i)->dumpleaks(l);
00640 #endif
00641         }
00642     }
00643 
00644     for (i=full_blocks.Len()-1;i>=0;i--){
00645         if (full_blocks(i)->value==blocksize)
00646             memblock::destroy(full_blocks(i));
00647         else{
00648 #ifdef LEAKFINDER
00649             if ( warn )
00650             {
00651                 std::cout << "Memmanager warning: leaving block untouched.\n";
00652                 warn = false;
00653             }
00654             std::ofstream l(leakname,std::ios::app);
00655             full_blocks(i)->dumpleaks(l);
00656 #endif
00657         }
00658     }
00659 
00660 }

Here is the call graph for this function:


Member Function Documentation

void * tMemManager::Alloc ( tAllocationInfo const &  info  ) 

Definition at line 860 of file tMemManager.cpp.

References tList< T, MALLOC, REFERENCE >::Add(), memblock::Alloc(), AllocDefault(), blocks, memblock::create(), full_blocks, memblock::hID, Insert(), GrowingArrayBase::Len(), Remove(), semaphore, memblock::size, size, tASSERT, and memblock::value.

Referenced by tMemMan::Alloc().

00860                                                        {
00861     semaphore--;
00862     if (semaphore<0 || size == 0){
00863         semaphore++;
00864         return AllocDefault(info, size);
00865     }
00866 
00867 #ifdef WIN32
00868     EnterCriticalSection(&mutex);
00869 #endif
00870 
00871     if (blocks.Len()==0) // no free space available. create new block...
00872         Insert(memblock::create(this));
00873     //tASSERT(_CrtCheckMemory());
00874 
00875     memblock *block=blocks(0);
00876     tASSERT (block->size == size);
00877     void *mem = block->Alloc( info );
00878     //tASSERT(_CrtCheckMemory());
00879     if (block->value == 0){
00880         Remove(block);
00881         full_blocks.Add(block,block->hID);
00882     }
00883 
00884 #ifdef WIN32
00885     LeaveCriticalSection(&mutex);
00886 #endif
00887 
00888     semaphore++;
00889     return mem;
00890 }

Here is the call graph for this function:

Here is the caller graph for this function:

void * tMemManager::AllocDefault ( tAllocationInfo const &  info,
size_t  size 
) [static]

Definition at line 1078 of file tMemManager.cpp.

References malloc.

Referenced by tMemMan::Alloc(), and Alloc().

01079                                                                      {
01080 #ifdef LEAKFINDER
01081     void *ret=malloc(s+sizeof(chunkinfo));//,classname,fileName,line);
01082     ((chunkinfo *)ret)->checksum=info.checksum;
01083     ((chunkinfo *)ret)->array=info.array;
01084 #ifdef PROFILER
01085     ((chunkinfo *)ret)->realSize=s;
01086     if ( info.checksum >= 0 )
01087     {
01088         if (!counter[info.checksum]){
01089             checksum_filename [info.checksum] = info.filename;
01090             checksum_classname[info.checksum] = info.classname;
01091             checksum_line     [info.checksum] = info.line;
01092         }
01093         checksum_blocks[info.checksum]++;
01094         checksum_bytes[info.checksum]+=s;
01095     }
01096 #endif
01097 #else
01098     void *ret=malloc(s+sizeof(chunkinfo));
01099 #endif
01100     ((chunkinfo *)ret)->size_in_dwords=0;
01101     ((chunkinfo *)ret)->occupied=true;
01102     ret = ((chunkinfo *)ret)+1;
01103     return ret;

Here is the caller graph for this function:

void tMemManager::Dispose ( tAllocationInfo const &  info,
void *  p,
bool  keep = false 
) [static]

Definition at line 1059 of file tMemManager.cpp.

References complete_Dispose(), memblock::Dispose(), and size.

01059                                                                          {
01060     int size;
01061 
01062     if (!p)
01063         return;
01064 
01065     memblock *block = memblock::Dispose(p,size,info, keep);
01066 #ifndef DOUBLEFREEFINDER
01067     if (inited && block){
01068         tBottleNeck neck;
01069         memman[size >> 2].complete_Dispose(block);
01070 #ifdef WIN32
01071         LeaveCriticalSection(&mutex);
01072 #endif
01073     }
01074 #else
01075     block = 0;
01076 #endif

Here is the call graph for this function:

void tMemManager::complete_Dispose ( memblock m  ) 

Definition at line 892 of file tMemManager.cpp.

References memblock::Check(), full_blocks, memblock::hID, Insert(), tList< T, MALLOC, REFERENCE >::Remove(), Replace(), semaphore, and memblock::value.

Referenced by Dispose().

00892                                                  {
00893 #ifdef MEM_DEB_SMALL
00894     block->Check();
00895 #endif
00896 
00897     semaphore--;
00898     if (semaphore>=0){
00899         if (block->value == 1){
00900             full_blocks.Remove(block,block->hID);
00901             Insert(block);
00902         }
00903         else{
00904             Replace(block);
00905         }
00906     }
00907     //else
00908     // tASSERT(0);
00909     semaphore++;
00910 
00911 #ifdef MEM_DEB_SMALL
00912     block->Check();
00913 #endif
00914 }

Here is the call graph for this function:

Here is the caller graph for this function:

void tMemManager::Check (  ) 

Definition at line 916 of file tMemManager.cpp.

References blocks, CheckHeap(), full_blocks, GrowingArrayBase::Len(), size, and tASSERT.

00916                        {
00917 #ifdef DEBUG_EXPENSIVE
00918     if ( !inited )
00919         return;
00920 
00921     CheckHeap();
00922     int i;
00923     for (i=blocks.Len()-1;i>=0;i--)
00924     {
00925         blocks(i)->Check();
00926         tASSERT (blocks(i)->myman == this);
00927         tASSERT (blocks(i)->size == size);
00928     }
00929     for (i=full_blocks.Len()-1;i>=0;i--)
00930     {
00931         if ( full_blocks(i) )
00932         {
00933             full_blocks(i)->Check();
00934             tASSERT (full_blocks(i)->myman == this);
00935             tASSERT (full_blocks(i)->size == size);
00936         }
00937     }
00938 #endif
00939 }

Here is the call graph for this function:

int tMemManager::Lower ( int  i  )  [inline, private]

Definition at line 208 of file tMemManager.cpp.

Referenced by CheckHeap(), Replace(), and SwapDown().

00208                      { // the element below i in the heap
00209         if(i%2==0)  // just to make sure what happens; you never know what 1/2 is..
00210             return i/2-1;
00211         else
00212             return (i-1)/2;
00213     }

Here is the caller graph for this function:

bool tMemManager::SwapIf ( int  i,
int  j 
) [private]

Definition at line 589 of file tMemManager.cpp.

References blocks, memblock::hID, Swap(), and memblock::value.

Referenced by SwapDown(), and SwapUp().

00589                                    {
00590     if (i==j || i<0) return false; // safety
00591 
00592     memblock *e1=blocks(i),*e2=blocks(j);
00593     if (e1->value > e2->value){
00594         Swap(blocks(i),blocks(j));
00595         e1->hID=j;
00596         e2->hID=i;
00597         return true;
00598     }
00599     else
00600         return false;
00601 
00602 }

Here is the call graph for this function:

Here is the caller graph for this function:

void tMemManager::SwapDown ( int  j  )  [private]

Definition at line 744 of file tMemManager.cpp.

References CheckHeap(), Lower(), and SwapIf().

Referenced by Insert(), and Replace().

00744                                {
00745     int i=j;
00746     // e is now at position i. swap it down
00747     // as far as it goes:
00748     do{
00749         j=i;
00750         i=Lower(j);
00751     }
00752     while(SwapIf(i,j)); // mean: relies on the fact that SwapIf returns -1
00753     // if i<0.
00754 
00755 #ifdef MEM_DEB
00756     CheckHeap();
00757 #endif
00758 }

Here is the call graph for this function:

Here is the caller graph for this function:

void tMemManager::SwapUp ( int  i  )  [private]

Definition at line 760 of file tMemManager.cpp.

References blocks, CheckHeap(), GrowingArrayBase::Len(), SwapIf(), UpperL(), and UpperR().

Referenced by Replace().

00760                              {
00761 #ifdef MEM_DEB
00762     //  static int su=0;
00763     //if (su%100 ==0 )
00764     // std::cout << "su=" << su << '\n';
00765     //  if (su > 11594 )
00766     // con << "su=" << su << '\n';
00767     // su ++;
00768 #endif
00769 
00770     int ul,ur;
00771     bool goon=1;
00772     while(goon && UpperL(i)<blocks.Len()){
00773         ul=UpperL(i);
00774         ur=UpperR(i);
00775         if(ur>=blocks.Len() ||
00776                 blocks(ul)->value < blocks(ur)->value){
00777             goon=SwapIf(i,ul);
00778             i=ul;
00779         }
00780         else{
00781             goon=SwapIf(i,ur);
00782             i=ur;
00783         }
00784     }
00785 
00786 #ifdef MEM_DEB
00787     CheckHeap();
00788 #endif
00789 
00790 }

Here is the call graph for this function:

Here is the caller graph for this function:

void tMemManager::CheckHeap (  )  [private]

Definition at line 731 of file tMemManager.cpp.

References blocks, GrowingArrayBase::Len(), Lower(), tERR_ERROR_INT, UpperL(), UpperR(), and memblock::value.

Referenced by Check(), Insert(), Remove(), Replace(), SwapDown(), and SwapUp().

00731                            {
00732     for(int i=blocks.Len()-1;i>0;i--){
00733         memblock *current=blocks(i);
00734         memblock *low=blocks(Lower(i));
00735         if (Lower(UpperL(i))!=i || Lower(UpperR(i))!=i)
00736             tERR_ERROR_INT("Error in lower/upper " << i << "!");
00737 
00738         if (low->value>current->value)
00739             tERR_ERROR_INT("Heap structure corrupt!");
00740     }
00741 }

Here is the call graph for this function:

Here is the caller graph for this function:

static int tMemManager::UpperL ( int  i  )  [inline, static, private]

Definition at line 226 of file tMemManager.cpp.

Referenced by CheckHeap(), and SwapUp().

00226 {return 2*i+1;} // the elements left and

Here is the caller graph for this function:

static int tMemManager::UpperR ( int  i  )  [inline, static, private]

Definition at line 227 of file tMemManager.cpp.

Referenced by CheckHeap(), and SwapUp().

00227 {return 2*i+2;} // right above i

Here is the caller graph for this function:

void tMemManager::Insert ( memblock b  )  [private]

Definition at line 792 of file tMemManager.cpp.

References tList< T, MALLOC, REFERENCE >::Add(), blocks, CheckHeap(), memblock::hID, GrowingArrayBase::Len(), and SwapDown().

Referenced by Alloc(), and complete_Dispose().

00792                                    {
00793     blocks.Add(e,e->hID); // relies on the implementation of List: e is
00794     // put to the back of the heap.
00795     SwapDown(blocks.Len()-1); // bring it to the right place
00796 
00797 #ifdef MEM_DEB
00798     CheckHeap();
00799 #endif
00800 }

Here is the call graph for this function:

Here is the caller graph for this function:

void tMemManager::Remove ( memblock b  )  [private]

Definition at line 802 of file tMemManager.cpp.

References CheckHeap(), memblock::hID, and tERR_ERROR_INT.

Referenced by Alloc().

00802                                    {
00803     int i=e->hID;
00804 
00805     if(i<0 || this != e->myman)
00806         tERR_ERROR_INT("Element is not in this heap!");
00807 
00808     Remove(i);
00809 
00810 #ifdef MEM_DEB
00811     CheckHeap();
00812 #endif
00813 }

Here is the call graph for this function:

Here is the caller graph for this function:

memblock * tMemManager::Remove ( int  i  )  [private]

Definition at line 841 of file tMemManager.cpp.

References blocks, CheckHeap(), memblock::hID, GrowingArrayBase::Len(), NULL, tList< T, MALLOC, REFERENCE >::Remove(), and Replace().

00841                                    {
00842     if (i>=0 && i<blocks.Len()){
00843         memblock *ret=blocks(i);
00844 
00845         blocks.Remove(ret,ret->hID);
00846 
00847         // now we have an misplaced element at pos i. (if i was not at the end..)
00848         if (i<blocks.Len())
00849             Replace(i);
00850 
00851 #ifdef MEM_DEB
00852         CheckHeap();
00853 #endif
00854 
00855         return ret;
00856     }
00857     return NULL;
00858 }

Here is the call graph for this function:

void tMemManager::Replace ( int  i  )  [private]

Definition at line 815 of file tMemManager.cpp.

References blocks, CheckHeap(), GrowingArrayBase::Len(), Lower(), SwapDown(), and SwapUp().

Referenced by complete_Dispose(), Remove(), and Replace().

00815                               {
00816     if (i>=0 && i < blocks.Len()){
00817         if (i==0 || blocks(i)->value > blocks(Lower(i))->value)
00818             SwapUp(i);          // put it up where it belongs
00819         else
00820             SwapDown(i);
00821 
00822 #ifdef MEM_DEB
00823         CheckHeap();
00824 #endif
00825     }
00826 }

Here is the call graph for this function:

Here is the caller graph for this function:

void tMemManager::Replace ( memblock e  )  [private]

Definition at line 828 of file tMemManager.cpp.

References CheckHeap(), memblock::hID, Replace(), and tERR_ERROR_INT.

00828                                     {
00829     int i=e->hID;
00830 
00831     if(i<0 || this != e->myman)
00832         tERR_ERROR_INT("Element is not in this heap!");
00833 
00834     Replace(i);
00835 
00836 #ifdef MEM_DEB
00837     CheckHeap();
00838 #endif
00839 }

Here is the call graph for this function:


Friends And Related Function Documentation

friend class memblock [friend]

Definition at line 195 of file tMemManager.cpp.


Member Data Documentation

int tMemManager::size [private]

Definition at line 237 of file tMemManager.cpp.

Referenced by Alloc(), Check(), memblock::Check(), memblock::create(), Dispose(), memblock::memblock(), and tMemManager().

int tMemManager::blocksize [private]

Definition at line 238 of file tMemManager.cpp.

Referenced by memblock::Check(), memblock::create(), memblock::Dispose(), memblock::memblock(), tMemManager(), and ~tMemManager().

tList<memblock, true> tMemManager::blocks [private]

Definition at line 240 of file tMemManager.cpp.

Referenced by Alloc(), Check(), CheckHeap(), Insert(), memblock::memblock(), Remove(), Replace(), SwapIf(), SwapUp(), tMemManager(), memblock::~memblock(), and ~tMemManager().

tList<memblock, true> tMemManager::full_blocks [private]

Definition at line 241 of file tMemManager.cpp.

Referenced by Alloc(), Check(), complete_Dispose(), tMemManager(), and ~tMemManager().

int tMemManager::semaphore [private]

Definition at line 243 of file tMemManager.cpp.

Referenced by Alloc(), complete_Dispose(), and tMemManager().


The documentation for this class was generated from the following file:
Generated on Sat Mar 15 23:57:38 2008 for Armagetron Advanced by  doxygen 1.5.4