After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 640786 - bad qsort_with_data definition in glib-2.0.vapi
bad qsort_with_data definition in glib-2.0.vapi
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Bindings: GLib
0.11.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
: 686338 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2011-01-28 00:42 UTC by Sebastian Reichel
Modified: 2014-01-29 20:17 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
qsort_with_data's CompareDataFunc takes pointers instead of the real values (549 bytes, patch)
2011-01-28 00:42 UTC, Sebastian Reichel
none Details | Review

Description Sebastian Reichel 2011-01-28 00:42:55 UTC
Created attachment 179483 [details] [review]
qsort_with_data's CompareDataFunc takes pointers instead of the real values

Currently glib-2.0.vapi contains the following qsort_with_data definition:

private static void qsort_with_data<T> (T[] elems, size_t size, [CCode (type = "GCompareDataFunc")] GLib.CompareDataFunc<T> compare_func);

The problem is, that the CompareDataFunc does get a pointer to the actual value, thus the following statement would be correct:

private static void qsort_with_data<T> (T[] elems, size_t size, [CCode (type = "GCompareDataFunc")] GLib.CompareDataFunc<T*> compare_func);

You can see the problem in this example - currently you need to use it like this (you will get some gcc warnings, but the code sorts the array):

int compare(int a, int b) {
	return (*((int*) a) - *((int *)b)) < 0 ? -1 : 1;
}

void main() {
	int[] testdata = {5,4,3,2,1};
	qsort_with_data<int>(testdata, sizeof(int), compare );

	foreach(int i in testdata)
		stdout.printf("%d, ", i);
	stdout.printf("\n");
}

After changing the qsort_with_data in the vapi file you can do it like this:

int compare(int *a, int *b) {
	return (*a - *b) < 0 ? -1 : 1;
}

void main() {
	int[] testdata = {5,4,3,2,1};
	qsort_with_data<int>(testdata, sizeof(int), compare );

	foreach(int i in testdata)
		stdout.printf("%d, ", i);
	stdout.printf("\n");
}

Still remaining are some of the gcc warnings. I'm not sure were these needs to be fixed. They
are printed because of a missing typecast in the generated C code (gconstpointer to gint* in this
example).
Comment 1 Evan Nemerson 2014-01-29 05:57:14 UTC
commit 994dd5674637dfe577408f486216728addbae8e5
Author: Evan Nemerson <evan@coeus-group.com>
Date:   Tue Jan 28 21:56:03 2014 -0800

    glib-2.0: fix qsort_with_data binding
    
    Fixes bug 640786.
Comment 2 Maciej (Matthew) Piechotka 2014-01-29 20:17:18 UTC
*** Bug 686338 has been marked as a duplicate of this bug. ***