2 Copyright (C) 2001-2007, The Perl Foundation.
7 src/vtables.c - Functions to build and manipulate vtables
19 #include "parrot/parrot.h"
20 #include "parrot/vtables.h"
22 /* HEADERIZER HFILE: include/parrot/vtables.h */
26 =item C<VTABLE * Parrot_new_vtable>
28 Creates and returns a pointer to the new C<VTABLE>.
36 PARROT_CANNOT_RETURN_NULL
38 Parrot_new_vtable(SHIM_INTERP
)
40 return mem_allocate_zeroed_typed(VTABLE
);
45 =item C<VTABLE * Parrot_clone_vtable>
47 Clones C<*base_vtable> and returns a pointer to the new C<VTABLE>.
55 PARROT_CANNOT_RETURN_NULL
57 Parrot_clone_vtable(PARROT_INTERP
, ARGIN(const VTABLE
*base_vtable
))
59 VTABLE
* const new_vtable
= mem_allocate_typed(VTABLE
);
61 STRUCT_COPY(new_vtable
, base_vtable
);
63 /* when called from global PMC initialization, not all vtables have isa_hash
64 * when called at runtime, they do */
65 if (base_vtable
->isa_hash
) {
66 parrot_new_hash(interp
, &new_vtable
->isa_hash
);
67 parrot_hash_clone(interp
, base_vtable
->isa_hash
, new_vtable
->isa_hash
);
77 =item C<void Parrot_destroy_vtable>
87 Parrot_destroy_vtable(PARROT_INTERP
, ARGMOD(VTABLE
*vtable
))
89 /* We sometimes get a type number allocated without any corresponding
90 * vtable. E.g. if you load perl_group, perlscalar is this way. */
91 PARROT_ASSERT(vtable
);
93 if (vtable
->ro_variant_vtable
) {
94 VTABLE
*ro_vtable
= vtable
->ro_variant_vtable
;
96 if (ro_vtable
->isa_hash
) {
97 parrot_hash_destroy(interp
, ro_vtable
->isa_hash
);
98 if (ro_vtable
->isa_hash
== vtable
->isa_hash
)
99 vtable
->isa_hash
= NULL
;
101 ro_vtable
->isa_hash
= NULL
;
104 mem_sys_free(ro_vtable
);
105 vtable
->ro_variant_vtable
= NULL
;
108 if (vtable
->isa_hash
) {
109 parrot_hash_destroy(interp
, vtable
->isa_hash
);
110 vtable
->isa_hash
= NULL
;
113 mem_sys_free(vtable
);
118 =item C<void parrot_alloc_vtables>
120 Allocate memory for the vtables for all known classes (PMC types).
127 parrot_alloc_vtables(PARROT_INTERP
)
129 interp
->vtables
= mem_allocate_n_zeroed_typed(PARROT_MAX_CLASSES
, VTABLE
*);
130 interp
->n_vtable_max
= enum_class_core_max
;
131 interp
->n_vtable_alloced
= PARROT_MAX_CLASSES
- 1;
136 =item C<void parrot_realloc_vtables>
138 Reallocate memory for vtables, increasing the number of vtables by 16.
145 parrot_realloc_vtables(PARROT_INTERP
)
147 /* 16 bigger seems reasonable, though it's only a pointer
148 table and we could get bigger without blowing much memory
150 const INTVAL new_max
= interp
->n_vtable_alloced
+ 16;
151 const INTVAL new_size
= new_max
* sizeof (VTABLE
*);
152 const INTVAL old_size
= interp
->n_vtable_max
* sizeof (VTABLE
*);
154 /* arrays start at zero, but we compare type numbers starting at 1 */
155 interp
->n_vtable_alloced
= new_max
- 1;
156 interp
->vtables
= (VTABLE
**)mem_sys_realloc_zeroed(
157 interp
->vtables
, new_size
, old_size
);
162 =item C<void parrot_free_vtables>
164 Free memory allocated for the vtables. Each vtable is destroyed
165 through its destructor Parrot_destroy_vtable, after which the list
166 of pointers to these vtables is freed.
173 parrot_free_vtables(PARROT_INTERP
)
177 for (i
= 1; i
< interp
->n_vtable_max
; i
++)
178 Parrot_destroy_vtable(interp
, interp
->vtables
[i
]);
180 mem_sys_free(interp
->vtables
);
185 =item C<void mark_vtables>
187 Mark all vtables as being alive for the garbage collector.
194 mark_vtables(PARROT_INTERP
)
198 for (i
= 1; i
< interp
->n_vtable_max
; i
++) {
199 const VTABLE
* const vtable
= interp
->vtables
[i
];
201 /* XXX dynpmc groups have empty slots for abstract objects */
206 pobject_lives(interp
, (PObj
*)vtable
->mro
);
207 if (vtable
->_namespace
)
208 pobject_lives(interp
, (PObj
*)vtable
->_namespace
);
210 pobject_lives(interp
, (PObj
*)vtable
->whoami
);
211 if (vtable
->provides_str
)
212 pobject_lives(interp
, (PObj
*)vtable
->provides_str
);
213 if (vtable
->pmc_class
)
214 pobject_lives(interp
, (PObj
*)vtable
->pmc_class
);
220 * c-file-style: "parrot"
222 * vim: expandtab shiftwidth=4: