Home | History | Annotate | Download | only in libffi.call
      1 /* Area:	ffi_call
      2    Purpose:	Check different structures.
      3    Limitations:	none.
      4    PR:		none.
      5    Originator:	Ronald Oussoren <oussoren (at) cistron.nl> 20030824	*/
      6 
      7 /* { dg-do run } */
      8 #include "ffitest.h"
      9 
     10 typedef struct Point {
     11 	float x;
     12 	float y;
     13 } Point;
     14 
     15 typedef struct Size {
     16 	float h;
     17 	float w;
     18 } Size;
     19 
     20 typedef struct Rect {
     21 	Point o;
     22 	Size  s;
     23 } Rect;
     24 
     25 int doit(int o, char* s, Point p, Rect r, int last)
     26 {
     27 	printf("CALLED WITH %d %s {%f %f} {{%f %f} {%f %f}} %d\n",
     28 		o, s, p.x, p.y, r.o.x, r.o.y, r.s.h, r.s.w, last);
     29 	return 42;
     30 }
     31 
     32 
     33 int main(void)
     34 {
     35 	ffi_type point_type;
     36 	ffi_type size_type;
     37 	ffi_type rect_type;
     38 	ffi_cif cif;
     39 	ffi_type* arglist[6];
     40 	void* values[6];
     41 	int r;
     42 
     43 	/*
     44 	 *  First set up FFI types for the 3 struct types
     45 	 */
     46 
     47 	point_type.size = 0; /*sizeof(Point);*/
     48 	point_type.alignment = 0; /*__alignof__(Point);*/
     49 	point_type.type = FFI_TYPE_STRUCT;
     50 	point_type.elements = malloc(3 * sizeof(ffi_type*));
     51 	point_type.elements[0] = &ffi_type_float;
     52 	point_type.elements[1] = &ffi_type_float;
     53 	point_type.elements[2] = NULL;
     54 
     55 	size_type.size = 0;/* sizeof(Size);*/
     56 	size_type.alignment = 0;/* __alignof__(Size);*/
     57 	size_type.type = FFI_TYPE_STRUCT;
     58 	size_type.elements = malloc(3 * sizeof(ffi_type*));
     59 	size_type.elements[0] = &ffi_type_float;
     60 	size_type.elements[1] = &ffi_type_float;
     61 	size_type.elements[2] = NULL;
     62 
     63 	rect_type.size = 0;/*sizeof(Rect);*/
     64 	rect_type.alignment =0;/* __alignof__(Rect);*/
     65 	rect_type.type = FFI_TYPE_STRUCT;
     66 	rect_type.elements = malloc(3 * sizeof(ffi_type*));
     67 	rect_type.elements[0] = &point_type;
     68 	rect_type.elements[1] = &size_type;
     69 	rect_type.elements[2] = NULL;
     70 
     71 	/*
     72 	 * Create a CIF
     73 	 */
     74 	arglist[0] = &ffi_type_sint;
     75 	arglist[1] = &ffi_type_pointer;
     76 	arglist[2] = &point_type;
     77 	arglist[3] = &rect_type;
     78 	arglist[4] = &ffi_type_sint;
     79 	arglist[5] = NULL;
     80 
     81 	r = ffi_prep_cif(&cif, FFI_DEFAULT_ABI,
     82 			5, &ffi_type_sint, arglist);
     83 	if (r != FFI_OK) {
     84 		abort();
     85 	}
     86 
     87 
     88 	/* And call the function through the CIF */
     89 
     90 	{
     91 	Point p = { 1.0, 2.0 };
     92 	Rect  r = { { 9.0, 10.0}, { -1.0, -2.0 } };
     93 	int   o = 0;
     94 	int   l = 42;
     95 	char* m = "myMethod";
     96 	ffi_arg result;
     97 
     98 	values[0] = &o;
     99 	values[1] = &m;
    100 	values[2] = &p;
    101 	values[3] = &r;
    102 	values[4] = &l;
    103 	values[5] = NULL;
    104 
    105 	printf("CALLING WITH %d %s {%f %f} {{%f %f} {%f %f}} %d\n",
    106 		o, m, p.x, p.y, r.o.x, r.o.y, r.s.h, r.s.w, l);
    107 
    108 	ffi_call(&cif, FFI_FN(doit), &result, values);
    109 
    110 	printf ("The result is %d\n", (int)result);
    111 
    112 	}
    113 	exit(0);
    114 }
    115