[Kde-bindings] KDE/kdebindings/qtruby

Richard Dale Richard_Dale at tipitina.demon.co.uk
Fri Oct 20 10:49:24 UTC 2006


SVN commit 597428 by rdale:

* All allocs and frees for the smokeruby_object struct now go through
  two methods alloc_smokeruby_object() and free_smokeruby_object().
  Before some instances were being allocated with Ruby's ALLOC, and
  some with malloc(). Now they are all created with malloc().
* Removed a call to ALLOC_N and replaced it will calloc().

CCMAIL: kde-bindings at kde.org



 M  +8 -0      ChangeLog  
 M  +58 -48    rubylib/qtruby/Qt.cpp  
 M  +36 -40    rubylib/qtruby/handlers.cpp  
 M  +7 -0      rubylib/qtruby/qtruby.h  


--- trunk/KDE/kdebindings/qtruby/ChangeLog #597427:597428
@@ -1,3 +1,11 @@
+2006-10-20  Richard Dale  <rdale at foton.es>
+
+	* All allocs and frees for the smokeruby_object struct now go through
+	  two methods alloc_smokeruby_object() and free_smokeruby_object().
+	  Before some instances were being allocated with Ruby's ALLOC, and
+	  some with malloc(). Now they are all created with malloc().
+	* Removed a call to ALLOC_N and replaced it will calloc().
+
 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 #597427:597428
@@ -139,6 +139,24 @@
 extern TypeHandler Qt_handlers[];
 void install_handlers(TypeHandler *);
 
+smokeruby_object * 
+alloc_smokeruby_object(bool allocated, Smoke * smoke, int classId, void * ptr)
+{
+    smokeruby_object * o = (smokeruby_object *) malloc(sizeof(smokeruby_object));
+	o->classId = classId;
+	o->smoke = smoke;
+	o->ptr = ptr;
+	o->allocated = allocated;
+	return o;
+}
+
+void
+free_smokeruby_object(smokeruby_object * o)
+{
+	free(o);
+	return;
+}
+
 smokeruby_object *value_obj_info(VALUE ruby_value) {  // ptr on success, null on fail
     if (TYPE(ruby_value) != T_DATA) {
 	return 0;
@@ -534,15 +552,11 @@
 		rb_raise(rb_eArgError, "unable to find class \"%s\" to cast to\n", StringValuePtr(new_klassname));
 	}
 
-    smokeruby_object *o_cast = (smokeruby_object *) malloc(sizeof(smokeruby_object));
-    memcpy(o_cast, o, sizeof(smokeruby_object));
+	smokeruby_object * o_cast = alloc_smokeruby_object(	o->allocated, 
+														o->smoke, 
+														(int) *cast_to_id, 
+														o->smoke->cast(o->ptr, o->classId, (int) *cast_to_id) );
 
-    o_cast->allocated = o->allocated;
-    o->allocated = false;
-
-    o_cast->classId = (int) *cast_to_id;
-    o_cast->ptr = o->smoke->cast(o->ptr, o->classId, o_cast->classId);
-
     VALUE obj = Data_Wrap_Struct(new_klass, smokeruby_mark, smokeruby_free, (void *) o_cast);
     mapPointer(obj, o_cast, o_cast->classId, 0);
     return obj;
@@ -629,11 +643,7 @@
 	}
 
 	VALUE result = Qnil;
-	smokeruby_object  * vo = ALLOC(smokeruby_object);
-	vo->smoke = o->smoke;
-	vo->classId = *value_class_id;
-	vo->ptr = value_ptr;
-	vo->allocated = true;
+	smokeruby_object * vo = alloc_smokeruby_object(true, o->smoke, *value_class_id, value_ptr);
 	result = set_obj_info(classname, vo);
 
 	return result;
@@ -694,11 +704,7 @@
 		return rb_funcall(qvariant_class, rb_intern("new"), 1, obj);
 	}
 
-	smokeruby_object  * vo = ALLOC(smokeruby_object);
-	vo->smoke = o->smoke;
-	vo->classId = o->smoke->idClass("QVariant");
-	vo->ptr = v;
-	vo->allocated = true;
+	smokeruby_object * vo = alloc_smokeruby_object(true, o->smoke, o->smoke->idClass("QVariant"), v);
 	VALUE result = set_obj_info("Qt::Variant", vo);
 
 	return result;
