Skip to content
6 changes: 4 additions & 2 deletions src/main/java/jnr/ffi/annotations/Direct.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@
package jnr.ffi.annotations;


import jnr.ffi.struct.Struct;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Indicates that a {@link jnr.ffi.Struct}} parameter should be
* Indicates that a {@link Struct}} parameter should be
* backed by a persistent native memory block.
*
* <p>Without the {@code @Direct} annotation, the native code will allocate a
* temporary native memory block for the parameter, and free it immediately after
* the call.
*
* <p>By using {@code @Direct}, the native memory block is permanently associated
* with the {@link jnr.ffi.Struct} instance, and will remain allocated
* with the {@link Struct} instance, and will remain allocated
* for as long as the {@code Struct} instance remains strongly referenced by java code.
*
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/jnr/ffi/annotations/In.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package jnr.ffi.annotations;

import jnr.ffi.struct.Struct;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -27,7 +29,7 @@
* Indicates that the parameter is an IN parameter.
*
* <p>When a java object is passed to a native function as a pointer
* (for example {@link jnr.ffi.Pointer}, {@link jnr.ffi.Struct}, {@link java.nio.ByteBuffer}),
* (for example {@link jnr.ffi.Pointer}, {@link Struct}, {@link java.nio.ByteBuffer}),
* then a temporary native memory block is allocated, the java data is copied to
* the temporary memory and the address of the temporary memory is passed to the function.
* After the function returns, the java data is automatically updated from the
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/jnr/ffi/annotations/Out.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package jnr.ffi.annotations;

import jnr.ffi.struct.Struct;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -27,7 +29,7 @@
* Indicates that the parameter is an OUT parameter.
*
* <p>When a java object is passed to a native function as a pointer
* (for example {@link jnr.ffi.Pointer}, {@link jnr.ffi.Struct}, {@link java.nio.ByteBuffer}),
* (for example {@link jnr.ffi.Pointer}, {@link Struct}, {@link java.nio.ByteBuffer}),
* then a temporary native memory block is allocated, the java data is copied to
* the temporary memory and the address of the temporary memory is passed to the function.
* After the function returns, the java data is automatically updated from the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
package jnr.ffi.provider.converters;

import jnr.ffi.Pointer;
import jnr.ffi.Struct;
import jnr.ffi.annotations.LongLong;
import jnr.ffi.struct.Struct;
import jnr.ffi.mapper.ToNativeContext;
import jnr.ffi.mapper.ToNativeConverter;
import jnr.ffi.provider.DelegatingMemoryIO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package jnr.ffi.provider.converters;

import jnr.ffi.Pointer;
import jnr.ffi.Struct;
import jnr.ffi.struct.Struct;
import jnr.ffi.mapper.FromNativeContext;
import jnr.ffi.mapper.FromNativeConverter;

Expand All @@ -28,7 +28,7 @@


