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 2011/02/17 21:41:14 UTC

[lucy-commits] svn commit: r1071776 - in /incubator/lucy/trunk/clownfish: include/ lib/ src/

Author: marvin
Date: Thu Feb 17 20:41:14 2011
New Revision: 1071776

URL: http://svn.apache.org/viewvc?rev=1071776&view=rev
Log:
Introduce CFCBase, consolidating allocation and refcounting code.

Added:
    incubator/lucy/trunk/clownfish/src/CFCBase.c
    incubator/lucy/trunk/clownfish/src/CFCBase.h
Modified:
    incubator/lucy/trunk/clownfish/include/CFC.h
    incubator/lucy/trunk/clownfish/lib/Clownfish.xs
    incubator/lucy/trunk/clownfish/src/CFCCBlock.c
    incubator/lucy/trunk/clownfish/src/CFCCBlock.h
    incubator/lucy/trunk/clownfish/src/CFCDocuComment.c
    incubator/lucy/trunk/clownfish/src/CFCDocuComment.h
    incubator/lucy/trunk/clownfish/src/CFCFile.c
    incubator/lucy/trunk/clownfish/src/CFCFile.h
    incubator/lucy/trunk/clownfish/src/CFCParamList.c
    incubator/lucy/trunk/clownfish/src/CFCParamList.h

Modified: incubator/lucy/trunk/clownfish/include/CFC.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/include/CFC.h?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/include/CFC.h (original)
+++ incubator/lucy/trunk/clownfish/include/CFC.h Thu Feb 17 20:41:14 2011
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include "CFCBase.h"
 #include "CFCCBlock.h"
 #include "CFCClass.h"
 #include "CFCDocuComment.h"

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Thu Feb 17 20:41:14 2011
@@ -56,8 +56,8 @@ _new(klass, contents)
     const char *contents;
 CODE:
     CFCCBlock *self = CFCCBlock_new(contents);
-    RETVAL = newRV(CFCCBlock_get_perl_obj(self));
-    CFCCBlock_decref(self);
+    RETVAL = newRV(CFCBase_get_perl_obj((CFCBase*)self));
+    CFCBase_decref((CFCBase*)self);
 OUTPUT: RETVAL
 
 void
@@ -120,8 +120,8 @@ parse(klass, text)
     const char *text;
 CODE:
     CFCDocuComment *self = CFCDocuComment_parse(text);
-    RETVAL = newRV(CFCDocuComment_get_perl_obj(self));
-    CFCDocuComment_decref(self);
+    RETVAL = newRV(CFCBase_get_perl_obj((CFCBase*)self));
+    CFCBase_decref((CFCBase*)self);
 OUTPUT: RETVAL
 
 void
@@ -198,8 +198,8 @@ _new(klass)
     const char *klass;
 CODE:
     CFCFile *self = CFCFile_new();
-    RETVAL = newRV(CFCFile_get_perl_obj(self));
-    CFCFile_decref(self);
+    RETVAL = newRV(CFCBase_get_perl_obj((CFCBase*)self));
+    CFCBase_decref((CFCBase*)self);
 OUTPUT: RETVAL
 
 void
@@ -357,8 +357,8 @@ _new(klass, variadic)
     int variadic;
 CODE:
     CFCParamList *self = CFCParamList_new(variadic);
-    RETVAL = newRV((SV*)CFCParamList_get_perl_obj(self));
-    CFCParamList_decref(self);
+    RETVAL = newRV((SV*)CFCBase_get_perl_obj((CFCBase*)self));
+    CFCBase_decref((CFCBase*)self);
 OUTPUT: RETVAL
 
 void

Added: incubator/lucy/trunk/clownfish/src/CFCBase.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCBase.c?rev=1071776&view=auto
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCBase.c (added)
+++ incubator/lucy/trunk/clownfish/src/CFCBase.c Thu Feb 17 20:41:14 2011
@@ -0,0 +1,67 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+
+#define CFC_NEED_BASE_STRUCT_DEF
+#include "CFCBase.h"
+#include "CFCUtil.h"
+
+CFCBase*
+CFCBase_allocate(size_t size, const char *klass)
+{
+    CFCBase *self = (CFCBase*)calloc(size, 1);
+    if (!self) {
+        croak("Failed to calloc %" UVuf " bytes", (UV)size);
+    }
+    self->perl_obj = CFCUtil_make_perl_obj(self, klass);
+    return self;
+}
+
+void
+CFCBase_destroy(CFCBase *self)
+{
+    free(self);
+}
+
+CFCBase*
+CFCBase_incref(CFCBase *self)
+{
+    SvREFCNT_inc((SV*)self->perl_obj);
+    return self;
+}
+
+unsigned
+CFCBase_decref(CFCBase *self)
+{
+    unsigned modified_refcount = SvREFCNT((SV*)self->perl_obj) - 1;
+    /* When the SvREFCNT for this Perl object falls to zero, DESTROY will be
+     * invoked from Perl space for the class that the Perl object was blessed
+     * into.  Thus even though the very simple CFC object model does not
+     * generally support polymorphism, we get it for object destruction. */
+    SvREFCNT_dec((SV*)self->perl_obj);
+    return modified_refcount;
+}
+
+void*
+CFCBase_get_perl_obj(CFCBase *self)
+{
+    return self->perl_obj;
+}
+