@@ -1129,11 +1135,11 @@
 		rb_raise(rb_eArgError, "Invalid argument list");
 	}
 
-	smokeruby_object  * result = (smokeruby_object *) malloc(sizeof(smokeruby_object));
-	result->smoke = o->smoke;
-	result->classId = o->smoke->idClass("QVariant");
-	result->ptr = new QVariant(value);
-	result->allocated = true;
+
+	smokeruby_object  * result = alloc_smokeruby_object(	true, 
+															o->smoke, 
+															o->smoke->idClass("QVariant"), 
+															new QVariant(value) );
 	return set_obj_info("Qt::Variant", result);
 }
 
@@ -1387,11 +1393,12 @@
     smokeruby_object *o = value_obj_info(self);
 	QItemSelection * item = (QItemSelection *) o->ptr;
 	QItemSelectionRange range = item->at(NUM2INT(i));
-	smokeruby_object  * result = (smokeruby_object *) malloc(sizeof(smokeruby_object));
-	result->smoke = o->smoke;
-	result->classId = o->smoke->idClass("QItemSelectionRange");
-	result->ptr = new QItemSelectionRange(range);
-	result->allocated = true;
+
+	smokeruby_object  * result = alloc_smokeruby_object(	true, 
+															o->smoke, 
+															o->smoke->idClass("QItemSelectionRange"), 
+															new QItemSelectionRange(range) );
+
 	return set_obj_info("Qt::ItemSelectionRange", result);
 }
 
@@ -1419,11 +1426,12 @@
 qobject_staticmetaobject(VALUE /*klass*/)
 {
 	QMetaObject * meta = new QMetaObject(QObject::staticMetaObject);
-	smokeruby_object  * m = (smokeruby_object *) malloc(sizeof(smokeruby_object));
-	m->smoke = qt_Smoke;
-	m->classId = m->smoke->idClass("QMetaObject");
-	m->ptr = meta;
-	m->allocated = true;
+
+	smokeruby_object  * m = alloc_smokeruby_object(	true, 
+													qt_Smoke, 
+													qt_Smoke->idClass("QMetaObject"), 
+													meta );
+
 	VALUE obj = set_obj_info("Qt::MetaObject", m);
 	return obj;
 }
@@ -1439,11 +1447,11 @@
 		return obj;
 	}
 
-	smokeruby_object  * m = (smokeruby_object *) malloc(sizeof(smokeruby_object));
-	m->smoke = o->smoke;
-	m->classId = m->smoke->idClass("QMetaObject");
-	m->ptr = meta;
-	m->allocated = false;
+	smokeruby_object  * m = alloc_smokeruby_object(	false, 
+													o->smoke, 
+													o->smoke->idClass("QMetaObject"), 
+													meta );
+
 	obj = set_obj_info("Qt::MetaObject", m);
 	return obj;
 }
@@ -1757,11 +1765,14 @@
 	
 	smokeruby_object * p = 0;
 	Data_Get_Struct(temp_obj, smokeruby_object, p);
-	smokeruby_object  * o = (smokeruby_object *) malloc(sizeof(smokeruby_object));
-	memcpy(o, p, sizeof(smokeruby_object));
+
+	smokeruby_object  * o = alloc_smokeruby_object(	true, 
+													p->smoke, 
+													p->classId, 
+													p->ptr );
 	p->ptr = 0;
 	p->allocated = false;
-	o->allocated = true;
+
 	free(temp_stack);
 	VALUE result = Data_Wrap_Struct(klass, smokeruby_mark, smokeruby_free, o);
 	mapObject(result, result);
@@ -1774,7 +1785,7 @@
 VALUE
 new_qt(int argc, VALUE * argv, VALUE klass)
 {
-	VALUE * temp_stack = ALLOCA_N(VALUE, argc + 1);
+    VALUE * temp_stack = (VALUE *) calloc(argc + 1, sizeof(VALUE));
 	temp_stack[0] = rb_obj_alloc(klass);
 
 	for (int count = 0; count < argc; count++) {
@@ -1784,6 +1795,7 @@
 	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;
 }
 
@@ -2321,13 +2333,11 @@
 	}
 	printf("\n");
 #endif
