changeset 15911:b18b7e560236 classdef

More flexible refcount to account for owned back-references. * libinterp/octave-value/ov-classdef.h (cdef_object_rep::static_count, cdef_object_rep::destroy): New virtual methods. (cdef_object_rep::refcount): Use octave_idx_type as counter type. (cdef_object_rep::release): New method. (cdef_object::~cdef_object, cdef_object::cdef_object (cdef_object)): Use it. (cdef_class::cdef_class_rep::member_count): New member. (cdef_class::cdef_class_rep::cdef_class_rep ()): Initialize it. (cdef_class::cdef_class_rep::static_count, cdef_class::cdef_class_rep::destroy): New methods. (cdef_package::cdef_package_rep::member_count): New member. (cdef_package::cdef_package_rep::cdef_package_rep ()): Initialize it. (cdef_package::cdef_package_rep::static_count, cdef_package::cdef_package_rep::destroy): New methods. * libinterp/octave-value/ov-classdef.cc (octave_classdef_proxy::~octave_classdef_proxy): Remove owned cdef_class from all_classes. (cdef_class::cdef_class_rep::cdef_class_rep (std::list<cdef_class>)): Initialize member_count. (cdef_class::cdef_class_rep::install_method, cdef_class::cdef_class_rep::install_property): Increment member_count. (cdef_package::cdef_package_rep::install_class, cdef_package::cdef_package_rep::install_package): Likewise.
author Michael Goffioul <michael.goffioul@gmail.com>
date Sun, 06 Jan 2013 22:22:13 -0500
parents dfd0dc594c4f
children 8521321604df
files libinterp/octave-value/ov-classdef.cc libinterp/octave-value/ov-classdef.h
diffstat 2 files changed, 72 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-classdef.cc	Sun Jan 06 15:45:28 2013 -0500
+++ b/libinterp/octave-value/ov-classdef.cc	Sun Jan 06 22:22:13 2013 -0500
@@ -814,7 +814,11 @@
   octave_classdef_proxy (const cdef_class& _klass)
     : klass (_klass) { }
 
-  ~octave_classdef_proxy (void) { }
+  ~octave_classdef_proxy (void)
+    {
+      // This means the class has been cleared from the symbol table.
+      all_classes.erase (klass.get_name ());
+    }
 
   octave_value_list
   subsref (const std::string& type,
@@ -1214,7 +1218,8 @@
 }
 
 cdef_class::cdef_class_rep::cdef_class_rep (const std::list<cdef_class>& superclasses)
-     : handle_cdef_object (), handle_class (false), object_count (0)
+     : handle_cdef_object (), member_count (0), handle_class (false),
+       object_count (0)
 {
   put ("SuperClasses", to_ov (superclasses));
   implicit_ctor_list = superclasses;
@@ -1386,6 +1391,8 @@
 {
   method_map[meth.get_name ()] = meth;
 
+  member_count++;
+
   if (meth.is_constructor ())
     {
       // Analyze the constructor code to determine what superclass
@@ -1543,6 +1550,8 @@
 cdef_class::cdef_class_rep::install_property (const cdef_property& prop)
 {
   property_map[prop.get_name ()] = prop;
+
+  member_count++;
 }
 
 Cell
@@ -2372,6 +2381,8 @@
                                                const std::string& nm)
 {
   class_map[nm] = cls;
+
+  member_count++;
 }
 
 void
@@ -2386,6 +2397,8 @@
                                                  const std::string& nm)
 {
   package_map[nm] = pack;
+
+  member_count++;
 }
 
 template<class T1, class T2>
--- a/libinterp/octave-value/ov-classdef.h	Sun Jan 06 15:45:28 2013 -0500
+++ b/libinterp/octave-value/ov-classdef.h	Sun Jan 06 22:22:13 2013 -0500
@@ -129,9 +129,19 @@
       return false;
     }
 
+  virtual octave_idx_type static_count (void) const { return 0; }
+
+  virtual void destroy (void) { delete this; }
+
+  void release (void)
+    {
+      if (--refcount == static_count ())
+        destroy ();
+    }
+
 protected:
   /* reference count */
-  octave_refcount<int> refcount;
+  octave_refcount<octave_idx_type> refcount;
 
 protected:
   /* Restricted copying */
@@ -164,17 +174,13 @@
       : rep (r) { }
 
   virtual ~cdef_object (void)
-    {
-      if (--rep->refcount == 0)
-	delete rep;
-    }
+    { rep->release (); }
 
   cdef_object& operator = (const cdef_object& obj)
     {
       if (rep != obj.rep)
 	{
-	  if (--rep->refcount == 0)
-	    delete rep;
+          rep->release ();
 
 	  rep = obj.rep;
 	  rep->refcount++;
@@ -405,7 +411,8 @@
   {
   public:
     cdef_class_rep (void)
-	: handle_cdef_object (), handle_class (false), object_count (0) { }
+	: handle_cdef_object (), member_count (0), handle_class (false),
+          object_count (0) { }
 
     cdef_class_rep (const std::list<cdef_class>& superclasses);
 
@@ -454,6 +461,23 @@
 
     void unregister_object (void) { object_count--; }
 
+    octave_idx_type static_count (void) const { return member_count; }
+
+    void destroy (void)
+      {
+        if (member_count)
+          {
+            refcount++;
+            cdef_class lock (this);
+
+            member_count = 0;
+            method_map.clear ();
+            property_map.clear ();
+          }
+        else
+          delete this;
+      }
+
   private:
     void load_all_methods (void);
 
@@ -482,6 +506,9 @@
     // The properties defined by this class.
     std::map<std::string,cdef_property> property_map;
 
+    // The number of members in this class (methods, properties...)
+    octave_idx_type member_count;
+
     // TRUE if this class is a handle class. A class is a handle
     // class when the abstract "handle" class is one of its superclasses.
     bool handle_class;
@@ -897,7 +924,7 @@
   cdef_package_rep : public handle_cdef_object
   {
   public:
-    cdef_package_rep (void) : handle_cdef_object () { }
+    cdef_package_rep (void) : handle_cdef_object (), member_count (0) { }
 
     bool is_package (void) const { return true; }
 
@@ -917,11 +944,32 @@
 
     Cell get_packages (void) const;
 
+    octave_idx_type static_count (void) const { return member_count; }
+
+    void destroy (void)
+      {
+        if (member_count)
+          {
+            refcount++;
+            cdef_package lock (this);
+
+            member_count = 0;
+            class_map.clear ();
+            package_map.clear ();
+          }
+        else
+          delete this;
+      }
+
   private:
     std::map<std::string, cdef_class> class_map;
     std::map<std::string, octave_value> function_map;
     std::map<std::string, cdef_package> package_map;
 
+    // The number of registered members in this package (classes, packages).
+    // This only accounts for the members that back-reference to this package.
+    octave_idx_type member_count;
+
     typedef std::map<std::string, cdef_class>::iterator class_iterator;
     typedef std::map<std::string, cdef_class>::const_iterator class_const_iterator;
     typedef std::map<std::string, octave_value>::iterator function_iterator;