Added: incubator/lucy/trunk/clownfish/src/CFCBase.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCBase.h?rev=1071776&view=auto
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCBase.h (added)
+++ incubator/lucy/trunk/clownfish/src/CFCBase.h Thu Feb 17 20:41:14 2011
@@ -0,0 +1,57 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+typedef struct CFCBase CFCBase;
+
+#ifdef CFC_NEED_BASE_STRUCT_DEF
+struct CFCBase {
+    void *perl_obj;
+};
+#endif
+
+/** Allocate a new CFC object.
+ *
+ * @param size Size of the desired allocation in bytes.
+ * @param klass Class name.
+ */
+CFCBase*
+CFCBase_allocate(size_t size, const char *klass);
+
+/** Clean up CFCBase member variables as necessary and free the object blob
+ * itself.
+ */
+void
+CFCBase_destroy(CFCBase *self);
+
+/** Increment the refcount of the object.
+ * 
+ * @return the object itself, allowing an assignment idiom.
+ */
+CFCBase*
+CFCBase_incref(CFCBase *self);
+
+/** Decrement the refcount of the object.
+ * 
+ * @return the modified refcount.
+ */
+unsigned
+CFCBase_decref(CFCBase *self);
+
+/** Return the CFC object's cached Perl object.
+ */
+void*
+CFCBase_get_perl_obj(CFCBase *self);
+

Modified: incubator/lucy/trunk/clownfish/src/CFCCBlock.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCCBlock.c?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCCBlock.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCCBlock.c Thu Feb 17 20:41:14 2011
@@ -19,19 +19,21 @@
 #include "perl.h"
 #include "XSUB.h"
 
+#define CFC_NEED_BASE_STRUCT_DEF
+#include "CFCBase.h"
 #include "CFCCBlock.h"
 #include "CFCUtil.h"
 
 struct CFCCBlock {
+    CFCBase base;
     char *contents;
-    void *perl_obj;
 };
 
 CFCCBlock*
 CFCCBlock_new(const char *contents)
 {
-    CFCCBlock *self = (CFCCBlock*)malloc(sizeof(CFCCBlock));
-    if (!self) { croak("malloc failed"); }
+    CFCCBlock *self = (CFCCBlock*)CFCBase_allocate(sizeof(CFCCBlock),
+        "Clownfish::CBlock");
     return CFCCBlock_init(self, contents);
 }
 
@@ -39,7 +41,6 @@ CFCCBlock*
 CFCCBlock_init(CFCCBlock *self, const char *contents) 
 {
     self->contents = CFCUtil_strdup(contents);
-    self->perl_obj = CFCUtil_make_perl_obj(self, "Clownfish::CBlock");
     return self;
 }
 
@@ -47,28 +48,7 @@ void
 CFCCBlock_destroy(CFCCBlock *self)
 {
     free(self->contents);
-    free(self);
-}
-
-CFCCBlock*
-CFCCBlock_incref(CFCCBlock *self)
-{
-    SvREFCNT_inc((SV*)self->perl_obj);
-    return self;
-}
-
-unsigned
-CFCCBlock_decref(CFCCBlock *self)
-{
-    unsigned modified_refcount = SvREFCNT((SV*)self->perl_obj) - 1;
-    SvREFCNT_dec((SV*)self->perl_obj);
-    return modified_refcount;
-}
-
-void*
-CFCCBlock_get_perl_obj(CFCCBlock *self)
-{
-    return self->perl_obj;
+    CFCBase_destroy((CFCBase*)self);
 }
 
 const char*

