Panda3D
memoryUsagePointers.h
1 // Filename: memoryUsagePointers.h
2 // Created by: drose (25May00)
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 #ifndef MEMORYUSAGEPOINTERS_H
16 #define MEMORYUSAGEPOINTERS_H
17 
18 #include "pandabase.h"
19 
20 #ifdef DO_MEMORY_USAGE
21 
22 #include "typedObject.h"
23 #include "pointerTo.h"
24 #include "referenceCount.h"
25 #include "pvector.h"
26 
27 #ifdef HAVE_PYTHON
28 
29 #undef _POSIX_C_SOURCE
30 #include <Python.h>
31 
32 #endif // HAVE_PYTHON
33 
34 ////////////////////////////////////////////////////////////////////
35 // Class : MemoryUsagePointers
36 // Description : This is a list of pointers returned by a MemoryUsage
37 // object in response to some query.
38 //
39 // Warning: once pointers are stored in a
40 // MemoryUsagePointers object, they are
41 // reference-counted, and will not be freed until the
42 // MemoryUsagePointers object is freed (or clear() is
43 // called on the object). However, they may not even be
44 // freed then; pointers may leak once they have been
45 // added to this structure. This is because we don't
46 // store enough information in this structure to
47 // correctly free the pointers that have been added.
48 // Since this is intended primarily as a debugging tool,
49 // this is not a major issue.
50 //
51 // This class is just a user interface to talk about
52 // pointers stored in a MemoryUsage object. It doesn't
53 // even exist when compiled with NDEBUG.
54 ////////////////////////////////////////////////////////////////////
55 class EXPCL_PANDAEXPRESS MemoryUsagePointers {
56 PUBLISHED:
57  MemoryUsagePointers();
58  ~MemoryUsagePointers();
59 
60  int get_num_pointers() const;
61  ReferenceCount *get_pointer(int n) const;
62  MAKE_SEQ(get_pointers, get_num_pointers, get_pointer);
63  TypedObject *get_typed_pointer(int n) const;
64  MAKE_SEQ(get_typed_pointers, get_num_pointers, get_typed_pointer);
65 
66  TypeHandle get_type(int n) const;
67  string get_type_name(int n) const;
68  double get_age(int n) const;
69 
70  EXTENSION(PyObject *get_python_pointer(int n) const);
71 
72  void clear();
73 
74  void output(ostream &out) const;
75 
76 private:
77  void add_entry(ReferenceCount *ref_ptr, TypedObject *typed_ptr,
78  TypeHandle type, double age);
79 
80  class Entry {
81  public:
82  INLINE Entry(ReferenceCount *ref_ptr, TypedObject *typed_ptr,
83  TypeHandle type, double age);
84  INLINE Entry(const Entry &copy);
85  INLINE void operator = (const Entry &copy);
86  INLINE ~Entry();
87 
88  // We have an ordinary pointer to a type ReferenceCount, and not a
89  // PT(ReferenceCount), because we can't actually delete this thing
90  // (since ReferenceCount has no public destructor). If we can't
91  // delete it, we can't make a PointerTo it, since PointerTo wants
92  // to be able to delete things.
93  ReferenceCount *_ref_ptr;
94  TypedObject *_typed_ptr;
95  TypeHandle _type;
96  double _age;
97  };
98 
99  typedef pvector<Entry> Entries;
100  Entries _entries;
101  friend class MemoryUsage;
102 };
103 
104 INLINE ostream &operator << (ostream &out, const MemoryUsagePointers &mup) {
105  mup.output(out);
106  return out;
107 }
108 
109 #include "memoryUsagePointers.I"
110 
111 #endif // MEMORY_USAGE_POINTERS
112 
113 #endif
114 
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
A base class for all things that want to be reference-counted.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85