[Kde-bindings] KDE/kdebindings/qtruby

Richard Dale Richard_Dale at tipitina.demon.co.uk
Fri Oct 20 19:17:18 UTC 2006


SVN commit 597568 by rdale:

* Reversed the decision to get rid of ALLOC, and replaced all mallocs
  with ALLOC and all callocs with ALLOCA_N
* Commented out some code that mapped an instance which was allocated
  in the C++ world onto the corresponding ruby instance. It doesn't
  work if the C++ address is reused and the Ruby side doesn't know
  about it.

CCMAIL: kde-bindings at kde.org



 M  +7 -0      ChangeLog  
 M  +22 -24    rubylib/qtruby/Qt.cpp  
 M  +1 -1      rubylib/qtruby/handlers.cpp  
 M  +11 -2     rubylib/qtruby/marshall_basetypes.h  


--- trunk/KDE/kdebindings/qtruby/ChangeLog #597567:597568
@@ -6,6 +6,13 @@
 	  some with malloc(). Now they are all created with malloc().
 	* Removed a call to ALLOC_N and replaced it will calloc().
 
+	* Reversed the decision to get rid of ALLOC, and replaced all mallocs
+	  with ALLOC and all callocs with ALLOCA_N
+	* Commented out some code that mapped an instance which was allocated
+	  in the C++ world onto the corresponding ruby instance. It doesn't
+	  work if the C++ address is reused and the Ruby side doesn't know
+	  about it.
+
 2006-10-19  Richard Dale  <rdale at foton.es>
 
 	* A Qt::DBusArgument can be obtained via a call to qVariantValue(), like
