[svn:parrot] r43805 - branches/gc_encapsulate_part2/src/gc

bacek at svn.parrot.org bacek at svn.parrot.org
Tue Feb 9 12:27:17 UTC 2010


Author: bacek
Date: Tue Feb  9 12:27:14 2010
New Revision: 43805
URL: https://trac.parrot.org/parrot/changeset/43805

Log:
Encapsulate allocation/deallocation of attributes and fixed size storage

Modified:
   branches/gc_encapsulate_part2/src/gc/api.c
   branches/gc_encapsulate_part2/src/gc/gc_ms.c
   branches/gc_encapsulate_part2/src/gc/gc_private.h

Modified: branches/gc_encapsulate_part2/src/gc/api.c
==============================================================================
--- branches/gc_encapsulate_part2/src/gc/api.c	Tue Feb  9 10:44:05 2010	(r43804)
+++ branches/gc_encapsulate_part2/src/gc/api.c	Tue Feb  9 12:27:14 2010	(r43805)
@@ -116,15 +116,6 @@
         __attribute__nonnull__(1)
         __attribute__nonnull__(2);
 
-static void Parrot_gc_free_attributes_from_pool(PARROT_INTERP,
-    ARGMOD(PMC_Attribute_Pool *pool),
-    ARGMOD(void *data))
-        __attribute__nonnull__(1)
-        __attribute__nonnull__(2)
-        __attribute__nonnull__(3)
-        FUNC_MODIFIES(*pool)
-        FUNC_MODIFIES(*data);
-
 static void Parrot_gc_merge_buffer_pools(PARROT_INTERP,
     ARGMOD(Fixed_Size_Pool *dest),
     ARGMOD(Fixed_Size_Pool *source))
@@ -163,11 +154,6 @@
 #define ASSERT_ARGS_get_free_buffer __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp) \
     , PARROT_ASSERT_ARG(pool))
-#define ASSERT_ARGS_Parrot_gc_free_attributes_from_pool \
-     __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
-       PARROT_ASSERT_ARG(interp) \
-    , PARROT_ASSERT_ARG(pool) \
-    , PARROT_ASSERT_ARG(data))
 #define ASSERT_ARGS_Parrot_gc_merge_buffer_pools __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp) \
     , PARROT_ASSERT_ARG(dest) \
