Panda3D

memoryUsagePointers.h

00001 // Filename: memoryUsagePointers.h
00002 // Created by:  drose (25May00)
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 #ifndef MEMORYUSAGEPOINTERS_H
00016 #define MEMORYUSAGEPOINTERS_H
00017 
00018 #include "pandabase.h"
00019 
00020 #ifdef DO_MEMORY_USAGE
00021 
00022 #include "typedObject.h"
00023 #include "pointerTo.h"
00024 #include "referenceCount.h"
00025 #include "pvector.h"
00026 
00027 #ifdef HAVE_PYTHON
00028 
00029 #undef _POSIX_C_SOURCE
00030 #include <Python.h>
00031 
00032 #endif  // HAVE_PYTHON
00033 
00034 ////////////////////////////////////////////////////////////////////
00035 //       Class : MemoryUsagePointers
00036 // Description : This is a list of pointers returned by a MemoryUsage
00037 //               object in response to some query.
00038 //
00039 //               Warning: once pointers are stored in a
00040 //               MemoryUsagePointers object, they are
00041 //               reference-counted, and will not be freed until the
00042 //               MemoryUsagePointers object is freed (or clear() is
00043 //               called on the object).  However, they may not even be
00044 //               freed then; pointers may leak once they have been
00045 //               added to this structure.  This is because we don't
00046 //               store enough information in this structure to
00047 //               correctly free the pointers that have been added.
00048 //               Since this is intended primarily as a debugging tool,
00049 //               this is not a major issue.
00050 //
00051 //               This class is just a user interface to talk about
00052 //               pointers stored in a MemoryUsage object.  It doesn't
00053 //               even exist when compiled with NDEBUG.
00054 ////////////////////////////////////////////////////////////////////
00055 class EXPCL_PANDAEXPRESS MemoryUsagePointers {
00056 PUBLISHED:
00057   MemoryUsagePointers();
00058   ~MemoryUsagePointers();
00059 
00060   int get_num_pointers() const;
00061   ReferenceCount *get_pointer(int n) const;
00062   MAKE_SEQ(get_pointers, get_num_pointers, get_pointer);
00063   TypedObject *get_typed_pointer(int n) const;
00064   MAKE_SEQ(get_typed_pointers, get_num_pointers, get_typed_pointer);
00065 
00066   TypeHandle get_type(int n) const;
00067   string get_type_name(int n) const;
00068   double get_age(int n) const;
00069 
00070 #ifdef HAVE_PYTHON
00071   PyObject *get_python_pointer(int n) const;
00072 #endif
00073 
00074   void clear();
00075 
00076   void output(ostream &out) const;
00077 
00078 private:
00079   void add_entry(ReferenceCount *ref_ptr, TypedObject *typed_ptr,
00080                  TypeHandle type, double age);
00081 
00082   class Entry {
00083   public:
00084     INLINE Entry(ReferenceCount *ref_ptr, TypedObject *typed_ptr,
00085                  TypeHandle type, double age);
00086     INLINE Entry(const Entry &copy);
00087     INLINE void operator = (const Entry &copy);
00088     INLINE ~Entry();
00089 
00090     // We have an ordinary pointer to a type ReferenceCount, and not a
00091     // PT(ReferenceCount), because we can't actually delete this thing
00092     // (since ReferenceCount has no public destructor).  If we can't
00093     // delete it, we can't make a PointerTo it, since PointerTo wants
00094     // to be able to delete things.
00095     ReferenceCount *_ref_ptr;
00096     TypedObject *_typed_ptr;
00097     TypeHandle _type;
00098     double _age;
00099   };
00100 
00101   typedef pvector<Entry> Entries;
00102   Entries _entries;
00103   friend class MemoryUsage;
00104 };
00105 
00106 INLINE ostream &operator << (ostream &out, const MemoryUsagePointers &mup) {
00107   mup.output(out);
00108   return out;
00109 }
00110 
00111 #include "memoryUsagePointers.I"
00112 
00113 #endif  // MEMORY_USAGE_POINTERS
00114 
00115 #endif
00116 
 All Classes Functions Variables Enumerations