/**
* Converts a native pointer result into a {@link jnr.ffi.Struct}
* Converts a native pointer result into a {@link Struct}
*/
public class StructByReferenceFromNativeConverter implements FromNativeConverter<Struct, Pointer> {
private final Constructor<? extends Struct> constructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package jnr.ffi.provider.converters;

import jnr.ffi.Pointer;
import jnr.ffi.Struct;
import jnr.ffi.struct.Struct;
import jnr.ffi.mapper.ToNativeContext;
import jnr.ffi.mapper.ToNativeConverter;
import jnr.ffi.provider.ParameterFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import jnr.ffi.Pointer;
import jnr.ffi.Runtime;
import jnr.ffi.Struct;
import jnr.ffi.struct.Struct;
import jnr.ffi.mapper.FromNativeContext;
import jnr.ffi.mapper.FromNativeConverter;
import org.objectweb.asm.ClassReader;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/jnr/ffi/provider/jffi/ClosureTypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

package jnr.ffi.provider.jffi;

import jnr.ffi.Struct;
import jnr.ffi.struct.Struct;
import jnr.ffi.mapper.*;
import jnr.ffi.mapper.FromNativeType;
import jnr.ffi.mapper.ToNativeType;
import jnr.ffi.provider.converters.EnumConverter;
import jnr.ffi.provider.ParameterFlags;
import jnr.ffi.provider.converters.StringResultConverter;
import jnr.ffi.provider.converters.StructByReferenceToNativeConverter;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jnr/ffi/provider/jffi/InvokerTypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import jnr.ffi.NativeLong;
import jnr.ffi.Pointer;
import jnr.ffi.Struct;
import jnr.ffi.struct.Struct;
import jnr.ffi.annotations.Delegate;
import jnr.ffi.byref.ByReference;
import jnr.ffi.mapper.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jnr.ffi.mapper.FromNativeContext;
import jnr.ffi.mapper.FromNativeConverter;
import jnr.ffi.provider.converters.StructByReferenceFromNativeConverter;
import jnr.ffi.struct.Struct;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/jnr/ffi/struct/AsciiString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package jnr.ffi.struct;

public class AsciiString extends UTFString {
private Struct struct;

public AsciiString(Struct struct, int size) {
super(struct, size, Struct.ASCII);
this.struct = struct;
}
}
68 changes: 68 additions & 0 deletions src/main/java/jnr/ffi/struct/Field.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package jnr.ffi.struct;

import jnr.ffi.NativeType;
import jnr.ffi.Runtime;
import jnr.ffi.Type;
import jnr.ffi.TypeAlias;

/**
* Structure field
*/
public abstract class Field {

private Struct.Info info;
private Offset offset;

protected Field(Struct struct, int size, int align, Offset offset) {
registerInStructure(struct, size, align, offset);
}

protected Field(Struct struct, int size, int align) {
this(struct, size, align, null);
}

protected Field(Struct struct, NativeType nativeType) {
this(struct, nativeType, null);
}

protected Field(Struct struct, NativeType nativeType, Offset offset) {
final Type type = struct.getRuntime().findType(nativeType);
registerInStructure(struct, type, offset);
}

protected Field(Struct struct, TypeAlias typeAlias) {
this(struct, typeAlias, null);
}

protected Field(Struct struct, TypeAlias typeAlias, Offset offset) {
Type type = struct.getRuntime().findType(typeAlias);
registerInStructure(struct, type, offset);
}

private void registerInStructure(Struct struct, Type type, Offset offset) {
registerInStructure(struct, type.size() * 8, type.alignment() * 8, offset);
}

public final jnr.ffi.Pointer getMemory() {
return info.getMemory();
}

public final Runtime getRuntime() {
return info.getRuntime();
}

protected long offset() {
return offset.intValue() + info.getOffset();
}

private void registerInStructure(Struct struct, int size, int alignment, Offset offset) {
info = struct.__info;
if (offset == null) {
int offsetAfterAllocation = struct.__info.addField(size, alignment);
this.offset = new Offset(offsetAfterAllocation);
} else {
struct.__info.addField(size, alignment, offset);
}

}
}
24 changes: 24 additions & 0 deletions src/main/java/jnr/ffi/struct/Offset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jnr.ffi.struct;

public final class Offset extends Number {
private final int offset;
public Offset(int offset) {
this.offset = offset;
}
@Override
public int intValue() {
return offset;
}
@Override
public long longValue() {
return offset;
}
@Override
public float floatValue() {
return offset;
}
@Override
public double doubleValue() {
return offset;
}
}
38 changes: 38 additions & 0 deletions src/main/java/jnr/ffi/struct/StringField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package jnr.ffi.struct;

import jnr.ffi.NativeType;

import java.nio.charset.Charset;

abstract public class StringField extends Field {
protected final Charset charset;
protected final int length;

protected StringField(Struct struct, NativeType nativeType, int length, Charset cs) {
super(struct, nativeType);
this.length = length;
this.charset = cs;
}
protected StringField(Struct struct, int size, int align, int length, Charset cs) {
super(struct, size, align);
this.length = length;
this.charset = cs;
}
protected StringField(Struct struct, int size, int align, Offset offset, int length, Charset cs) {
super(struct, size, align, offset);
this.length = length;
this.charset = cs;
}
public final int length() {
return length;
}

protected abstract jnr.ffi.Pointer getStringMemory();
public abstract java.lang.String get();
public abstract void set(java.lang.String value);

@Override
public final java.lang.String toString() {
return get();
}
}
Loading