… | |
… | |
107 | } |
107 | } |
108 | |
108 | |
109 | int |
109 | int |
110 | attachable::refcnt_cnt () const |
110 | attachable::refcnt_cnt () const |
111 | { |
111 | { |
112 | return refcnt + (self ? SvREFCNT (self) : 0); |
112 | return refcnt + (self ? SvREFCNT (self) - 1 : 0); |
113 | } |
113 | } |
114 | |
114 | |
115 | void |
115 | void |
116 | attachable::optimise () |
116 | attachable::optimise () |
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 | refcnt_inc (); |
|
|
123 | SvREFCNT_dec ((SV *)self); |
122 | SvREFCNT_dec ((SV *)self); |
|
|
123 | assert (!self); |
124 | } |
124 | } |
125 | } |
125 | } |
126 | |
126 | |
127 | // check wether the object really is dead |
127 | // check wether the object really is dead |
128 | void |
128 | void |
129 | attachable::do_check () |
129 | attachable::do_check () |
130 | { |
130 | { |
131 | if (refcnt > 0 || refcnt_cnt () > 0) |
131 | if (refcnt_cnt () > 0) |
132 | return; |
132 | return; |
133 | |
133 | |
134 | destroy (); |
134 | destroy (); |
135 | |
135 | |
136 | if (self) |
136 | if (self) |
… | |
… | |
173 | if (i >= mortals.size ()) |
173 | if (i >= mortals.size ()) |
174 | { |
174 | { |
175 | i = 0; |
175 | i = 0; |
176 | |
176 | |
177 | if (mortals.size () > 1000) |
177 | if (mortals.size () > 1000) |
178 | fprintf (stderr, "mortal queue size (%d) exceeds 1000.\n", mortals.size ()); |
178 | fprintf (stderr, "mortal queue size (%d) exceeds 1000.\n", (int)mortals.size ()); |
179 | |
179 | |
180 | break; |
180 | break; |
181 | } |
181 | } |
182 | |
182 | |
183 | attachable *obj = mortals [i]; |
183 | attachable *obj = mortals [i]; |
… | |
… | |
245 | |
245 | |
246 | if (!obj->self) |
246 | if (!obj->self) |
247 | { |
247 | { |
248 | obj->self = newHV (); |
248 | obj->self = newHV (); |
249 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
249 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
250 | |
|
|
251 | // borrow the refcnt from the object |
|
|
252 | // it is important thta no refcnt_chk is being executed here |
|
|
253 | obj->refcnt_dec (); |
|
|
254 | |
250 | |
255 | // now bless the object _once_ |
251 | // now bless the object _once_ |
256 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
252 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
257 | } |
253 | } |
258 | else |
254 | else |