+	smokeruby_object  * m = alloc_smokeruby_object(	true, 
+													qt_Smoke, 
+													qt_Smoke->idClass("QMetaObject"), 
+													meta );
 
-    smokeruby_object * m = (smokeruby_object *) malloc(sizeof(smokeruby_object));
-    m->smoke = qt_Smoke;
-    m->classId = qt_Smoke->idClass("QMetaObject");
-    m->ptr = meta;
-    m->allocated = true;
-
     return Data_Wrap_Struct(qmetaobject_class, smokeruby_mark, smokeruby_free, m);
 }
 
--- trunk/KDE/kdebindings/qtruby/rubylib/qtruby/handlers.cpp #597427:597428
@@ -246,7 +246,7 @@
 	if(do_debug & qtdb_gc) qWarning("Checking for delete (%s*)%p allocated: %s", className, o->ptr, o->allocated ? "true" : "false");
     
 	if(application_terminated || !o->allocated || o->ptr == 0) {
-		free(o);
+		free_smokeruby_object(o);
 		return;
 	}
 	
@@ -260,54 +260,54 @@
 			|| strcmp(className, "QModelIndex") == 0 )
 	{
 		// Don't delete instances of these classes for now
-		free(o);
+		free_smokeruby_object(o);
 		return;
 	} else if (isDerivedFromByName(o->smoke, className, "QLayoutItem")) {
 		QLayoutItem * item = (QLayoutItem *) o->smoke->cast(o->ptr, o->classId, o->smoke->idClass("QLayoutItem"));
 		if (item->layout() != 0 || item->widget() != 0 || item->spacerItem() != 0) {
-			free(o);
+			free_smokeruby_object(o);
 			return;
 		}
 //	} else if (strcmp(className, "QIconViewItem") == 0) {
 //		Q3IconViewItem * item = (Q3IconViewItem *) o->ptr;
 //		if (item->iconView() != 0) {
-//			free(o);
+//			free_smokeruby_object(o);
 //			return;
 //		}
 //	} else if (strcmp(className, "QCheckListItem") == 0) {
 //		Q3CheckListItem * item = (Q3CheckListItem *) o->ptr;
 //		if (item->parent() != 0 || item->listView() != 0) {
-//			free(o);
+//			free_smokeruby_object(o);
 //			return;
 //		}
 	} else if (strcmp(className, "QListWidgetItem") == 0) {
 		QListWidgetItem * item = (QListWidgetItem *) o->ptr;
 		if (item->listWidget() != 0) {
-			free(o);
+			free_smokeruby_object(o);
 			return;
 		}
 	} else if (isDerivedFromByName(o->smoke, className, "QTableWidgetItem")) {
 		QTableWidgetItem * item = (QTableWidgetItem *) o->smoke->cast(o->ptr, o->classId, o->smoke->idClass("QTableWidgetItem"));
 		if (item->tableWidget() != 0) {
-			free(o);
+			free_smokeruby_object(o);
 			return;
 		}
 //	} else if (strcmp(className, "QPopupMenu") == 0) {
 //		Q3PopupMenu * item = (Q3PopupMenu *) o->ptr;
 //		if (item->parentWidget(false) != 0) {
-//			free(o);
+//			free_smokeruby_object(o);
 //			return;
 //		}
 	} else if (isDerivedFromByName(o->smoke, className, "QWidget")) {
 		QWidget * qwidget = (QWidget *) o->smoke->cast(o->ptr, o->classId, o->smoke->idClass("QWidget"));
 		if (qwidget->parentWidget() != 0) {
-			free(o);
+			free_smokeruby_object(o);
 			return;
 		}
 	} else if (isDerivedFromByName(o->smoke, className, "QObject")) {
 		QObject * qobject = (QObject *) o->smoke->cast(o->ptr, o->classId, o->smoke->idClass("QObject"));
 		if (qobject->parent() != 0) {
-			free(o);
+			free_smokeruby_object(o);
 			return;
 		}
 	}
@@ -326,7 +326,7 @@
 		(*fn)(m.method, o->ptr, i);
 	}
 	delete[] methodName;