Modified: incubator/lucy/trunk/clownfish/src/CFCCBlock.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCCBlock.h?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCCBlock.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCCBlock.h Thu Feb 17 20:41:14 2011
@@ -25,15 +25,6 @@ CFCCBlock_init(CFCCBlock *self, const ch
 void
 CFCCBlock_destroy(CFCCBlock *self);
 
-CFCCBlock*
-CFCCBlock_incref(CFCCBlock *self);
-
-unsigned
-CFCCBlock_decref(CFCCBlock *self);
-
-void*
-CFCCBlock_get_perl_obj(CFCCBlock *self);
-
 const char*
 CFCCBlock_get_contents(CFCCBlock *self);
 

Modified: incubator/lucy/trunk/clownfish/src/CFCDocuComment.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCDocuComment.c?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCDocuComment.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCDocuComment.c Thu Feb 17 20:41:14 2011
@@ -20,17 +20,19 @@
 #include "perl.h"
 #include "XSUB.h"
 
+#define CFC_NEED_BASE_STRUCT_DEF
+#include "CFCBase.h"
 #include "CFCDocuComment.h"
 #include "CFCUtil.h"
 
 struct CFCDocuComment {
+    CFCBase base;
     char *description;
     char *brief;
     char *long_des;
     char **param_names;
     char **param_docs;
     char *retval;
-    void *perl_obj;
 };
 
 /** Remove comment open, close, and left border from raw comment text.
@@ -87,9 +89,8 @@ CFCDocuComment*
 CFCDocuComment_parse(const char *raw_text)
 {
     char *text = CFCUtil_strdup(raw_text);
-    CFCDocuComment *self = (CFCDocuComment*)calloc(1, sizeof(CFCDocuComment));
-    if (!self) { croak("calloc failed"); }
-    self->perl_obj = CFCUtil_make_perl_obj(self, "Clownfish::DocuComment");
+    CFCDocuComment *self = (CFCDocuComment*)CFCBase_allocate(
+        sizeof(CFCDocuComment), "Clownfish::DocuComment");
 
     // Strip whitespace, comment open, close, and left border.
     CFCUtil_trim_whitespace(text);
@@ -240,28 +241,7 @@ CFCDocuComment_destroy(CFCDocuComment *s
     free(self->brief);
     free(self->long_des);
     free(self->retval);
-    free(self);
-}
-
-CFCDocuComment*
-CFCDocuComment_incref(CFCDocuComment *self)
-{
-    SvREFCNT_inc((SV*)self->perl_obj);
-    return self;
-}
-
-unsigned
-CFCDocuComment_decref(CFCDocuComment *self)
-{
-    unsigned modified_refcount = SvREFCNT((SV*)self->perl_obj) - 1;
-    SvREFCNT_dec((SV*)self->perl_obj);
-    return modified_refcount;
-}
-
-void*
-CFCDocuComment_get_perl_obj(CFCDocuComment *self)
-{
-    return self->perl_obj;
+    CFCBase_destroy((CFCBase*)self);
 }
 
 const char*

Modified: incubator/lucy/trunk/clownfish/src/CFCDocuComment.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCDocuComment.h?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCDocuComment.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCDocuComment.h Thu Feb 17 20:41:14 2011
@@ -22,15 +22,6 @@ CFCDocuComment_parse(const char *raw_tex
 void
 CFCDocuComment_destroy(CFCDocuComment *self);
 
-CFCDocuComment*
-CFCDocuComment_incref(CFCDocuComment *self);
-
-unsigned
-CFCDocuComment_decref(CFCDocuComment *self);
-
-void*
-CFCDocuComment_get_perl_obj(CFCDocuComment *self);
-
 const char*
 CFCDocuComment_get_description(CFCDocuComment *self);
 

Modified: incubator/lucy/trunk/clownfish/src/CFCFile.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCFile.c?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCFile.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCFile.c Thu Feb 17 20:41:14 2011
@@ -19,52 +19,32 @@
 #include "perl.h"
 #include "XSUB.h"
 
+#define CFC_NEED_BASE_STRUCT_DEF
+#include "CFCBase.h"
 #include "CFCFile.h"
 #include "CFCUtil.h"
 
 struct CFCFile {
-    void *perl_obj;
+    CFCBase base;
 };
 
 CFCFile*
 CFCFile_new(void)
 {
-    CFCFile *self = (CFCFile*)malloc(sizeof(CFCFile));
-    if (!self) { croak("malloc failed"); }
+    CFCFile *self = (CFCFile*)CFCBase_allocate(sizeof(CFCFile),
+        "Clownfish::File");
     return CFCFile_init(self);
 }
 
 CFCFile*
 CFCFile_init(CFCFile *self) 
 {
-    self->perl_obj = CFCUtil_make_perl_obj(self, "Clownfish::File");
     return self;
 }
 
 void
 CFCFile_destroy(CFCFile *self)
 {
-    free(self);
-}
-
-CFCFile*
-CFCFile_incref(CFCFile *self)
-{
-    SvREFCNT_inc((SV*)self->perl_obj);
-    return self;
-}
-
-unsigned
-CFCFile_decref(CFCFile *self)
-{
-    unsigned modified_refcount = SvREFCNT((SV*)self->perl_obj) - 1;
-    SvREFCNT_dec((SV*)self->perl_obj);
-    return modified_refcount;
-}
-
-void*
-CFCFile_get_perl_obj(CFCFile *self)
-{
-    return self->perl_obj;
+    CFCBase_destroy((CFCBase*)self);
 }
 

Modified: incubator/lucy/trunk/clownfish/src/CFCFile.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCFile.h?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCFile.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCFile.h Thu Feb 17 20:41:14 2011
@@ -25,12 +25,3 @@ CFCFile_init(CFCFile *self);
 void
 CFCFile_destroy(CFCFile *self);
 
-CFCFile*
-CFCFile_incref(CFCFile *self);
-
-unsigned
-CFCFile_decref(CFCFile *self);
-
-void*
-CFCFile_get_perl_obj(CFCFile *self);
-

Modified: incubator/lucy/trunk/clownfish/src/CFCParamList.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCParamList.c?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCParamList.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCParamList.c Thu Feb 17 20:41:14 2011
@@ -19,23 +19,25 @@
 #include "perl.h"
 #include "XSUB.h"
 
+#define CFC_NEED_BASE_STRUCT_DEF
+#include "CFCBase.h"
 #include "CFCParamList.h"
 #include "CFCVariable.h"
 #include "CFCUtil.h"
 
 struct CFCParamList {
+    CFCBase       base;
     CFCVariable **variables;
     char        **values;
     int           variadic;
     size_t        num_vars;
-    void         *perl_obj;
 };
 
 CFCParamList*
 CFCParamList_new(int variadic)
 {
-    CFCParamList *self = (CFCParamList*)malloc(sizeof(CFCParamList));
-    if (!self) { croak("malloc failed"); }
+    CFCParamList *self = (CFCParamList*)CFCBase_allocate(sizeof(CFCParamList), 
+        "Clownfish::ParamList");
     return CFCParamList_init(self, variadic);
 }
 
@@ -46,7 +48,6 @@ CFCParamList_init(CFCParamList *self, in
     self->num_vars  = 0;
     self->variables = (CFCVariable**)calloc(1, sizeof(void*));
     self->values    = (char**)calloc(1, sizeof(char*));
-    self->perl_obj  = CFCUtil_make_perl_obj(self, "Clownfish::ParamList");
     return self;
 }
 
@@ -78,28 +79,7 @@ CFCParamList_destroy(CFCParamList *self)
     }
     free(self->variables);
     free(self->values);
-    free(self);
-}
-
-CFCParamList*
-CFCParamList_incref(CFCParamList *self)
-{
-    SvREFCNT_inc((SV*)self->perl_obj);
-    return self;
-}
-
-unsigned
-CFCParamList_decref(CFCParamList *self)
-{
-    unsigned modified_refcount = SvREFCNT((SV*)self->perl_obj) - 1;
-    SvREFCNT_dec((SV*)self->perl_obj);
-    return modified_refcount;
-}
-
-void*
-CFCParamList_get_perl_obj(CFCParamList *self)
-{
-    return self->perl_obj;
+    CFCBase_destroy((CFCBase*)self);
 }
 
 CFCVariable**

Modified: incubator/lucy/trunk/clownfish/src/CFCParamList.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCParamList.h?rev=1071776&r1=1071775&r2=1071776&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCParamList.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCParamList.h Thu Feb 17 20:41:14 2011
@@ -26,15 +26,6 @@ CFCParamList_init(CFCParamList *self, in
 void
 CFCParamList_destroy(CFCParamList *self);
 
-CFCParamList*
-CFCParamList_incref(CFCParamList *self);
-
-unsigned
-CFCParamList_decref(CFCParamList *self);
-
-void*
-CFCParamList_get_perl_obj(CFCParamList *self);
-
 void
 CFCParamList_add_param(CFCParamList *self, struct CFCVariable *variable,  
                        const char *value);