/js/src/ctypes/libffi/testsuite/libffi.call/struct6.c

http://github.com/zpao/v8monkey · C · 64 lines · 42 code · 14 blank · 8 comment · 3 complexity · 488c99a40ae8724b649937ed0f736352 MD5 · raw file

  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. float f;
  11. double d;
  12. } test_structure_6;
  13. static test_structure_6 struct6 (test_structure_6 ts)
  14. {
  15. ts.f += 1;
  16. ts.d += 1;
  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. ffi_type ts6_type;
  25. ffi_type *ts6_type_elements[3];
  26. ts6_type.size = 0;
  27. ts6_type.alignment = 0;
  28. ts6_type.type = FFI_TYPE_STRUCT;
  29. ts6_type.elements = ts6_type_elements;
  30. ts6_type_elements[0] = &ffi_type_float;
  31. ts6_type_elements[1] = &ffi_type_double;
  32. ts6_type_elements[2] = NULL;
  33. test_structure_6 ts6_arg;
  34. /* This is a hack to get a properly aligned result buffer */
  35. test_structure_6 *ts6_result =
  36. (test_structure_6 *) malloc (sizeof(test_structure_6));
  37. args[0] = &ts6_type;
  38. values[0] = &ts6_arg;
  39. /* Initialize the cif */
  40. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ts6_type, args) == FFI_OK);
  41. ts6_arg.f = 5.55f;
  42. ts6_arg.d = 6.66;
  43. printf ("%g\n", ts6_arg.f);
  44. printf ("%g\n", ts6_arg.d);
  45. ffi_call(&cif, FFI_FN(struct6), ts6_result, values);
  46. CHECK(ts6_result->f == 5.55f + 1);
  47. CHECK(ts6_result->d == 6.66 + 1);
  48. free (ts6_result);
  49. exit(0);
  50. }