* src/pbc_merge.c:
[parrot.git] / src / vtables.c
blobd2d8949c531fd853bdf2572c891daea04086cf69
1 /*
2 Copyright (C) 2001-2007, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/vtables.c - Functions to build and manipulate vtables
9 =head1 DESCRIPTION
11 =head2 Functions
13 =over 4
15 =cut
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>.
30 =cut
34 PARROT_API
35 PARROT_MALLOC
36 PARROT_CANNOT_RETURN_NULL
37 VTABLE *
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>.
49 =cut
53 PARROT_API
54 PARROT_MALLOC
55 PARROT_CANNOT_RETURN_NULL
56 VTABLE *
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);
71 return new_vtable;
77 =item C<void Parrot_destroy_vtable>
79 Destroys C<*vtable>.
81 =cut
85 PARROT_API
86 void
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).
122 =cut
126 void
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.
140 =cut
144 void
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.
168 =cut
172 void
173 parrot_free_vtables(PARROT_INTERP)
175 int i;
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.
189 =cut
193 void
194 mark_vtables(PARROT_INTERP)
196 INTVAL i;
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 */
202 if (!vtable)
203 continue;
205 if (vtable->mro)
206 pobject_lives(interp, (PObj *)vtable->mro);
207 if (vtable->_namespace)
208 pobject_lives(interp, (PObj *)vtable->_namespace);
209 if (vtable->whoami)
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);
219 * Local variables:
220 * c-file-style: "parrot"
221 * End:
222 * vim: expandtab shiftwidth=4: