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/03 03:42:20 UTC

[lucy-commits] svn commit: r1066724 - in /incubator/lucy/trunk/clownfish: lib/Clownfish.xs lib/Clownfish/Parcel.pm src/CFCParcel.c src/CFCParcel.h

Author: marvin
Date: Thu Feb  3 02:42:20 2011
New Revision: 1066724

URL: http://svn.apache.org/viewvc?rev=1066724&view=rev
Log:
Port the rest of Clownfish::Parcel to C.

Modified:
    incubator/lucy/trunk/clownfish/lib/Clownfish.xs
    incubator/lucy/trunk/clownfish/lib/Clownfish/Parcel.pm
    incubator/lucy/trunk/clownfish/src/CFCParcel.c
    incubator/lucy/trunk/clownfish/src/CFCParcel.h

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1066724&r1=1066723&r2=1066724&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Thu Feb  3 02:42:20 2011
@@ -162,16 +162,16 @@ PPCODE:
 MODULE = Clownfish    PACKAGE = Clownfish::Parcel
 
 SV*
-_new(klass, name_sv, cnick_sv)
+_singleton(klass, name_sv, cnick_sv)
     const char *klass;
     SV *name_sv;
     SV *cnick_sv;
 CODE:
     const char *name  = SvOK(name_sv)  ? SvPV_nolen(name_sv)  : NULL;
     const char *cnick = SvOK(cnick_sv) ? SvPV_nolen(cnick_sv) : NULL;
-    CFCParcel *self = CFCParcel_new(name, cnick);
-    RETVAL = newSV(0);
-	sv_setref_pv(RETVAL, klass, (void*)self);
+    CFCParcel *self = CFCParcel_singleton(name, cnick);
+    SV *inner_object = SvRV((SV*)CFCParcel_get_perl_object(self));
+    RETVAL = newRV(inner_object);
 OUTPUT: RETVAL
 
 void
@@ -191,14 +191,9 @@ OUTPUT: RETVAL
 SV*
 default_parcel(...)
 CODE:
-    static SV *default_parcel_sv = NULL;
-    if (default_parcel_sv == NULL) {
-        // This leaks, but that's OK.
-        CFCParcel *default_parcel = CFCParcel_default_parcel();
-        default_parcel_sv = newSV(0);
-        sv_setref_pv(default_parcel_sv, "Clownfish::Parcel", (void*)default_parcel);
-    }
-    RETVAL = newSVsv(default_parcel_sv);
+    CFCParcel *default_parcel = CFCParcel_default_parcel();
+    SV *inner_obj = SvRV((SV*)CFCParcel_get_perl_object(default_parcel));
+    RETVAL = newRV(inner_obj);
 OUTPUT: RETVAL
 
 void

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Parcel.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Parcel.pm?rev=1066724&r1=1066723&r2=1066724&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Parcel.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Parcel.pm Thu Feb  3 02:42:20 2011
@@ -22,8 +22,6 @@ use Clownfish;
 use Clownfish::Util qw( verify_args );
 use Carp;
 
