… | |
… | |
99 | |
99 | |
100 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
100 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
101 | |
101 | |
102 | unordered_vector<attachable *> attachable::mortals; |
102 | unordered_vector<attachable *> attachable::mortals; |
103 | |
103 | |
104 | #if 0 |
|
|
105 | attachable *attachable::rc_first; |
|
|
106 | |
|
|
107 | attachable::attachable () |
|
|
108 | { |
|
|
109 | refcnt = 0; |
|
|
110 | rc_next = rc_first; |
|
|
111 | rc_first = this; |
|
|
112 | } |
|
|
113 | #endif |
|
|
114 | |
|
|
115 | attachable::~attachable () |
104 | attachable::~attachable () |
116 | { |
105 | { |
117 | assert (!(flags & F_BORROWED));//D//TODO//remove when stable |
106 | assert (!(flags & F_BORROWED)); |
118 | #if 0 |
107 | } |
119 | assert (!rc_next); |
108 | |
120 | assert (!refcnt); |
109 | void attachable::optimise () |
121 | #endif |
110 | { |
|
|
111 | if (self |
|
|
112 | && SvREFCNT (self) == 1 |
|
|
113 | && !HvTOTALKEYS (self)) |
|
|
114 | { |
|
|
115 | flags &= ~F_BORROWED; |
|
|
116 | refcnt_inc (); |
|
|
117 | SvREFCNT_dec ((SV *)self); |
|
|
118 | } |
122 | } |
119 | } |
123 | |
120 | |
124 | // check wether the object really is dead |
121 | // check wether the object really is dead |
125 | void |
122 | void |
126 | attachable::do_check () |
123 | attachable::do_check () |
127 | { |
124 | { |
128 | if (refcnt > 0) |
125 | if (refcnt > 0) |
129 | return; |
126 | return; |
130 | |
127 | |
131 | // try to unborrow the refcnt from perl |
128 | optimise (); |
132 | if (flags & F_BORROWED) |
129 | |
|
|
130 | if (self) |
133 | { |
131 | { |
134 | assert (self);//D//TODO//remove when stable |
132 | if (refcnt + SvREFCNT (self) > 0) |
|
|
133 | return; |
|
|
134 | |
|
|
135 | assert (flags & F_BORROWED); |
|
|
136 | |
135 | flags &= ~F_BORROWED; |
137 | flags &= ~F_BORROWED; |
136 | refcnt_inc (); |
138 | refcnt_inc (); |
137 | SvREFCNT_dec (self); |
139 | SvREFCNT_dec (self); |
|
|
140 | self = 0; |
138 | } |
141 | } |
139 | |
142 | |
140 | if (refcnt > 0 || self) |
143 | if (refcnt > 0) |
141 | return; |
144 | return; |
142 | |
145 | |
143 | destroy (); |
146 | destroy (); |
144 | } |
147 | } |
145 | |
148 | |
… | |
… | |
149 | invoke (EVENT_ATTACHABLE_DESTROY, DT_END); |
152 | invoke (EVENT_ATTACHABLE_DESTROY, DT_END); |
150 | |
153 | |
151 | if (self) |
154 | if (self) |
152 | hv_clear (self); |
155 | hv_clear (self); |
153 | |
156 | |
154 | //TODO: call generic destroy callback |
|
|
155 | mortals.push_back (this); |
157 | mortals.push_back (this); |
156 | } |
158 | } |
157 | |
159 | |
158 | void |
160 | void |
159 | attachable::destroy () |
161 | attachable::destroy () |
… | |
… | |
217 | |
219 | |
218 | static int |
220 | static int |
219 | attachable_free (pTHX_ SV *sv, MAGIC *mg) |
221 | attachable_free (pTHX_ SV *sv, MAGIC *mg) |
220 | { |
222 | { |
221 | attachable *at = (attachable *)mg->mg_ptr; |
223 | attachable *at = (attachable *)mg->mg_ptr; |
222 | assert (!(at->flags & attachable::F_BORROWED));//D//TODO//remove when stable |
224 | assert (!(at->flags & attachable::F_BORROWED)); |
223 | at->self = 0; |
225 | at->self = 0; |
224 | // next line makes sense, but most objects still have refcnt 0 by default |
226 | // next line makes sense, but most objects still have refcnt 0 by default |
225 | //at->refcnt_chk (); |
227 | //at->refcnt_chk (); |
226 | return 0; |
228 | return 0; |
227 | } |
229 | } |
… | |
… | |
240 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
242 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
241 | |
243 | |
242 | // borrow the refcnt from the object |
244 | // borrow the refcnt from the object |
243 | obj->flags |= attachable::F_BORROWED; |
245 | obj->flags |= attachable::F_BORROWED; |
244 | obj->refcnt_dec (); |
246 | obj->refcnt_dec (); |
|
|
247 | |
|
|
248 | // now bless the object _once_ |
|
|
249 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
245 | } |
250 | } |
246 | |
251 | else |
247 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
252 | return newRV_inc ((SV *)obj->self); |
248 | } |
253 | } |
249 | |
254 | |
250 | static void |
255 | static void |
251 | clearSVptr (SV *sv) |
256 | clearSVptr (SV *sv) |
252 | { |
257 | { |
… | |
… | |
513 | } |
518 | } |
514 | |
519 | |
515 | attach = 0; |
520 | attach = 0; |
516 | } |
521 | } |
517 | #endif |
522 | #endif |
518 | |
|
|
519 | void attachable::optimise () |
|
|
520 | { |
|
|
521 | if (self |
|
|
522 | && SvREFCNT (self) == 1 |
|
|
523 | && !HvTOTALKEYS (self)) |
|
|
524 | { |
|
|
525 | flags &= ~F_BORROWED; |
|
|
526 | refcnt_inc (); |
|
|
527 | SvREFCNT_dec ((SV *)self); |
|
|
528 | } |
|
|
529 | } |
|
|
530 | |
523 | |
531 | ///////////////////////////////////////////////////////////////////////////// |
524 | ///////////////////////////////////////////////////////////////////////////// |
532 | |
525 | |
533 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
526 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
534 | { |
527 | { |