Panda3D
 All Classes Functions Variables Enumerations
memoryUsagePointerCounts.cxx
1 // Filename: memoryUsagePointerCounts.cxx
2 // Created by: drose (04Jun01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "memoryUsagePointerCounts.h"
16 
17 #ifdef DO_MEMORY_USAGE
18 
19 #include "memoryInfo.h"
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: MemoryUsagePointerCounts::add_info
23 // Access: Public
24 // Description: Adds a pointer definition to the counter.
25 ////////////////////////////////////////////////////////////////////
26 void MemoryUsagePointerCounts::
27 add_info(MemoryInfo *info) {
28  _count++;
29 
30  if (info->is_size_known()) {
31  _size += info->get_size();
32  } else {
33  _unknown_size_count++;
34  }
35 }
36 
37 ////////////////////////////////////////////////////////////////////
38 // Function: MemoryUsagePointerCounts::output
39 // Access: Public
40 // Description:
41 ////////////////////////////////////////////////////////////////////
42 void MemoryUsagePointerCounts::
43 output(ostream &out) const {
44  out << _count << " pointers";
45  if (_unknown_size_count < _count) {
46  out << ", ";
47  output_bytes(out, _size);
48  out << ", avg ";
49  output_bytes(out, _size / (_count - _unknown_size_count));
50  out << " each";
51 
52  if (_unknown_size_count != 0) {
53  out << " (" << _unknown_size_count << " of unknown size)";
54  }
55  }
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: MemoryUsagePointerCounts::output_bytes
60 // Access: Private, Static
61 // Description: Formats a size in bytes in a meaningful and concise
62 // way for output, with units.
63 ////////////////////////////////////////////////////////////////////
64 void MemoryUsagePointerCounts::
65 output_bytes(ostream &out, size_t size) {
66  if (size < 4 * 1024) {
67  out << size << " bytes";
68 
69  } else if (size < 4 * 1024 * 1024) {
70  out << size / 1024 << " Kb";
71 
72  } else {
73  out << size / (1024 * 1024) << " Mb";
74  }
75 }
76 
77 #endif // DO_MEMORY_USAGE
78