Skip to content

Commit

Permalink
Document why we still pass the NULL
Browse files Browse the repository at this point in the history
When using FFI's ffi_prep_cif_var, we do not need to pass a
trailing null for va_arg since the FFI logic handles setting up
varargs appropriately. However not all platform stubs have been
rebuilt with this new logic, so we continue to pass the null to
keep working with ffi_prep_cif and va_arg layout.
  • Loading branch information
headius committed Jan 5, 2022
1 parent e0bcc35 commit b5c0934
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main/java/jnr/ffi/provider/jffi/DefaultInvokerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,17 @@ public final Object invoke(Object self, Object[] parameters) {
}
}

//Add one extra vararg of NULL to meet the common convention of ending
//varargs with a NULL. Functions that get a length from the fixed arguments
//will ignore the extra, and funtions that expect the extra NULL will get it.
//This matches what JNA does.
// Add one extra vararg of NULL to meet the common convention of ending
// varargs with a NULL. Functions that get a length from the fixed arguments
// will ignore the extra, and funtions that expect the extra NULL will get it.
// This matches what JNA does.
//
// Note: After https://github.com/jnr/jffi/pull/121 we now use the variadic ffi_prep_cif_var function for
// setup of the call when invoking a variadic function, which does not need the trailing null. However, for
// platforms where we have not rebuilt the jffi stub we still set up this trailing NULL to be compatible
// with ffi_prep_cif and the common va_arg layout. Once all platforms have been rebuilt to use
// ffi_prep_cif_var, this NULL and the +1 on variableArgs allocation above can be removed.

argTypes[fixedParameterTypes.length + variableArgsCount - 1] = new ParameterType(
Pointer.class,
Types.getType(runtime, Pointer.class, Collections.<Annotation>emptyList()).getNativeType(),
Expand Down

0 comments on commit b5c0934

Please sign in to comment.