Consolidate refcount manipulation for "immortals". Instead of having the "immortal classes" override refcounting methods, special-case them within a consolidated refcounting function.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/d1a20342 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/d1a20342 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/d1a20342 Branch: refs/heads/master Commit: d1a20342781b2aff9f6c360590fc55856516ae90 Parents: 532c6d6 Author: Marvin Humphrey <[email protected]> Authored: Thu Feb 5 20:21:31 2015 -0800 Committer: Marvin Humphrey <[email protected]> Committed: Sat Feb 7 07:40:07 2015 -0800 ---------------------------------------------------------------------- runtime/c/src/Clownfish/Obj.c | 41 +++++++++++++++++++++++++++++++--- runtime/core/Clownfish/Class.c | 29 ------------------------ runtime/core/Clownfish/Class.cfh | 9 -------- runtime/core/Clownfish/Hash.c | 20 ----------------- runtime/core/Clownfish/Hash.cfh | 9 -------- runtime/core/Clownfish/Method.c | 18 --------------- runtime/core/Clownfish/Method.cfh | 9 -------- runtime/core/Clownfish/Num.c | 11 --------- runtime/core/Clownfish/Num.cfh | 6 ----- runtime/perl/xs/XSBind.c | 38 +++++++++++++++++++++++++++++-- 10 files changed, 74 insertions(+), 116 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/c/src/Clownfish/Obj.c ---------------------------------------------------------------------- diff --git a/runtime/c/src/Clownfish/Obj.c b/runtime/c/src/Clownfish/Obj.c index 24cbfd1..e17feef 100644 --- a/runtime/c/src/Clownfish/Obj.c +++ b/runtime/c/src/Clownfish/Obj.c @@ -27,14 +27,44 @@ Obj_Get_RefCount_IMP(Obj *self) { return self->refcount; } +static CFISH_INLINE bool +SI_immortal(cfish_Class *klass) { + if (klass == CFISH_CLASS + || klass == CFISH_METHOD + || klass == CFISH_BOOLNUM + || klass == CFISH_HASHTOMBSTONE + ){ + return true; + } + return false; +} + +Obj* +cfish_inc_refcount(void *vself) { + Obj *self = (Obj*)vself; + cfish_Class *const klass = self->klass; + if (SI_immortal(klass)) { + return self; + } + else { + self->refcount++; + return self; + } +} + Obj* Obj_Inc_RefCount_IMP(Obj *self) { - self->refcount++; - return self; + return cfish_inc_refcount(self); } uint32_t -Obj_Dec_RefCount_IMP(Obj *self) { +cfish_dec_refcount(void *vself) { + cfish_Obj *self = (Obj*)vself; + cfish_Class *klass = self->klass; + if (SI_immortal(klass)) { + return self->refcount; + } + uint32_t modified_refcount = INT32_MAX; switch (self->refcount) { case 0: @@ -51,6 +81,11 @@ Obj_Dec_RefCount_IMP(Obj *self) { return modified_refcount; } +uint32_t +Obj_Dec_RefCount_IMP(Obj *self) { + return cfish_dec_refcount(self); +} + void* Obj_To_Host_IMP(Obj *self) { UNUSED_VAR(self); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/core/Clownfish/Class.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Class.c b/runtime/core/Clownfish/Class.c index 81c3138..b30b124 100644 --- a/runtime/core/Clownfish/Class.c +++ b/runtime/core/Clownfish/Class.c @@ -183,35 +183,6 @@ Class_Clone_IMP(Class *self) { return twin; } -Obj* -Class_Inc_RefCount_IMP(Class *self) { - return (Obj*)self; -} - -uint32_t -Class_Dec_RefCount_IMP(Class *self) { - UNUSED_VAR(self); - return 1; -} - -uint32_t -Class_Get_RefCount_IMP(Class *self) { - UNUSED_VAR(self); - /* Class_Get_RefCount() lies to other Clownfish 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 - * Clownfish 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 Clownfish/host border, but returning 1 is good enough to fool - * Clownfish code. - */ - return 1; -} - void Class_Override_IMP(Class *self, cfish_method_t method, size_t offset) { union { char *char_ptr; cfish_method_t *func_ptr; } pointer; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/core/Clownfish/Class.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Class.cfh b/runtime/core/Clownfish/Class.cfh index e4dff41..23623b7 100644 --- a/runtime/core/Clownfish/Class.cfh +++ b/runtime/core/Clownfish/Class.cfh @@ -138,15 +138,6 @@ class Clownfish::Class inherits Clownfish::Obj { public incremented Class* Clone(Class *self); - incremented Obj* - Inc_RefCount(Class *self); - - uint32_t - Dec_RefCount(Class *self); - - uint32_t - Get_RefCount(Class *self); - void* To_Host(Class *self); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/core/Clownfish/Hash.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Hash.c b/runtime/core/Clownfish/Hash.c index ac04562..9024624 100644 --- a/runtime/core/Clownfish/Hash.c +++ b/runtime/core/Clownfish/Hash.c @@ -353,23 +353,3 @@ Hash_get_tombstone() { return TOMBSTONE; } -/***************************************************************************/ - -uint32_t -HashTombStone_Get_RefCount_IMP(HashTombStone* self) { - UNUSED_VAR(self); - return 1; -} - -HashTombStone* -HashTombStone_Inc_RefCount_IMP(HashTombStone* self) { - return self; -} - -uint32_t -HashTombStone_Dec_RefCount_IMP(HashTombStone* self) { - UNUSED_VAR(self); - return 1; -} - - http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/core/Clownfish/Hash.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Hash.cfh b/runtime/core/Clownfish/Hash.cfh index 0b09785..cb65921 100644 --- a/runtime/core/Clownfish/Hash.cfh +++ b/runtime/core/Clownfish/Hash.cfh @@ -142,15 +142,6 @@ class Clownfish::Hash inherits Clownfish::Obj { class Clownfish::Hash::HashTombStone inherits Clownfish::Obj { - - uint32_t - Get_RefCount(HashTombStone* self); - - incremented HashTombStone* - Inc_RefCount(HashTombStone* self); - - uint32_t - Dec_RefCount(HashTombStone* self); } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/core/Clownfish/Method.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Method.c b/runtime/core/Clownfish/Method.c index d7a7327..bf76313 100644 --- a/runtime/core/Clownfish/Method.c +++ b/runtime/core/Clownfish/Method.c @@ -44,24 +44,6 @@ Method_Destroy_IMP(Method *self) { THROW(ERR, "Insane attempt to destroy Method '%o'", self->name); } -Obj* -Method_Inc_RefCount_IMP(Method *self) { - return (Obj*)self; -} - -uint32_t -Method_Dec_RefCount_IMP(Method *self) { - UNUSED_VAR(self); - return 1; -} - -uint32_t -Method_Get_RefCount_IMP(Method *self) { - UNUSED_VAR(self); - // See comments in Class.c - return 1; -} - String* Method_Get_Name_IMP(Method *self) { return self->name; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/core/Clownfish/Method.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Method.cfh b/runtime/core/Clownfish/Method.cfh index feab12f..57bc1ad 100644 --- a/runtime/core/Clownfish/Method.cfh +++ b/runtime/core/Clownfish/Method.cfh @@ -46,15 +46,6 @@ class Clownfish::Method inherits Clownfish::Obj { incremented String* Host_Name(Method *self); - incremented Obj* - Inc_RefCount(Method *self); - - uint32_t - Dec_RefCount(Method *self); - - uint32_t - Get_RefCount(Method *self); - public void Destroy(Method *self); } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/core/Clownfish/Num.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Num.c b/runtime/core/Clownfish/Num.c index 774e578..895be7e 100644 --- a/runtime/core/Clownfish/Num.c +++ b/runtime/core/Clownfish/Num.c @@ -382,14 +382,3 @@ Bool_Equals_IMP(BoolNum *self, Obj *other) { return self == (BoolNum*)other; } -BoolNum* -Bool_Inc_RefCount_IMP(BoolNum *self) { - return self; -} - -uint32_t -Bool_Dec_RefCount_IMP(BoolNum *self) { - UNUSED_VAR(self); - return 1; -} - http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/core/Clownfish/Num.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Num.cfh b/runtime/core/Clownfish/Num.cfh index cd2780b..788a0bc 100644 --- a/runtime/core/Clownfish/Num.cfh +++ b/runtime/core/Clownfish/Num.cfh @@ -260,12 +260,6 @@ class Clownfish::BoolNum nickname Bool inherits Clownfish::IntNum { public incremented String* To_String(BoolNum *self); - - incremented BoolNum* - Inc_RefCount(BoolNum *self); - - uint32_t - Dec_RefCount(BoolNum *self); } __C__ http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d1a20342/runtime/perl/xs/XSBind.c ---------------------------------------------------------------------- diff --git a/runtime/perl/xs/XSBind.c b/runtime/perl/xs/XSBind.c index de8b310..ca58dfe 100644 --- a/runtime/perl/xs/XSBind.c +++ b/runtime/perl/xs/XSBind.c @@ -596,6 +596,18 @@ XSBind_allot_params(SV** stack, int32_t start, int32_t num_stack_elems, ...) { /**************************** Clownfish::Obj *******************************/ +static CFISH_INLINE bool +SI_immortal(cfish_Class *klass) { + if (klass == CFISH_CLASS + || klass == CFISH_METHOD + || klass == CFISH_BOOLNUM + || klass == CFISH_HASHTOMBSTONE + ){ + return true; + } + return false; +} + static void S_lazy_init_host_obj(cfish_Obj *self) { SV *inner_obj = newSV(0); @@ -630,7 +642,13 @@ CFISH_Obj_Get_RefCount_IMP(cfish_Obj *self) { } cfish_Obj* -CFISH_Obj_Inc_RefCount_IMP(cfish_Obj *self) { +cfish_inc_refcount(void *vself) { + cfish_Obj *self = (cfish_Obj*)vself; + cfish_Class *const klass = self->klass; + if (SI_immortal(klass)) { + return self; + } + if (self->ref.count & XSBIND_REFCOUNT_FLAG) { if (self->ref.count == XSBIND_REFCOUNT_FLAG) { CFISH_THROW(CFISH_ERR, "Illegal refcount of 0"); @@ -643,8 +661,19 @@ CFISH_Obj_Inc_RefCount_IMP(cfish_Obj *self) { return self; } +cfish_Obj* +CFISH_Obj_Inc_RefCount_IMP(cfish_Obj *self) { + return cfish_inc_refcount(self); +} + uint32_t -CFISH_Obj_Dec_RefCount_IMP(cfish_Obj *self) { +cfish_dec_refcount(void *vself) { + cfish_Obj *self = (cfish_Obj*)vself; + cfish_Class *klass = self->klass; + if (SI_immortal(klass)) { + return 1; + } + uint32_t modified_refcount = I32_MAX; if (self->ref.count & XSBIND_REFCOUNT_FLAG) { if (self->ref.count == XSBIND_REFCOUNT_FLAG) { @@ -669,6 +698,11 @@ CFISH_Obj_Dec_RefCount_IMP(cfish_Obj *self) { return modified_refcount; } +uint32_t +CFISH_Obj_Dec_RefCount_IMP(cfish_Obj *self) { + return cfish_dec_refcount(self); +} + void* CFISH_Obj_To_Host_IMP(cfish_Obj *self) { if (self->ref.count & XSBIND_REFCOUNT_FLAG) { S_lazy_init_host_obj(self); }
