Skip to content

Commit 827ee27

Browse files
committed
Require rb_during_gc() and remove in_gc flag of registered objects
This function is available in all supported ruby versions and it is enough to detect situations where calling overloaded function in ruby is not allowed.
1 parent 0edea9c commit 827ee27

6 files changed

Lines changed: 6 additions & 69 deletions

File tree

‎ext/fox16_c/FXRbObjRegistry.cpp‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ FXRbObjRegistry::FXRbObjRegistry(){
3131
const char * FXRbObjRegistry::safe_rb_obj_classname(VALUE obj)
3232
{
3333
int tdata = TYPE(obj)==T_DATA;
34-
if( (tdata && IsInGC(DATA_PTR(obj)))
35-
#ifdef HAVE_RB_DURING_GC
36-
|| rb_during_gc()
37-
#endif
38-
){
34+
if (rb_during_gc()) {
3935
/* It's not safe to call rb_obj_classname() during GC.
4036
* Return dummy value in this case. */
4137
return "during GC";
@@ -56,7 +52,6 @@ VALUE FXRbObjRegistry::NewBorrowedObj(void *ptr,swig_type_info* ty){
5652
FXTRACE((1,"FXRbNewPointerObj(foxObj=%p) => rubyObj=%p (%s)\n",ptr,(void *)obj,safe_rb_obj_classname(obj)));
5753
desc->obj = obj;
5854
desc->type = borrowed;
59-
desc->in_gc = false;
6055
int overwritten = st_insert(FXRuby_Objects,reinterpret_cast<st_data_t>(ptr),reinterpret_cast<st_data_t>(desc));
6156
FXASSERT(!overwritten);
6257
return obj;
@@ -91,7 +86,6 @@ void FXRbObjRegistry::RegisterRubyObj(VALUE rubyObj,const void* foxObj) {
9186
if(FXMALLOC(&desc,ObjDesc,1)){
9287
desc->obj = rubyObj;
9388
desc->type = own;
94-
desc->in_gc = false;
9589
int overwritten = st_insert(FXRuby_Objects,reinterpret_cast<st_data_t>(const_cast<void*>(foxObj)),reinterpret_cast<st_data_t>(desc));
9690
FXASSERT(!overwritten);
9791
} else {
@@ -148,29 +142,5 @@ bool FXRbObjRegistry::IsBorrowed(void* ptr){
148142
}
149143
}
150144

151-
bool FXRbObjRegistry::SetInGC(const void* ptr, bool enabled){
152-
FXASSERT(ptr!=0);
153-
ObjDesc *desc;
154-
if(st_lookup(FXRuby_Objects,reinterpret_cast<st_data_t>(ptr),reinterpret_cast<st_data_t *>(&desc))!=0){
155-
desc->in_gc=enabled;
156-
return enabled;
157-
}
158-
return false;
159-
}
160-
161-
bool FXRbObjRegistry::IsInGC(const void* ptr){
162-
FXASSERT(ptr!=0);
163-
ObjDesc *desc;
164-
165-
#ifdef HAVE_RB_DURING_GC
166-
if( rb_during_gc() ){
167-
return true;
168-
}
169-
#endif
170-
if(st_lookup(FXRuby_Objects,reinterpret_cast<st_data_t>(ptr),reinterpret_cast<st_data_t *>(&desc))!=0){
171-
return desc->in_gc;
172-
}
173-
return false;
174-
}
175145

176146
FXRbObjRegistry FXRbObjRegistry::main;

‎ext/fox16_c/FXRuby.cpp‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ bool FXRbIsBorrowed(void* ptr){
8787
return FXRbObjRegistry::main.IsBorrowed(ptr);
8888
}
8989

90-
bool FXRbSetInGC(const void* ptr, bool enabled){
91-
return FXRbObjRegistry::main.SetInGC(ptr, enabled);
92-
}
93-
9490
bool FXRbIsInGC(const void* ptr){
95-
return FXRbObjRegistry::main.IsInGC(ptr);
91+
if( rb_during_gc() ){
92+
return true;
93+
}
94+
return false;
9695
}
9796

9897

@@ -1610,11 +1609,9 @@ void* FXRbConvertPtr(VALUE obj,swig_type_info* ty, int flags){
16101609
void *ptr;
16111610
int res = SWIG_ConvertPtr(obj,&ptr,ty,flags);
16121611
if( res == SWIG_OK ) return ptr;
1613-
#ifdef HAVE_RB_DURING_GC
16141612
if( rb_during_gc() ){
16151613
rb_bug( "FXRbConvertPtr got wrong argument type rubyObj=%p", (void*)obj);
16161614
}
1617-
#endif
16181615
if( res == SWIG_ERROR_RELEASE_NOT_OWNED ){
16191616
rb_raise( rb_eTypeError, "clean and disown of non-owned object is not allowed: %" PRIsVALUE, obj);
16201617
}

‎ext/fox16_c/extconf.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def install
307307
have_func('rb_thread_call_without_gvl') &&
308308
have_func('rb_thread_call_with_gvl')
309309

310-
have_func('rb_during_gc')
310+
have_func('rb_during_gc') || raise("fxruby requires rb_during_gc but it is not available")
311311
end
312312

313313
# This directive processes the "--with-fox-include" and "--with-fox-lib"

‎ext/fox16_c/include/FXRbObjRegistry.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class FXRbObjRegistry {
5454
struct ObjDesc {
5555
VALUE obj;
5656
enum ObjType type;
57-
bool in_gc;
5857
};
5958

6059
const char * safe_rb_obj_classname(VALUE obj);
@@ -89,9 +88,6 @@ class FXRbObjRegistry {
8988
*/
9089
bool IsBorrowed(void* ptr);
9190

92-
bool SetInGC(const void* ptr, bool enabled);
93-
bool IsInGC(const void* ptr);
94-
9591
static FXRbObjRegistry main;
9692
};
9793

‎ext/fox16_c/include/FXRuby.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ VALUE showHelper(VALUE self, int argc, VALUE *argv, TYPE *p, swig_type_info *typ
6666
VALUE FXRbNewPointerObj(void *ptr, swig_type_info *typeinfo);
6767
VALUE FXRbNewPointerObjCb(void *ptr, swig_type_info *typeinfo);
6868
bool FXRbIsBorrowed(void* ptr);
69-
bool FXRbSetInGC(const void* ptr, bool enabled);
7069
bool FXRbIsInGC(const void* ptr);
7170

7271
// Wrapper around SWIG_TypeQuery()
@@ -407,7 +406,6 @@ template<class TYPE>
407406
void FXRbCallVoidMethod_gvlcb(FXObject* recv,const char *func, TYPE& arg){
408407
VALUE obj=FXRbGetRubyObj(recv,false);
409408
FXASSERT(!NIL_P(obj));
410-
FXASSERT(!FXRbIsInGC(recv));
411409
rb_funcall(obj,rb_intern(func),1,to_ruby_cb(arg));
412410
}
413411

@@ -422,7 +420,6 @@ template<class TYPE>
422420
void FXRbCallVoidMethod_gvlcb(const FXObject* recv, const char *func, TYPE& arg){
423421
VALUE obj=FXRbGetRubyObj(recv,false);
424422
FXASSERT(!NIL_P(obj));
425-
FXASSERT(!FXRbIsInGC(recv));
426423
rb_funcall(obj,rb_intern(func),1,to_ruby_cb(arg));
427424
}
428425

‎ext/fox16_c/markfuncs.cpp‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ void FXRbObject::markfunc(FXObject* obj){
5454
FXTRACE((100,"%s::markfunc(%p)\n",obj?obj->getClassName():"FXRbObject",obj));
5555
}
5656

57-
static void FXRbSetInGCParentsRecursive(FXWindow *window, bool enabled){
58-
FXRbSetInGC( window, true );
59-
if(window->getParent()) FXRbSetInGCParentsRecursive( window->getParent(), enabled );
60-
}
61-
62-
static void FXRbSetInGCChildrenRecursive(FXWindow *window, bool enabled){
63-
FXRbSetInGC( window, true );
64-
for(FXWindow* child=window->getFirst(); child; child=child->getNext()){
65-
FXRbSetInGCChildrenRecursive( child, enabled );
66-
}
67-
}
68-
6957

7058
void FXRbObject::freefunc(FXObject* self){
7159
if(self!=0){
@@ -79,17 +67,6 @@ void FXRbObject::freefunc(FXObject* self){
7967
FXASSERT(classname!=0);
8068
FXASSERT(strlen(classname)>3);
8169
if(classname[0]=='F' && classname[1]=='X' && classname[2]=='R' && classname[3]=='b'){
82-
// FXWindow destructor calls recalc() and changeFocus() of it's parent window.
83-
// Since these methods are routed back to Ruby code, but calling Ruby code from
84-
// GC isn't a good idea, we mark the parent window as "in_gc", so that it will
85-
// ignore recalc() and changeFocus() calls completely.
86-
// The parent window should also be scheduled to be free'd. In the other case,
87-
// the child window would have been marked as used.
88-
if(self->isMemberOf(FXMETACLASS(FXWindow))){
89-
FXWindow *window = dynamic_cast<FXWindow*>(self);
90-
FXRbSetInGCParentsRecursive( window, true );
91-
FXRbSetInGCChildrenRecursive( window, true );
92-
}
9370
delete self;
9471
}
9572
else{

0 commit comments

Comments
 (0)