-	free(o);
+	free_smokeruby_object(o);
 	
     return;
 }
@@ -1127,21 +1127,21 @@
 
 			VALUE av = rb_ary_new();
 
-			for(int i=0;i<valuelist->size();++i) {
+			for (int i=0;i<valuelist->size();++i) {
 				void *p = valuelist->at(i);
 
-				if(m->item().s_voidp == 0) {
+				if (m->item().s_voidp == 0) {
 					*(m->var()) = Qnil;
 					break;
 				}
 
 				VALUE obj = getPointerObject(p);
-				if(obj == Qnil) {
-					smokeruby_object  * o = ALLOC(smokeruby_object);
-					o->smoke = m->smoke();
-					o->classId = m->smoke()->idClass(ItemSTR);
-					o->ptr = p;
-					o->allocated = false;
+				if (obj == Qnil) {
+					smokeruby_object  * o = alloc_smokeruby_object(	false, 
+																	m->smoke(), 
+																	m->smoke()->idClass(ItemSTR), 
+																	p );
+
 					obj = set_obj_info(resolve_classname(o->smoke, o->classId, o->ptr), o);
 				}
 			
@@ -1505,11 +1505,10 @@
 			VALUE obj = getPointerObject(p);
 				
 			if (obj == Qnil) {
-				smokeruby_object  * o = ALLOC(smokeruby_object);
-				o->classId = m->smoke()->idClass("QVariant");
-				o->smoke = m->smoke();
-				o->ptr = p;
-				o->allocated = true;
+				smokeruby_object  * o = alloc_smokeruby_object(	true, 
+																m->smoke(), 
+																m->smoke()->idClass("QVariant"), 
+																p );
 				obj = set_obj_info("Qt::Variant", o);
 			}
 			
@@ -1580,11 +1579,10 @@
 			VALUE obj = getPointerObject(p);
 				
 			if (obj == Qnil) {
-				smokeruby_object  * o = ALLOC(smokeruby_object);
-				o->classId = m->smoke()->idClass("QVariant");
-				o->smoke = m->smoke();
-				o->ptr = p;
-				o->allocated = true;
+				smokeruby_object  * o = alloc_smokeruby_object(	true, 
+																m->smoke(), 
+																m->smoke()->idClass("QVariant"), 
+																p );
 				obj = set_obj_info("Qt::Variant", o);
 			}
 			
@@ -1775,12 +1773,11 @@
 
 		void *p = (void *) &(qpair->second);
 		VALUE rv2 = getPointerObject(p);
-		if(rv2 == Qnil) {
-			smokeruby_object  * o = ALLOC(smokeruby_object);
-			o->smoke = m->smoke();
-			o->classId = o->smoke->idClass("QColor");
-			o->ptr = p;
-			o->allocated = false;
+		if (rv2 == Qnil) {
+			smokeruby_object  * o = alloc_smokeruby_object(	false, 
+															m->smoke(), 
+															m->smoke()->idClass("QColor"), 
+															p );
 			rv2 = set_obj_info("Qt::Color", o);
 		}
 
@@ -1930,11 +1927,10 @@
 
 				VALUE obj = getPointerObject(p);
 				if(obj == Qnil) {
-					smokeruby_object  * o = ALLOC(smokeruby_object);
-					o->smoke = m->smoke();
-					o->classId = o->smoke->idClass(ItemSTR);
-					o->ptr = p;
-					o->allocated = false;
+					smokeruby_object  * o = alloc_smokeruby_object(	false, 
+																	m->smoke(), 
+																	m->smoke()->idClass(ItemSTR), 
+																	p );
 					obj = set_obj_info(className, o);
 				}
 		
--- trunk/KDE/kdebindings/qtruby/rubylib/qtruby/qtruby.h #597427:597428
@@ -27,6 +27,13 @@
     void *ptr;
 };
 
+extern smokeruby_object * alloc_smokeruby_object(	bool allocated, 
+													Smoke * smoke, 
+													int classId, 
+													void * ptr );
+
+extern void free_smokeruby_object(smokeruby_object * o);
+
 struct TypeHandler {
     const char *name;
     Marshall::HandlerFn fn;



More information about the Kde-bindings mailing list