--- trunk/KDE/kdebindings/qtruby/rubylib/qtruby/Qt.cpp #597567:597568
@@ -142,7 +142,8 @@
 smokeruby_object * 
 alloc_smokeruby_object(bool allocated, Smoke * smoke, int classId, void * ptr)
 {
-    smokeruby_object * o = (smokeruby_object *) malloc(sizeof(smokeruby_object));
+    smokeruby_object * o = ALLOC(smokeruby_object);
+//    smokeruby_object * o = (smokeruby_object *) malloc(sizeof(smokeruby_object));
 	o->classId = classId;
 	o->smoke = smoke;
 	o->ptr = ptr;
@@ -153,7 +154,7 @@
 void
 free_smokeruby_object(smokeruby_object * o)
 {
-	free(o);
+	xfree(o);
 	return;
 }
 
@@ -210,9 +211,15 @@
 }
 
 VALUE getPointerObject(void *ptr) {
-	if (pointer_map[ptr] == 0) {
+	if (!pointer_map.contains(ptr)) {
+		if (do_debug & qtdb_gc) {
+			qWarning("getPointerObject %p -> nil", ptr);
+		}
 	    return Qnil;
 	} else {
+		if (do_debug & qtdb_gc) {
+			qWarning("getPointerObject %p -> %p", ptr, (void *) *(pointer_map[ptr]));
+		}
 		return *(pointer_map[ptr]);
 	}
 }
@@ -221,16 +228,16 @@
     void *ptr = o->smoke->cast(o->ptr, o->classId, classId);
     if(ptr != lastptr) {
 	lastptr = ptr;
-	if (pointer_map[ptr] != 0) {
+	if (pointer_map.contains(ptr)) {
 		VALUE * obj_ptr = pointer_map[ptr];
 		
 		if (do_debug & qtdb_gc) {
 			const char *className = o->smoke->classes[o->classId].className;
-			qWarning("unmapPointer (%s*)%p -> %p", className, ptr, obj_ptr);
+			qWarning("unmapPointer (%s*)%p -> %p size: %d", className, ptr, obj_ptr, pointer_map.size() - 1);
 		}
 	    
 		pointer_map.remove(ptr);
-		free((void*) obj_ptr);
+		xfree((void*) obj_ptr);
 	}
     }
     for(Smoke::Index *i = o->smoke->inheritanceList + o->smoke->classes[classId].parents;
@@ -248,12 +255,13 @@
 	
     if (ptr != lastptr) {
 		lastptr = ptr;
-		VALUE * obj_ptr = (VALUE *) malloc(sizeof(VALUE));
+		VALUE * obj_ptr = ALLOC(VALUE);
+//		VALUE * obj_ptr = (VALUE *) malloc(sizeof(VALUE));
 		memcpy(obj_ptr, &obj, sizeof(VALUE));
 		
 		if (do_debug & qtdb_gc) {
 			const char *className = o->smoke->classes[o->classId].className;
-			qWarning("mapPointer (%s*)%p -> %p", className, ptr, (void*)obj);
+			qWarning("mapPointer (%s*)%p -> %p size: %d", className, ptr, (void*)obj, pointer_map.size() + 1);
 		}
 	
 		pointer_map.insert(ptr, obj_ptr);
@@ -1527,7 +1535,7 @@
 		}
 	}
 		
-	VALUE * temp_stack = (VALUE *) calloc(argc+3, sizeof(VALUE));
+	VALUE * temp_stack = ALLOCA_N(VALUE, argc+3);
     temp_stack[0] = rb_str_new2("Qt");
     temp_stack[1] = rb_str_new2(methodName);
     temp_stack[2] = klass;
@@ -1561,7 +1569,6 @@
 				}
 
 				if (_current_method == -1) {
-					free(temp_stack);
 
 					// Check for property getter/setter calls
 					smokeruby_object *o = value_obj_info(self);
@@ -1610,7 +1617,6 @@
     MethodCall c(qt_Smoke, _current_method, self, temp_stack+4, argc-1);
     c.next();
     VALUE result = *(c.var());
-	free(temp_stack);
     return result;
 }
 
@@ -1619,7 +1625,7 @@
 {
 	VALUE result = Qnil;
 	char * methodName = rb_id2name(SYM2ID(argv[0]));
-	VALUE * temp_stack = (VALUE *) calloc(argc+3, sizeof(VALUE));
+	VALUE * temp_stack = ALLOCA_N(VALUE, argc+3);
     temp_stack[0] = rb_str_new2("Qt");
     temp_stack[1] = rb_str_new2(methodName);
     temp_stack[2] = klass;
@@ -1650,17 +1656,14 @@
 		if (rx->indexIn(methodName) == -1) {
 			// If an operator method hasn't been found as an instance method,
 			// then look for a class method - after 'op(self,a)' try 'self.op(a)' 
-	    	VALUE * method_stack = (VALUE *) calloc(argc - 1, sizeof(VALUE));
+	    	VALUE * method_stack = ALLOCA_N(VALUE, argc - 1);
 	    	method_stack[0] = argv[0];
 	    	for (int count = 1; count < argc - 1; count++) {
 				method_stack[count] = argv[count+1];
     		}
 			result = method_missing(argc-1, method_stack, argv[1]);
-			free(method_stack);
-			free(temp_stack);
 			return result;
 		} else {
-			free(temp_stack);
 			return rb_call_super(argc, argv);
 		}
     }
@@ -1668,7 +1671,6 @@
     MethodCall c(qt_Smoke, _current_method, Qnil, temp_stack+4, argc-1);
     c.next();
     result = *(c.var());
-	free(temp_stack);
     return result;
 }
 
@@ -1724,7 +1726,7 @@
 	VALUE klass = rb_funcall(self, rb_intern("class"), 0);
 	VALUE constructor_name = rb_str_new2("new");
 
-	VALUE * temp_stack = (VALUE *) calloc(argc+4, sizeof(VALUE));
+	VALUE * temp_stack = ALLOCA_N(VALUE, argc+4);
 
 	temp_stack[0] = rb_str_new2("Qt");
 	temp_stack[1] = constructor_name;
@@ -1750,7 +1752,6 @@
 	}
 
 	if (_current_method == -1) {
-		free(temp_stack);
 		// Another longjmp here..
 		rb_raise(rb_eArgError, "unresolved constructor call %s\n", rb_class2name(klass));
 	}
@@ -1773,7 +1774,6 @@
 	p->ptr = 0;
 	p->allocated = false;
 
