pax_global_header 0000666 0000000 0000000 00000000064 12227113360 0014507 g ustar 00root root 0000000 0000000 52 comment=4f8178828164efe93f17181f6d6699ecff22b5f2
jnr-ffi-1.0.10/ 0000775 0000000 0000000 00000000000 12227113360 0013121 5 ustar 00root root 0000000 0000000 jnr-ffi-1.0.10/.gitignore 0000664 0000000 0000000 00000000156 12227113360 0015113 0 ustar 00root root 0000000 0000000 *.orig$
*.rej$
*.class$
*~
.idea
*.iml
nbproject/private
build
dist
target
lib/nblibraries-private.properties
jnr-ffi-1.0.10/.travis.yml 0000664 0000000 0000000 00000000060 12227113360 0015226 0 ustar 00root root 0000000 0000000 language: java
jdk:
- oraclejdk7
- openjdk6
jnr-ffi-1.0.10/LICENSE 0000664 0000000 0000000 00000002253 12227113360 0014130 0 ustar 00root root 0000000 0000000 Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
version 3 for more details.
You should have received a copy of the GNU Lesser General Public License
version 3 along with this work. If not, see This class extends {@link java.lang.Number} and implements all {@code Number} methods for
* converting to primitive integer types.
*
* An {@code Address} instance is lighter weight than most {@link Pointer}
* instances, and may be used when a native address needs to be stored in java,
* but no other operations (such as reading/writing values) need be performed on
* the native memory. For most cases, a {@link Pointer} offers more flexibility
* and should be preferred instead.
*/
public final class Address extends Number implements Comparable This is only needed on windows platforms - all platforms assume
* {@link #DEFAULT} as the calling convention.
*/
public enum CallingConvention {
/**
* The default C calling convention
*/
DEFAULT,
/**
* Windows stdcall calling convention
*/
STDCALL
}
jnr-ffi-1.0.10/src/main/java/jnr/ffi/LastError.java 0000664 0000000 0000000 00000002523 12227113360 0021674 0 ustar 00root root 0000000 0000000 /*
* Copyright (C) 2008-2010 Wayne Meissner
*
* This file is part of the JNR project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jnr.ffi;
/**
* Provides access to the unix errno and windows GetLastError() value.
*/
public final class LastError {
private LastError() {}
/**
* Gets the value of errno from the last native call.
*
* @param runtime FFI runtime to get errno for.
* @return An integer containing the errno value.
*/
public static int getLastError(Runtime runtime) {
return runtime.getLastError();
}
/**
* Sets the native errno value.
*
* @param runtime FFI runtime to set errno for.
* @param error The value to set errno to.
*/
public static void setLastError(Runtime runtime, int error) {
runtime.setLastError(error);
}
}
jnr-ffi-1.0.10/src/main/java/jnr/ffi/Library.java 0000664 0000000 0000000 00000014364 12227113360 0021371 0 ustar 00root root 0000000 0000000 /*
* Copyright (C) 2008-2010 Wayne Meissner
*
* This file is part of the JNR project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jnr.ffi;
import jnr.ffi.provider.FFIProvider;
import jnr.ffi.provider.LoadedLibrary;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* @deprecated Use {@link LibraryLoader} instead.
*/
public final class Library {
private static final Map Example usage This is only needed on windows platforms - unless explicitly specified, all platforms assume
* {@link CallingConvention#DEFAULT} as the calling convention.
*
* @return The {@code LibraryLoader} instance.
*/
public LibraryLoader
* In C, a long can be either 32 bits or 64bits, depending on the platform.
*
* The native addresses generated by this class do not relate to the object's true native address
* (since that is not supported by most java virtual machines), but is guaranteed to be unique within
* an ObjectReferenceManager instance.
*
* This would be commonly used to create a unique native pointer that can be used to retrieve an object
* from within a callback.
* e.g.
*
* {@code
*
* public interface LibC {
* int puts(String str);
* }
*
* LibC libc = LibraryLoader.create(LibC.class).load("c");
*
* libc.puts("Hello, World");
*
* }
*
* {@code
*
* public interface MyLib {
* public static interface MyCallback {
* @Delegate public void call(Pointer value);
* }
*
* public void do_something_with_callback(MyCallback cb, Pointer cb_argument);
* }
*
* MyLib lib = LibraryLoader.create(MyLib.class).load("mylib");
* final ObjectReferenceManager referenceManager = Runtime.getRuntime(lib).newObjectReferenceManager();
*
* MyCallback cb = new MyCallback {
* public void call(Pointer cb_argument) {
* Object javaCallbackArgument = referenceManager.get(cb_argument);
* System.out.println("java callback parameter=" + javaCallbackArgument);
* }
* }
*
* String callbackArgument = "Hello, World";
* Pointer cb_argument = referenceManager.add(callback);
* lib.do_something_with_callback(cb, cb_argument);
* referenceManager.remove(cb_argument);
*
* }
*
*
* Each call to {@link #add(Object)} will return a unique native address, even for the same object, so each call to * {@link #add(Object)} must be matched with a call to {@link #remove(Pointer)}. *
*/ public abstract class ObjectReferenceManager* A strong reference to {@code object} is maintained internally, until {@link #remove(Pointer)} is called. *
* * @param object The java object to generate a reference for * @return A pointer representing the unique id. * @deprecated use {@link #add(Object)} */ @Deprecated public Pointer newReference(T object) { return add(object); } /** * Removes a mapping from java object to native pointer. * * @param reference a native memory pointer. * @deprecated use {@link #remove(Pointer)} */ @Deprecated public void freeReference(Pointer reference) { remove(reference); } /** * Gets the java object that is mapped to the native memory address referred to by {@code reference}. * * @param reference a native memory pointer. * @return The java object corresponding to {@code pointer}. * @deprecated use {@link #get(Pointer)} */ @Deprecated public T getObject(Pointer reference) { return get(reference); } /** * Adds a mapping from a java object to a unique native address. * * Each call to this method is guaranteed to produce a memory address unique within the ObjectReferenceManager * instance, even for the same object. * ** A strong reference to {@code object} is maintained internally, until {@link #remove(Pointer)} is called. *
* * @param object The java object to generate a reference for * @return A pointer representing the unique id. */ public abstract Pointer add(T object); /** * Removes a mapping from java object to native pointer. * * @param reference a native memory pointer. * @return true if the mapping was removed. */ public abstract boolean remove(Pointer reference); /** * Gets the java object that is mapped to the native memory address referred to by {@code reference}. * * @param reference a native memory pointer. * @return The java object corresponding to {@code pointer}. */ public abstract T get(Pointer reference); } jnr-ffi-1.0.10/src/main/java/jnr/ffi/Platform.java 0000664 0000000 0000000 00000035144 12227113360 0021550 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi; import java.io.File; import java.io.FilenameFilter; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.regex.Pattern; public abstract class Platform { private static final java.util.Locale LOCALE = java.util.Locale.ENGLISH; private final OS os; private final CPU cpu; private final int addressSize; private final int longSize; protected final Pattern libPattern; private static final class SingletonHolder { static final Platform PLATFORM = determinePlatform(); } /** * The common names of supported operating systems. */ public enum OS { /* * Note The names of the enum values are used in other parts of the * code to determine where to find the native stub library. Do not rename. */ /** MacOSX */ DARWIN, /** FreeBSD */ FREEBSD, /** NetBSD */ NETBSD, /** OpenBSD */ OPENBSD, /** Linux */ LINUX, /** Solaris (and OpenSolaris) */ SOLARIS, /** The evil borg operating system */ WINDOWS, /** IBM AIX */ AIX, /** IBM zOS **/ ZLINUX, /** No idea what the operating system is */ UNKNOWN; @Override public String toString() { return name().toLowerCase(LOCALE); } } /** * The supported CPU architectures. */ public enum CPU { /* * Note The names of the enum values are used in other parts of the * code to determine where to find the native stub library. Do NOT rename. */ /** 32 bit legacy Intel */ I386, /** 64 bit AMD (aka EM64T/X64) */ X86_64, /** 32 bit Power PC */ PPC, /** 64 bit Power PC */ PPC64, /** 32 bit Sun sparc */ SPARC, /** 64 bit Sun sparc */ SPARCV9, /** IBM zSeries S/390 */ S390X, /** 32 bit MIPS (used by nestedvm) */ MIPS32, /** 32 bit ARM */ ARM, /** * Unknown CPU architecture. A best effort will be made to infer architecture * specific values such as address and long size. */ UNKNOWN; /** * Returns a {@code String} object representing this {@code CPU} object. * * @return the name of the cpu architecture as a lower case {@code String}. */ @Override public String toString() { return name().toLowerCase(LOCALE); } } /** * Determines the operating system jffi is running on * * @return An member of the OS enum. */ private static OS determineOS() { String osName = System.getProperty("os.name").split(" ")[0]; if (startsWithIgnoreCase(osName, "mac") || startsWithIgnoreCase(osName, "darwin")) { return OS.DARWIN; } else if (startsWithIgnoreCase(osName, "linux")) { return OS.LINUX; } else if (startsWithIgnoreCase(osName, "sunos") || startsWithIgnoreCase(osName, "solaris")) { return OS.SOLARIS; } else if (startsWithIgnoreCase(osName, "aix")) { return OS.AIX; } else if (startsWithIgnoreCase(osName, "openbsd")) { return OS.OPENBSD; } else if (startsWithIgnoreCase(osName, "freebsd")) { return OS.FREEBSD; } else if (startsWithIgnoreCase(osName, "windows")) { return OS.WINDOWS; } else { return OS.UNKNOWN; } } /** * Determines the Platform that best describes the OS * * @param os The operating system. * @return An instance of Platform */ private static Platform determinePlatform(OS os) { switch (os) { case DARWIN: return new Darwin(); case LINUX: return new Linux(); case WINDOWS: return new Windows(); case UNKNOWN: return new Unsupported(os); default: return new Default(os); } } private static Platform determinePlatform() { String providerName = System.getProperty("jnr.ffi.provider"); try { Class c = Class.forName(providerName + "$Platform"); return (Platform) c.newInstance(); } catch (ClassNotFoundException ex) { return determinePlatform(determineOS()); } catch (IllegalAccessException ex) { throw new ExceptionInInitializerError(ex); } catch (InstantiationException ex) { throw new ExceptionInInitializerError(ex); } } private static CPU determineCPU() { String archString = System.getProperty("os.arch"); if (equalsIgnoreCase("x86", archString) || equalsIgnoreCase("i386", archString) || equalsIgnoreCase("i86pc", archString)) { return CPU.I386; } else if (equalsIgnoreCase("x86_64", archString) || equalsIgnoreCase("amd64", archString)) { return CPU.X86_64; } else if (equalsIgnoreCase("ppc", archString) || equalsIgnoreCase("powerpc", archString)) { return CPU.PPC; } else if (equalsIgnoreCase("ppc64", archString) || equalsIgnoreCase("powerpc64", archString)) { return CPU.PPC64; } else if (equalsIgnoreCase("s390", archString) || equalsIgnoreCase("s390x", archString)) { return CPU.S390X; } // Try to find by lookup up in the CPU list for (CPU cpu : CPU.values()) { if (equalsIgnoreCase(cpu.name(), archString)) { return cpu; } } return CPU.UNKNOWN; } public Platform(OS os, CPU cpu, int addressSize, int longSize, String libPattern) { this.os = os; this.cpu = cpu; this.addressSize = addressSize; this.longSize = longSize; this.libPattern = Pattern.compile(libPattern); } private Platform(OS os) { this.os = os; this.cpu = determineCPU(); String libpattern; switch (os) { case WINDOWS: libpattern = ".*\\.dll$"; break; case DARWIN: libpattern = "lib.*\\.(dylib|jnilib)$"; break; default: libpattern = "lib.*\\.so.*$"; break; } libPattern = Pattern.compile(libpattern); this.addressSize = calculateAddressSize(cpu); this.longSize = os == OS.WINDOWS ? 32 : addressSize; } private static int calculateAddressSize(CPU cpu) { int dataModel = Integer.getInteger("sun.arch.data.model"); if (dataModel != 32 && dataModel != 64) { switch (cpu) { case I386: case PPC: case SPARC: dataModel = 32; break; case X86_64: case PPC64: case SPARCV9: case S390X: dataModel = 64; break; default: throw new ExceptionInInitializerError("Cannot determine cpu address size"); } } return dataModel; } /** * Gets the native Platform * * @return The current platform. */ public static Platform getNativePlatform() { return SingletonHolder.PLATFORM; } @Deprecated public static Platform getPlatform() { return SingletonHolder.PLATFORM; } /** * Gets the current Operating System. * * @return A OS value representing the current Operating System. */ public final OS getOS() { return os; } /** * Gets the current processor architecture the JVM is running on. * * @return A CPU value representing the current processor architecture. */ public final CPU getCPU() { return cpu; } public final boolean isBSD() { return os == OS.FREEBSD || os == OS.OPENBSD || os == OS.NETBSD || os == OS.DARWIN; } public final boolean isUnix() { return os != OS.WINDOWS; } /** * Gets the size of a C 'long' on the native platform. * * @return the size of a long in bits * @deprecated Use {@link Runtime#longSize()} instead. */ public final int longSize() { return longSize; } /** * Gets the size of a C address/pointer on the native platform. * * @return the size of a pointer in bits * @deprecated Use {@link Runtime#addressSize()} instead. */ public final int addressSize() { return addressSize; } /** * Gets the name of this Platform. * * @return The name of this platform. */ public String getName() { return cpu + "-" + os; } /** * Maps from a generic library name (e.g. "c") to the platform specific library name. * * @param libName The library name to map * @return The mapped library name. */ public String mapLibraryName(String libName) { // // A specific version was requested - use as is for search // if (libPattern.matcher(libName).find()) { return libName; } return System.mapLibraryName(libName); } /** * Searches through a list of directories for a native library. * * @param libName the base name (e.g. "c") of the library to locate * @param libraryPath the list of directories to search * @return the path of the library */ public String locateLibrary(String libName, ListWrapping a ByteBuffer is only neccessary if the native function parameter * was declared as a {@code Pointer}. The if the method will always be used * with {@code ByteBuffer} parameters, then the parameter type can just be declared * as {@code ByteBuffer} and the conversion will be performed automatically. * * @param runtime the {@code Runtime} the wrapped {@code ByteBuffer} will * be used with. * @param buffer the {@code ByteBuffer} to wrap. * * @return a {@code Pointer} instance that will proxy all accesses to the ByteBuffer contents. */ public static Pointer wrap(Runtime runtime, ByteBuffer buffer) { return runtime.getMemoryManager().newPointer(buffer); } /** * Wraps an integer value in an opaque {@link Pointer} instance. This is a Pointer instance that * throws errors when any of the memory access methods are used, but can be otherwise used interchangeably * with a real Pointer. * * @param runtime the {@code Runtime} of the pointer. * @param address the {@code address} to wrap in a Pointer instance. * * @return a {@code Pointer} instance. */ public static Pointer newIntPointer(Runtime runtime, long address) { return runtime.getMemoryManager().newOpaquePointer(address); } protected Pointer(Runtime runtime, long address, boolean direct) { this.runtime = runtime; this.address = address; isDirect = direct; } /** * Indicates whether or not this memory object represents a native memory address. * *
Memory objects can be either direct (representing native memory), or * non-direct (representing java heap memory). * *
Non-direct memory objects can still be passed to native functions as pointer * (void *, char *, etc) parameters, but the java memory will first be copied * to a temporary native memory area. The temporary memory area will then be * used as the parameter value for the call. If needed, the java memory * will be automatically reloaded from the temporary native memory after the * native function returns. *
Note: the transient nature of the temporary memory allocated for * non-direct memory means native functions which store the address value * passed to them will fail in unpredictable ways when using non-direct memory. * You will need to explicitly allocate direct memory to use those types of * functions. * * @return true if, and only if, this memory object represents a native address. */ public final boolean isDirect() { return isDirect; } /** * Gets the native address of this memory object (optional operation). * * @return the native address of this memory object. If this object is not * a native memory address, an address of zero is returned. */ public final long address() { return address; } /** * Gets the {@link Runtime} this {@code Pointer} instance belongs to. * * @return the {@code Runtime} instance of this {@code Pointer}. */ public final Runtime getRuntime() { return runtime; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getName()); sb.append(String.format("[address=%#x", address())); if (size() != Long.MAX_VALUE) { sb.append(String.format(" size=%d", size())); } sb.append(']'); return sb.toString(); } /** * Gets the size of this memory object in bytes (optional operation). * * @return the size of the memory area this {@code Pointer} points to. If * the size is unknown, {@link java.lang.Long#MAX_VALUE} is returned}. */ abstract public long size(); /** * Indicates whether this Pointer instance is backed by an array. * * @return true if, and only if, this memory object is backed by an array */ abstract public boolean hasArray(); /** * Returns the array that backs this pointer. * * @throws {@link java.lang.UnsupportedOperationException} if this pointer does not have a backing array * @return The array that backs this pointer */ abstract public Object array(); /** * Returns the offset within this pointer's backing array of the first element. * * @throws {@link java.lang.UnsupportedOperationException} if this pointer does not have a backing array * @return The offset of the first element on the backing array */ abstract public int arrayOffset(); /** * Returns the length of this pointer's backing array that is used by this pointer. * * @throws {@link UnsupportedOperationException} if this pointer does not have a backing array * @return The length of the backing array used */ abstract public int arrayLength(); /** * Reads an {@code byte} (8 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the {@code byte} value at the offset. */ abstract public byte getByte(long offset); /** * Reads a {@code short} (16 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the {@code short} value at the offset. */ abstract public short getShort(long offset); /** * Reads an {@code int} (32 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the {@code int} value contained in the memory at the offset. */ abstract public int getInt(long offset); /** * Reads a {@code long} (64 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the {@code long} value at the offset. */ abstract public long getLong(long offset); /** * Reads a {@code long} (64 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the {@code long} value at the offset. */ abstract public long getLongLong(long offset); /** * Reads a {@code float} (32 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the {@code float} value at the offset. */ abstract public float getFloat(long offset); /** * Reads a {@code double} (64 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the {@code double} value at the offset. */ abstract public double getDouble(long offset); /** * Reads a native {@code long} value at the given offset. *
A native {@code long} can be either 32 or 64 bits in size, depending * on the cpu architecture, and the C ABI in use. * *
For windows, a long is always 32 bits (4 bytes) in size, but on unix * systems, a long on a 32bit system is 32 bits, and on a 64bit system, is 64 bits. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the native {@code long} value at the offset. * * @see NativeLong */ abstract public long getNativeLong(long offset); /** * Reads an integer value of the given type, at the given offset. * * @param type Type of integer to read. * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the {@code int} value contained in the memory at the offset. */ abstract public long getInt(Type type, long offset); /** * Writes a {@code byte} (8 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value the {@code byte} value to be written. */ abstract public void putByte(long offset, byte value); /** * Writes a {@code short} (16 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value the {@code short} value to be written. */ abstract public void putShort(long offset, short value); /** * Writes an {@code int} (32 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value the {@code int} value to be written. */ abstract public void putInt(long offset, int value); /** * Writes a {@code native long} value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value the {@code long} value to be written. */ abstract public void putLong(long offset, long value); /** * Writes a {@code long} (64 bit) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value the {@code long} value to be written. */ abstract public void putLongLong(long offset, long value); /** * Writes a {@code float} (32 bit, single precision) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value the {@code float} value to be written. */ abstract public void putFloat(long offset, float value); /** * Writes a {@code double} (64 bit, double precision) value at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value the {@code double} value to be written. */ abstract public void putDouble(long offset, double value); /** * Writes a native {@code long} value at the given offset. * *
A native {@code long} can be either 32 or 64 bits in size, depending * on the cpu architecture, and the C ABI in use. * *
For windows, a long is always 32 bits (4 bytes) in size, but on unix * systems, a long on a 32bit system is 32 bits, and on a 64bit system, is 64 bits. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value the native {@code long} value to be written. */ abstract public void putNativeLong(long offset, long value); /** * Writes an integer of a specific type, at the given offset. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value the {@code int} value to be written. */ abstract public void putInt(Type type, long offset, long value); /** * Reads a native memory address value at the given offset. *
A native address can be either 32 or 64 bits in size, depending * on the cpu architecture. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be read. * @return the native address value contained in the memory at the offset * * @see Address */ abstract public long getAddress(long offset); /** * Writes a native memory address value at the given offset. *
A native address can be either 32 or 64 bits in size, depending * on the cpu architecture. * * @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written. * @param value The native address value to be written. * * @see Address */ abstract public void putAddress(long offset, long value); /** * Writes a native memory address value at the given offset. *
A native address can be either 32 or 64 bits in size, depending
* on the cpu architecture.
*
* @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written.
* @param value The native address value to be written.
*
* @see Address
*/
abstract public void putAddress(long offset, Address value);
/**
* Bulk get method for multiple {@code byte} values.
*
* This method reads multiple {@code byte} values from consecutive addresses,
* beginning at the given offset, and stores them in an array.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the first value will be read.
* @param dst the array into which values are to be stored.
* @param idx the start index in the {@code dst} array to begin storing the values.
* @param len the number of values to be read.
*/
abstract public void get(long offset, byte[] dst, int idx, int len);
/**
* Bulk put method for multiple {@code byte} values.
*
* This method writes multiple {@code byte} values to consecutive addresses,
* beginning at the given offset, from an array.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the first value will be written.
* @param src the array to get values from.
* @param idx the start index in the {@code dst} array to begin reading values.
* @param len the number of values to be written.
*/
abstract public void put(long offset, byte[] src, int idx, int len);
/**
* Bulk get method for multiple {@code short} values.
*
* This method reads multiple {@code short} values from consecutive addresses,
* beginning at the given offset, and stores them in an array.
*
* @param offset The offset from the start of the memory this {@code Pointer} represents at which the first value will be read.
* @param dst The array into which values are to be stored.
* @param idx the start index in the {@code dst} array to begin storing the values.
* @param len the number of values to be read.
*/
abstract public void get(long offset, short[] dst, int idx, int len);
/**
* Bulk put method for multiple {@code short} values.
*
* This method writes multiple {@code short} values to consecutive addresses,
* beginning at the given offset, from an array.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the first value will be written.
* @param src the array to get values from.
* @param idx the start index in the {@code dst} array to begin reading values.
* @param len the number of values to be written.
*/
abstract public void put(long offset, short[] src, int idx, int len);
/**
* Bulk get method for multiple {@code int} values.
*
* This method reads multiple {@code int} values from consecutive addresses,
* beginning at the given offset, and stores them in an array.
*
* @param offset The offset from the start of the memory this {@code Pointer} represents at which the first value will be read.
* @param dst The array into which values are to be stored.
* @param idx the start index in the {@code dst} array to begin storing the values.
* @param len the number of values to be read.
*/
abstract public void get(long offset, int[] dst, int idx, int len);
/**
* Bulk put method for multiple {@code int} values.
*
* This method writes multiple {@code int} values to consecutive addresses,
* beginning at the given offset, from an array.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the first value will be written.
* @param src the array to get values from.
* @param idx the start index in the {@code dst} array to begin reading values.
* @param len the number of values to be written.
*/
abstract public void put(long offset, int[] src, int idx, int len);
/**
* Bulk get method for multiple {@code long} values.
*
* This method reads multiple {@code long} values from consecutive addresses,
* beginning at the given offset, and stores them in an array.
*
* @param offset The offset from the start of the memory this {@code Pointer} represents at which the first value will be read.
* @param dst The array into which values are to be stored.
* @param idx the start index in the {@code dst} array to begin storing the values.
* @param len the number of values to be read.
*/
abstract public void get(long offset, long[] dst, int idx, int len);
/**
* Bulk put method for multiple {@code long} values.
*
* This method writes multiple {@code long} values to consecutive addresses,
* beginning at the given offset, from an array.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the first value will be written.
* @param src the array to get values from.
* @param idx the start index in the {@code dst} array to begin reading values.
* @param len the number of values to be written.
*/
abstract public void put(long offset, long[] src, int idx, int len);
/**
* Bulk get method for multiple {@code float} values.
*
* This method reads multiple {@code float} values from consecutive addresses,
* beginning at the given offset, and stores them in an array.
*
* @param offset The offset from the start of the memory this {@code Pointer} represents at which the first value will be read.
* @param dst The array into which values are to be stored.
* @param idx the start index in the {@code dst} array to begin storing the values.
* @param len the number of values to be read.
*/
abstract public void get(long offset, float[] dst, int idx, int len);
/**
* Bulk put method for multiple {@code float} values.
*
* This method writes multiple {@code float} values to consecutive addresses,
* beginning at the given offset, from an array.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the first value will be written.
* @param src the array to get values from.
* @param idx the start index in the {@code dst} array to begin reading values.
* @param len the number of values to be written.
*/
abstract public void put(long offset, float[] src, int idx, int len);
/**
* Bulk get method for multiple {@code double} values.
*
* This method reads multiple {@code double} values from consecutive addresses,
* beginning at the given offset, and stores them in an array.
*
* @param offset The offset from the start of the memory this {@code Pointer} represents at which the first value will be read.
* @param dst The array into which values are to be stored.
* @param idx the start index in the {@code dst} array to begin storing the values.
* @param len the number of values to be read.
*/
abstract public void get(long offset, double[] dst, int idx, int len);
/**
* Bulk put method for multiple {@code double} values.
*
* This method writes multiple {@code double} values to consecutive addresses,
* beginning at the given offset, from an array.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the first value will be written.
* @param src the array to get values from.
* @param idx the start index in the {@code dst} array to begin reading values.
* @param len the number of values to be written.
*/
abstract public void put(long offset, double[] src, int idx, int len);
/**
* Reads an {@code Pointer} value at the given offset.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the value will be read.
* @return the {@code Pointer} value read from memory.
*/
abstract public Pointer getPointer(long offset);
/**
* Reads an {@code Pointer} value at the given offset.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the value will be read.
* @param size the maximum size of the memory location the returned {@code Pointer} represents.
* @return the {@code Pointer} value read from memory.
*/
abstract public Pointer getPointer(long offset, long size);
/**
* Writes a {@code Pointer} value at the given offset.
*
* @param offset The offset from the start of the memory this {@code Pointer} represents at which the value will be written.
* @param value the {@code Pointer} value to be written to memory.
*/
abstract public void putPointer(long offset, Pointer value);
/**
* Reads an {@code String} value at the given offset.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the value will be read.
* @return the {@code String} value read from memory.
*/
abstract public String getString(long offset);
/**
* Reads a {@code String} value at the given offset, using a specific {@code Charset}
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the value will be read.
* @param maxLength the maximum size of memory to search for a NUL byte.
* @param cs the {@code Charset} to use to decode the string.
* @return the {@code String} value read from memory.
*/
abstract public String getString(long offset, int maxLength, Charset cs);
/**
* Writes a {@code String} value at the given offset, using a specific {@code Charset}
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the value will be written.
* @param maxLength the maximum size of memory to use to store the string.
* @param cs the {@code Charset} to use to decode the string.
*/
abstract public void putString(long offset, String string, int maxLength, Charset cs);
/**
* Creates a new {@code Pointer} representing a sub-region of the memory
* referred to by this {@code Pointer}.
*
* @param offset the offset from the start of the memory this {@code Pointer}
* represents at which the new {@code Pointer} will start.
* @return a {@code Pointer} instance representing the new sub-region.
*/
abstract public Pointer slice(long offset);
/**
* Creates a new {@code Pointer} representing a sub-region of the memory
* referred to by this {@code Pointer}.
*
* @param offset the offset from the start of the memory this {@code Pointer}
* represents at which the new {@code Pointer} will start.
* @param size the maximum size of the memory sub-region.
*
* @return a {@code Pointer} instance representing the new sub-region.
*/
abstract public Pointer slice(long offset, long size);
/**
* Bulk data transfer from one memory location to another.
*
* @param offset the offset from the start of the memory location this {@code Pointer} represents to begin copying from.
* @param dst the destination memory location to transfer data to.
* @param dstOffset the offset from the start of the memory location the destination {@code Pointer} represents to begin copying to.
* @param count the number of bytes to transfer.
*/
abstract public void transferTo(long offset, Pointer dst, long dstOffset, long count);
/**
* Bulk data transfer from one memory location to another.
*
* @param offset the offset from the start of the memory location this {@code Pointer} represents to begin copying to.
* @param src the destination memory location to transfer data from.
* @param srcOffset the offset from the start of the memory location the destination {@code Pointer} represents to begin copying from.
* @param count the number of bytes to transfer.
*/
abstract public void transferFrom(long offset, Pointer src, long srcOffset, long count);
/**
* Checks that the memory region is within the bounds of this memory object
*
* @param offset the starting point within this memory region.
* @param length the length of the memory region in bytes
* @throws java.lang.IndexOutOfBoundsException
*/
abstract public void checkBounds(long offset, long length);
/**
* Sets the value of each byte in the memory area represented by this {@code Pointer}.
* to a specified value.
*
* @param offset the offset from the start of the memory location this {@code Pointer} represents to begin writing to.
* @param size the number of bytes to set to the value.
* @param value the value to set each byte to.
*/
abstract public void setMemory(long offset, long size, byte value);
/**
* Returns the location of a byte value within the memory area represented by this {@code Pointer}.
*
* @param offset the offset from the start of the memory location this {@code Pointer} represents to begin searching.
* @param value the {@code byte} value to locate.
* @return the offset from the start of the search area (i.e. relative to the offset parameter), or -1 if not found.
*/
abstract public int indexOf(long offset, byte value);
/**
* Returns the location of a byte value within the memory area represented by this {@code Pointer}.
*
* @param offset the offset from the start of the memory location this {@code Pointer} represents to begin searching.
* @param value the {@code byte} value to locate.
* @param maxlen the maximum number of bytes to search for the desired value.
* @return the offset from the start of the search area (i.e. relative to the offset parameter), or -1 if not found.
*/
abstract public int indexOf(long offset, byte value, int maxlen);
/**
* Bulk get method for multiple {@code Pointer} values.
*
* This method reads multiple {@code Pointer} values from consecutive addresses,
* beginning at the given offset, and stores them in an array.
*
* @param offset The offset from the start of the memory this {@code Pointer} represents at which the first value will be read.
* @param dst The array into which values are to be stored.
* @param idx the start index in the {@code dst} array to begin storing the values.
* @param len the number of values to be read.
*/
public void get(long offset, Pointer[] dst, int idx, int len) {
final int pointerSize = getRuntime().addressSize();
for (int i = 0; i < len; i++) {
dst[idx + i] = getPointer(offset + (i * pointerSize));
}
}
/**
* Bulk put method for multiple {@code Pointer} values.
*
* This method writes multiple {@code Pointer} values to consecutive addresses,
* beginning at the given offset, from an array.
*
* @param offset the offset from the start of the memory this {@code Pointer} represents at which the first value will be written.
* @param src the array to get values from.
* @param idx the start index in the {@code src} array to begin reading values.
* @param len the number of values to be written.
*/
public void put(long offset, Pointer[] src, int idx, int len) {
final int pointerSize = getRuntime().addressSize();
for (int i = 0; i < len; i++) {
putPointer(offset + (i * pointerSize), src[idx + i]);
}
}
public String[] getNullTerminatedStringArray(long offset) {
Pointer ptr;
if ((ptr = getPointer(offset)) == null) {
return new String[0];
}
final int pointerSize = getRuntime().addressSize();
List
* This class is needed by many classes to correctly initialize internal data structures, and each library loaded
* has its own instance of this class.
*
* To obtain an instance of this class, use {@link #getRuntime(Object)} on a loaded library.
*
* Example
*
* {@code
*
* public interface LibC {
* public long write(int fd, Pointer data, long len);
* }
*
* LibC library = LibraryLoader.create(LibC.class).load("c");
*
* byte[] bytes = "Hello, World\n".getBytes("UTF-8");
*
* // Use the loaded library's Runtime to allocate memory for the string
* jnr.ffi.Runtime runtime = jnr.ffi.Runtime.getRuntime(library);
* Pointer buffer = Memory.allocateDirect(runtime, bytes.length);
*
* // Copy the java string data to the native memory, then write the contents to STDOUT
* buffer.put(0, bytes, 0, bytes.length);
* library.write(1, buffer, bytes.length);
* }
*
*
* This returns the errno value that was set at the time of the last native * function call. * * @return The errno value. */ public abstract int getLastError(); /** * Sets the native error code. * * @param error The value to set errno to. */ public abstract void setLastError(int error); /** * Gets the address mask for this runtime * * @return The address mask for the runtime. */ public abstract long addressMask(); /** * Gets the size of an address (e.g. a pointer) for this runtime * * @return The size of an address in bytes. */ public abstract int addressSize(); /** * Gets the size of a C long integer for this runtime * * @return The size of a C long integer in bytes. */ public abstract int longSize(); /** * Gets the native byte order of the runtime. * * @return The byte order of the runtime */ public abstract ByteOrder byteOrder(); /** * Indicates whether this Runtime instance is compatible with another Runtime instance. * *
* This is not the same as calling {@link #equals} - this method only indicates whether or not artifacts from the * runtime (e.g. memory addresses) are compatible with artifacts from this one. *
* ** This is mostly for internal use. *
* * @param other the other runtime to test for compatibility * @return true if the other runtime is compatible with this one */ public abstract boolean isCompatible(jnr.ffi.Runtime other); } jnr-ffi-1.0.10/src/main/java/jnr/ffi/Struct.java 0000664 0000000 0000000 00000175163 12227113360 0021256 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * Some of the design and code of this class is from the javolution project. * * Copyright (C) 2006 - Javolution (http://javolution.org/) * All rights reserved. * * Permission to use, copy, modify, and distribute this software is * freely granted, provided that this notice is preserved. */ package jnr.ffi; import jnr.ffi.provider.ParameterFlags; import jnr.ffi.util.EnumMapper; import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.nio.charset.Charset; /** * Representation of C structures in java. * * Note: This class is not threadsafe. */ public abstract class Struct { static final Charset ASCII = Charset.forName("ASCII"); static final Charset UTF8 = Charset.forName("UTF-8"); static final class Info { private final Runtime runtime; private jnr.ffi.Pointer memory = null; Struct enclosing = null; int offset = 0; // offset within enclosing Struct int size = 0; int minAlign = 1; boolean isUnion = false; boolean resetIndex = false; public Info(Runtime runtime) { this.runtime = runtime; } public final jnr.ffi.Pointer getMemory(int flags) { return enclosing != null ? enclosing.__info.getMemory(flags) : memory != null ? memory : (memory = allocateMemory(flags)); } public final jnr.ffi.Pointer getMemory() { return getMemory(ParameterFlags.TRANSIENT); } final boolean isDirect() { return (enclosing != null && enclosing.__info.isDirect()) || (memory != null && memory.isDirect()); } final int size() { return size; } final int getMinimumAlignment() { return minAlign; } private jnr.ffi.Pointer allocateMemory(int flags) { if (ParameterFlags.isDirect(flags)) { return runtime.getMemoryManager().allocateDirect(size(), true); } else { return runtime.getMemoryManager().allocate(size()); } } public final void useMemory(jnr.ffi.Pointer io) { this.memory = io; } protected final int addField(int sizeBits, int alignBits, Offset offset) { this.size = Math.max(this.size, offset.intValue() + (sizeBits >> 3)); this.minAlign = Math.max(this.minAlign, alignBits >> 3); return offset.intValue(); } protected final int addField(int sizeBits, int alignBits) { final int off = resetIndex ? 0 : align(this.size, alignBits >> 3); this.size = Math.max(this.size, off + (sizeBits >> 3)); this.minAlign = Math.max(this.minAlign, alignBits >> 3); return off; } } final Info __info; /** * Creates a new Struct. */ protected Struct(Runtime runtime) { this.__info = new Info(runtime); } /** * Creates a new Struct. * * @param isUnion if this Struct is a Union */ Struct(Runtime runtime, final boolean isUnion) { this(runtime); __info.resetIndex = isUnion; } public final Runtime getRuntime() { return __info.runtime; } /** * Uses the specified memory address as the backing store for this structure. * * @param address the native memory area. */ public final void useMemory(jnr.ffi.Pointer address) { __info.useMemory(address); } public static jnr.ffi.Pointer getMemory(Struct struct) { return struct.__info.getMemory(0); } public static jnr.ffi.Pointer getMemory(Struct struct, int flags) { return struct.__info.getMemory(flags); } public static int size(Struct struct) { return struct.__info.size(); } public static int alignment(Struct struct) { return struct.__info.getMinimumAlignment(); } public static boolean isDirect(Struct struct) { return struct.__info.isDirect(); } private static int align(int offset, int align) { return (offset + align - 1) & ~(align - 1); } @SuppressWarnings("unchecked") public staticAddress
.
*
* @return a string representation of this Address
.
*/
@Override
public java.lang.String toString() {
return java.lang.Boolean.toString(get());
}
}
/**
* A normal C boolean - 1 byte in size
*/
public final class Boolean extends AbstractBoolean {
public Boolean() {
super(NativeType.SCHAR);
}
public final boolean get() {
return (getMemory().getByte(offset()) & 0x1) != 0;
}
public final void set(boolean value) {
getMemory().putByte(offset(), (byte) (value ? 1 : 0));
}
}
/**
* A Windows BOOL - 4 bytes
*/
public final class WBOOL extends AbstractBoolean {
public WBOOL() {
super(NativeType.SINT);
}
public final boolean get() {
return (getMemory().getInt(offset()) & 0x1) != 0;
}
public final void set(boolean value) {
getMemory().putInt(offset(), value ? 1 : 0);
}
}
/**
* Base class for all Number structure fields.
*/
public abstract class NumberField extends Member {
/**
* Offset from the start of the Struct memory this field is located at.
*/
private final int offset;
protected final Type type;
protected NumberField(NativeType type) {
Type t = this.type = getRuntime().findType(type);
this.offset = __info.addField(t.size() * 8, t.alignment() * 8);
}
protected NumberField(NativeType type, Offset offset) {
Type t = this.type = getRuntime().findType(type);
this.offset = __info.addField(t.size() * 8, t.alignment() * 8, offset);
}
protected NumberField(TypeAlias type) {
Type t = this.type = getRuntime().findType(type);
this.offset = __info.addField(t.size() * 8, t.alignment() * 8);
}
protected NumberField(TypeAlias type, Offset offset) {
Type t = this.type = getRuntime().findType(type);
this.offset = __info.addField(t.size() * 8, t.alignment() * 8, offset);
}
public final jnr.ffi.Pointer getMemory() {
return __info.getMemory();
}
/**
* Gets the Struct this Member is in.
*
* @return a Struct.
*/
public final Struct struct() {
return Struct.this;
}
/**
* Gets the offset within the structure for this field.
*/
public final long offset() {
return offset + __info.offset;
}
/**
* Sets the field to a new value.
*
* @param value The new value.
*/
public abstract void set(java.lang.Number value);
/**
* Returns an {@code float} representation of this Number.
*
* @return an {@code float} value for this Number.
*/
public double doubleValue() {
return (double) longValue();
}
/**
* Returns an {@code float} representation of this Number.
*
* @return an {@code float} value for this Number.
*/
public float floatValue() {
return (float) intValue();
}
/**
* Returns a {@code byte} representation of this Number.
*
* @return a {@code byte} value for this Number.
*/
public byte byteValue() {
return (byte) intValue();
}
/**
* Returns a {@code short} representation of this Number.
*
* @return a {@code short} value for this Number.
*/
public short shortValue() {
return (short) intValue();
}
/**
* Returns a {@code int} representation of this Number.
*
* @return a {@code int} value for this Number.
*/
public abstract int intValue();
/**
* Returns a {@code long} representation of this Number.
*
* @return a {@code long} value for this Number.
*/
public long longValue() {
return intValue();
}
/**
* Returns a string representation of this Address
.
*
* @return a string representation of this Address
.
*/
@Override
public java.lang.String toString() {
return java.lang.Integer.toString(intValue(), 10);
}
}
public abstract class IntegerAlias extends NumberField {
IntegerAlias(TypeAlias type) {
super(type);
}
IntegerAlias(TypeAlias type, Offset offset) {
super(type, offset);
}
@Override
public void set(Number value) {
getMemory().putInt(type, offset(), value.longValue());
}
public void set(long value) {
getMemory().putInt(type, offset(), value);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get() {
return getMemory().getInt(type, offset());
}
@Override
public int intValue() {
return (int) get();
}
@Override
public long longValue() {
return get();
}
}
/**
* An 8 bit signed integer
*/
public class Signed8 extends NumberField {
/**
* Creates a new 8 bit integer field.
*/
public Signed8() {
super(NativeType.SCHAR);
}
/**
* Creates a new 8 bit integer field at a specific offset
*
* @param offset The offset within the memory area
*/
public Signed8(Offset offset) {
super(NativeType.SCHAR, offset);
}
/**
* Gets the value for this field.
*
* @return a byte.
*/
public final byte get() {
return getMemory().getByte(offset());
}
/**
* Sets the value for this field.
*
* @param value the 8 bit value to set.
*/
public final void set(byte value) {
getMemory().putByte(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putByte(offset(), value.byteValue());
}
/**
* Returns a java byte representation of this field.
*
* @return a java byte value for this field.
*/
@Override
public final byte byteValue() {
return get();
}
/**
* Returns a java short representation of this field.
*
* @return a java short value for this field.
*/
@Override
public final short shortValue() {
return get();
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return get();
}
}
/**
* An 8 bit unsigned integer
*/
public class Unsigned8 extends NumberField {
/**
* Creates a new 8 bit unsigned integer field.
*/
public Unsigned8() {
super(NativeType.UCHAR);
}
/**
* Creates a new 8 bit unsigned integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Unsigned8(Offset offset) {
super(NativeType.UCHAR, offset);
}
/**
* Gets the value for this field.
*
* @return a byte.
*/
public final short get() {
short value = getMemory().getByte(offset());
return value < 0 ? (short) ((value & 0x7F) + 0x80) : value;
}
/**
* Sets the value for this field.
*
* @param value the 8 bit value to set.
*/
public final void set(short value) {
getMemory().putByte(offset(), (byte) value);
}
public void set(java.lang.Number value) {
getMemory().putByte(offset(), value.byteValue());
}
/**
* Returns a java short representation of this field.
*
* @return a java short value for this field.
*/
@Override
public final short shortValue() {
return get();
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return get();
}
}
/**
* A 16 bit signed integer field.
*/
public class Signed16 extends NumberField {
/**
* Creates a new 16 bit integer field.
*/
public Signed16() {
super(NativeType.SSHORT);
}
/**
* Creates a new 16 bit signed integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Signed16(Offset offset) {
super(NativeType.SSHORT, offset);
}
/**
* Gets the value for this field.
*
* @return a short.
*/
public final short get() {
return getMemory().getShort(offset());
}
/**
* Sets the value for this field.
*
* @param value the 16 bit value to set.
*/
public final void set(short value) {
getMemory().putShort(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putShort(offset(), value.shortValue());
}
/**
* Returns a java short representation of this field.
*
* @return a java short value for this field.
*/
@Override
public final short shortValue() {
return get();
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return get();
}
}
/**
* A 16 bit signed integer field.
*/
public class Unsigned16 extends NumberField {
/**
* Creates a new 16 bit integer field.
*/
public Unsigned16() {
super(NativeType.USHORT);
}
/**
* Creates a new 16 bit unsigned integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Unsigned16(Offset offset) {
super(NativeType.USHORT, offset);
}
/**
* Gets the value for this field.
*
* @return a short.
*/
public final int get() {
int value = getMemory().getShort(offset());
return value < 0 ? (int)((value & 0x7FFF) + 0x8000) : value;
}
/**
* Sets the value for this field.
*
* @param value the 16 bit unsigned value to set.
*/
public final void set(int value) {
getMemory().putShort(offset(), (short) value);
}
public void set(Number value) {
getMemory().putShort(offset(), value.shortValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return get();
}
}
/**
* A 32 bit signed integer field.
*/
public class Signed32 extends NumberField {
/**
* Creates a new 32 bit integer field.
*/
public Signed32() {
super(NativeType.SINT);
}
/**
* Creates a new 32 bit signed integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Signed32(Offset offset) {
super(NativeType.SINT, offset);
}
/**
* Gets the value for this field.
*
* @return a int.
*/
public final int get() {
return getMemory().getInt(offset());
}
/**
* Sets the value for this field.
*
* @param value the 32 bit value to set.
*/
public final void set(int value) {
getMemory().putInt(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putInt(offset(), value.intValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return get();
}
}
/**
* A 32 bit signed integer field.
*/
public class Unsigned32 extends NumberField {
/**
* Creates a new 32 bit integer field.
*/
public Unsigned32() {
super(NativeType.UINT);
}
/**
* Creates a new 32 bit unsigned integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Unsigned32(Offset offset) {
super(NativeType.UINT, offset);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get() {
long value = getMemory().getInt(offset());
return value < 0 ? (long)((value & 0x7FFFFFFFL) + 0x80000000L) : value;
}
/**
* Sets the value for this field.
*
* @param value the 32 bit unsigned value to set.
*/
public final void set(long value) {
getMemory().putInt(offset(), (int) value);
}
public void set(java.lang.Number value) {
getMemory().putInt(offset(), value.intValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return (int) get();
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue() {
return get();
}
}
/**
* A 64 bit signed integer field.
*/
public class Signed64 extends NumberField {
/**
* Creates a new 64 bit integer field.
*/
public Signed64() {
super(NativeType.SLONGLONG);
}
/**
* Creates a new 64 bit signed integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Signed64(Offset offset) {
super(NativeType.SLONGLONG, offset);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get() {
return getMemory().getLongLong(offset());
}
/**
* Sets the value for this field.
*
* @param value the 64 bit value to set.
*/
public final void set(long value) {
getMemory().putLongLong(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putLongLong(offset(), value.longValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return (int) get();
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue() {
return get();
}
/**
* Returns a string representation of this field.
*
* @return a string representation of this field.
*/
@Override
public final java.lang.String toString() {
return java.lang.Long.toString(get());
}
}
/**
* A 64 bit unsigned integer field.
*/
public class Unsigned64 extends NumberField {
/**
* Creates a new 64 bit integer field.
*/
public Unsigned64() {
super(NativeType.ULONGLONG);
}
/**
* Creates a new 64 bit unsigned integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Unsigned64(Offset offset) {
super(NativeType.ULONGLONG, offset);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get() {
return getMemory().getLongLong(offset());
}
/**
* Sets the value for this field.
*
* @param value the 64 bit value to set.
*/
public final void set(long value) {
getMemory().putLongLong(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putLongLong(offset(), value.longValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return (int) get();
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue() {
return get();
}
/**
* Returns a string representation of this field.
*
* @return a string representation of this field.
*/
@Override
public final java.lang.String toString() {
return java.lang.Long.toString(get());
}
}
/**
* A native long integer field.
*/
public class SignedLong extends NumberField {
/**
* Creates a new native long field.
*/
public SignedLong() {
super(NativeType.SLONG);
}
/**
* Creates a new signed native long field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public SignedLong(Offset offset) {
super(NativeType.SLONG, offset);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get() {
return getMemory().getNativeLong(offset());
}
/**
* Sets the value for this field.
*
* @param value the 32/64 bit value to set.
*/
public final void set(long value) {
getMemory().putNativeLong(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putNativeLong(offset(), value.longValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return (int) get();
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue() {
return get();
}
/**
* Returns a string representation of this field.
*
* @return a string representation of this field.
*/
@Override
public final java.lang.String toString() {
return java.lang.Long.toString(get());
}
}
/**
* A native long integer field.
*/
public class UnsignedLong extends NumberField {
/**
* Creates a new native long field.
*/
public UnsignedLong() {
super(NativeType.ULONG);
}
/**
* Creates a new unsigned native long field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public UnsignedLong(Offset offset) {
super(NativeType.ULONG, offset);
}
/**
* Gets the value for this field.
*
* @return a int.
*/
public final long get() {
long value = getMemory().getNativeLong(offset());
final long mask = getRuntime().findType(NativeType.SLONG).size() == 32 ? 0xffffffffL : 0xffffffffffffffffL;
return value < 0
? (long) ((value & mask) + mask + 1)
: value;
}
/**
* Sets the value for this field.
*
* @param value the 32/64 bit value to set.
*/
public final void set(long value) {
getMemory().putNativeLong(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putNativeLong(offset(), value.longValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue() {
return (int) get();
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue() {
return get();
}
/**
* Returns a string representation of this field.
*
* @return a string representation of this field.
*/
@Override
public final java.lang.String toString() {
return java.lang.Long.toString(get());
}
}
public class Float extends NumberField {
public Float() {
super(NativeType.FLOAT);
}
/**
* Creates a new float field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Float(Offset offset) {
super(NativeType.FLOAT, offset);
}
public final float get() {
return getMemory().getFloat(offset());
}
public final void set(float value) {
getMemory().putFloat(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putFloat(offset(), value.floatValue());
}
@Override
public final int intValue() {
return (int) get();
}
@Override
public final double doubleValue() {
return get();
}
@Override
public final float floatValue() {
return get();
}
@Override
public final long longValue() {
return (long) get();
}
@Override
public final java.lang.String toString() {
return java.lang.String.valueOf(get());
}
}
public final class Double extends NumberField {
public Double() {
super(NativeType.DOUBLE);
}
public Double(Offset offset) {
super(NativeType.DOUBLE, offset);
}
public final double get() {
return getMemory().getDouble(offset());
}
public final void set(double value) {
getMemory().putDouble(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putDouble(offset(), value.doubleValue());
}
@Override
public final int intValue() {
return (int) get();
}
@Override
public final long longValue() {
return (long) get();
}
@Override
public final float floatValue() {
return (float) get();
}
@Override
public final double doubleValue() {
return get();
}
@Override
public final java.lang.String toString() {
return java.lang.String.valueOf(get());
}
}
/**
* Represents a native memory address.
*/
public class Address extends NumberField {
/**
* Creates a new Address field.
*/
public Address() {
super(NativeType.ADDRESS);
}
public Address(Offset offset) {
super(NativeType.ADDRESS, offset);
}
/**
* Reads an {@code Address} value from the struct.
*
* @return a {@link jnr.ffi.Address}.
*/
public final jnr.ffi.Address get() {
return jnr.ffi.Address.valueOf(getMemory().getAddress(offset()));
}
/**
* Puts a {@link jnr.ffi.Address} value into the native memory.
*/
public final void set(jnr.ffi.Address value) {
getMemory().putAddress(offset(), value != null ? value.nativeAddress() : 0);
}
public void set(java.lang.Number value) {
getMemory().putAddress(offset(), value.longValue());
}
/**
* Returns an integer representation of this address.
*
* @return an integer value for this address.
*/
@Override
public final int intValue() {
return get().intValue();
}
/**
* Returns an {@code long} representation of this address.
*
* @return an {@code long} value for this address.
*/
@Override
public final long longValue() {
return get().longValue();
}
/**
* Returns a string representation of this Address
.
*
* @return a string representation of this Address
.
*/
@Override
public final java.lang.String toString() {
return get().toString();
}
}
/**
* Represents a native memory address.
*/
public class Pointer extends NumberField {
/**
* Creates a new Address field.
*/
public Pointer() {
super(NativeType.ADDRESS);
}
public Pointer(Offset offset) {
super(NativeType.ADDRESS, offset);
}
/**
* Gets the {@link jnr.ffi.Pointer} value from the native memory.
*
* @return a {@link jnr.ffi.Pointer}.
*/
public final jnr.ffi.Pointer get() {
return getMemory().getPointer(offset());
}
/**
* Gets the size of a Pointer in bits
*
* @return the size of the Pointer
*/
public final int size() {
return getRuntime().findType(NativeType.ADDRESS).size() * 8;
}
/**
* Puts a {@link jnr.ffi.Address} value into the native memory.
*/
public final void set(jnr.ffi.Pointer value) {
getMemory().putPointer(offset(), value);
}
public void set(java.lang.Number value) {
getMemory().putAddress(offset(), value.longValue());
}
/**
* Returns an integer representation of this Pointer
.
*
* @return an integer value for this Pointer
.
*/
@Override
public final int intValue() {
return (int) getMemory().getAddress(offset());
}
/**
* Returns an {@code long} representation of this Pointer
.
*
* @return an {@code long} value for this Pointer
.
*/
@Override
public final long longValue() {
return getMemory().getAddress(offset());
}
/**
* Returns a string representation of this Pointer
.
*
* @return a string representation of this Pointer
.
*/
@Override
public final java.lang.String toString() {
return get().toString();
}
}
/**
* Base for all the Enum fields.
*
* @param Boolean
.
*
* @return a string representation of this Boolean
.
*/
public java.lang.String toString(jnr.ffi.Pointer ptr) {
return java.lang.Boolean.toString(get(ptr));
}
}
/**
* A normal C boolean - 1 byte in size
*/
protected final class Boolean extends AbstractBoolean {
protected Boolean() {
super(NativeType.SCHAR);
}
protected Boolean(Offset offset) {
super(NativeType.SCHAR, offset);
}
public final boolean get(jnr.ffi.Pointer ptr) {
return (ptr.getByte(offset()) & 0x1) != 0;
}
public final void set(jnr.ffi.Pointer ptr, boolean value) {
ptr.putByte(offset(), (byte) (value ? 1 : 0));
}
}
/**
* A Windows BOOL - 4 bytes
*/
protected final class WBOOL extends AbstractBoolean {
protected WBOOL() {
super(NativeType.SINT);
}
protected WBOOL(Offset offset) {
super(NativeType.SINT, offset);
}
public final boolean get(jnr.ffi.Pointer ptr) {
return (ptr.getInt(offset()) & 0x1) != 0;
}
public final void set(jnr.ffi.Pointer ptr, boolean value) {
ptr.putInt(offset(), value ? 1 : 0);
}
}
/**
* Base class for all Number structure fields.
*/
protected abstract class NumberField extends Field {
protected final Type type;
protected NumberField(NativeType nativeType) {
this(getRuntime().findType(nativeType));
}
protected NumberField(Type type) {
super(addField(type));
this.type = type;
}
protected NumberField(NativeType nativeType, Offset offset) {
this(getRuntime().findType(nativeType), offset);
}
protected NumberField(Type type, Offset offset) {
super(addField(type, offset));
this.type = type;
}
/**
* Sets the field to a new value.
*
* @param value The new value.
*/
public abstract void set(jnr.ffi.Pointer ptr, java.lang.Number value);
/**
* Returns an {@code float} representation of this Number.
*
* @return an {@code float} value for this Number.
*/
public double doubleValue(jnr.ffi.Pointer ptr) {
return (double) longValue(ptr);
}
/**
* Returns an {@code float} representation of this Number.
*
* @return an {@code float} value for this Number.
*/
public float floatValue(jnr.ffi.Pointer ptr) {
return (float) intValue(ptr);
}
/**
* Returns a {@code byte} representation of this Number.
*
* @return a {@code byte} value for this Number.
*/
public byte byteValue(jnr.ffi.Pointer ptr) {
return (byte) intValue(ptr);
}
/**
* Returns a {@code short} representation of this Number.
*
* @return a {@code short} value for this Number.
*/
public short shortValue(jnr.ffi.Pointer ptr) {
return (short) intValue(ptr);
}
/**
* Returns a {@code int} representation of this Number.
*
* @return a {@code int} value for this Number.
*/
public abstract int intValue(jnr.ffi.Pointer ptr);
/**
* Returns a {@code long} representation of this Number.
*
* @return a {@code long} value for this Number.
*/
public long longValue(jnr.ffi.Pointer ptr) {
return intValue(ptr);
}
/**
* Returns a string representation of this Number
.
*
* @return a string representation of this Number
.
*/
public java.lang.String toString(jnr.ffi.Pointer ptr) {
return java.lang.Integer.toString(intValue(ptr), 10);
}
}
public abstract class IntegerAlias extends NumberField {
protected IntegerAlias(TypeAlias type) {
super(getRuntime().findType(type));
}
protected IntegerAlias(TypeAlias type, Offset offset) {
super(getRuntime().findType(type), offset);
}
@Override
public void set(jnr.ffi.Pointer ptr, Number value) {
ptr.putInt(type, offset(), value.longValue());
}
public void set(jnr.ffi.Pointer ptr, long value) {
ptr.putInt(type, offset(), value);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get(jnr.ffi.Pointer ptr) {
return ptr.getInt(type, offset());
}
@Override
public int intValue(jnr.ffi.Pointer ptr) {
return (int) get(ptr);
}
@Override
public long longValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
}
/**
* An 8 bit signed integer
*/
public class Signed8 extends NumberField {
/**
* Creates a new 8 bit integer field.
*/
public Signed8() {
super(NativeType.SCHAR);
}
/**
* Creates a new 8 bit integer field at a specific offset
*
* @param offset The offset within the memory area
*/
public Signed8(Offset offset) {
super(NativeType.SCHAR, offset);
}
/**
* Gets the value for this field.
*
* @return a byte.
*/
public final byte get(jnr.ffi.Pointer ptr) {
return ptr.getByte(offset());
}
/**
* Sets the value for this field.
*
* @param ptr The memory to set the value in.
* @param value the 8 bit value to set.
*/
public final void set(jnr.ffi.Pointer ptr, byte value) {
ptr.putByte(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putByte(offset(), value.byteValue());
}
/**
* Returns a java byte representation of this field.
*
* @return a java byte value for this field.
*/
@Override
public final byte byteValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
/**
* Returns a java short representation of this field.
*
* @return a java short value for this field.
*/
@Override
public final short shortValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
}
/**
* An 8 bit unsigned integer
*/
public class Unsigned8 extends NumberField {
/**
* Creates a new 8 bit unsigned integer field.
*/
public Unsigned8() {
super(NativeType.UCHAR);
}
/**
* Creates a new 8 bit unsigned integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Unsigned8(Offset offset) {
super(NativeType.UCHAR, offset);
}
/**
* Gets the value for this field.
*
* @return a byte.
*/
public final short get(jnr.ffi.Pointer ptr) {
short value = ptr.getByte(offset());
return value < 0 ? (short) ((value & 0x7F) + 0x80) : value;
}
/**
* Sets the value for this field.
*
* @param value the 8 bit value to set.
*/
public final void set(jnr.ffi.Pointer ptr, short value) {
ptr.putByte(offset(), (byte) value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putByte(offset(), value.byteValue());
}
/**
* Returns a java short representation of this field.
*
* @return a java short value for this field.
*/
@Override
public final short shortValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
}
/**
* A 16 bit signed integer field.
*/
public class Signed16 extends NumberField {
/**
* Creates a new 16 bit integer field.
*/
public Signed16() {
super(NativeType.SSHORT);
}
/**
* Creates a new 16 bit signed integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Signed16(Offset offset) {
super(NativeType.SSHORT, offset);
}
/**
* Gets the value for this field.
*
* @return a short.
*/
public final short get(jnr.ffi.Pointer ptr) {
return ptr.getShort(offset());
}
/**
* Sets the value for this field.
*
* @param value the 16 bit value to set.
*/
public final void set(jnr.ffi.Pointer ptr, short value) {
ptr.putShort(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putShort(offset(), value.shortValue());
}
/**
* Returns a java short representation of this field.
*
* @return a java short value for this field.
*/
@Override
public final short shortValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
}
/**
* A 16 bit signed integer field.
*/
public class Unsigned16 extends NumberField {
/**
* Creates a new 16 bit integer field.
*/
public Unsigned16() {
super(NativeType.USHORT);
}
/**
* Creates a new 16 bit unsigned integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Unsigned16(Offset offset) {
super(NativeType.USHORT, offset);
}
/**
* Gets the value for this field.
*
* @return a short.
*/
public final int get(jnr.ffi.Pointer ptr) {
int value = ptr.getShort(offset());
return value < 0 ? (int)((value & 0x7FFF) + 0x8000) : value;
}
/**
* Sets the value for this field.
*
* @param value the 16 bit unsigned value to set.
*/
public final void set(jnr.ffi.Pointer ptr, int value) {
ptr.putShort(offset(), (short) value);
}
public void set(jnr.ffi.Pointer ptr, Number value) {
ptr.putShort(offset(), value.shortValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
}
/**
* A 32 bit signed integer field.
*/
public class Signed32 extends NumberField {
/**
* Creates a new 32 bit integer field.
*/
public Signed32() {
super(NativeType.SINT);
}
/**
* Creates a new 32 bit signed integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Signed32(Offset offset) {
super(NativeType.SINT, offset);
}
/**
* Gets the value for this field.
*
* @return a int.
*/
public final int get(jnr.ffi.Pointer ptr) {
return ptr.getInt(offset());
}
/**
* Sets the value for this field.
*
* @param value the 32 bit value to set.
*/
public final void set(jnr.ffi.Pointer ptr, int value) {
ptr.putInt(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putInt(offset(), value.intValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
}
/**
* A 32 bit signed integer field.
*/
public class Unsigned32 extends NumberField {
/**
* Creates a new 32 bit integer field.
*/
public Unsigned32() {
super(NativeType.UINT);
}
/**
* Creates a new 32 bit unsigned integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Unsigned32(Offset offset) {
super(NativeType.SINT, offset);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get(jnr.ffi.Pointer ptr) {
long value = ptr.getInt(offset());
return value < 0 ? (long)((value & 0x7FFFFFFFL) + 0x80000000L) : value;
}
/**
* Sets the value for this field.
*
* @param value the 32 bit unsigned value to set.
*/
public final void set(jnr.ffi.Pointer ptr, long value) {
ptr.putInt(offset(), (int) value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putInt(offset(), value.intValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return (int) get(ptr);
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
}
/**
* A 64 bit signed integer field.
*/
public class Signed64 extends NumberField {
/**
* Creates a new 64 bit integer field.
*/
public Signed64() {
super(NativeType.SLONGLONG);
}
/**
* Creates a new 64 bit signed integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Signed64(Offset offset) {
super(NativeType.SLONGLONG, offset);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get(jnr.ffi.Pointer ptr) {
return ptr.getLongLong(offset());
}
/**
* Sets the value for this field.
*
* @param value the 64 bit value to set.
*/
public final void set(jnr.ffi.Pointer ptr, long value) {
ptr.putLongLong(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putLongLong(offset(), value.longValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return (int) get(ptr);
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
/**
* Returns a string representation of this field.
*
* @return a string representation of this field.
*/
@Override
public final java.lang.String toString(jnr.ffi.Pointer ptr) {
return java.lang.Long.toString(get(ptr));
}
}
/**
* A 64 bit unsigned integer field.
*/
public class Unsigned64 extends NumberField {
/**
* Creates a new 64 bit integer field.
*/
public Unsigned64() {
super(NativeType.ULONGLONG);
}
/**
* Creates a new 64 bit unsigned integer field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Unsigned64(Offset offset) {
super(NativeType.ULONGLONG, offset);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get(jnr.ffi.Pointer ptr) {
return ptr.getLongLong(offset());
}
/**
* Sets the value for this field.
*
* @param value the 64 bit value to set.
*/
public final void set(jnr.ffi.Pointer ptr, long value) {
ptr.putLongLong(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putLongLong(offset(), value.longValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return (int) get(ptr);
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
/**
* Returns a string representation of this field.
*
* @return a string representation of this field.
*/
@Override
public final java.lang.String toString(jnr.ffi.Pointer ptr) {
return java.lang.Long.toString(get(ptr));
}
}
/**
* A native long integer field.
*/
public class SignedLong extends NumberField {
/**
* Creates a new native long field.
*/
public SignedLong() {
super(NativeType.SLONG);
}
/**
* Creates a new signed native long field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public SignedLong(Offset offset) {
super(NativeType.SLONG, offset);
}
/**
* Gets the value for this field.
*
* @return a long.
*/
public final long get(jnr.ffi.Pointer ptr) {
return ptr.getNativeLong(offset());
}
/**
* Sets the value for this field.
*
* @param value the 32/64 bit value to set.
*/
public final void set(jnr.ffi.Pointer ptr, long value) {
ptr.putNativeLong(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putNativeLong(offset(), value.longValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return (int) get(ptr);
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
/**
* Returns a string representation of this field.
*
* @return a string representation of this field.
*/
@Override
public final java.lang.String toString(jnr.ffi.Pointer ptr) {
return java.lang.Long.toString(get(ptr));
}
}
/**
* A native long integer field.
*/
public class UnsignedLong extends NumberField {
/**
* Creates a new native long field.
*/
public UnsignedLong() {
super(NativeType.ULONG);
}
/**
* Creates a new unsigned native long field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public UnsignedLong(Offset offset) {
super(NativeType.ULONG, offset);
}
/**
* Gets the value for this field.
*
* @return a int.
*/
public final long get(jnr.ffi.Pointer ptr) {
long value = ptr.getNativeLong(offset());
final long mask = getRuntime().findType(NativeType.SLONG).size() == 4 ? 0xffffffffL : 0xffffffffffffffffL;
return value < 0
? (long) ((value & mask) + mask + 1)
: value;
}
/**
* Sets the value for this field.
*
* @param value the 32/64 bit value to set.
*/
public final void set(jnr.ffi.Pointer ptr, long value) {
ptr.putNativeLong(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putNativeLong(offset(), value.longValue());
}
/**
* Returns a java int representation of this field.
*
* @return a java int value for this field.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return (int) get(ptr);
}
/**
* Returns a java long representation of this field.
*
* @return a java long value for this field.
*/
@Override
public final long longValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
/**
* Returns a string representation of this field.
*
* @return a string representation of this field.
*/
@Override
public final java.lang.String toString(jnr.ffi.Pointer ptr) {
return java.lang.Long.toString(get(ptr));
}
}
public class Float extends NumberField {
public Float() {
super(NativeType.FLOAT);
}
/**
* Creates a new float field at a specific offset
*
* @param offset The offset within the memory area for this field.
*/
public Float(Offset offset) {
super(NativeType.FLOAT, offset);
}
public final float get(jnr.ffi.Pointer ptr) {
return ptr.getFloat(offset());
}
public final void set(jnr.ffi.Pointer ptr, float value) {
ptr.putFloat(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putFloat(offset(), value.floatValue());
}
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return (int) get(ptr);
}
@Override
public final double doubleValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
@Override
public final float floatValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
@Override
public final long longValue(jnr.ffi.Pointer ptr) {
return (long) get(ptr);
}
@Override
public final java.lang.String toString(jnr.ffi.Pointer ptr) {
return java.lang.String.valueOf(get(ptr));
}
}
public final class Double extends NumberField {
public Double() {
super(NativeType.DOUBLE);
}
public Double(Offset offset) {
super(NativeType.DOUBLE, offset);
}
public final double get(jnr.ffi.Pointer ptr) {
return ptr.getDouble(offset());
}
public final void set(jnr.ffi.Pointer ptr, double value) {
ptr.putDouble(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putDouble(offset(), value.doubleValue());
}
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return (int) get(ptr);
}
@Override
public final long longValue(jnr.ffi.Pointer ptr) {
return (long) get(ptr);
}
@Override
public final float floatValue(jnr.ffi.Pointer ptr) {
return (float) get(ptr);
}
@Override
public final double doubleValue(jnr.ffi.Pointer ptr) {
return get(ptr);
}
@Override
public final java.lang.String toString(jnr.ffi.Pointer ptr) {
return java.lang.String.valueOf(get(ptr));
}
}
/**
* Represents a native memory address.
*/
public class Pointer extends NumberField {
/**
* Creates a new Address field.
*/
public Pointer() {
super(NativeType.ADDRESS);
}
public Pointer(Offset offset) {
super(NativeType.ADDRESS, offset);
}
/**
* Gets the {@link jnr.ffi.Pointer} value from the native memory.
*
* @return a {@link jnr.ffi.Pointer}.
*/
public final jnr.ffi.Pointer get(jnr.ffi.Pointer ptr) {
return ptr.getPointer(offset());
}
/**
* Gets the size of a Pointer in bits
*
* @return the size of the Pointer
*/
public final int size() {
return getRuntime().findType(NativeType.ADDRESS).size();
}
/**
* Sets a {@link jnr.ffi.Pointer} value in the native memory.
*/
public final void set(jnr.ffi.Pointer ptr, jnr.ffi.Pointer value) {
ptr.putPointer(offset(), value);
}
public void set(jnr.ffi.Pointer ptr, java.lang.Number value) {
ptr.putAddress(offset(), value.longValue());
}
/**
* Returns an integer representation of this Pointer
.
*
* @return an integer value for this Pointer
.
*/
@Override
public final int intValue(jnr.ffi.Pointer ptr) {
return (int) ptr.getAddress(offset());
}
/**
* Returns an {@code long} representation of this Pointer
.
*
* @return an {@code long} value for this Pointer
.
*/
@Override
public final long longValue(jnr.ffi.Pointer ptr) {
return ptr.getAddress(offset());
}
/**
* Returns a string representation of this Pointer
.
*
* @return a string representation of this Pointer
.
*/
@Override
public final java.lang.String toString(jnr.ffi.Pointer ptr) {
return get(ptr).toString();
}
}
/**
* Base for all the Enum fields.
*
* @param * Use this type to access meta-data about a native type, such as its size or natural alignment. *
* ** To obtain an instance of this class, use {@link jnr.ffi.Runtime#findType(NativeType)} or * {@link jnr.ffi.Runtime#findType(TypeAlias)}. *
** Example *
* {@code * * Type pointerType = runtime.findType(NativeType.ADDRESS); * * System.out.println("The size of a pointer on this platform is " + pointerType.size()); * } ** */ public abstract class Type { /** * The size in bytes of this type. * * @return An integer */ public abstract int size(); /** * The native alignment of this type, in bytes * * @return An integer */ public abstract int alignment(); /** * The native type of this type * * @return the native type of this type */ public abstract NativeType getNativeType(); } jnr-ffi-1.0.10/src/main/java/jnr/ffi/TypeAlias.java 0000664 0000000 0000000 00000001007 12227113360 0021646 0 ustar 00root root 0000000 0000000 package jnr.ffi; public enum TypeAlias { int8_t, u_int8_t, int16_t, u_int16_t, int32_t, u_int32_t, int64_t, u_int64_t, intptr_t, uintptr_t, caddr_t, dev_t, blkcnt_t, blksize_t, gid_t, in_addr_t, in_port_t, ino_t, ino64_t, key_t, mode_t, nlink_t, id_t, pid_t, off_t, swblk_t, uid_t, clock_t, size_t, ssize_t, time_t, fsblkcnt_t, fsfilcnt_t, sa_family_t, socklen_t, rlim_t, } jnr-ffi-1.0.10/src/main/java/jnr/ffi/Union.java 0000664 0000000 0000000 00000002145 12227113360 0021047 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * Some of the design and code of this class is from the javolution project. * * Copyright (C) 2006 - Javolution (http://javolution.org/) * All rights reserved. * * Permission to use, copy, modify, and distribute this software is * freely granted, provided that this notice is preserved. */ package jnr.ffi; /** * Represents a C union */ public abstract class Union extends Struct { protected Union(Runtime runtime) { super(runtime, true); } } jnr-ffi-1.0.10/src/main/java/jnr/ffi/Variable.java 0000664 0000000 0000000 00000001525 12227113360 0021505 0 ustar 00root root 0000000 0000000 package jnr.ffi; /** * Access library global variables. * *
* To access global variables, declare a method with a parameterized return type of this class. *
*Example *
* {@code * * public interface MyLib { * public Variable* */ public interface Variablemy_int_var(); * } * * MyLib lib = LibraryLoader.create(MyLib.class).load("mylib"): * System.out.println("native value=" + lib.my_int_var().get()) * * lib.my_int_var().set(0xdeadbeef); * } *
By default, parameters that are annotated as {@code @Out} only do not clear * the data in the temporary native memory area allocated when a java heap object * is passed in as the parameter, so the memory passed to the native function is * full of garbage data. After the native method returns, the native memory is * copied back to java, which is usually not a problem, since the native function * will have updated the memory with valid data. However, if the native function * fails, the garbage data that was in the temporary native memory will be copied * back to java. * * @see In * @see Out */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Clear { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/Delegate.java 0000664 0000000 0000000 00000000624 12227113360 0024026 0 ustar 00root root 0000000 0000000 package jnr.ffi.annotations; import jnr.ffi.CallingConvention; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Delegate { CallingConvention convention() default CallingConvention.DEFAULT; } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/Direct.java 0000664 0000000 0000000 00000002714 12227113360 0023530 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; 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 * backed by a persistent native memory block. * *
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. * *
By using {@code @Direct}, the native memory block is permanently associated * with the {@link jnr.ffi.Struct} instance, and will remain allocated * for as long as the {@code Struct} instance remains strongly referenced by java code. * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Direct { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/Encoding.java 0000664 0000000 0000000 00000002133 12227113360 0024037 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2012 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Used to specify the {@link java.nio.charset.Charset} to use for encoding/decoding a {@link String} */ @Retention(RetentionPolicy.RUNTIME) @Target(value = { ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE }) public @interface Encoding { String value(); } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/IgnoreError.java 0000664 0000000 0000000 00000003206 12227113360 0024550 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * Indicates that the errno valud for a native function need not be saved after * the function returns. * *
Due to the nature of the Java Virtual Machine, the errno value must be saved * immediately after the native function is called, otherwise internal jvm operations * may overwrite it before control is returned to java code. * *
Since it is not possible for jnr-ffi to infer in a generic way whether a native * function has succeeded or failed, the C errno value is saved after every native * function call - even for the ones that succeed. This can have a significant * performance impact, so for those functions which either don't fail, or for which * the errno value can be ignored, can be annotated with {@code @IgnoreError} to * avoid unneccessary saving of the errno value. * * @see SaveError */ @Retention(RetentionPolicy.RUNTIME) public @interface IgnoreError { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/In.java 0000664 0000000 0000000 00000003416 12227113360 0022664 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Indicates that the parameter is an IN parameter. * *
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}), * 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 * contents of the native memory. * *
As this extra copying can be expensive, parameters can be annotated with {@code @In} * so the data is only copied from java {@code IN} to native memory, but not copied * back {@code OUT} from native memory to java memory. * *
Parameters with neither a {@code @In} nor a {@code @Out} annotation will copy both ways. * */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.PARAMETER, ElementType.METHOD }) public @interface In { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/LongLong.java 0000664 0000000 0000000 00000002230 12227113360 0024026 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2011 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import jnr.ffi.TypeAlias; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Indicates that a long parameter should be treated as native long-long (64bit) * instead of the platform-dependent long size. */ @Retention(RetentionPolicy.RUNTIME) @Target(value = { ElementType.PARAMETER, ElementType.METHOD }) @TypeDefinition(alias = TypeAlias.int64_t) public @interface LongLong { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/NulTerminate.java 0000664 0000000 0000000 00000002120 12227113360 0024714 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Indicates that a byte array or ByteBuffer should be terminated with a zero byte * before passing it to a native function. */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.PARAMETER, ElementType.METHOD }) public @interface NulTerminate { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/Out.java 0000664 0000000 0000000 00000003716 12227113360 0023070 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Indicates that the parameter is an OUT parameter. * *
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}), * 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 * contents of the native memory. * *
As this extra copying can be expensive, for native functions which only * write to the passed in memory block and do not use the existing contents, * parameters can be annotated with {@code @Out} so there is only copied {@code OUT} * from native memory to java memory after the call, and the unneccessary copy {@code IN} * from java to native memory before the call can be avoided. * *
Parameters with neither a {@code @In} nor a {@code @Out} annotation will copy both ways. * * @see In * @see Clear */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.PARAMETER, ElementType.METHOD }) public @interface Out { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/Pinned.java 0000664 0000000 0000000 00000002455 12227113360 0023535 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Marks a method parameter as being pinnable. *
* This means the data for the parameter is not copied to/from native memory. * Instead, the JVM memory is locked and passed directly to the native code. *
** IMPORTANT: This should not be used for functions that may block on * network or filesystem access such as read(2), write(2), stat(2). *
*/ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Pinned { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/SaveError.java 0000664 0000000 0000000 00000001736 12227113360 0024231 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * Tags a library method as requiring any error codes as returned * by errno on unix, or GetLastError on windows be saved. * * @see IgnoreError */ @Retention(RetentionPolicy.RUNTIME) public @interface SaveError { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/StdCall.java 0000664 0000000 0000000 00000001652 12227113360 0023644 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface StdCall { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/Synchronized.java 0000664 0000000 0000000 00000002026 12227113360 0024771 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * Indicates that a library or a library method requires all calls to be * synchronized. * * i.e. calls from multiple threads will synchronize on a monitor object, * then call the native method. */ @Retention(RetentionPolicy.RUNTIME) public @interface Synchronized { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/Transient.java 0000664 0000000 0000000 00000002175 12227113360 0024266 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Indicates that the parameter is transient. * * This means it can be backed by a temporarily allocated native memory block, * and after the method call, the native memory can be freed again. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Transient { } jnr-ffi-1.0.10/src/main/java/jnr/ffi/annotations/TypeDefinition.java 0000664 0000000 0000000 00000000710 12227113360 0025242 0 ustar 00root root 0000000 0000000 package jnr.ffi.annotations; import jnr.ffi.TypeAlias; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * This is used internally by jnr-ffi to define type aliases. e.g. ssize_t => long */ @Retention(RetentionPolicy.RUNTIME) @Target(value = { ElementType.ANNOTATION_TYPE }) public @interface TypeDefinition { TypeAlias alias(); } jnr-ffi-1.0.10/src/main/java/jnr/ffi/byref/ 0000775 0000000 0000000 00000000000 12227113360 0020221 5 ustar 00root root 0000000 0000000 jnr-ffi-1.0.10/src/main/java/jnr/ffi/byref/AbstractNumberReference.java 0000664 0000000 0000000 00000003526 12227113360 0025625 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.byref; /** * An abstract class for common PrimitiveReference functionality */ abstract public class AbstractNumberReferenceFor example, the following C code, *
* {@code * * extern void get_a(void** ap); * * void* foo(void) { * void* a; * // pass a reference to 'a' so get_a() can fill it out * get_a(&a); * * return a; * } * * } ** *
Would be declared in java as *
* {@code * * interface Lib { * void get_a(@Out AddressByReference ap); * } * * } **
and used like this * *
*/ public final class AddressByReference extends AbstractReference { /** * Creates a new reference to an integer value */ public AddressByReference() { super(Address.valueOf(0)); } /** * Creates a new reference to an address value * * @param value the initial native value */ public AddressByReference(Address value) { super(checkNull(value)); } /** * Copies the address value to native memory * * @param runtime * @param memory the native memory buffer */ public void toNative(Runtime runtime, Pointer memory, long offset) { memory.putAddress(offset, value.nativeAddress()); } /** * Copies the address value from native memory * * @param runtime * @param memory the native memory buffer. */ public void fromNative(Runtime runtime, Pointer memory, long offset) { value = Address.valueOf(memory.getAddress(offset)); } /** * Gets the native size of type of reference * * @return The size of the native type this reference points to */ public int nativeSize(Runtime runtime) { return runtime.addressSize(); } } jnr-ffi-1.0.10/src/main/java/jnr/ffi/byref/ByReference.java 0000664 0000000 0000000 00000004125 12227113360 0023257 0 ustar 00root root 0000000 0000000 /* * Copyright (C) 2008-2010 Wayne Meissner * * This file is part of the JNR project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jnr.ffi.byref; import jnr.ffi.Pointer; import jnr.ffi.Runtime; /** * A ByReference subclass is used when a primitive parameter must be passed * by-reference. * ** AddressByReference ap = new AddressByReference(); * lib.get_a(ap); * System.out.println("a from lib=" + a.getValue()); *
For example, the following C code, *
** {@code * * extern void get_a(int * ap); * * int foo(void) { * int a; * // pass a reference to 'a' so get_a() can fill it out * get_a(&a); * * return a; * } * } *
Would be declared in java as *
** {@code * * interface Lib { * void get_a(@Out IntByReference ap); * } * * } *
and used like this *
*{@code * * IntByReference ap = new IntByReference(); * lib.get_a(ap); * System.out.printf("a from lib=%d\n", a.getValue()); * * } ** @param
For example, the following C code, *
** {@code * extern void get_a(char *ap); * * int foo(void) { * char a; * // pass a reference to 'a' so get_a() can fill it out * get_a(&a); * * return a; * } * } *
Would be declared in java as *
** {@code * interface Lib { * void get_a(@Out ByteByReference ap); * } * } *
and used like this *
*/ public final class ByteByReference extends AbstractNumberReference* ByteByReference ap = new ByteByReference(); * lib.get_a(ap); * System.out.printf("a from lib=%d\n", a.byteValue()); *
For example, the following C code, *
** {@code * extern void get_a(int * ap); * * int foo(void) { * int a; * // pass a reference to 'a' so get_a() can fill it out * get_a(&a); * * return a; * } * } *
Would be declared in java as *
** {@code * interface Lib { * void get_a(@Out IntByReference ap); * } * } *
and used like this *
*/ public final class IntByReference extends AbstractNumberReference* IntByReference ap = new IntByReference(); * lib.get_a(ap); * System.out.printf("a from lib=%d\n", a.intValue()); *
For example, the following C code, *
** {@code * extern void get_a(long long * ap); * * long long foo(void) { * long long a; * // pass a reference to 'a' so get_a() can fill it out * get_a(&a); * * return a; * } * } *
Would be declared in java as *
** {@code * interface Lib { * void get_a(@Out LongLongByReference ap); * } * } *
and used like this *
*/ public final class LongLongByReference extends AbstractNumberReference* LongLongByReference ap = new LongLongByReference(); * lib.get_a(ap); * System.out.printf("a from lib=%d\n", a.longValue()); *
For example, the following C code, *
** {@code * extern void get_a(long * ap); * * long foo(void) { * long a; * // pass a reference to 'a' so get_a() can fill it out * get_a(&a); * * return a; * } * } *
Would be declared in java as *
** {@code * interface Lib { * void get_a(@Out NativeLongByReference ap); * } * } *
and used like this *
*/ public final class NativeLongByReference extends AbstractNumberReference* NativeLongByReference ap = new NativeLongByReference(); * lib.get_a(ap); * System.out.printf("a from lib=%d\n", a.longValue()); *
For example, the following C code, *
* {@code * * extern void get_size(ssize_t *sp); * * ssize_t foo(void) { * ssize_t n; * // pass a reference to 'n' so get_size() can fill it out * get_size(&n); * * return n; * } * * } **
Would be declared in java as *
* {@code * * interface Lib { * void get_size(@Out NumberByReference ap); * } * * } **
and used like this *
* {@code * * NumberByReference size = new NumberByReference(ssize_t); * lib.get_size(size); * System.out.printf("size from lib=%d\n", size.longValue()); * * } **/ public class NumberByReference extends AbstractNumberReference
For example, the following C code, *
** {@code * extern void get_a(void** ap); * * void* foo(void) { * void* a; * // pass a reference to 'a' so get_a() can fill it out * get_a(&a); * * return a; * } * } *
Would be declared in java as *
** {@code * interface Lib { * void get_a(@Out PointerByReference ap); * } * } *
and used like this *
*/ public final class PointerByReference extends AbstractReference* PointerByReference ap = new PointerByReference(); * lib.get_a(ap); * Pointer ptr = ap.getValue(); * System.out.println("ptr from lib=" + a.getValue()); * System.out.println("ptr contents=" + ptr.getInt(0)); *
For example, the following C code, *
** {@code * extern void get_a(short * ap); * * short foo(void) { * short a; * // pass a reference to 'a' so get_a() can fill it out * get_a(&a); * * return a; * } * } *
Would be declared in java as *
** {@code * interface Lib { * void get_a(@Out ShortByReference ap); * } * } *
and used like this *
*/ public final class ShortByReference extends AbstractNumberReference* ShortByReference ap = new ShortByReference(); * lib.get_a(ap); * System.out.printf("a from lib=%d\n", a.getValue()); *
* This returns the errno value that was set at the time of the last native
* function call.
*
* @return The errno value.
*/
public abstract int getLastError();
/**
* Sets the native error code.
*
* @param error The value to set errno to.
*/
public abstract void setLastError(int error);
/** Gets the address mask for this runtime */
public final long addressMask() {
return addressMask;
}
/**
* Gets the size of an address (e.g. a pointer) for this runtime
*
* @return The size of an address in bytes.
*/
public final int addressSize() {
return addressSize;
}
/**
* Gets the size of a C long integer for this runtime
*
* @return The size of a C long integer in bytes.
*/
public final int longSize() {
return longSize;
}
/**
* Retrieves this runtime's native byte order.
*
* @return this runtime's byte order
*/
public final ByteOrder byteOrder() {
return byteOrder;
}
}
jnr-ffi-1.0.10/src/main/java/jnr/ffi/provider/BadType.java 0000664 0000000 0000000 00000002265 12227113360 0023144 0 ustar 00root root 0000000 0000000 /*
* Copyright (C) 2008-2010 Wayne Meissner
*
* This file is part of the JNR project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jnr.ffi.provider;
import jnr.ffi.NativeType;
/**
*
*/
public final class BadType extends jnr.ffi.Type {
private final String typeName;
public BadType(String typeName) {
this.typeName = typeName;
}
public final int alignment() {
throw new RuntimeException("invalid type: " + typeName);
}
public final int size() {
throw new RuntimeException("invalid type: " + typeName);
}
public NativeType getNativeType() {
throw new RuntimeException("invalid type: " + typeName);
}
}
jnr-ffi-1.0.10/src/main/java/jnr/ffi/provider/BoundedMemoryIO.java 0000664 0000000 0000000 00000021273 12227113360 0024615 0 ustar 00root root 0000000 0000000 /*
* Copyright (C) 2008-2010 Wayne Meissner
*
* This file is part of the JNR project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jnr.ffi.provider;
import jnr.ffi.Address;
import jnr.ffi.Pointer;
import java.nio.charset.Charset;
public final class BoundedMemoryIO extends AbstractMemoryIO implements DelegatingMemoryIO {
private final long base, size;
private final Pointer io;
public BoundedMemoryIO(Pointer parent, long offset, long size) {
super(parent.getRuntime(), parent.address() != 0L ? parent.address() + offset : 0L, parent.isDirect());
this.io = parent;
this.base = offset;
this.size = size;
}
public long size() {
return this.size;
}
@Override
public final boolean hasArray() {
return io.hasArray();
}
@Override
public final Object array() {
return io.array();
}
@Override
public final int arrayOffset() {
return io.arrayOffset() + (int) base;
}
@Override
public final int arrayLength() {
return (int) size;
}
@Override
public void checkBounds(long offset, long length) {
checkBounds(this.size, offset, length);
getDelegatedMemoryIO().checkBounds(base + offset, length);
}
public Pointer getDelegatedMemoryIO() {
return io;
}
@Override
public int hashCode() {
return getDelegatedMemoryIO().hashCode();
}
@Override
public boolean equals(Object obj) {
return (obj instanceof BoundedMemoryIO && io.equals(((BoundedMemoryIO) obj).io) &&
((BoundedMemoryIO) obj).base == base && ((BoundedMemoryIO) obj).size == size)
|| io.equals(obj);
}
@Override
public byte getByte(long offset) {
checkBounds(size, offset, 1);
return io.getByte(base + offset);
}
@Override
public short getShort(long offset) {
checkBounds(size, offset, 2);
return io.getShort(base + offset);
}
@Override
public int getInt(long offset) {
checkBounds(size, offset, 4);
return io.getInt(base + offset);
}
@Override
public long getLongLong(long offset) {
checkBounds(size, offset, 8);
return io.getLongLong(base + offset);
}
@Override
public float getFloat(long offset) {
checkBounds(size, offset, 4);
return io.getFloat(base + offset);
}
@Override
public double getDouble(long offset) {
checkBounds(size, offset, 8);
return io.getDouble(base + offset);
}
public Pointer getPointer(long offset) {
checkBounds(size, offset, getRuntime().addressSize());
return io.getPointer(base + offset);
}
public Pointer getPointer(long offset, long size) {
checkBounds(this.size, base + offset, getRuntime().addressSize());
return io.getPointer(base + offset, size);
}
@Override
public void putByte(long offset, byte value) {
checkBounds(size, offset, 1);
io.putByte(base + offset, value);
}
@Override
public void putShort(long offset, short value) {
checkBounds(size, offset, 2);
io.putShort(base + offset, value);
}
@Override
public void putInt(long offset, int value) {
checkBounds(size, offset, 4);
io.putInt(base + offset, value);
}
@Override
public void putLongLong(long offset, long value) {
checkBounds(size, offset, 8);
io.putLongLong(base + offset, value);
}
@Override
public void putFloat(long offset, float value) {
checkBounds(size, offset, 4);
io.putFloat(base + offset, value);
}
@Override
public void putDouble(long offset, double value) {
checkBounds(size, offset, 8);
io.putDouble(base + offset, value);
}
public void putPointer(long offset, Pointer value) {
checkBounds(size, offset, getRuntime().addressSize());
io.putPointer(base + offset, value);
}
@Override
public void get(long offset, byte[] dst, int off, int len) {
checkBounds(size, offset, len);
io.get(base + offset, dst, off, len);
}
@Override
public void put(long offset, byte[] dst, int off, int len) {
checkBounds(size, offset, len);
io.put(base + offset, dst, off, len);
}
@Override
public void get(long offset, short[] dst, int off, int len) {
checkBounds(size, offset, len * Short.SIZE / 8);
io.get(base + offset, dst, off, len);
}
@Override
public void put(long offset, short[] dst, int off, int len) {
checkBounds(size, offset, len * Short.SIZE / 8);
io.put(base + offset, dst, off, len);
}
@Override
public void get(long offset, int[] dst, int off, int len) {
checkBounds(size, offset, len * Integer.SIZE / 8);
io.get(base + offset, dst, off, len);
}
@Override
public void put(long offset, int[] src, int off, int len) {
checkBounds(size, offset, len * Integer.SIZE / 8);
io.put(base + offset, src, off, len);
}
@Override
public void get(long offset, long[] dst, int off, int len) {
checkBounds(size, offset, len * Long.SIZE / 8);
io.get(base + offset, dst, off, len);
}
@Override
public void put(long offset, long[] src, int off, int len) {
checkBounds(size, offset, len * Long.SIZE / 8);
io.put(base + offset, src, off, len);
}
@Override
public void get(long offset, float[] dst, int off, int len) {
checkBounds(size, offset, len * Float.SIZE / 8);
io.get(base + offset, dst, off, len);
}
@Override
public void put(long offset, float[] src, int off, int len) {
checkBounds(size, offset, len * Float.SIZE / 8);
io.put(base + offset, src, off, len);
}
@Override
public void get(long offset, double[] dst, int off, int len) {
checkBounds(size, offset, len * Double.SIZE / 8);
io.get(base + offset, dst, off, len);
}
@Override
public void put(long offset, double[] src, int off, int len) {
checkBounds(size, offset, len * Double.SIZE / 8);
io.put(base + offset, src, off, len);
}
@Override
public long getAddress(long offset) {
checkBounds(size, offset, getRuntime().addressSize());
return io.getAddress(base + offset);
}
@Override
public String getString(long offset, int maxLength, Charset cs) {
checkBounds(size, offset, maxLength);
return io.getString(base + offset, maxLength, cs);
}
@Override
public String getString(long offset) {
return io.getString(base + offset, (int) size, Charset.defaultCharset());
}
@Override
public void putAddress(long offset, long value) {
checkBounds(size, offset, getRuntime().addressSize());
io.putAddress(base + offset, value);
}
@Override
public void putAddress(long offset, Address value) {
checkBounds(size, offset, getRuntime().addressSize());
io.putAddress(base + offset, value);
}
@Override
public void putString(long offset, String string, int maxLength, Charset cs) {
checkBounds(size, offset, maxLength);
io.putString(base + offset, string, maxLength, cs);
}
@Override
public int indexOf(long offset, byte value) {
return io.indexOf(base + offset, value, (int) size);
}
@Override
public int indexOf(long offset, byte value, int maxlen) {
checkBounds(size, offset, maxlen);
return io.indexOf(base + offset, value, maxlen);
}
@Override
public void setMemory(long offset, long size, byte value) {
checkBounds(this.size, base + offset, size);
io.setMemory(base + offset, size, value);
}
@Override
public void transferFrom(long offset, Pointer other, long otherOffset, long count) {
checkBounds(this.size, base + offset, count);
getDelegatedMemoryIO().transferFrom(offset, other, otherOffset, count);
}
@Override
public void transferTo(long offset, Pointer other, long otherOffset, long count) {
checkBounds(this.size, base + offset, count);
getDelegatedMemoryIO().transferTo(offset, other, otherOffset, count);
}
}
jnr-ffi-1.0.10/src/main/java/jnr/ffi/provider/ClosureManager.java 0000664 0000000 0000000 00000000405 12227113360 0024515 0 ustar 00root root 0000000 0000000 package jnr.ffi.provider;
/**
*
*/
public interface ClosureManager {
public abstract