/
managedstruct.pmc
119 lines (76 loc) · 1.75 KB
/
managedstruct.pmc
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
/*
Copyright (C) 2001-2006, The Perl Foundation.
$Id$
=head1 DESCRIPTION
src/pmc/managedstruct.pmc - Memory-managed C struct
=head1 DESCRIPTION
C<ManagedStruct> extends C<UnManagedStruct> to provide a class to hold C
C<struct> values that Parrot is responsible for disposing of.
=head2 Methods
=over 4
=cut
*/
#include "parrot/parrot.h"
pmclass ManagedStruct extends UnManagedStruct need_ext {
/*
=item C<void init()>
Initializes an empty struct.
=cut
*/
void init() {
PObj_active_destroy_SET(SELF);
PMC_pmc_val(SELF) = NULL;
PMC_int_val(SELF) = 0;
}
/*
=item C<void init_pmc(PMC *value)>
Initializes the struct with C<*value>.
=cut
*/
void init_pmc(PMC* value) {
SELF.init();
SUPER(value);
}
/*
=item C<void destroy()>
Destroys the struct, freeing the allocated memory.
=cut
*/
void destroy() {
if (PMC_data(SELF))
mem_sys_free(PMC_data(SELF));
}
/*
=item C<void set_integer_native(INTVAL value)>
(Re)allocates C<value> bytes for the struct.
=cut
*/
void set_integer_native(INTVAL value) {
if (PMC_data(SELF) && (! value)) {
mem_sys_free(PMC_data(SELF));
PMC_data(SELF) = NULL;
PMC_int_val(SELF) = 0;
}
else if (value && (! PMC_data(SELF))) {
PMC_data(SELF) = mem_sys_allocate_zeroed(value);
PMC_int_val(SELF) = value;
}
else if (value && PMC_data(SELF)) {
PMC_data(SELF) = mem_sys_realloc(PMC_data(SELF), value);
PMC_int_val(SELF) = value;
}
return;
}
}
/*
=back
=head1 HISTORY
Initial revision by sean 2002/08/04.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/