@@ -818,19 +804,7 @@
 Parrot_gc_allocate_pmc_attributes(PARROT_INTERP, ARGMOD(PMC *pmc))
 {
     ASSERT_ARGS(Parrot_gc_allocate_pmc_attributes)
-    const size_t attr_size = pmc->vtable->attr_size;
-#if GC_USE_FIXED_SIZE_ALLOCATOR
-    PMC_Attribute_Pool * const pool = Parrot_gc_get_attribute_pool(interp,
-            interp->mem_pools, attr_size);
-    void * const attrs = Parrot_gc_get_attributes_from_pool(interp, pool);
-    memset(attrs, 0, attr_size);
-    PMC_data(pmc) = attrs;
-    return attrs;
-#else
-    void * const data =  mem_sys_allocate_zeroed(attr_size);
-    PMC_data(pmc) = data;
-    return data;
-#endif
+    return interp->gc_sys->allocate_pmc_attributes(interp, pmc);
 }
 
 /*
@@ -846,21 +820,7 @@
 Parrot_gc_free_pmc_attributes(PARROT_INTERP, ARGMOD(PMC *pmc))
 {
     ASSERT_ARGS(Parrot_gc_free_pmc_attributes)
-    void * const data = PMC_data(pmc);
-
-    if (data) {
-
-#if GC_USE_FIXED_SIZE_ALLOCATOR
-        const size_t attr_size = pmc->vtable->attr_size;
-        const size_t item_size = attr_size < sizeof (void *) ? sizeof (void *) : attr_size;
-        PMC_Attribute_Pool ** const pools = interp->mem_pools->attrib_pools;
-        const size_t idx = item_size - sizeof (void *);
-        Parrot_gc_free_attributes_from_pool(interp, pools[idx], data);
-#else
-        mem_sys_free(PMC_data(pmc));
-        PMC_data(pmc) = NULL;
-#endif
-    }
+    interp->gc_sys->free_pmc_attributes(interp, pmc);
 }
 
 /*
@@ -878,18 +838,7 @@
 Parrot_gc_allocate_fixed_size_storage(PARROT_INTERP, size_t size)
 {
     ASSERT_ARGS(Parrot_gc_allocate_fixed_size_storage)
-    PMC_Attribute_Pool *pool = NULL;
-    const size_t idx = (size < sizeof (void *)) ? 0 : (size - sizeof (void *));
-
-    /* get the pool directly, if possible, for great speed */
-    if (interp->mem_pools->num_attribs > idx)
-        pool = interp->mem_pools->attrib_pools[idx];
-
-    /* otherwise create it */
-    if (!pool)
-        pool = Parrot_gc_get_attribute_pool(interp, interp->mem_pools, size);
-
-    return Parrot_gc_get_attributes_from_pool(interp, pool);
+    return interp->gc_sys->allocate_fixed_size_storage(interp, size);
 }
 
 /*
@@ -906,40 +855,12 @@
 Parrot_gc_free_fixed_size_storage(PARROT_INTERP, size_t size, ARGMOD(void *data))
 {
     ASSERT_ARGS(Parrot_gc_free_fixed_size_storage)
-
-    const size_t item_size = size < sizeof (void *) ? sizeof (void *) : size;
-    const size_t idx   = size - sizeof (void *);
-    PMC_Attribute_Pool ** const pools = interp->mem_pools->attrib_pools;
-    Parrot_gc_free_attributes_from_pool(interp, pools[idx], data);
+    interp->gc_sys->free_fixed_size_storage(interp, size, data);
 }
 
 
 /*
 
-=item C<static void Parrot_gc_free_attributes_from_pool(PARROT_INTERP,
-PMC_Attribute_Pool *pool, void *data)>
-
-Frees a fixed-size data item back to the pool for later reallocation.  Private
-to this file.
-
-*/
-
-static void
-Parrot_gc_free_attributes_from_pool(PARROT_INTERP,
-    ARGMOD(PMC_Attribute_Pool *pool),
-    ARGMOD(void *data))
-{
-    ASSERT_ARGS(Parrot_gc_free_attributes_from_pool)
-    PMC_Attribute_Free_List * const item = (PMC_Attribute_Free_List *)data;
-
-    item->next      = pool->free_list;
-    pool->free_list = item;
-
-    pool->num_free_objects++;
-}
-
-/*
-
 =item C<void Parrot_gc_mark_and_sweep(PARROT_INTERP, UINTVAL flags)>
 
 Calls the configured garbage collector to find and reclaim unused

Modified: branches/gc_encapsulate_part2/src/gc/gc_ms.c
==============================================================================
--- branches/gc_encapsulate_part2/src/gc/gc_ms.c	Tue Feb  9 10:44:05 2010	(r43804)
+++ branches/gc_encapsulate_part2/src/gc/gc_ms.c	Tue Feb  9 12:27:14 2010	(r43805)
@@ -103,6 +103,15 @@
     Parrot_gc_trace_type trace)
         __attribute__nonnull__(1);
 
+static void Parrot_gc_free_attributes_from_pool(PARROT_INTERP,
+    ARGMOD(PMC_Attribute_Pool *pool),
+    ARGMOD(void *data))
+        __attribute__nonnull__(1)
+        __attribute__nonnull__(2)
+        __attribute__nonnull__(3)
+        FUNC_MODIFIES(*pool)
+        FUNC_MODIFIES(*data);
+
 #define ASSERT_ARGS_gc_ms_add_free_object __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(mem_pools) \
     , PARROT_ASSERT_ARG(pool) \
@@ -143,6 +152,11 @@
     , PARROT_ASSERT_ARG(arg))
 #define ASSERT_ARGS_gc_ms_trace_active_PMCs __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp))
+#define ASSERT_ARGS_Parrot_gc_free_attributes_from_pool \
+     __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp) \
+    , PARROT_ASSERT_ARG(pool) \
+    , PARROT_ASSERT_ARG(data))
 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
 /* HEADERIZER END: static */
 
@@ -178,11 +192,17 @@
     interp->gc_sys->finalize_gc_system = NULL;
     interp->gc_sys->init_pool          = gc_ms_pool_init;
 
-    interp->gc_sys->allocate_pmc_header = gc_ms_allocate_pmc_header;
-    interp->gc_sys->free_pmc_header     = gc_ms_free_pmc_header;
+    interp->gc_sys->allocate_pmc_header     = gc_ms_allocate_pmc_header;
+    interp->gc_sys->free_pmc_header         = gc_ms_free_pmc_header;
+
+    interp->gc_sys->allocate_string_header  = gc_ms_allocate_string_header;
+    interp->gc_sys->free_string_header      = gc_ms_free_string_header;
+
+    interp->gc_sys->allocate_pmc_attributes = gc_ms_allocate_pmc_attributes;
+    interp->gc_sys->free_pmc_attributes     = gc_ms_free_pmc_attributes;
 
-    interp->gc_sys->allocate_string_header = gc_ms_allocate_string_header;
-    interp->gc_sys->free_string_header     = gc_ms_free_string_header;
+    interp->gc_sys->allocate_fixed_size_storage = gc_ms_allocate_fixed_size_storage;
+    interp->gc_sys->free_fixed_size_storage     = gc_ms_free_fixed_size_storage;
 
     initialize_var_size_pools(interp, interp->mem_pools);
     initialize_fixed_size_pools(interp, interp->mem_pools);
@@ -380,6 +400,142 @@
     }
 }
 
+
+/*
+
+=item C<void * gc_ms_allocate_pmc_attributes(PARROT_INTERP, PMC *pmc)>
+
+Allocates a new attribute structure for a PMC if it has the auto_attrs flag
+set.
+
+=cut
+
+*/
+
+PARROT_CANNOT_RETURN_NULL
+void *
+gc_ms_allocate_pmc_attributes(PARROT_INTERP, ARGMOD(PMC *pmc))
+{
+    ASSERT_ARGS(Parrot_gc_allocate_pmc_attributes)
+    const size_t attr_size = pmc->vtable->attr_size;
+#if GC_USE_FIXED_SIZE_ALLOCATOR
+    PMC_Attribute_Pool * const pool = Parrot_gc_get_attribute_pool(interp,
+            interp->mem_pools, attr_size);
+    void * const attrs = Parrot_gc_get_attributes_from_pool(interp, pool);
+    memset(attrs, 0, attr_size);
+    PMC_data(pmc) = attrs;
+    return attrs;
+#else
+    void * const data =  mem_sys_allocate_zeroed(attr_size);
+    PMC_data(pmc) = data;
+    return data;
+#endif
+}
+
+/*
+
+=item C<void gc_ms_free_pmc_attributes(PARROT_INTERP, PMC *pmc)>
+
+Deallocates an attibutes structure from a PMC if it has the auto_attrs
+flag set.
+
+*/
+
+void
+gc_ms_free_pmc_attributes(PARROT_INTERP, ARGMOD(PMC *pmc))
+{
+    ASSERT_ARGS(Parrot_gc_free_pmc_attributes)
+    void * const data = PMC_data(pmc);
+
+    if (data) {
+
+#if GC_USE_FIXED_SIZE_ALLOCATOR
+        const size_t attr_size = pmc->vtable->attr_size;
+        const size_t item_size = attr_size < sizeof (void *) ? sizeof (void *) : attr_size;
+        PMC_Attribute_Pool ** const pools = interp->mem_pools->attrib_pools;
+        const size_t idx = item_size - sizeof (void *);
+        Parrot_gc_free_attributes_from_pool(interp, pools[idx], data);
+#else
+        mem_sys_free(PMC_data(pmc));
+        PMC_data(pmc) = NULL;
+#endif
+    }
+}
+
+/*
+
+=item C<static void Parrot_gc_free_attributes_from_pool(PARROT_INTERP,
+PMC_Attribute_Pool *pool, void *data)>
+
+Frees a fixed-size data item back to the pool for later reallocation.  Private
+to this file.
+
+*/
+
+static void
+Parrot_gc_free_attributes_from_pool(PARROT_INTERP,
+    ARGMOD(PMC_Attribute_Pool *pool),
+    ARGMOD(void *data))
+{
+    ASSERT_ARGS(Parrot_gc_free_attributes_from_pool)
+    PMC_Attribute_Free_List * const item = (PMC_Attribute_Free_List *)data;
+
+    item->next      = pool->free_list;
+    pool->free_list = item;
+
+    pool->num_free_objects++;
+}
+
+/*
+
+=item C<void * gc_ms_allocate_fixed_size_storage(PARROT_INTERP, size_t size)>
+
+Allocates a fixed-size chunk of memory for use. This memory is not manually
+managed and needs to be freed with C<Parrot_gc_free_fixed_size_storage>
+
+*/
+
+PARROT_CANNOT_RETURN_NULL
+void *
+gc_ms_allocate_fixed_size_storage(PARROT_INTERP, size_t size)
+{
+    ASSERT_ARGS(Parrot_gc_allocate_fixed_size_storage)
+    PMC_Attribute_Pool *pool = NULL;
+    const size_t idx = (size < sizeof (void *)) ? 0 : (size - sizeof (void *));
+
+    /* get the pool directly, if possible, for great speed */
+    if (interp->mem_pools->num_attribs > idx)
+        pool = interp->mem_pools->attrib_pools[idx];
+
+    /* otherwise create it */
+    if (!pool)
+        pool = Parrot_gc_get_attribute_pool(interp, interp->mem_pools, size);
+
+    return Parrot_gc_get_attributes_from_pool(interp, pool);
+}
+
+/*
+
+=item C<void gc_ms_free_fixed_size_storage(PARROT_INTERP, size_t size, void
+*data)>
+
+Manually deallocates fixed size storage allocated with
+C<Parrot_gc_allocate_fixed_size_storage>
+
+*/
+
+void
+gc_ms_free_fixed_size_storage(PARROT_INTERP, size_t size, ARGMOD(void *data))
+{
+    ASSERT_ARGS(Parrot_gc_free_fixed_size_storage)
+
+    const size_t item_size = size < sizeof (void *) ? sizeof (void *) : size;
+    const size_t idx   = size - sizeof (void *);
+    PMC_Attribute_Pool ** const pools = interp->mem_pools->attrib_pools;
+    Parrot_gc_free_attributes_from_pool(interp, pools[idx], data);
+}
+
+
 /*
 
 =item C<static int gc_ms_trace_active_PMCs(PARROT_INTERP, Parrot_gc_trace_type

Modified: branches/gc_encapsulate_part2/src/gc/gc_private.h
==============================================================================
--- branches/gc_encapsulate_part2/src/gc/gc_private.h	Tue Feb  9 10:44:05 2010	(r43804)
+++ branches/gc_encapsulate_part2/src/gc/gc_private.h	Tue Feb  9 12:27:14 2010	(r43805)
@@ -111,6 +111,13 @@
     STRING* (*allocate_string_header)(PARROT_INTERP, UINTVAL flags);
     void    (*free_string_header)(PARROT_INTERP, STRING*);
 
+    void* (*allocate_pmc_attributes)(PARROT_INTERP, PMC *);
+    void (*free_pmc_attributes)(PARROT_INTERP, PMC *);
+
+    void* (*allocate_fixed_size_storage)(PARROT_INTERP, size_t size);
+    void (*free_fixed_size_storage)(PARROT_INTERP, size_t size, void *);
+
+
     /*Function hooks that GC systems can CHOOSE to provide if they need them
      *These will be called via the GC API functions Parrot_gc_func_name
      *e.g. read barrier && write barrier hooks can go here later ...*/
@@ -537,9 +544,43 @@
 /* HEADERIZER BEGIN: src/gc/gc_ms.c */
 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
 
+PARROT_CANNOT_RETURN_NULL
+void * gc_ms_allocate_fixed_size_storage(PARROT_INTERP, size_t size)
+        __attribute__nonnull__(1);
+
+PARROT_CANNOT_RETURN_NULL
+void * gc_ms_allocate_pmc_attributes(PARROT_INTERP, ARGMOD(PMC *pmc))
+        __attribute__nonnull__(1)
+        __attribute__nonnull__(2)
+        FUNC_MODIFIES(*pmc);
+
+void gc_ms_free_fixed_size_storage(PARROT_INTERP,
+    size_t size,
+    ARGMOD(void *data))
+        __attribute__nonnull__(1)
+        __attribute__nonnull__(3)
+        FUNC_MODIFIES(*data);
+
+void gc_ms_free_pmc_attributes(PARROT_INTERP, ARGMOD(PMC *pmc))
+        __attribute__nonnull__(1)
+        __attribute__nonnull__(2)
+        FUNC_MODIFIES(*pmc);
+
 void Parrot_gc_ms_init(PARROT_INTERP)
         __attribute__nonnull__(1);
 
+#define ASSERT_ARGS_gc_ms_allocate_fixed_size_storage \
+     __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp))
+#define ASSERT_ARGS_gc_ms_allocate_pmc_attributes __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp) \
+    , PARROT_ASSERT_ARG(pmc))
+#define ASSERT_ARGS_gc_ms_free_fixed_size_storage __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp) \
+    , PARROT_ASSERT_ARG(data))
+#define ASSERT_ARGS_gc_ms_free_pmc_attributes __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp) \
+    , PARROT_ASSERT_ARG(pmc))
 #define ASSERT_ARGS_Parrot_gc_ms_init __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp))
 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */


More information about the parrot-commits mailing list