Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 263 lines (179 sloc) 5.626 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
/*
Copyright (C) 2001-2009, Parrot Foundation.

=head1 NAME

src/vtables.c

=head1 DESCRIPTION

Functions to build and manipulate vtables

=head2 Functions

=over 4

=cut

*/

#include "parrot/parrot.h"

/* This function is defined in the auto-generated file core_pmcs.c */
/* XXX Get it into some public place */
extern void Parrot_gbl_initialize_core_pmcs(PARROT_INTERP, int pass);

/* HEADERIZER HFILE: include/parrot/vtables.h */

/*

=item C<VTABLE * Parrot_vtbl_new_vtable(PARROT_INTERP)>

Creates and returns a pointer to the new C<VTABLE>.

=cut

*/

PARROT_EXPORT
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
VTABLE *
Parrot_vtbl_new_vtable(SHIM_INTERP)
{
    ASSERT_ARGS(Parrot_vtbl_new_vtable)
    return mem_internal_allocate_zeroed_typed(VTABLE);
}

/*

=item C<VTABLE * Parrot_vtbl_clone_vtable(PARROT_INTERP, const VTABLE
*base_vtable)>

Clones C<*base_vtable> and returns a pointer to the new C<VTABLE>.

=cut

*/

PARROT_EXPORT
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
VTABLE *
Parrot_vtbl_clone_vtable(PARROT_INTERP, ARGIN(const VTABLE *base_vtable))
{
    ASSERT_ARGS(Parrot_vtbl_clone_vtable)
    VTABLE * const new_vtable = mem_internal_allocate_typed(VTABLE);

    STRUCT_COPY(new_vtable, base_vtable);

    /* when called from global PMC initialization, not all vtables have isa_hash
* when called at runtime, they do */
    if (base_vtable->isa_hash) {
        new_vtable->isa_hash = Parrot_hash_new(interp);
        Parrot_hash_clone(interp, base_vtable->isa_hash, new_vtable->isa_hash);
    }


    return new_vtable;
}


/*

=item C<void Parrot_vtbl_destroy_vtable(PARROT_INTERP, VTABLE *vtable)>

Destroys C<*vtable>.

=cut

*/

PARROT_EXPORT
void
Parrot_vtbl_destroy_vtable(PARROT_INTERP, ARGFREE_NOTNULL(VTABLE *vtable))
{
    ASSERT_ARGS(Parrot_vtbl_destroy_vtable)
    /* We sometimes get a type number allocated without any corresponding
* vtable. E.g. if you load perl_group, perlscalar is this way. */

    if (vtable->ro_variant_vtable) {
        VTABLE * const ro_vtable = vtable->ro_variant_vtable;

        if (ro_vtable->isa_hash) {
            Parrot_hash_destroy(interp, ro_vtable->isa_hash);
            if (ro_vtable->isa_hash == vtable->isa_hash)
                vtable->isa_hash = NULL;

            ro_vtable->isa_hash = NULL;
        }

        mem_internal_free(ro_vtable);
        vtable->ro_variant_vtable = NULL;
    }

    if (vtable->isa_hash) {
        Parrot_hash_destroy(interp, vtable->isa_hash);
        vtable->isa_hash = NULL;
    }

    mem_internal_free(vtable);
}

/*

=item C<void Parrot_vtbl_alloc_vtables(PARROT_INTERP)>

Allocate memory for the vtables for all known classes (PMC types).

=cut

*/

void
Parrot_vtbl_alloc_vtables(PARROT_INTERP)
{
    ASSERT_ARGS(Parrot_vtbl_alloc_vtables)
    interp->vtables = mem_internal_allocate_n_zeroed_typed(PARROT_MAX_CLASSES, VTABLE *);
    interp->n_vtable_max = enum_class_core_max;
    interp->n_vtable_alloced = PARROT_MAX_CLASSES - 1;
}

/*

=item C<void Parrot_vtbl_realloc_vtables(PARROT_INTERP)>

Reallocate memory for vtables, increasing the number of vtables by 16.

=cut

*/

void
Parrot_vtbl_realloc_vtables(PARROT_INTERP)
{
    ASSERT_ARGS(Parrot_vtbl_realloc_vtables)
    /* 16 bigger seems reasonable, though it's only a pointer
table and we could get bigger without blowing much memory
*/
    const INTVAL new_max = interp->n_vtable_alloced + 16;
    const INTVAL new_size = new_max * sizeof (VTABLE *);
    const INTVAL old_size = interp->n_vtable_max * sizeof (VTABLE *);

    /* arrays start at zero, but we compare type numbers starting at 1 */
    interp->n_vtable_alloced = new_max - 1;
    interp->vtables = (VTABLE **)mem_internal_realloc_zeroed(
        interp->vtables, new_size, old_size);
}

/*

=item C<void Parrot_vtbl_free_vtables(PARROT_INTERP)>

Free memory allocated for the vtables. Each vtable is destroyed
through its destructor Parrot_vtbl_destroy_vtable, after which the list
of pointers to these vtables is freed.

=cut

*/

void
Parrot_vtbl_free_vtables(PARROT_INTERP)
{
    ASSERT_ARGS(Parrot_vtbl_free_vtables)
    int i;

    for (i = 0; i < interp->n_vtable_max; ++i)
        Parrot_vtbl_destroy_vtable(interp, interp->vtables[i]);

    mem_internal_free(interp->vtables);
}

/*

=item C<void Parrot_vtbl_mark_vtables(PARROT_INTERP)>

Mark all vtables as being alive for the garbage collector.

=cut

*/

void
Parrot_vtbl_mark_vtables(PARROT_INTERP)
{
    ASSERT_ARGS(Parrot_vtbl_mark_vtables)
    INTVAL i;

    for (i = 1; i < interp->n_vtable_max; ++i) {
        const VTABLE * const vtable = interp->vtables[i];

        /* XXX dynpmc groups have empty slots for abstract objects */
        if (!vtable)
            continue;

        Parrot_gc_mark_PMC_alive(interp, vtable->mro);
        Parrot_gc_mark_PMC_alive(interp, vtable->_namespace);
        Parrot_gc_mark_STRING_alive(interp, vtable->whoami);
        Parrot_gc_mark_STRING_alive(interp, vtable->provides_str);
        Parrot_gc_mark_PMC_alive(interp, vtable->pmc_class);
    }
}

/*

=item C<void Parrot_vtbl_initialize_core_vtables(PARROT_INTERP)>

Initialize vtables for the core PMCs.

=cut

*/

PARROT_EXPORT
void
Parrot_vtbl_initialize_core_vtables(PARROT_INTERP)
{
    ASSERT_ARGS(Parrot_vtbl_initialize_core_vtables)

    if (! interp->vtables) {
        Parrot_vtbl_alloc_vtables(interp);
        Parrot_gbl_initialize_core_pmcs(interp, 0);
    }
}

/*

=back

=head1 SEE ALSO

F<include/parrot/vtables.h>

=cut

*/

/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/
Something went wrong with that request. Please try again.