struct2.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Area: ffi_call
  2. Purpose: Check structures.
  3. Limitations: none.
  4. PR: none.
  5. Originator: From the original ffitest.c */
  6. /* { dg-do run } */
  7. #include "ffitest.h"
  8. typedef struct
  9. {
  10. double d1;
  11. double d2;
  12. } test_structure_2;
  13. static test_structure_2 ABI_ATTR struct2(test_structure_2 ts)
  14. {
  15. ts.d1--;
  16. ts.d2--;
  17. return ts;
  18. }
  19. int main (void)
  20. {
  21. ffi_cif cif;
  22. ffi_type *args[MAX_ARGS];
  23. void *values[MAX_ARGS];
  24. test_structure_2 ts2_arg;
  25. ffi_type ts2_type;
  26. ffi_type *ts2_type_elements[3];
  27. /* This is a hack to get a properly aligned result buffer */
  28. test_structure_2 *ts2_result =
  29. (test_structure_2 *) malloc (sizeof(test_structure_2));
  30. ts2_type.size = 0;
  31. ts2_type.alignment = 0;
  32. ts2_type.type = FFI_TYPE_STRUCT;
  33. ts2_type.elements = ts2_type_elements;
  34. ts2_type_elements[0] = &ffi_type_double;
  35. ts2_type_elements[1] = &ffi_type_double;
  36. ts2_type_elements[2] = NULL;
  37. args[0] = &ts2_type;
  38. values[0] = &ts2_arg;
  39. /* Initialize the cif */
  40. CHECK(ffi_prep_cif(&cif, ABI_NUM, 1, &ts2_type, args) == FFI_OK);
  41. ts2_arg.d1 = 5.55;
  42. ts2_arg.d2 = 6.66;
  43. printf ("%g\n", ts2_arg.d1);
  44. printf ("%g\n", ts2_arg.d2);
  45. ffi_call(&cif, FFI_FN(struct2), ts2_result, values);
  46. printf ("%g\n", ts2_result->d1);
  47. printf ("%g\n", ts2_result->d2);
  48. CHECK(ts2_result->d1 == 5.55 - 1);
  49. CHECK(ts2_result->d2 == 6.66 - 1);
  50. free (ts2_result);
  51. exit(0);
  52. }