… | |
… | |
101 | |
101 | |
102 | unordered_vector<attachable *> attachable::mortals; |
102 | unordered_vector<attachable *> attachable::mortals; |
103 | |
103 | |
104 | attachable::~attachable () |
104 | attachable::~attachable () |
105 | { |
105 | { |
106 | assert (!(flags & F_BORROWED)); |
106 | assert (!self); |
107 | } |
107 | } |
108 | |
108 | |
109 | int |
109 | int |
110 | attachable::refcnt_cnt () const |
110 | attachable::refcnt_cnt () const |
111 | { |
111 | { |
… | |
… | |
117 | { |
117 | { |
118 | if (self |
118 | if (self |
119 | && SvREFCNT (self) == 1 |
119 | && SvREFCNT (self) == 1 |
120 | && !HvTOTALKEYS (self)) |
120 | && !HvTOTALKEYS (self)) |
121 | { |
121 | { |
122 | flags &= ~F_BORROWED; |
|
|
123 | refcnt_inc (); |
122 | refcnt_inc (); |
124 | SvREFCNT_dec ((SV *)self); |
123 | SvREFCNT_dec ((SV *)self); |
125 | } |
124 | } |
126 | } |
125 | } |
127 | |
126 | |
128 | // check wether the object really is dead |
127 | // check wether the object really is dead |
129 | void |
128 | void |
130 | attachable::do_check () |
129 | attachable::do_check () |
131 | { |
130 | { |
132 | if (refcnt > 0) |
131 | if (refcnt > 0 || refcnt_cnt () > 0) |
133 | return; |
132 | return; |
134 | |
133 | |
135 | optimise (); |
134 | destroy (); |
136 | |
135 | |
137 | if (self) |
136 | if (self) |
138 | { |
137 | { |
139 | if (refcnt + SvREFCNT (self) > 0) |
138 | hv_clear (self); |
140 | return; |
139 | sv_unmagic ((SV *)self, PERL_MAGIC_ext); |
141 | |
|
|
142 | if (flags & F_BORROWED) |
|
|
143 | { |
|
|
144 | flags &= ~F_BORROWED; |
|
|
145 | refcnt_inc (); |
|
|
146 | SvREFCNT_dec (self); |
140 | SvREFCNT_dec (self); |
147 | } |
141 | self = 0; |
148 | |
|
|
149 | if (self) |
|
|
150 | if (refcnt + SvREFCNT (self) > 0) |
|
|
151 | return; |
|
|
152 | } |
142 | } |
153 | |
|
|
154 | if (refcnt > 0) |
|
|
155 | return; |
|
|
156 | |
|
|
157 | destroy (); |
|
|
158 | } |
143 | } |
159 | |
144 | |
160 | void |
145 | void |
161 | attachable::do_destroy () |
146 | attachable::do_destroy () |
162 | { |
147 | { |
… | |
… | |
231 | |
216 | |
232 | static int |
217 | static int |
233 | attachable_free (pTHX_ SV *sv, MAGIC *mg) |
218 | attachable_free (pTHX_ SV *sv, MAGIC *mg) |
234 | { |
219 | { |
235 | attachable *at = (attachable *)mg->mg_ptr; |
220 | attachable *at = (attachable *)mg->mg_ptr; |
236 | assert (!(at->flags & attachable::F_BORROWED)); |
|
|
237 | at->self = 0; |
221 | at->self = 0; |
238 | // next line makes sense, but most objects still have refcnt 0 by default |
222 | // next line makes sense, but most objects still have refcnt 0 by default |
239 | //at->refcnt_chk (); |
223 | //at->refcnt_chk (); |
240 | return 0; |
224 | return 0; |
241 | } |
225 | } |
… | |
… | |
252 | { |
236 | { |
253 | obj->self = newHV (); |
237 | obj->self = newHV (); |
254 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
238 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
255 | |
239 | |
256 | // borrow the refcnt from the object |
240 | // borrow the refcnt from the object |
257 | obj->flags |= attachable::F_BORROWED; |
241 | // it is important thta no refcnt_chk is being executed here |
258 | obj->refcnt_dec (); |
242 | obj->refcnt_dec (); |
259 | |
243 | |
260 | // now bless the object _once_ |
244 | // now bless the object _once_ |
261 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
245 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
262 | } |
246 | } |