Return values smaller than ‘int’

Ludovic Courtès ludo@gnu.org
Sun May 8 13:44:00 GMT 2011


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’.



More information about the Libffi-discuss mailing list