You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2009/12/29 08:03:16 UTC

svn commit: r894319 - in /lucene/lucy/trunk: clownfish/lib/Clownfish/Binding/Core/Class.pm core/Lucy/Object/Obj.c core/Lucy/Object/VTable.bp core/Lucy/Object/VTable.c perl/xs/Lucy/Object/VTable.c

Author: marvin
Date: Tue Dec 29 07:03:16 2009
New Revision: 894319

URL: http://svn.apache.org/viewvc?rev=894319&view=rev
Log:
Commit LUCY-90, eliminating VTable refcounting and making them (mostly)
stateless.

Modified:
    lucene/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm
    lucene/lucy/trunk/core/Lucy/Object/Obj.c
    lucene/lucy/trunk/core/Lucy/Object/VTable.bp
    lucene/lucy/trunk/core/Lucy/Object/VTable.c
    lucene/lucy/trunk/perl/xs/Lucy/Object/VTable.c

Modified: lucene/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm?rev=894319&r1=894318&r2=894319&view=diff
==============================================================================
--- lucene/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm (original)
+++ lucene/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm Tue Dec 29 07:03:16 2009
@@ -105,7 +105,7 @@
     {1}, /* ref.count */
     $parent_ref, /* parent */
     (${prefix}CharBuf*)&${PREFIX}$name_var,
-    ${PREFIX}VTABLE_F_IMMORTAL, /* flags */
+    0, /* flags */
     NULL, /* "void *x" member reserved for future use */
     sizeof($self->{full_struct_sym}), /* obj_alloc_size */
     offsetof(${prefix}VTable, methods) 

Modified: lucene/lucy/trunk/core/Lucy/Object/Obj.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/Obj.c?rev=894319&r1=894318&r2=894319&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/Obj.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/Obj.c Tue Dec 29 07:03:16 2009
@@ -26,7 +26,6 @@
 void
 Obj_destroy(Obj *self)
 {
-    VTable_Dec_RefCount(self->vtable);
     FREEMEM(self);
 }
 

Modified: lucene/lucy/trunk/core/Lucy/Object/VTable.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/VTable.bp?rev=894319&r1=894318&r2=894319&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/VTable.bp (original)
+++ lucene/lucy/trunk/core/Lucy/Object/VTable.bp Tue Dec 29 07:03:16 2009
@@ -98,21 +98,22 @@
     public incremented VTable* 
     Clone(VTable *self);
 
+    incremented Obj*
+    Inc_RefCount(VTable *self);
+
     u32_t
     Dec_RefCount(VTable *self);
+
+    u32_t
+    Get_RefCount(VTable *self);
+
+    void*
+    To_Host(VTable *self);
     
     public void 
     Destroy(VTable *self);
 }
 
-__C__
-#define LUCY_VTABLE_F_IMMORTAL 0x1
-
-#ifdef LUCY_USE_SHORT_NAMES
-  #define VTABLE_F_IMMORTAL LUCY_VTABLE_F_IMMORTAL
-#endif
-__END_C__
-
 /* Copyright 2009 The Apache Software Foundation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");

Modified: lucene/lucy/trunk/core/Lucy/Object/VTable.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/VTable.c?rev=894319&r1=894318&r2=894319&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/VTable.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/VTable.c Tue Dec 29 07:03:16 2009
@@ -17,11 +17,6 @@
 
 size_t lucy_VTable_offset_of_parent = offsetof(lucy_VTable, parent);
 
-/* Clean up a VTable when its refcount drops to 0. 
- */
-static void 
-S_remove_from_registry(const CharBuf *name);
-
 /* Remove spaces and underscores, convert to lower case. */
 static void
 S_scrunch_charbuf(CharBuf *source, CharBuf *target);
@@ -31,15 +26,7 @@
 void
 VTable_destroy(VTable *self)
 {
-    if (self->flags & VTABLE_F_IMMORTAL) {
-        THROW(ERR, "Attempt to destroy immortal VTable for class %o", self->name);
-    }
-    if (self->parent && (VTable_Get_RefCount(self->parent) == 2)) {
-        S_remove_from_registry(self->parent->name);
-    }
-    DECREF(self->name);
-    DECREF(self->parent);
-    SUPER_DESTROY(self, VTABLE);
+    THROW(ERR, "Insane attempt to destroy VTable for class '%o'", self->name);
 }
 
 VTable*
@@ -49,30 +36,42 @@
         = (VTable*)Memory_wrapped_calloc(self->vt_alloc_size, 1);
 
     memcpy(evil_twin, self, self->vt_alloc_size);
-    INCREF(evil_twin->vtable);
     evil_twin->name = CB_Clone(self->name);
     evil_twin->ref.count = 1; 
 