-our %parcels;
-
 our %singleton_PARAMS = (
     name  => undef,
     cnick => undef,
@@ -32,28 +30,8 @@ our %singleton_PARAMS = (
 sub singleton {
     my ( $either, %args ) = @_;
     verify_args( \%singleton_PARAMS, %args ) or confess $@;
-    my ( $name, $cnick ) = @args{qw( name cnick )};
-
-    # Return the default parcel for either a blank name or an undefined name.
-    return __PACKAGE__->default_parcel unless $name;
-
-    # Return an existing singleton if the parcel has already been registered.
-    my $existing = $parcels{$name};
-    if ($existing) {
-        if ( $cnick and $cnick ne $existing->get_cnick ) {
-            confess(  "cnick '$cnick' for parcel '$name' conflicts with '"
-                    . $existing->get_cnick
-                    . "'" );
-        }
-        return $existing;
-    }
-
-    # Register new parcel.  Default cnick to name.
     my $package = ref($either) || $either;
-    my $self = $package->_new($name, $cnick);
-    $parcels{$name} = $self;
-
-    return $self;
+    return $package->_singleton( @args{qw( name cnick )} );
 }
 
 1;

Modified: incubator/lucy/trunk/clownfish/src/CFCParcel.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCParcel.c?rev=1066724&r1=1066723&r2=1066724&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCParcel.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCParcel.c Thu Feb  3 02:42:20 2011
@@ -32,8 +32,51 @@ struct CFCParcel {
     const char *prefix;
     const char *Prefix;
     const char *PREFIX;
+    void       *perl_object;
 };
 
+#define MAX_PARCELS 100
+static CFCParcel *registry[MAX_PARCELS + 1];
+static int first_time = true;
+
+CFCParcel*
+CFCParcel_singleton(const char *name, const char *cnick)
+{
+    // Set up registry.
+    if (first_time) {
+        size_t i;
+        for (i = 1; i < MAX_PARCELS; i++) { registry[i] = NULL; }
+        first_time = false;
+    }
+
+    // Return the default parcel for either a blank name or a NULL name.
+    if (!name || !strlen(name)) {
+        return CFCParcel_default_parcel();
+    }
+
+    // Return an existing singleton if the parcel has already been registered.
+    size_t i;
+    for (i = 1; registry[i] != NULL; i++) {
+        CFCParcel *existing = registry[i];
+        if (strcmp(existing->name, name) == 0) {
+            if (cnick && strcmp(existing->cnick, cnick) != 0) {
+                croak("cnick '%s' for parcel '%s' conflicts with '%s'", 
+                    cnick, name, existing->cnick);
+            }
+            return existing;
+        }
+    }
+    if (i == MAX_PARCELS) {
+        croak("Exceeded maximum number of parcels (%d)", MAX_PARCELS);
+    }
+
+    // Register new parcel.
+    CFCParcel *singleton = CFCParcel_new(name, cnick);
+    registry[i] = singleton;
+
+    return singleton;
+}
+
 static int
 S_validate_name_or_cnick(const char *orig)
 {
@@ -68,7 +111,8 @@ CFCParcel_init(CFCParcel *self, const ch
         }
         self->cnick = savepv(cnick);
     }
-    else {
+    else {  
+        // Default cnick to name.
         self->cnick = savepv(name);
     }
     
@@ -95,6 +139,9 @@ CFCParcel_init(CFCParcel *self, const ch
     *((char*)&self->Prefix[prefix_len]) = '\0';
     *((char*)&self->PREFIX[prefix_len]) = '\0';
 
+    self->perl_object = newSV(0);
+	sv_setref_pv(self->perl_object, "Clownfish::Parcel", (void*)self);
+
     return self;
 }
 
@@ -116,6 +163,7 @@ CFCParcel_default_parcel(void)
 {
     if (default_parcel == NULL) {
         default_parcel = CFCParcel_new("DEFAULT", "");
+        registry[0] = default_parcel;
     }
     return default_parcel;
 }
@@ -158,4 +206,9 @@ CFCParcel_get_PREFIX(CFCParcel *self)
     return self->PREFIX;
 }
 
+void*
+CFCParcel_get_perl_object(CFCParcel *self)
+{
+    return self->perl_object;
+}
 

Modified: incubator/lucy/trunk/clownfish/src/CFCParcel.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCParcel.h?rev=1066724&r1=1066723&r2=1066724&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCParcel.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCParcel.h Thu Feb  3 02:42:20 2011
@@ -17,6 +17,9 @@
 typedef struct CFCParcel CFCParcel;
 
 CFCParcel*
+CFCParcel_singleton(const char *name, const char *cnick);
+
+CFCParcel*
 CFCParcel_new(const char *name, const char *cnick);
 
 CFCParcel*
@@ -46,3 +49,6 @@ CFCParcel_get_Prefix(CFCParcel *self);
 const char*
 CFCParcel_get_PREFIX(CFCParcel *self);
 
+void*
+CFCParcel_get_perl_object(CFCParcel *self);
+