This is the mail archive of the libffi-discuss@sourceware.org mailing list for the libffi project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Return values smaller than âintâ


Hello,

I browsed the test suite and found that the correct way to do it is to
use âffi_argâ and suitably cast it:

--8<---------------cut here---------------start------------->8---
#include <ffi.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>

static uint16_t
foo (void)
{
  return 0x9876U;
}

int
main (int argc, char *argv[])
{
  ffi_cif cif;
  ffi_arg rvalue;

  if (FFI_OK != ffi_prep_cif (&cif, FFI_DEFAULT_ABI, 0,
			      &ffi_type_uint16, NULL))
    abort ();

  ffi_call (&cif, (void *) &foo, &rvalue, NULL);

  printf ("-> %04x -- expected: %04x\n", (uint16_t) rvalue, foo ());
  assert ((uint16_t) rvalue == foo ());

  return 0;
}
--8<---------------cut here---------------end--------------->8---

Tested on x86_64, PPC64, and SPARC64.

Thanks,
Ludoâ.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]