-    /* Mark evil_twin as dynamic. */
-    evil_twin->flags = self->flags & ~VTABLE_F_IMMORTAL;
-
-    if (evil_twin->parent != NULL)
-        (void)INCREF(evil_twin->parent);
-
     return evil_twin;
 }
 
+Obj*
+VTable_inc_refcount(VTable *self)
+{
+    return (Obj*)self;
+}
+
 u32_t
 VTable_dec_refcount(VTable *self)
 {
-    VTable_dec_refcount_t super_decref 
-        = (VTable_dec_refcount_t)SUPER_METHOD(VTABLE, VTable, Dec_RefCount);
-    u32_t modified_refcount = super_decref(self);
-    if (modified_refcount == 1) {
-        S_remove_from_registry(self->name);
-        modified_refcount--;
-    }
-    return modified_refcount;
+    UNUSED_VAR(self);
+    return 1;
+}
+
+u32_t
+VTable_get_refcount(VTable *self)
+{
+    UNUSED_VAR(self);
+    /* VTable_Get_RefCount() lies to other Lucy code about the refcount
+     * because we don't want to have to synchronize access to the cached host
+     * object to which we have delegated responsibility for keeping refcounts.
+     * It always returns 1 because 1 is a positive number, and thus other Lucy
+     * code will be fooled into believing it never needs to take action such
+     * as initiating a destructor.
+     * 
+     * It's possible that the host has in fact increased the refcount of the
+     * cached host object if there are multiple refs to it on the other side
+     * of the Lucy/host border, but returning 1 is good enough to fool Lucy
+     * code.
+     */
+    return 1;
 }
 
 void
@@ -116,18 +115,13 @@
             }
             else {
                 parent = VTable_singleton(parent_class, NULL);
-                DECREF(parent_class);
             }
         }
-        (void)INCREF(parent);
 
         /* Copy source vtable. */
         singleton = VTable_Clone(parent);
-        DECREF(singleton->vtable);
-        singleton->vtable = (VTable*)INCREF(VTABLE);
 
         /* Turn clone into child. */
-        DECREF(singleton->parent);
         singleton->parent = parent; 
         DECREF(singleton->name);
         singleton->name = CB_Clone(subclass_name);
@@ -172,7 +166,7 @@
 VTable_make_obj(VTable *self)
 {
     Obj *obj = (Obj*)Memory_wrapped_calloc(self->obj_alloc_size, 1);
-    obj->vtable = (VTable*)INCREF(self);
+    obj->vtable = self;
     obj->ref.count = 1;
     return obj;
 }
@@ -217,7 +211,7 @@
         }
     }
     else {
-        Hash_Store(VTable_registry, (Obj*)vtable->name, INCREF(vtable));
+        Hash_Store(VTable_registry, (Obj*)vtable->name, (Obj*)vtable);
     }
 }
 
@@ -231,20 +225,6 @@
     return vtable;
 }
 
-static void 
-S_remove_from_registry(const CharBuf *name)
-{
-    if (VTable_registry == NULL) {
-        THROW(ERR, "Attempt to remove '%o', but registry is NULL", name);
-    }
-    else {
-        VTable *vtable = (VTable*)Hash_Delete(VTable_registry, (Obj*)name);
-        if (vtable) {
-            VTable_Dec_RefCount(vtable);
-        }
-    }
-}
-
 /* Copyright 2009 The Apache Software Foundation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");

Modified: lucene/lucy/trunk/perl/xs/Lucy/Object/VTable.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/xs/Lucy/Object/VTable.c?rev=894319&r1=894318&r2=894319&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/xs/Lucy/Object/VTable.c (original)
+++ lucene/lucy/trunk/perl/xs/Lucy/Object/VTable.c Tue Dec 29 07:03:16 2009
@@ -12,7 +12,7 @@
     lucy_Obj *obj 
         = (lucy_Obj*)lucy_Memory_wrapped_calloc(self->obj_alloc_size, 1);
     SV *inner_obj = SvRV((SV*)host_obj);
-    obj->vtable = (lucy_VTable*)LUCY_INCREF(self);
+    obj->vtable = self;
     sv_setiv(inner_obj, PTR2IV(obj));
     obj->ref.host_obj = inner_obj;
     return obj;
@@ -40,6 +40,19 @@
         LUCY_ARG_STR("class_name", class_name));
 }
 
+void*
+lucy_VTable_to_host(lucy_VTable *self)
+{
+    chy_bool_t first_time = self->ref.count < 4 ? true : false;
+    lucy_VTable_to_host_t to_host = (lucy_VTable_to_host_t)
+        LUCY_SUPER_METHOD(LUCY_VTABLE, VTable, To_Host);
+    SV *host_obj = (SV*)to_host(self);
+    if (first_time) {
+        SvSHARE((SV*)self->ref.host_obj);
+    }
+    return host_obj;
+}
+
 /* Copyright 2009 The Apache Software Foundation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");