reSIProcate/rutil  9694
HeapInstanceCounter.cxx
Go to the documentation of this file.
00001 #include "rutil/HeapInstanceCounter.hxx"
00002 #include "rutil/Mutex.hxx"
00003 #include "rutil/Logger.hxx"
00004 #include "rutil/Data.hxx"
00005 
00006 #include <assert.h>
00007 #include <map>
00008 
00009 using namespace std;
00010 using namespace resip;
00011 
00012 #define RESIPROCATE_SUBSYSTEM resip::Subsystem::STATS
00013 
00014 typedef map<void *, unsigned long> AllocationSizeMap;
00015 
00016 //namespace   //  unnamed namespace
00017 //{
00018 struct InstanceCounts
00019 {
00020       InstanceCounts()
00021          : total(0),
00022            outstanding(0),
00023            totalBytesOutstanding(0)
00024       {}
00025 
00026       size_t total;
00027       size_t outstanding;
00028       size_t totalBytesOutstanding;
00029       AllocationSizeMap allocationSizes;
00030 };
00031 
00032 // .dlb. should be using comparitor on typeinfo
00033 typedef map<Data, InstanceCounts> AllocationMap;
00034 Mutex allocationMutex;
00035 AllocationMap allocationMap;
00036 //}
00037 
00038 #ifdef RESIP_HEAP_COUNT
00039 void
00040 HeapInstanceCounter::dump()
00041 {
00042    Lock l(allocationMutex);
00043    if (allocationMap.empty())
00044    {
00045       WarningLog(<< "No allocations.");
00046    }
00047    else
00048    {
00049       AllocationMap::const_iterator i(allocationMap.begin());
00050       AllocationMap::const_iterator iEnd(allocationMap.end());
00051       for (; i != iEnd; ++i)
00052       {
00053          if (i->second.total)
00054          {
00055             //abi::__cxa_demangle(typeid(obj).name(), 0, 0, &status);
00056             WarningLog(<< i->first << " " << i->second.total << " > " << i->second.outstanding << ", " << 
00057                 i->second.totalBytesOutstanding);
00058          }
00059       }
00060    }
00061 }
00062 
00063 void* 
00064 HeapInstanceCounter::allocate(size_t bytes, 
00065                               const type_info& ti)
00066 {
00067    void* addr = ::operator new(bytes);
00068 
00069    { //lock scope
00070       // WarningLog(<< "allocated " << ti.name());
00071       Lock l(allocationMutex);
00072 
00073       const Data name(Data::Share, ti.name(), strlen(ti.name()));
00074       InstanceCounts &counts = allocationMap[name];
00075       
00076       counts.total += 1;
00077       counts.outstanding += 1;
00078       counts.totalBytesOutstanding += bytes;
00079       counts.allocationSizes[addr] = bytes;       
00080    }
00081    return addr;
00082 }
00083 
00084 void
00085 HeapInstanceCounter::deallocate(void* addr, 
00086                                 const type_info& ti)
00087 {
00088    {//lock scope
00089       // WarningLog(<< "deallocated " << ti.name());
00090       Lock l(allocationMutex);
00091       const Data name(Data::Share, ti.name(), strlen(ti.name()));
00092       if (allocationMap.count(name) != 0)
00093       {
00094          InstanceCounts &counts = allocationMap[name];
00095 
00096          if (counts.allocationSizes.count(addr) != 0)
00097          {
00098             counts.outstanding -= 1;
00099             counts.totalBytesOutstanding -= counts.allocationSizes[addr];
00100             counts.allocationSizes.erase(addr);
00101          }
00102       }
00103    }
00104    ::operator delete(addr);
00105 }
00106 
00107 #else // RESIP_HEAP_COUNT
00108 void
00109 HeapInstanceCounter::dump()
00110 {}
00111 
00112 #endif // RESIP_HEAP_COUNT
00113 
00114 /* ====================================================================
00115  * The Vovida Software License, Version 1.0 
00116  * 
00117  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00118  * 
00119  * Redistribution and use in source and binary forms, with or without
00120  * modification, are permitted provided that the following conditions
00121  * are met:
00122  * 
00123  * 1. Redistributions of source code must retain the above copyright
00124  *    notice, this list of conditions and the following disclaimer.
00125  * 
00126  * 2. Redistributions in binary form must reproduce the above copyright
00127  *    notice, this list of conditions and the following disclaimer in
00128  *    the documentation and/or other materials provided with the
00129  *    distribution.
00130  * 
00131  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00132  *    and "Vovida Open Communication Application Library (VOCAL)" must
00133  *    not be used to endorse or promote products derived from this
00134  *    software without prior written permission. For written
00135  *    permission, please contact vocal@vovida.org.
00136  *
00137  * 4. Products derived from this software may not be called "VOCAL", nor
00138  *    may "VOCAL" appear in their name, without prior written
00139  *    permission of Vovida Networks, Inc.
00140  * 
00141  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00142  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00143  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00144  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00145  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00146  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00147  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00148  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00149  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00150  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00151  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00152  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00153  * DAMAGE.
00154  * 
00155  * ====================================================================
00156  * 
00157  * This software consists of voluntary contributions made by Vovida
00158  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00159  * Inc.  For more information on Vovida Networks, Inc., please see
00160  * <http://www.vovida.org/>.
00161  *
00162  */