-	free(temp_stack);
 	VALUE result = Data_Wrap_Struct(klass, smokeruby_mark, smokeruby_free, o);
 	mapObject(result, result);
 	// Off with a longjmp, never to return..
@@ -1785,7 +1785,7 @@
 VALUE
 new_qt(int argc, VALUE * argv, VALUE klass)
 {
-    VALUE * temp_stack = (VALUE *) calloc(argc + 1, sizeof(VALUE));
+    VALUE * temp_stack = ALLOCA_N(VALUE, argc + 1);
 	temp_stack[0] = rb_obj_alloc(klass);
 
 	for (int count = 0; count < argc; count++) {
@@ -1795,7 +1795,6 @@
 	VALUE result = rb_funcall2(qt_internal_module, rb_intern("try_initialize"), argc+1, temp_stack);
 	rb_obj_call_init(result, argc, argv);
 	
-	free(temp_stack);
 	return result;
 }
 
@@ -1806,13 +1805,12 @@
 
 	if (argc == 1 && TYPE(argv[0]) == T_ARRAY) {
 		// Convert '(ARGV)' to '(NUM, [$0]+ARGV)'
-		VALUE * local_argv = (VALUE *) calloc(argc + 1, sizeof(VALUE));
+		VALUE * local_argv = ALLOCA_N(VALUE, argc + 1);
 		VALUE temp = rb_ary_dup(argv[0]);
 		rb_ary_unshift(temp, rb_gv_get("$0"));
 		local_argv[0] = INT2NUM(RARRAY(temp)->len);
 		local_argv[1] = temp;
 		result = new_qt(2, local_argv, klass);
-		free(local_argv);
 	} else {
 		result = new_qt(argc, argv, klass);
 	}
--- trunk/KDE/kdebindings/qtruby/rubylib/qtruby/handlers.cpp #597567:597568
@@ -143,7 +143,7 @@
     const char *className = o->smoke->classes[o->classId].className;
 	
 	if (do_debug & qtdb_gc) qWarning("Checking for mark (%s*)%p", className, o->ptr);
-		
+
     if (o->ptr && o->allocated) {
 		if (isDerivedFromByName(o->smoke, className, "QListWidget")) {
 			QListWidget * listwidget = (QListWidget *) o->smoke->cast(o->ptr, o->classId, o->smoke->idClass("QListWidget"));
--- trunk/KDE/kdebindings/qtruby/rubylib/qtruby/marshall_basetypes.h #597567:597568
@@ -102,6 +102,9 @@
 	void *ptr = o->ptr;
 	if(!m->cleanup() && m->type().isStack()) {
 		ptr = construct_copy(o);
+		if (do_debug & qtdb_gc) {
+			qWarning("copying %s %p to %p\n", resolve_classname(o->smoke, o->classId, o->ptr), o->ptr, ptr);
+		}
 	}
 		
 	const Smoke::Class &cl = m->smoke()->classes[m->type().classId()];
@@ -135,6 +138,10 @@
 	const char * classname = resolve_classname(o->smoke, o->classId, o->ptr);
 	if(m->type().isConst() && m->type().isRef()) {
 		p = construct_copy( o );
+		if (do_debug & qtdb_gc) {
+			qWarning("copying %s %p to %p\n", classname, o->ptr, p);
+		}
+
 		if(p) {
 			o->ptr = p;
 			o->allocated = true;
@@ -142,16 +149,18 @@
 	}
 		
 	obj = set_obj_info(classname, o);
-	if (do_debug & qtdb_calls) {
+	if (do_debug & qtdb_gc) {
 		qWarning("allocating %s %p -> %p\n", classname, o->ptr, (void*)obj);
 	}
 
+/*
 	if(m->type().isStack()) {
 		o->allocated = true;
 		// Keep a mapping of the pointer so that it is only wrapped once as a ruby VALUE
 		mapPointer(obj, o, o->classId, 0);
 	}
-			
+*/			
+
 	*(m->var()) = obj;
 }
 



More information about the Kde-bindings mailing list