Panda3D

memoryUsagePointerCounts.cxx

00001 // Filename: memoryUsagePointerCounts.cxx
00002 // Created by:  drose (04Jun01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "memoryUsagePointerCounts.h"
00016 
00017 #ifdef DO_MEMORY_USAGE
00018 
00019 #include "memoryInfo.h"
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //     Function: MemoryUsagePointerCounts::add_info
00023 //       Access: Public
00024 //  Description: Adds a pointer definition to the counter.
00025 ////////////////////////////////////////////////////////////////////
00026 void MemoryUsagePointerCounts::
00027 add_info(MemoryInfo *info) {
00028   _count++;
00029 
00030   if (info->is_size_known()) {
00031     _size += info->get_size();
00032   } else {
00033     _unknown_size_count++;
00034   }
00035 }
00036 
00037 ////////////////////////////////////////////////////////////////////
00038 //     Function: MemoryUsagePointerCounts::output
00039 //       Access: Public
00040 //  Description: 
00041 ////////////////////////////////////////////////////////////////////
00042 void MemoryUsagePointerCounts::
00043 output(ostream &out) const {
00044   out << _count << " pointers";
00045   if (_unknown_size_count < _count) {
00046     out << ", ";
00047     output_bytes(out, _size);
00048     out << ", avg ";
00049     output_bytes(out, _size / (_count - _unknown_size_count));
00050     out << " each";
00051       
00052     if (_unknown_size_count != 0) {
00053       out << " (" << _unknown_size_count << " of unknown size)";
00054     }
00055   }
00056 }
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //     Function: MemoryUsagePointerCounts::output_bytes
00060 //       Access: Private, Static
00061 //  Description: Formats a size in bytes in a meaningful and concise
00062 //               way for output, with units.
00063 ////////////////////////////////////////////////////////////////////
00064 void MemoryUsagePointerCounts::
00065 output_bytes(ostream &out, size_t size) {
00066   if (size < 4 * 1024) {
00067     out << size << " bytes";
00068     
00069   } else if (size < 4 * 1024 * 1024) {
00070     out << size / 1024 << " Kb";
00071     
00072   } else {
00073     out << size / (1024 * 1024) << " Mb";
00074   }
00075 }
00076 
00077 #endif  // DO_MEMORY_USAGE
00078 
 All Classes Functions Variables Enumerations