Author: nwellnhof
Date: Sat Jul 14 13:48:19 2012
New Revision: 1361543

URL: http://svn.apache.org/viewvc?rev=1361543&view=rev
Log:
LUCY-233 Rework VTable bootstrapping

Set needed VTable fields manually before VTable initialization, so we
can call methods normally during initialization.

Modified:
    lucy/trunk/clownfish/src/CFCBindAliases.c
    lucy/trunk/clownfish/src/CFCBindClass.c
    lucy/trunk/clownfish/src/CFCBindClass.h
    lucy/trunk/clownfish/src/CFCBindCore.c
    lucy/trunk/core/Lucy/Object/VTable.c
    lucy/trunk/core/Lucy/Object/VTable.cfh
    lucy/trunk/devel/conf/lucyperl.supp

Modified: lucy/trunk/clownfish/src/CFCBindAliases.c
URL: 
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCBindAliases.c?rev=1361543&r1=1361542&r2=1361543&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCBindAliases.c (original)
+++ lucy/trunk/clownfish/src/CFCBindAliases.c Sat Jul 14 13:48:19 2012
@@ -98,10 +98,11 @@ struct alias aliases[] = {
     {"cfish_VTable", "lucy_VTable"},
     {"CFISH_VTABLE", "LUCY_VTABLE"},
     {"cfish_VTable_allocate", "lucy_VTable_allocate"},
-    {"cfish_VTable_bootstrap", "lucy_VTable_bootstrap"},
+    {"cfish_VTable_init", "lucy_VTable_init"},
     {"cfish_VTable_add_to_registry", "lucy_VTable_add_to_registry"},
     {"cfish_VTable_add_alias_to_registry", 
"lucy_VTable_add_alias_to_registry"},
     {"cfish_VTable_offset_of_parent", "lucy_VTable_offset_of_parent"},
+    {"cfish_VTable_override", "lucy_VTable_override"},
     {"cfish_VTable_singleton", "lucy_VTable_singleton"},
     {"Cfish_VTable_Get_Name", "Lucy_VTable_Get_Name"},
     {"Cfish_VTable_Make_Obj", "Lucy_VTable_Make_Obj"},

Modified: lucy/trunk/clownfish/src/CFCBindClass.c
URL: 
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCBindClass.c?rev=1361543&r1=1361542&r2=1361543&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCBindClass.c (original)
+++ lucy/trunk/clownfish/src/CFCBindClass.c Sat Jul 14 13:48:19 2012
@@ -450,9 +450,9 @@ CFCBindClass_to_vtable_allocate(CFCBindC
     return code;
 }
 
-// Return C code bootstrapping the class's VTable.
+// Return C code initializing the class's VTable.
 char*
-CFCBindClass_to_vtable_bootstrap(CFCBindClass *self) {
+CFCBindClass_to_vtable_init(CFCBindClass *self) {
     CFCClass *client = self->client;
 
     if (CFCClass_inert(client)) {
@@ -470,7 +470,7 @@ CFCBindClass_to_vtable_bootstrap(CFCBind
                              : "NULL"; // No parent, e.g. Obj or inert classes.
 
     char pattern[] =
-        "    cfish_VTable_bootstrap(\n"
+        "    cfish_VTable_init(\n"
         "        %s, /* vtable */\n"
         "        %s, /* parent */\n"
         "        \"%s\", /* name */\n"

Modified: lucy/trunk/clownfish/src/CFCBindClass.h
URL: 
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCBindClass.h?rev=1361543&r1=1361542&r2=1361543&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCBindClass.h (original)
+++ lucy/trunk/clownfish/src/CFCBindClass.h Sat Jul 14 13:48:19 2012
@@ -59,10 +59,10 @@ CFCBindClass_to_c_data(CFCBindClass *sel
 char*
 CFCBindClass_to_vtable_allocate(CFCBindClass *self);
 
-/** Return the autogenerated C code necessary to bootstrap the class's VTable.
+/** Return the autogenerated C code necessary to initialize the class's VTable.
  */
 char*
-CFCBindClass_to_vtable_bootstrap(CFCBindClass *self);
+CFCBindClass_to_vtable_init(CFCBindClass *self);
 
 /** Return the autogenerated C code necessary to register the class's VTable.
  */

Modified: lucy/trunk/clownfish/src/CFCBindCore.c
URL: 
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCBindCore.c?rev=1361543&r1=1361542&r2=1361543&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCBindCore.c (original)
+++ lucy/trunk/clownfish/src/CFCBindCore.c Sat Jul 14 13:48:19 2012
@@ -266,13 +266,13 @@ S_write_parcel_c(CFCBindCore *self) {
 
     // Aggregate C code from all files.
     // Obtain parcel prefix for use in bootstrap function name.
-    char *privacy_syms = CFCUtil_strdup("");
-    char *includes     = CFCUtil_strdup("");
-    char *c_data       = CFCUtil_strdup("");
-    char *vt_allocate  = CFCUtil_strdup("");
-    char *vt_bootstrap = CFCUtil_strdup("");
-    char *vt_register  = CFCUtil_strdup("");
-    CFCClass **ordered = CFCHierarchy_ordered_classes(hierarchy);
+    char *privacy_syms  = CFCUtil_strdup("");
+    char *includes      = CFCUtil_strdup("");
+    char *c_data        = CFCUtil_strdup("");
+    char *vt_allocate   = CFCUtil_strdup("");
+    char *vt_initialize = CFCUtil_strdup("");
+    char *vt_register   = CFCUtil_strdup("");
+    CFCClass **ordered  = CFCHierarchy_ordered_classes(hierarchy);
     for (int i = 0; ordered[i] != NULL; i++) {
         CFCClass *klass = ordered[i];
         if (CFCClass_included(klass)) { continue; }
@@ -284,9 +284,9 @@ S_write_parcel_c(CFCBindCore *self) {
         char *vt_alloc = CFCBindClass_to_vtable_allocate(class_binding);
         vt_allocate = CFCUtil_cat(vt_allocate, vt_alloc, NULL);
         FREEMEM(vt_alloc);
-        char *vt_boot = CFCBindClass_to_vtable_bootstrap(class_binding);
-        vt_bootstrap = CFCUtil_cat(vt_bootstrap, vt_boot, NULL);
-        FREEMEM(vt_boot);
+        char *vt_init = CFCBindClass_to_vtable_init(class_binding);
+        vt_initialize = CFCUtil_cat(vt_initialize, vt_init, NULL);
+        FREEMEM(vt_init);
         char *vt_reg = CFCBindClass_to_vtable_register(class_binding);
         vt_register = CFCUtil_cat(vt_register, vt_reg, NULL);
         FREEMEM(vt_reg);
@@ -310,6 +310,20 @@ S_write_parcel_c(CFCBindCore *self) {
     const char *prefix = CFCParcel_get_prefix(parcel);
     FREEMEM(ordered);
 
+    const char *vt_bootstrap = "";
+    if (strcmp(prefix, "lucy_") == 0) {
+        vt_bootstrap =
+            "    /* Bootstrap VTables.\n"
+            "     */\n"
+            "    cfish_VTable_override(CFISH_VTABLE, 
(cfish_method_t)lucy_VTable_make_obj, Lucy_VTable_Make_Obj_OFFSET);\n"
+            "    CFISH_CHARBUF->vtable = CFISH_VTABLE;\n"
+            "    CFISH_CHARBUF->obj_alloc_size = sizeof(cfish_CharBuf);\n"
+            "    cfish_VTable_override(CFISH_CHARBUF, 
(cfish_method_t)lucy_CB_cat_trusted_str, Lucy_CB_Cat_Trusted_Str_OFFSET);\n"
+            "    cfish_VTable_override(CFISH_CHARBUF, 
(cfish_method_t)lucy_CB_grow, Lucy_CB_Grow_OFFSET);\n"
+            "    cfish_VTable_override(CFISH_CHARBUF, 
(cfish_method_t)lucy_CB_vcatf, Lucy_CB_VCatF_OFFSET);\n"
+            "\n";
+    }
+
     char pattern[] =
         "%s\n"
         "\n"
@@ -323,10 +337,17 @@ S_write_parcel_c(CFCBindCore *self) {
         "\n"
         "void\n"
         "%sbootstrap_parcel() {\n"
+        "    /* Allocate memory for VTables.\n"
+        "     */\n"
         "%s"
         "\n"
         "%s"
+        "    /* Initialize VTables.\n"
+        "     */\n"
+        "%s"
         "\n"
+        "    /* Register VTables.\n"
+        "     */\n"
         "%s"
         "\n"
         "    %sinit_parcel();\n"
@@ -341,14 +362,15 @@ S_write_parcel_c(CFCBindCore *self) {
                   + strlen(prefix)
                   + strlen(vt_allocate)
                   + strlen(vt_bootstrap)
+                  + strlen(vt_initialize)
                   + strlen(vt_register)
                   + strlen(prefix)
                   + strlen(self->footer)
                   + 100;
     char *file_content = (char*)MALLOCATE(size);
     sprintf(file_content, pattern, self->header, privacy_syms, includes,
-            c_data, prefix, vt_allocate, vt_bootstrap, vt_register, prefix,
-            self->footer);
+            c_data, prefix, vt_allocate, vt_bootstrap, vt_initialize,
+            vt_register, prefix, self->footer);
 
     // Unlink then open file.
     const char *src_dest = CFCHierarchy_get_source_dest(hierarchy);
@@ -362,7 +384,7 @@ S_write_parcel_c(CFCBindCore *self) {
     FREEMEM(includes);
     FREEMEM(c_data);
     FREEMEM(vt_allocate);
-    FREEMEM(vt_bootstrap);
+    FREEMEM(vt_initialize);
     FREEMEM(vt_register);
 }
 

Modified: lucy/trunk/core/Lucy/Object/VTable.c
URL: 
http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Object/VTable.c?rev=1361543&r1=1361542&r2=1361543&view=diff
==============================================================================
--- lucy/trunk/core/Lucy/Object/VTable.c (original)
+++ lucy/trunk/core/Lucy/Object/VTable.c Sat Jul 14 13:48:19 2012
@@ -51,24 +51,12 @@ VTable_allocate(size_t num_methods) {
 }
 
 VTable*
-VTable_bootstrap(VTable *self, VTable *parent, const char *name, int flags,
-                 void *x, size_t obj_alloc_size, void *method_meta) {
-    // Create CharBuf manually, since the CharBuf VTable might not be
-    // bootstrapped yet.
-    CharBuf *name_cb = (CharBuf*)Memory_wrapped_calloc(sizeof(CharBuf), 1);
-    size_t name_len  = strlen(name);
-
-    name_cb->vtable    = CHARBUF;
-    name_cb->ref.count = 1;
-    name_cb->ptr       = (char*)MALLOCATE(name_len + 1);
-    name_cb->size      = name_len;
-    name_cb->cap       = name_len + 1;
-    strcpy(name_cb->ptr, name);
-
+VTable_init(VTable *self, VTable *parent, const char *name, int flags,
+            void *x, size_t obj_alloc_size, void *method_meta) {
     self->vtable         = CFISH_VTABLE;
     self->ref.count      = 1;
     self->parent         = parent;
-    self->name           = name_cb;
+    self->name           = CB_newf("%s", name);
     self->flags          = flags;
     self->x              = x;
     self->obj_alloc_size = obj_alloc_size;

Modified: lucy/trunk/core/Lucy/Object/VTable.cfh
URL: 
http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Object/VTable.cfh?rev=1361543&r1=1361542&r2=1361543&view=diff
==============================================================================
--- lucy/trunk/core/Lucy/Object/VTable.cfh (original)
+++ lucy/trunk/core/Lucy/Object/VTable.cfh Sat Jul 14 13:48:19 2012
@@ -41,8 +41,8 @@ class Lucy::Object::VTable inherits Lucy
     allocate(size_t num_methods);
 
     inert VTable*
-    bootstrap(VTable *self, VTable *parent, const char *name, int flags,
-              void *x, size_t obj_alloc_size, void *method_meta);
+    init(VTable *self, VTable *parent, const char *name, int flags,
+         void *x, size_t obj_alloc_size, void *method_meta);
 
     /** Return a singleton.  If a VTable can be found in the registry based on
      * the supplied class name, it will be returned.  Otherwise, a new VTable

Modified: lucy/trunk/devel/conf/lucyperl.supp
URL: 
http://svn.apache.org/viewvc/lucy/trunk/devel/conf/lucyperl.supp?rev=1361543&r1=1361542&r2=1361543&view=diff
==============================================================================
--- lucy/trunk/devel/conf/lucyperl.supp (original)
+++ lucy/trunk/devel/conf/lucyperl.supp Sat Jul 14 13:48:19 2012
@@ -191,24 +191,6 @@
 {
    <insert_a_suppression_name_here>
    Memcheck:Leak
-   fun:malloc
-   fun:lucy_Memory_wrapped_malloc
-   fun:lucy_VTable_bootstrap
-   fun:*
-}
-
-{
-   <insert_a_suppression_name_here>
-   Memcheck:Leak
-   fun:calloc
-   fun:lucy_Memory_wrapped_calloc
-   fun:lucy_VTable_bootstrap
-   fun:*
-}
-
-{
-   <insert_a_suppression_name_here>
-   Memcheck:Leak
    fun:calloc
    fun:lucy_Memory_wrapped_calloc
    fun:lucy_VTable_allocate


Reply via email to