├── .github
└── workflows
│ └── ci.yml
├── .gitignore
├── .mvn
└── wrapper
│ ├── maven-wrapper.jar
│ └── maven-wrapper.properties
├── .travis.yml
├── LICENSE
├── README.md
├── docs
├── ComparisonToSimilarProjects.md
├── FAQ.md
├── GettingStarted.md
├── MemoryManagement.md
├── ProjectsUsingJNR.md
├── README.md
├── SqueezingPerformance.md
├── TypeMappings.md
└── WhyUseJNR.md
├── libtest
├── Benchmark.c
├── BufferTest.c
├── ClosureTest.c
├── GNUmakefile
├── GlobalVariable.c
├── LastErrorTest.c
├── NumberTest.c
├── PointerNumericTest.c
├── PointerTest.c
├── ReferenceTest.c
├── StringTest.c
├── StructTest.c
├── struct
│ ├── NestedStruct.c
│ ├── NestedUnion.c
│ ├── NumericStruct.c
│ ├── NumericUnion.c
│ └── StructTypes.h
└── types.h
├── mvnw
├── pom.xml
└── src
├── main
└── java
│ └── jnr
│ └── ffi
│ ├── Address.java
│ ├── CallingConvention.java
│ ├── LastError.java
│ ├── Library.java
│ ├── LibraryLoader.java
│ ├── LibraryOption.java
│ ├── Memory.java
│ ├── NativeLong.java
│ ├── NativeType.java
│ ├── ObjectReferenceManager.java
│ ├── Platform.java
│ ├── Pointer.java
│ ├── Runtime.java
│ ├── Struct.java
│ ├── StructLayout.java
│ ├── Type.java
│ ├── TypeAlias.java
│ ├── Union.java
│ ├── Variable.java
│ ├── annotations
│ ├── Clear.java
│ ├── Delegate.java
│ ├── Direct.java
│ ├── Encoding.java
│ ├── IgnoreError.java
│ ├── In.java
│ ├── LongLong.java
│ ├── Meta.java
│ ├── NulTerminate.java
│ ├── Out.java
│ ├── Pinned.java
│ ├── SaveError.java
│ ├── StdCall.java
│ ├── Synchronized.java
│ ├── Transient.java
│ ├── TypeDefinition.java
│ └── Variadic.java
│ ├── byref
│ ├── AbstractNumberReference.java
│ ├── AbstractReference.java
│ ├── AddressByReference.java
│ ├── ByReference.java
│ ├── ByteByReference.java
│ ├── DoubleByReference.java
│ ├── FloatByReference.java
│ ├── IntByReference.java
│ ├── LongLongByReference.java
│ ├── NativeLongByReference.java
│ ├── NumberByReference.java
│ ├── PointerByReference.java
│ └── ShortByReference.java
│ ├── mapper
│ ├── AbstractDataConverter.java
│ ├── AbstractFromNativeType.java
│ ├── AbstractSignatureTypeMapper.java
│ ├── AbstractToNativeType.java
│ ├── CachingTypeMapper.java
│ ├── CompositeFunctionMapper.java
│ ├── CompositeTypeMapper.java
│ ├── DataConverter.java
│ ├── DefaultSignatureType.java
│ ├── DefaultTypeMapper.java
│ ├── FromNativeContext.java
│ ├── FromNativeConverter.java
│ ├── FromNativeType.java
│ ├── FromNativeTypes.java
│ ├── FunctionMapper.java
│ ├── MethodParameterContext.java
│ ├── MethodResultContext.java
│ ├── SignatureType.java
│ ├── SignatureTypeMapper.java
│ ├── SignatureTypeMapperAdapter.java
│ ├── SimpleFunctionMapper.java
│ ├── SimpleTypeMapper.java
│ ├── ToNativeContext.java
│ ├── ToNativeConverter.java
│ ├── ToNativeType.java
│ ├── ToNativeTypes.java
│ ├── TypeMapper.java
│ └── Util.java
│ ├── provider
│ ├── AbstractArrayMemoryIO.java
│ ├── AbstractBufferMemoryIO.java
│ ├── AbstractMemoryIO.java
│ ├── AbstractRuntime.java
│ ├── BadType.java
│ ├── BoundedMemoryIO.java
│ ├── ClosureManager.java
│ ├── DefaultObjectReferenceManager.java
│ ├── DelegatingMemoryIO.java
│ ├── FFIProvider.java
│ ├── FromNativeType.java
│ ├── IdentityFunctionMapper.java
│ ├── InAccessibleMemoryIO.java
│ ├── IntPointer.java
│ ├── InterfaceScanner.java
│ ├── InvalidProvider.java
│ ├── InvalidRuntime.java
│ ├── InvocationSession.java
│ ├── Invoker.java
│ ├── LoadedLibrary.java
│ ├── MemoryManager.java
│ ├── NativeFunction.java
│ ├── NativeInvocationHandler.java
│ ├── NativeVariable.java
│ ├── NullMemoryIO.java
│ ├── NullTypeMapper.java
│ ├── ParameterFlags.java
│ ├── ParameterType.java
│ ├── ResultType.java
│ ├── ShareMemoryIO.java
│ ├── SigType.java
│ ├── ToNativeType.java
│ ├── converters
│ │ ├── BoxedBooleanArrayParameterConverter.java
│ │ ├── BoxedByteArrayParameterConverter.java
│ │ ├── BoxedDoubleArrayParameterConverter.java
│ │ ├── BoxedFloatArrayParameterConverter.java
│ │ ├── BoxedIntegerArrayParameterConverter.java
│ │ ├── BoxedLong32ArrayParameterConverter.java
│ │ ├── BoxedLong64ArrayParameterConverter.java
│ │ ├── BoxedShortArrayParameterConverter.java
│ │ ├── ByReferenceParameterConverter.java
│ │ ├── CharSequenceArrayParameterConverter.java
│ │ ├── CharSequenceParameterConverter.java
│ │ ├── EnumConverter.java
│ │ ├── EnumSetConverter.java
│ │ ├── Long32ArrayParameterConverter.java
│ │ ├── NativeLong32ArrayParameterConverter.java
│ │ ├── NativeLong64ArrayParameterConverter.java
│ │ ├── NativeLongConverter.java
│ │ ├── Pointer32ArrayParameterConverter.java
│ │ ├── Pointer64ArrayParameterConverter.java
│ │ ├── StringBufferParameterConverter.java
│ │ ├── StringBuilderParameterConverter.java
│ │ ├── StringResultConverter.java
│ │ ├── StringUtil.java
│ │ ├── StructArrayParameterConverter.java
│ │ ├── StructByReferenceFromNativeConverter.java
│ │ └── StructByReferenceToNativeConverter.java
│ └── jffi
│ │ ├── ARM_64StubCompiler.java
│ │ ├── AbstractA64StubCompiler.java
│ │ ├── AbstractAsmLibraryInterface.java
│ │ ├── AbstractFastNumericMethodGenerator.java
│ │ ├── AbstractX86StubCompiler.java
│ │ ├── AllocatedDirectMemoryIO.java
│ │ ├── AnnotationTypeMapper.java
│ │ ├── ArrayMemoryIO.java
│ │ ├── AsmBuilder.java
│ │ ├── AsmClassLoader.java
│ │ ├── AsmLibraryLoader.java
│ │ ├── AsmRuntime.java
│ │ ├── AsmStructByReferenceFromNativeConverter.java
│ │ ├── AsmUtil.java
│ │ ├── BaseMethodGenerator.java
│ │ ├── BufferMethodGenerator.java
│ │ ├── BufferParameterStrategy.java
│ │ ├── ByteBufferMemoryIO.java
│ │ ├── ClosureFromNativeConverter.java
│ │ ├── ClosureTypeMapper.java
│ │ ├── ClosureUtil.java
│ │ ├── CodegenUtils.java
│ │ ├── ConverterMetaData.java
│ │ ├── DefaultInvokerFactory.java
│ │ ├── DirectMemoryIO.java
│ │ ├── FastIntMethodGenerator.java
│ │ ├── FastLongMethodGenerator.java
│ │ ├── FastNumericMethodGenerator.java
│ │ ├── HeapBufferParameterStrategy.java
│ │ ├── InvokerTypeMapper.java
│ │ ├── InvokerUtil.java
│ │ ├── JNIInvokeInterface.java
│ │ ├── JNINativeInterface.java
│ │ ├── LibraryLoader.java
│ │ ├── LocalVariable.java
│ │ ├── LocalVariableAllocator.java
│ │ ├── MemoryUtil.java
│ │ ├── MethodGenerator.java
│ │ ├── NativeClosureFactory.java
│ │ ├── NativeClosureManager.java
│ │ ├── NativeClosurePointer.java
│ │ ├── NativeClosureProxy.java
│ │ ├── NativeFinalizer.java
│ │ ├── NativeFunctionMapperContext.java
│ │ ├── NativeLibrary.java
│ │ ├── NativeLibraryLoader.java
│ │ ├── NativeMemoryManager.java
│ │ ├── NativeRuntime.java
│ │ ├── NoTrace.java
│ │ ├── NoX86.java
│ │ ├── NotImplMethodGenerator.java
│ │ ├── NullObjectParameterStrategy.java
│ │ ├── NumberUtil.java
│ │ ├── ParameterStrategy.java
│ │ ├── PointerParameterStrategy.java
│ │ ├── PrimitiveArrayParameterStrategy.java
│ │ ├── Provider.java
│ │ ├── ReflectionLibraryLoader.java
│ │ ├── ReflectionVariableAccessorGenerator.java
│ │ ├── SimpleNativeContext.java
│ │ ├── SkinnyMethodAdapter.java
│ │ ├── StructByReferenceResultConverterFactory.java
│ │ ├── StubCompiler.java
│ │ ├── SymbolNotFoundError.java
│ │ ├── ToNativeOp.java
│ │ ├── TransientNativeMemory.java
│ │ ├── Types.java
│ │ ├── Util.java
│ │ ├── VariableAccessorGenerator.java
│ │ ├── X86Disassembler.java
│ │ ├── X86MethodGenerator.java
│ │ ├── X86_32StubCompiler.java
│ │ ├── X86_64StubCompiler.java
│ │ └── platform
│ │ ├── aarch64
│ │ ├── darwin
│ │ │ └── TypeAliases.java
│ │ ├── freebsd
│ │ │ └── TypeAliases.java
│ │ ├── linux
│ │ │ └── TypeAliases.java
│ │ └── windows
│ │ │ └── TypeAliases.java
│ │ ├── arm
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── i386
│ │ ├── darwin
│ │ │ └── TypeAliases.java
│ │ ├── freebsd
│ │ │ └── TypeAliases.java
│ │ ├── linux
│ │ │ └── TypeAliases.java
│ │ ├── midnightbsd
│ │ │ └── TypeAliases.java
│ │ ├── openbsd
│ │ │ └── TypeAliases.java
│ │ ├── solaris
│ │ │ └── TypeAliases.java
│ │ └── windows
│ │ │ └── TypeAliases.java
│ │ ├── loongarch64
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── mips
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── mips64
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── mips64el
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── mipsel
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── ppc
│ │ ├── aix
│ │ │ └── TypeAliases.java
│ │ ├── darwin
│ │ │ └── TypeAliases.java
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── ppc64
│ │ ├── aix
│ │ │ └── TypeAliases.java
│ │ ├── freebsd
│ │ │ └── TypeAliases.java
│ │ ├── ibmi
│ │ │ └── TypeAliases.java
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── ppc64le
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── riscv64
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── s390
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── s390x
│ │ └── linux
│ │ │ └── TypeAliases.java
│ │ ├── sparc
│ │ └── solaris
│ │ │ └── TypeAliases.java
│ │ ├── sparcv9
│ │ ├── linux
│ │ │ └── TypeAliases.java
│ │ └── solaris
│ │ │ └── TypeAliases.java
│ │ └── x86_64
│ │ ├── darwin
│ │ └── TypeAliases.java
│ │ ├── dragonfly
│ │ └── TypeAliases.java
│ │ ├── freebsd
│ │ └── TypeAliases.java
│ │ ├── linux
│ │ └── TypeAliases.java
│ │ ├── midnightbsd
│ │ └── TypeAliases.java
│ │ ├── openbsd
│ │ └── TypeAliases.java
│ │ ├── solaris
│ │ └── TypeAliases.java
│ │ └── windows
│ │ └── TypeAliases.java
│ ├── types
│ ├── blkcnt_t.java
│ ├── blksize_t.java
│ ├── caddr_t.java
│ ├── clock_t.java
│ ├── dev_t.java
│ ├── fsblkcnt_t.java
│ ├── fsfilcnt_t.java
│ ├── gid_t.java
│ ├── id_t.java
│ ├── in_addr_t.java
│ ├── in_port_t.java
│ ├── ino64_t.java
│ ├── ino_t.java
│ ├── int16_t.java
│ ├── int32_t.java
│ ├── int64_t.java
│ ├── int8_t.java
│ ├── intptr_t.java
│ ├── key_t.java
│ ├── mode_t.java
│ ├── nlink_t.java
│ ├── off_t.java
│ ├── pid_t.java
│ ├── rlim_t.java
│ ├── sa_family_t.java
│ ├── size_t.java
│ ├── socklen_t.java
│ ├── ssize_t.java
│ ├── swblk_t.java
│ ├── time_t.java
│ ├── u_int16_t.java
│ ├── u_int32_t.java
│ ├── u_int64_t.java
│ ├── u_int8_t.java
│ ├── uid_t.java
│ └── uintptr_t.java
│ └── util
│ ├── AnnotationNameComparator.java
│ ├── AnnotationProperty.java
│ ├── AnnotationProxy.java
│ ├── Annotations.java
│ ├── BufferUtil.java
│ ├── EnumMapper.java
│ └── ref
│ ├── FinalizablePhantomReference.java
│ ├── FinalizableReference.java
│ ├── FinalizableReferenceQueue.java
│ ├── FinalizableSoftReference.java
│ ├── FinalizableWeakReference.java
│ └── internal
│ └── Finalizer.java
└── test
└── java
└── jnr
└── ffi
├── ArrayTest.java
├── BufferTest.java
├── DelegateTest.java
├── GetLoadedLibrariesTest.java
├── GlobalVariableTest.java
├── InvocationTest.java
├── LastErrorTest.java
├── LibraryLoaderTest.java
├── LibraryTest.java
├── MemoryIOTest.java
├── ObjectReferenceManagerTest.java
├── PlatformTest.java
├── PointerTest.java
├── ResultConverterTest.java
├── StringArrayTest.java
├── StringTest.java
├── TstUtil.java
├── TypeDefinitionTest.java
├── VarargsTest.java
├── byref
├── AddressByReferenceTest.java
├── ByteByReferenceTest.java
├── IntByReferenceTest.java
└── PointerByReferenceTest.java
├── mapper
├── AnnotatedMappedTypeTest.java
└── CachingTypeMapperTest.java
├── numeric
├── EnumTest.java
├── NativeLongTest.java
├── NumberTest.java
└── utils
│ ├── BooleanNumberTestLib.java
│ ├── NumberOps.java
│ ├── NumberRange.java
│ ├── NumberTestLib.java
│ └── NumberUtils.java
├── pointer
└── PointerNumericTest.java
├── provider
└── InterfaceScannerTest.java
└── struct
├── AlignmentTest.java
├── ArrayTest.java
├── AsciiStringFieldTest.java
├── EnumTest.java
├── NestedStructTest.java
├── NestedUnionTest.java
├── NumericStructTest.java
├── NumericUnionTest.java
├── PaddingTest.java
├── StructLayoutTest.java
├── StructToStringTest.java
├── StructureTest.java
└── UTF8StringFieldTest.java
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a Java project with Maven
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3 |
4 | name: Java CI with Maven
5 |
6 | on:
7 | push:
8 | branches: [ master ]
9 | pull_request:
10 | branches: [ master ]
11 |
12 | jobs:
13 |
14 | mvn-test-x86:
15 |
16 | runs-on: ${{ matrix.os }}
17 |
18 | strategy:
19 | matrix:
20 | os: [ubuntu-latest, macos-latest, [self-hosted, macos, aarch64]]
21 | java-version: ['8', '11']
22 | fail-fast: false
23 |
24 | steps:
25 | - uses: actions/checkout@v2
26 | - name: Cache dependencies
27 | uses: actions/cache@v2
28 | with:
29 | path: ~/.m2
30 | key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
31 | restore-keys: ${{ runner.os }}-m2
32 | - name: Set up JDK ${{ matrix.java-version }}
33 | uses: actions/setup-java@v1.4.3
34 | with:
35 | java-version: ${{ matrix.java-version }}
36 | - name: Build with Maven
37 | run: mvn -B package --file pom.xml
38 |
39 | mvn-test-macos-aarch64:
40 |
41 | runs-on: [self-hosted, macos, aarch64]
42 |
43 | name: mvn test on Apple M1
44 |
45 | strategy:
46 | fail-fast: false
47 |
48 | steps:
49 | - uses: actions/checkout@v2
50 | - name: Cache dependencies
51 | uses: actions/cache@v2
52 | with:
53 | path: ~/.m2
54 | key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
55 | restore-keys: ${{ runner.os }}-m2
56 | - name: Set up JDK 11
57 | uses: actions/setup-java@v2
58 | with:
59 | distribution: 'zulu'
60 | java-version: 11
61 | architecture: arm
62 | - name: Build with Maven
63 | run: mvn -B package --file pom.xml
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.orig$
2 | *.rej$
3 | *.class$
4 | *~
5 | .idea
6 | *.iml
7 | *.iws
8 | *.ipr
9 | *.log
10 | nbproject/private
11 | build
12 | dist
13 | target
14 | lib/nblibraries-private.properties
15 |
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jnr/jnr-ffi/683b8531776588b040cde17bb71a550ca19fc353/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip
2 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | notifications:
3 | irc:
4 | channels:
5 | - "irc.freenode.org#jnr"
6 | on_success: change
7 | on_failure: always
8 | template:
9 | - "%{repository} (%{branch}:%{commit} by %{author}): %{message} (%{build_url})"
10 | matrix:
11 | include:
12 | - os: osx
13 | - os: linux
14 | jdk: oraclejdk8
15 | - os: linux
16 | jdk: openjdk7
17 | sudo: false
18 | cache:
19 | directories:
20 | - $HOME/.m2
21 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Licensed under the Apache License, Version 2.0 (the "License");
2 | you may not use this file except in compliance with the License.
3 | You may obtain a copy of the License at
4 |
5 | http://www.apache.org/licenses/LICENSE-2.0
6 |
7 | Unless required by applicable law or agreed to in writing, software
8 | distributed under the License is distributed on an "AS IS" BASIS,
9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 | See the License for the specific language governing permissions and
11 | limitations under the License.
12 |
13 |
14 | Alternatively, you can redistribute it and/or modify it under
15 | the terms of the GNU Lesser General Public License as published by
16 | the Free Software Foundation, either version 3 of the License, or
17 | (at your option) any later version.
18 |
19 | This code is distributed in the hope that it will be useful, but WITHOUT
20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
22 | version 3 for more details.
23 |
24 | You should have received a copy of the GNU Lesser General Public License
25 | version 3 along with this work. If not, see .
26 |
--------------------------------------------------------------------------------
/docs/MemoryManagement.md:
--------------------------------------------------------------------------------
1 | # Memory Management
2 |
3 | ## Pointers
4 | A `Pointer` instance stores reference to a native memory address. As it can
5 | represent various data like struct, arrays, java objects, etc. it needs to
6 | be used with caution since any illegal access can cause segfaults and JVM crash.
7 | The `Pointer` class provides methods to return different representations of
8 | the stored data, for example:
9 | - `getInt(long offset)`: Reads 32-bit int value at given offset
10 | - `getPointer(long offset, int size)`: Reads a pointer at given offset
11 | - `array()`: Returns an array if it backs this pointer
12 |
13 | ## Runtime
14 | A `Runtime` instance for the loaded library can be obtained using
15 | the `Runtime.getRuntime()` method. It gives access to important
16 | services like `ObjectReferenceManager` and `MemoryManager`.
17 |
18 | ### MemoryManager
19 | As the name suggests, it provides various methods to allocate memory
20 | for use with native functions.
21 | - `allocate`: Allocates Java memory
22 | - `allocateDirect`: Allocates native memory
23 | - `allocateTemporary`: Allocates transient native memory
24 |
25 | The `Memory` class also provides utility methods to handle memory
26 | allocation for common use-cases. It uses `MemoryManager` internally.
27 |
28 | ### ObjectReferenceManager
29 | Any native memory associated with a Java object is released as soon as
30 | the object gets garbage-collected. An `ObjectReferenceManager` provides
31 | handy methods to keep objects strongly-referenced as long as its native
32 | memory is in use. This can be helpful while working with function pointers,
33 | which are associated with lambda functions on the Java side. Use `add`
34 | to register any object and use `remove` to dereference the registered object.
--------------------------------------------------------------------------------
/docs/ProjectsUsingJNR.md:
--------------------------------------------------------------------------------
1 | # Projects Using JNR-FFI
2 |
3 | ## Our Projects
4 |
5 | These are projects developed and maintained by (generally) the same team that develops and maintains JNR-FFI.
6 |
7 | ### JRuby
8 |
9 | By far the largest, most substantial and most important project using JNR-FFI is JRuby, a JVM implementation of the Ruby
10 | programming language. In fact JNR-FFI started out as a library for JRuby to solve Ruby's need for native system calls
11 | and provide the same level of intractability with the native world as other Ruby implementations did. JRuby is JNR-FFI's
12 | largest *"client"* and indeed the lead maintainers of JNR-FFI and the JNR project in general are also the lead
13 | developers of JRuby currently employed by Red Hat. This is all to say that we are *"dogfooding"* JNR-FFI in our own
14 | major large-scale project JRuby.
15 |
16 | If you had doubts about whether this library or even the JNR project in general is professional or will survive the
17 | foreseeable future, JRuby's deep dependence on JNR-FFI (and the JNR project) should eliminate those doubts.
18 |
19 | ### JNR Projects
20 |
21 | The JNR Project has multiple libraries that use JNR-FFI to call to native libraries such as JNR-POSIX, JNR-Process and
22 | JNR-ENXIO. Many of these themselves used in JRuby to help increase ease (and performance) of native library calls for
23 | JRuby.
24 |
25 | ## Community Projects
26 |
27 | These are projects developed by the community.
28 |
29 | ### [JRtMidi](https://github.com/basshelal/JRtMidi)
30 |
31 | Java bindings to [RtMidi](https://github.com/thestk/rtmidi) written in Kotlin.
32 |
33 | --------------------------------------------------
34 |
35 | If you're using JNR-FFI and want to add your project here, make a pull request!
--------------------------------------------------------------------------------
/docs/README.md:
--------------------------------------------------------------------------------
1 | # User Documentation
2 |
3 | Here is where you will find documents that explain aspects about JNR-FFI such as how to quickly get started or whether
4 | to even use JNR-FFI.
5 |
6 | * [Getting Started](./GettingStarted.md)
7 | * [Type Mappings](./TypeMappings.md)
8 | * [Memory Management](./MemoryManagement.md)
9 | * [Why Use JNR-FFI](./WhyUseJNR.md)
10 | * [Comparison to Similar Projects](./ComparisonToSimilarProjects.md)
11 | * [Projects using JNR-FFI](./ProjectsUsingJNR.md)
12 | * [Squeezing Performance](./SqueezingPerformance.md)
13 | * [Frequently Asked Questions](./FAQ.md)
--------------------------------------------------------------------------------
/libtest/Benchmark.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | void returnVoid() {
20 |
21 | }
22 |
23 | void returnVoidI(int arg) {
24 | (void)(arg);
25 | }
26 | int returnInt() {
27 | return 0;
28 | }
29 |
30 | int returnIntI(int arg) {
31 | return arg;
32 | }
33 |
34 |
--------------------------------------------------------------------------------
/libtest/BufferTest.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 |
20 | #define MEMSET(buf, value, size) do { \
21 | int i; for (i = 0; i < size; ++i) buf[i] = value; \
22 | } while(0)
23 | #define MEMCPY(dst, src, size) do { \
24 | int i; for (i = 0; i < size; ++i) dst[i] = src[i]; \
25 | } while(0)
26 |
27 | #define FILL(JTYPE, CTYPE) \
28 | void fill##JTYPE##Buffer(CTYPE* buf, CTYPE value, int size) { MEMSET(buf, value, size); }
29 |
30 | #define COPY(JTYPE, CTYPE) \
31 | void copy##JTYPE##Buffer(CTYPE* dst, CTYPE* src, int size) { MEMCPY(dst, src, size); }
32 |
33 | #define FUNC(JTYPE, CTYPE) \
34 | FILL(JTYPE, CTYPE); \
35 | COPY(JTYPE, CTYPE)
36 |
37 | FUNC(Byte, char);
38 | FUNC(Short, short);
39 | FUNC(Int, int);
40 | FUNC(Long, long long);
41 | FUNC(Float, float);
42 | FUNC(Double, double);
43 |
44 |
--------------------------------------------------------------------------------
/libtest/GlobalVariable.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | #include
20 | #include
21 |
22 | typedef int8_t s8;
23 | typedef uint8_t u8;
24 | typedef int16_t s16;
25 | typedef uint16_t u16;
26 | typedef int32_t s32;
27 | typedef uint32_t u32;
28 | typedef int64_t s64;
29 | typedef uint64_t u64;
30 | typedef signed long sL;
31 | typedef unsigned long uL;
32 | typedef float f32;
33 | typedef double f64;
34 | #if !defined(__OpenBSD__)
35 | typedef unsigned long ulong;
36 | #endif
37 | typedef void* pointer;
38 | typedef void* P;
39 |
40 | #define GVAR(T) \
41 | extern T gvar_##T; \
42 | T gvar_##T = (T) -1; \
43 | T gvar_##T##_get() { return gvar_##T; }; \
44 | void gvar_##T##_set(T v) { gvar_##T = v; }
45 |
46 | GVAR(s8);
47 | GVAR(u8);
48 | GVAR(s16);
49 | GVAR(u16);
50 | GVAR(s32);
51 | GVAR(u32);
52 | GVAR(s64);
53 | GVAR(u64);
54 | GVAR(long);
55 | GVAR(ulong);
56 | GVAR(pointer);
57 |
58 | struct gstruct {
59 | long data;
60 | };
61 |
62 | struct gstruct gvar_gstruct = { -1 };
63 |
64 | struct gstruct*
65 | gvar_gstruct_get(void)
66 | {
67 | return &gvar_gstruct;
68 | }
69 |
70 | void
71 | gvar_gstruct_set(const struct gstruct* val)
72 | {
73 | gvar_gstruct = *val;
74 | }
75 |
--------------------------------------------------------------------------------
/libtest/LastErrorTest.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 | #include
19 |
20 | #ifdef _WIN32
21 | #include
22 | #endif
23 |
24 | int setLastError(int error) {
25 | #ifdef _WIN32
26 | SetLastError(error);
27 | #else
28 | errno = error;
29 | #endif
30 |
31 | return -1;
32 | }
33 |
--------------------------------------------------------------------------------
/libtest/PointerNumericTest.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #ifndef __mips__
5 | # include
6 | #endif
7 |
8 | #if defined(__mips64) || defined(__PASE__)
9 | # include
10 | #endif
11 |
12 | #include
13 | #include
14 | #include
15 | #include
16 |
17 | // Let's define the stdint.h typedefs ourselves if they can't be found
18 | #if !defined(_STDINT_H_) && !defined(_STDINT_H) && !defined(_SYS__STDINT_H_) && !defined(_H_STDINT)
19 | typedef signed char int8_t;
20 | typedef signed short int16_t;
21 | typedef signed int int32_t;
22 | typedef signed long long int64_t;
23 | typedef unsigned char uint8_t;
24 | typedef unsigned short uint16_t;
25 | typedef unsigned int uint32_t;
26 | typedef unsigned long long uint64_t;
27 | #endif
28 |
29 | typedef long double ldouble;
30 | typedef unsigned long ulong;
31 | typedef void* pointer;
32 | typedef void* ptr;
33 | typedef enum Enum_t {e0, e1, e2, e3} Enum;
34 | typedef bool boolean;
35 |
36 | #define RET(T) T ptr_num_ret_##T(void *p, int offset) { \
37 | T tmp; memcpy(&tmp, (ptr) (p + offset), sizeof(tmp)); return tmp; \
38 | }
39 | #define SET(T) void ptr_num_set_##T(void *p, int offset, T value) { \
40 | memcpy((ptr) (p + offset), &value, sizeof(value)); \
41 | }
42 |
43 | #define TEST(T) SET(T) RET(T)
44 |
45 | TEST(int8_t);
46 | TEST(int16_t);
47 | TEST(int32_t);
48 | TEST(long);
49 | TEST(int64_t);
50 |
51 | TEST(uint8_t);
52 | TEST(uint16_t);
53 | TEST(uint32_t);
54 | TEST(ulong);
55 | TEST(uint64_t);
56 |
57 | TEST(float);
58 | TEST(double);
59 | TEST(ldouble);
60 |
61 | TEST(boolean);
62 | TEST(pointer);
63 | TEST(Enum);
64 |
65 | void *ptr_num_arr_get(void **array, int index) {
66 | return array[index];
67 | }
68 |
69 | void ptr_num_arr_set(void **array, int index, void *value) {
70 | array[index] = value;
71 | }
72 |
--------------------------------------------------------------------------------
/libtest/ReferenceTest.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | #if !defined(__mips__) || defined(__PASE__)
20 | # include
21 | #endif
22 |
23 | #if !defined(_STDINT_H_) && !defined(_STDINT_H) && !defined(_SYS__STDINT_H_) && !defined(_H_STDINT)
24 | typedef signed char int8_t;
25 | typedef signed short int16_t;
26 | typedef signed int int32_t;
27 | typedef signed long long int64_t;
28 | #endif
29 |
30 | #define REF(T) void ref_##T(T arg, T* result) { *result = arg; }
31 | #define ADD(T) void ref_add_##T(T arg1, T arg2, T* result) { *result = arg1 + arg2; }
32 | #define SUB(T) void ref_sub_##T(T arg1, T arg2, T* result) { *result = arg1 - arg2; }
33 | #define MUL(T) void ref_mul_##T(T arg1, T arg2, T* result) { *result = arg1 * arg2; }
34 | #define DIV(T) void ref_div_##T(T arg1, T arg2, T* result) { *result = arg1 / arg2; }
35 | #define TEST(T) ADD(T) SUB(T) MUL(T) DIV(T) REF(T)
36 |
37 | TEST(int8_t);
38 | TEST(int16_t);
39 | TEST(int32_t);
40 | TEST(int64_t);
41 | TEST(float);
42 | TEST(double);
43 |
44 |
--------------------------------------------------------------------------------
/libtest/StringTest.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | #include
20 |
21 | int
22 | string_equals(const char* s1, const char* s2)
23 | {
24 | return strcmp(s1, s2) == 0;
25 | }
26 |
27 | void
28 | string_set(char* s1, const char* s2)
29 | {
30 | strcpy(s1, s2);
31 | }
32 |
33 | void
34 | string_concat(char* dst, const char* src)
35 | {
36 | char* ep = dst;
37 | while (*ep)
38 | ep++;
39 | strcpy(ep, src);
40 | }
41 |
42 | char*
43 | string_duplicate(const char* s1)
44 | {
45 | return strdup(s1);
46 | }
47 |
--------------------------------------------------------------------------------
/libtest/struct/NestedUnion.c:
--------------------------------------------------------------------------------
1 | #include "StructTypes.h"
2 |
3 | #define SET_VAL(TYPE) \
4 | TYPE nested_union_inner_struct_get_##TYPE(NestedUnion *s) { \
5 | return s->inner_NumericStruct.val_##TYPE; \
6 | } \
7 | TYPE nested_union_ptr_struct_get_##TYPE(NestedUnion *s) { \
8 | return s->ptr_NumericStruct->val_##TYPE; \
9 | } \
10 | TYPE nested_union_inner_union_get_##TYPE(NestedUnion *s) { \
11 | return s->inner_NumericUnion.val_##TYPE; \
12 | } \
13 | TYPE nested_union_ptr_union_get_##TYPE(NestedUnion *s) { \
14 | return s->ptr_NumericUnion->val_##TYPE; \
15 | }
16 |
17 | #define GET_VAL(TYPE) \
18 | void nested_union_inner_struct_set_##TYPE(NestedUnion *s, TYPE v) { \
19 | s->inner_NumericStruct.val_##TYPE = v; \
20 | } \
21 | void nested_union_ptr_struct_set_##TYPE(NestedUnion *s, TYPE v) { \
22 | s->ptr_NumericStruct->val_##TYPE = v; \
23 | } \
24 | void nested_union_inner_union_set_##TYPE(NestedUnion *s, TYPE v) { \
25 | s->inner_NumericUnion.val_##TYPE = v; \
26 | } \
27 | void nested_union_ptr_union_set_##TYPE(NestedUnion *s, TYPE v) { \
28 | s->ptr_NumericUnion->val_##TYPE = v; \
29 | }
30 |
31 |
32 | // ============================= Set Functions ======================
33 |
34 | SET_VAL(int8_t);
35 | SET_VAL(int16_t);
36 | SET_VAL(int32_t);
37 | SET_VAL(long);
38 | SET_VAL(int64_t);
39 |
40 | SET_VAL(uint8_t);
41 | SET_VAL(uint16_t);
42 | SET_VAL(uint32_t);
43 | SET_VAL(ulong);
44 | SET_VAL(uint64_t);
45 |
46 | SET_VAL(float);
47 | SET_VAL(double);
48 |
49 | SET_VAL(bool);
50 | SET_VAL(Enum);
51 | SET_VAL(pointer);
52 |
53 | // ============================= Get Functions ======================
54 |
55 | GET_VAL(int8_t);
56 | GET_VAL(int16_t);
57 | GET_VAL(int32_t);
58 | GET_VAL(long);
59 | GET_VAL(int64_t);
60 |
61 | GET_VAL(uint8_t);
62 | GET_VAL(uint16_t);
63 | GET_VAL(uint32_t);
64 | GET_VAL(ulong);
65 | GET_VAL(uint64_t);
66 |
67 | GET_VAL(float);
68 | GET_VAL(double);
69 |
70 | GET_VAL(bool);
71 | GET_VAL(Enum);
72 | GET_VAL(pointer);
73 |
74 | int nested_union_size() {
75 | return sizeof(NestedUnion);
76 | }
77 |
--------------------------------------------------------------------------------
/libtest/struct/NumericStruct.c:
--------------------------------------------------------------------------------
1 | #include "StructTypes.h"
2 |
3 | #define SET_VAL(TYPE) \
4 | TYPE struct_num_get_##TYPE(NumericStruct *s) { \
5 | return s->val_##TYPE; \
6 | }
7 |
8 | #define GET_VAL(TYPE) \
9 | void struct_num_set_##TYPE(NumericStruct *s, TYPE v) { \
10 | s->val_##TYPE = v; \
11 | }
12 |
13 | // ============================= Set Functions ======================
14 |
15 | SET_VAL(int8_t);
16 | SET_VAL(int16_t);
17 | SET_VAL(int32_t);
18 | SET_VAL(long);
19 | SET_VAL(int64_t);
20 |
21 | SET_VAL(uint8_t);
22 | SET_VAL(uint16_t);
23 | SET_VAL(uint32_t);
24 | SET_VAL(ulong);
25 | SET_VAL(uint64_t);
26 |
27 | SET_VAL(float);
28 | SET_VAL(double);
29 |
30 | SET_VAL(bool);
31 | SET_VAL(Enum);
32 | SET_VAL(pointer);
33 |
34 | // ============================= Get Functions ======================
35 |
36 | GET_VAL(int8_t);
37 | GET_VAL(int16_t);
38 | GET_VAL(int32_t);
39 | GET_VAL(long);
40 | GET_VAL(int64_t);
41 |
42 | GET_VAL(uint8_t);
43 | GET_VAL(uint16_t);
44 | GET_VAL(uint32_t);
45 | GET_VAL(ulong);
46 | GET_VAL(uint64_t);
47 |
48 | GET_VAL(float);
49 | GET_VAL(double);
50 |
51 | GET_VAL(bool);
52 | GET_VAL(Enum);
53 | GET_VAL(pointer);
54 |
55 | int struct_num_size() {
56 | return sizeof(NumericStruct);
57 | }
58 |
--------------------------------------------------------------------------------
/libtest/struct/NumericUnion.c:
--------------------------------------------------------------------------------
1 | #include "StructTypes.h"
2 |
3 | #define SET_VAL(TYPE) \
4 | TYPE union_num_get_##TYPE(NumericUnion *s) { \
5 | return s->val_##TYPE; \
6 | }
7 |
8 | #define GET_VAL(TYPE) \
9 | void union_num_set_##TYPE(NumericUnion *s, TYPE v) { \
10 | s->val_##TYPE = v; \
11 | }
12 |
13 | // ============================= Set Functions ======================
14 |
15 | SET_VAL(int8_t);
16 | SET_VAL(int16_t);
17 | SET_VAL(int32_t);
18 | SET_VAL(long);
19 | SET_VAL(int64_t);
20 |
21 | SET_VAL(uint8_t);
22 | SET_VAL(uint16_t);
23 | SET_VAL(uint32_t);
24 | SET_VAL(ulong);
25 | SET_VAL(uint64_t);
26 |
27 | SET_VAL(float);
28 | SET_VAL(double);
29 |
30 | SET_VAL(bool);
31 | SET_VAL(Enum);
32 | SET_VAL(pointer);
33 |
34 | // ============================= Get Functions ======================
35 |
36 | GET_VAL(int8_t);
37 | GET_VAL(int16_t);
38 | GET_VAL(int32_t);
39 | GET_VAL(long);
40 | GET_VAL(int64_t);
41 |
42 | GET_VAL(uint8_t);
43 | GET_VAL(uint16_t);
44 | GET_VAL(uint32_t);
45 | GET_VAL(ulong);
46 | GET_VAL(uint64_t);
47 |
48 | GET_VAL(float);
49 | GET_VAL(double);
50 |
51 | GET_VAL(bool);
52 | GET_VAL(Enum);
53 | GET_VAL(pointer);
54 |
55 | int union_num_size() {
56 | return sizeof(NumericUnion);
57 | }
58 |
--------------------------------------------------------------------------------
/libtest/struct/StructTypes.h:
--------------------------------------------------------------------------------
1 | #include "../types.h"
2 |
3 | typedef struct NumericStruct_t {
4 | int8_t val_int8_t;
5 | int16_t val_int16_t;
6 | int32_t val_int32_t;
7 | long val_long;
8 | int64_t val_int64_t;
9 |
10 | uint8_t val_uint8_t;
11 | uint16_t val_uint16_t;
12 | uint32_t val_uint32_t;
13 | ulong val_ulong;
14 | uint64_t val_uint64_t;
15 |
16 | float val_float;
17 | double val_double;
18 |
19 | bool val_bool;
20 | Enum val_Enum;
21 | pointer val_pointer;
22 | } NumericStruct;
23 |
24 | typedef union NumericUnion_t {
25 | int8_t val_int8_t;
26 | int16_t val_int16_t;
27 | int32_t val_int32_t;
28 | long val_long;
29 | int64_t val_int64_t;
30 |
31 | uint8_t val_uint8_t;
32 | uint16_t val_uint16_t;
33 | uint32_t val_uint32_t;
34 | ulong val_ulong;
35 | uint64_t val_uint64_t;
36 |
37 | float val_float;
38 | double val_double;
39 |
40 | bool val_bool;
41 | Enum val_Enum;
42 | pointer val_pointer;
43 | } NumericUnion;
44 |
45 | typedef struct NestedStruct_t {
46 | NumericStruct inner_NumericStruct;
47 | NumericUnion inner_NumericUnion;
48 |
49 | NumericStruct *ptr_NumericStruct;
50 | NumericUnion *ptr_NumericUnion;
51 | } NestedStruct;
52 |
53 | typedef union NestedUnion_t {
54 | NumericStruct inner_NumericStruct;
55 | NumericUnion inner_NumericUnion;
56 |
57 | NumericStruct *ptr_NumericStruct;
58 | NumericUnion *ptr_NumericUnion;
59 | } NestedUnion;
60 |
--------------------------------------------------------------------------------
/libtest/types.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #if !defined(__mips___) || defined(__PASE__)
4 | # include
5 | #endif
6 |
7 | // Let's define the stdint.h typedefs ourselves if they can't be found
8 | #if !defined (_STDINT_H_) && !defined(_STDINT_H) && !defined(_SYS__STDINT_H_) && !defined(_H_STDINT)
9 | typedef signed char int8_t;
10 | typedef signed short int16_t;
11 | typedef signed int int32_t;
12 | typedef signed long long int64_t;
13 | typedef unsigned char uint8_t;
14 | typedef unsigned short uint16_t;
15 | typedef unsigned int uint32_t;
16 | typedef unsigned long long uint64_t;
17 | #endif
18 |
19 | typedef unsigned long ulong;
20 | typedef long double ldouble;
21 | typedef void* pointer;
22 | typedef enum Enum_t {e0, e1, e2, e3} Enum;
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/CallingConvention.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi;
20 |
21 | /**
22 | * Native function calling conventions.
23 | *
24 | *
This is only needed on windows platforms - all platforms assume
25 | * {@link #DEFAULT} as the calling convention.
26 | */
27 | public enum CallingConvention {
28 | /**
29 | * The default C calling convention
30 | */
31 | DEFAULT,
32 | /**
33 | * Windows stdcall calling convention
34 | */
35 | STDCALL
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/LastError.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi;
20 |
21 | /**
22 | * Provides access to the unix errno and windows GetLastError() value.
23 | */
24 | public final class LastError {
25 | private LastError() {}
26 |
27 | /**
28 | * Gets the value of errno from the last native call.
29 | *
30 | * @param runtime FFI runtime to get errno for.
31 | * @return An integer containing the errno value.
32 | */
33 | public static int getLastError(Runtime runtime) {
34 | return runtime.getLastError();
35 | }
36 |
37 | /**
38 | * Sets the native errno value.
39 | *
40 | * @param runtime FFI runtime to set errno for.
41 | * @param error The value to set errno to.
42 | */
43 | public static void setLastError(Runtime runtime, int error) {
44 | runtime.setLastError(error);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/Type.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi;
20 |
21 | /**
22 | * Type is the superclass for all internal types used by jnr-ffi.
23 | *
24 | *
25 | * Use this type to access meta-data about a native type, such as its size or natural alignment.
26 | *
27 | * To obtain an instance of this class, use {@link jnr.ffi.Runtime#findType(NativeType)} or
28 | * {@link jnr.ffi.Runtime#findType(TypeAlias)}.
29 | *
30 | * Example
31 | *
32 | * {@code
33 | *
34 | * Type pointerType = runtime.findType(NativeType.ADDRESS);
35 | *
36 | * System.out.println("The size of a pointer on this platform is " + pointerType.size());
37 | * }
38 | *
39 | */
40 | public abstract class Type {
41 | /**
42 | * The size in bytes of this type.
43 | *
44 | * @return An integer
45 | */
46 | public abstract int size();
47 |
48 | /**
49 | * The native alignment of this type, in bytes
50 | *
51 | * @return An integer
52 | */
53 | public abstract int alignment();
54 |
55 | /**
56 | * The native type of this type
57 | *
58 | * @return the native type of this type
59 | */
60 | public abstract NativeType getNativeType();
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/TypeAlias.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi;
20 |
21 | public enum TypeAlias {
22 | int8_t,
23 | u_int8_t,
24 | int16_t,
25 | u_int16_t,
26 | int32_t,
27 | u_int32_t,
28 | int64_t,
29 | u_int64_t,
30 | intptr_t,
31 | uintptr_t,
32 | caddr_t,
33 | dev_t,
34 | blkcnt_t,
35 | blksize_t,
36 | gid_t,
37 | in_addr_t,
38 | in_port_t,
39 | ino_t,
40 | ino64_t,
41 | key_t,
42 | mode_t,
43 | nlink_t,
44 | id_t,
45 | pid_t,
46 | off_t,
47 | swblk_t,
48 | uid_t,
49 | clock_t,
50 | size_t,
51 | ssize_t,
52 | time_t,
53 | fsblkcnt_t,
54 | fsfilcnt_t,
55 | sa_family_t,
56 | socklen_t,
57 | rlim_t,
58 | cc_t,
59 | speed_t,
60 | tcflag_t,
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/Union.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 | /*
19 | * Some of the design and code of this class is from the javolution project.
20 | *
21 | * Copyright (C) 2006 - Javolution (http://javolution.org/)
22 | * All rights reserved.
23 | *
24 | * Permission to use, copy, modify, and distribute this software is
25 | * freely granted, provided that this notice is preserved.
26 | */
27 |
28 | package jnr.ffi;
29 |
30 | /**
31 | * Represents a C union
32 | */
33 | public abstract class Union extends Struct {
34 | protected Union(Runtime runtime) {
35 | super(runtime, true);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/Variable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi;
20 |
21 | /**
22 | * Access library global variables.
23 | *
24 | *
25 | * To access global variables, declare a method with a parameterized return type of this class.
26 | *
41 | */
42 | public interface Variable {
43 | /**
44 | * Gets the current value of the global variable
45 | *
46 | * @return The value of the variable
47 | */
48 | public T get();
49 |
50 | /**
51 | * Sets the global variable to a value
52 | *
53 | * @param value The value to set the global variable to.
54 | */
55 | public void set(T value);
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/Clear.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 |
22 | import java.lang.annotation.ElementType;
23 | import java.lang.annotation.Retention;
24 | import java.lang.annotation.RetentionPolicy;
25 | import java.lang.annotation.Target;
26 |
27 | /**
28 | * Indicates that the temporary native memory allocated for an {@code @Out} paramneter
29 | * should be cleared before passing to the native function.
30 | *
31 | *
By default, parameters that are annotated as {@code @Out} only do not clear
32 | * the data in the temporary native memory area allocated when a java heap object
33 | * is passed in as the parameter, so the memory passed to the native function is
34 | * full of garbage data. After the native method returns, the native memory is
35 | * copied back to java, which is usually not a problem, since the native function
36 | * will have updated the memory with valid data. However, if the native function
37 | * fails, the garbage data that was in the temporary native memory will be copied
38 | * back to java.
39 | *
40 | * @see In
41 | * @see Out
42 | */
43 | @Retention(RetentionPolicy.RUNTIME)
44 | @Target({ ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
45 | public @interface Clear {
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/Delegate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import jnr.ffi.CallingConvention;
22 |
23 | import java.lang.annotation.ElementType;
24 | import java.lang.annotation.Retention;
25 | import java.lang.annotation.RetentionPolicy;
26 | import java.lang.annotation.Target;
27 |
28 | /**
29 | *
30 | */
31 | @Retention(RetentionPolicy.RUNTIME)
32 | @Target(ElementType.METHOD)
33 | public @interface Delegate {
34 | CallingConvention convention() default CallingConvention.DEFAULT;
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/Direct.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 |
22 | import java.lang.annotation.ElementType;
23 | import java.lang.annotation.Retention;
24 | import java.lang.annotation.RetentionPolicy;
25 | import java.lang.annotation.Target;
26 |
27 | /**
28 | * Indicates that a {@link jnr.ffi.Struct}} parameter should be
29 | * backed by a persistent native memory block.
30 | *
31 | *
Without the {@code @Direct} annotation, the native code will allocate a
32 | * temporary native memory block for the parameter, and free it immediately after
33 | * the call.
34 | *
35 | *
By using {@code @Direct}, the native memory block is permanently associated
36 | * with the {@link jnr.ffi.Struct} instance, and will remain allocated
37 | * for as long as the {@code Struct} instance remains strongly referenced by java code.
38 | *
39 | */
40 | @Retention(RetentionPolicy.RUNTIME)
41 | @Target({ ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
42 | public @interface Direct {
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/Encoding.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 | package jnr.ffi.annotations;
19 |
20 | import java.lang.annotation.ElementType;
21 | import java.lang.annotation.Retention;
22 | import java.lang.annotation.RetentionPolicy;
23 | import java.lang.annotation.Target;
24 |
25 | /**
26 | * Used to specify the {@link java.nio.charset.Charset} to use for encoding/decoding a {@link String}
27 | */
28 | @Retention(RetentionPolicy.RUNTIME)
29 | @Target(value = { ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE, ElementType.ANNOTATION_TYPE })
30 | public @interface Encoding {
31 | String value();
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/IgnoreError.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import jnr.ffi.LibraryLoader;
22 |
23 | import java.lang.annotation.Retention;
24 | import java.lang.annotation.RetentionPolicy;
25 | import java.util.Map;
26 |
27 | /**
28 | * Indicates that the errno value for a native function need not be saved after
29 | * the function returns.
30 | *
31 | *
Due to the nature of the Java Virtual Machine, the errno value must be saved
32 | * immediately after the native function is called, otherwise internal jvm operations
33 | * may overwrite it before control is returned to java code.
34 | *
35 | *
Since it is not possible for jnr-ffi to infer in a generic way whether a native
36 | * function has succeeded or failed, the C errno value is saved after every native
37 | * function call - even for the ones that succeed. This can have a significant
38 | * performance impact, so for those functions which either don't fail, or for which
39 | * the errno value can be ignored, can be annotated with {@code @IgnoreError} to
40 | * avoid unneccessary saving of the errno value.
41 | *
42 | * @see SaveError
43 | * @see LibraryLoader#saveError(Map, boolean, boolean)
44 | */
45 | @Retention(RetentionPolicy.RUNTIME)
46 | public @interface IgnoreError {
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/In.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Indicates that the parameter is an IN parameter.
28 | *
29 | *
When a java object is passed to a native function as a pointer
30 | * (for example {@link jnr.ffi.Pointer}, {@link jnr.ffi.Struct}, {@link java.nio.ByteBuffer}),
31 | * then a temporary native memory block is allocated, the java data is copied to
32 | * the temporary memory and the address of the temporary memory is passed to the function.
33 | * After the function returns, the java data is automatically updated from the
34 | * contents of the native memory.
35 | *
36 | *
As this extra copying can be expensive, parameters can be annotated with {@code @In}
37 | * so the data is only copied from java {@code IN} to native memory, but not copied
38 | * back {@code OUT} from native memory to java memory.
39 | *
40 | *
Parameters with neither a {@code @In} nor a {@code @Out} annotation will copy both ways.
41 | *
42 | */
43 | @Retention(RetentionPolicy.RUNTIME)
44 | @Target({ ElementType.PARAMETER, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
45 | public @interface In {
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/LongLong.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import jnr.ffi.TypeAlias;
22 |
23 | import java.lang.annotation.ElementType;
24 | import java.lang.annotation.Retention;
25 | import java.lang.annotation.RetentionPolicy;
26 | import java.lang.annotation.Target;
27 |
28 | /**
29 | * Indicates that a long parameter should be treated as native long-long (64bit)
30 | * instead of the platform-dependent long size.
31 | */
32 | @Retention(RetentionPolicy.RUNTIME)
33 | @Target(value = { ElementType.PARAMETER, ElementType.METHOD })
34 | @TypeDefinition(alias = TypeAlias.int64_t)
35 | public @interface LongLong {
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/Meta.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | @Retention(RetentionPolicy.RUNTIME)
27 | @Target(ElementType.ANNOTATION_TYPE)
28 | public @interface Meta {
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/NulTerminate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Indicates that a byte array or ByteBuffer should be terminated with a zero byte
28 | * before passing it to a native function.
29 | */
30 | @Retention(RetentionPolicy.RUNTIME)
31 | @Target({ ElementType.PARAMETER, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
32 | public @interface NulTerminate {
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/Pinned.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Marks a method parameter as being pinnable.
28 | *
29 | * This means the data for the parameter is not copied to/from native memory.
30 | * Instead, the JVM memory is locked and passed directly to the native code.
31 | *
32 | *
33 | * Because the data is passed to native code unconverted, care must be taken to
34 | * ensure element widths align for types wider than byte.
35 | *
36 | *
37 | * IMPORTANT: This should not be used for functions that may block on
38 | * network or filesystem access such as read(2), write(2), stat(2).
39 | *
40 | */
41 | @Retention(RetentionPolicy.RUNTIME)
42 | @Target({ ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
43 | public @interface Pinned {
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/SaveError.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import jnr.ffi.LibraryLoader;
22 |
23 | import java.lang.annotation.Retention;
24 | import java.lang.annotation.RetentionPolicy;
25 | import java.util.Map;
26 |
27 | /**
28 | * Tags a library method as requiring any error codes as returned
29 | * by errno on unix, or GetLastError on windows be saved.
30 | *
31 | * @see IgnoreError
32 | * @see LibraryLoader#saveError(Map, boolean, boolean)
33 | */
34 | @Retention(RetentionPolicy.RUNTIME)
35 | public @interface SaveError {
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/StdCall.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | @Retention(RetentionPolicy.RUNTIME)
27 | @Target(ElementType.METHOD)
28 | public @interface StdCall {
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/Synchronized.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import java.lang.annotation.Retention;
22 | import java.lang.annotation.RetentionPolicy;
23 |
24 | /**
25 | * Indicates that a library or a library method requires all calls to be
26 | * synchronized.
27 | *
28 | * i.e. calls from multiple threads will synchronize on a monitor object,
29 | * then call the native method.
30 | */
31 | @Retention(RetentionPolicy.RUNTIME)
32 | public @interface Synchronized {
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/Transient.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 |
22 | import java.lang.annotation.ElementType;
23 | import java.lang.annotation.Retention;
24 | import java.lang.annotation.RetentionPolicy;
25 | import java.lang.annotation.Target;
26 |
27 | /**
28 | * Indicates that the parameter is transient.
29 | *
30 | * This means it can be backed by a temporarily allocated native memory block,
31 | * and after the method call, the native memory can be freed again.
32 | */
33 | @Retention(RetentionPolicy.RUNTIME)
34 | @Target({ ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
35 | public @interface Transient {
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/TypeDefinition.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.annotations;
20 |
21 | import jnr.ffi.TypeAlias;
22 |
23 | import java.lang.annotation.ElementType;
24 | import java.lang.annotation.Retention;
25 | import java.lang.annotation.RetentionPolicy;
26 | import java.lang.annotation.Target;
27 |
28 | /**
29 | * This is used internally by jnr-ffi to define type aliases, e.g. {@code ssize_t => long}.
30 | */
31 | @Retention(RetentionPolicy.RUNTIME)
32 | @Target(value = { ElementType.ANNOTATION_TYPE })
33 | public @interface TypeDefinition {
34 | TypeAlias alias();
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/annotations/Variadic.java:
--------------------------------------------------------------------------------
1 | package jnr.ffi.annotations;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | /**
7 | * Specifies that a non-varargs function binding will call a variadic C function with the specified number of fixed
8 | * arguments.
9 | *
10 | * Platforms that pass variadic arguments differently than fixed arguments will need this annotation if the Java binding
11 | * does not itself use varargs. Without Java varargs or this annotation, there's no way for jnr-ffi to know where fixed
12 | * args end and variadic arguments begin, causing them all to be passed the way fixed arguments get passed on the given
13 | * platform.
14 | *
15 | * See https://github.com/jnr/jnr-ffi/pull/292
16 | */
17 | @Retention(RetentionPolicy.RUNTIME)
18 | public @interface Variadic {
19 | int fixedCount();
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/byref/AbstractReference.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.byref;
20 |
21 | /**
22 | * An abstract class for common PrimitiveReference functionality
23 | */
24 | abstract public class AbstractReference implements ByReference {
25 | T value;
26 |
27 | protected AbstractReference(T value) {
28 | this.value = value;
29 | }
30 |
31 | protected static T checkNull(T value) {
32 | if (value == null) {
33 | throw new NullPointerException("reference value cannot be null");
34 | }
35 |
36 | return value;
37 | }
38 |
39 | /**
40 | * Gets the current value the reference points to.
41 | *
42 | * @return the current value.
43 | */
44 | public T getValue() {
45 | return value;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/AbstractDataConverter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | /**
22 | *
23 | */
24 | abstract public class AbstractDataConverter implements DataConverter {
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/AbstractFromNativeType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | /**
22 | *
23 | */
24 | abstract public class AbstractFromNativeType implements FromNativeType {
25 | private final FromNativeConverter converter;
26 |
27 | AbstractFromNativeType(FromNativeConverter converter) {
28 | this.converter = converter;
29 | }
30 |
31 | @Override
32 | public FromNativeConverter getFromNativeConverter() {
33 | return converter;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/AbstractSignatureTypeMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | /**
22 | *
23 | */
24 | abstract public class AbstractSignatureTypeMapper implements SignatureTypeMapper {
25 |
26 | @Override
27 | public FromNativeType getFromNativeType(SignatureType type, FromNativeContext context) {
28 | return null;
29 | }
30 |
31 | @Override
32 | public ToNativeType getToNativeType(SignatureType type, ToNativeContext context) {
33 | return null;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/AbstractToNativeType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | /**
22 | *
23 | */
24 | abstract public class AbstractToNativeType implements ToNativeType {
25 | private final ToNativeConverter converter;
26 |
27 | AbstractToNativeType(ToNativeConverter converter) {
28 | this.converter = converter;
29 | }
30 |
31 | @Override
32 | public ToNativeConverter getToNativeConverter() {
33 | return converter;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/CompositeFunctionMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.util.ArrayList;
22 | import java.util.Collection;
23 | import java.util.Collections;
24 |
25 | /**
26 | *
27 | */
28 | public final class CompositeFunctionMapper implements FunctionMapper {
29 | private final Collection functionMappers;
30 |
31 | public CompositeFunctionMapper(Collection functionMappers) {
32 | this.functionMappers = Collections.unmodifiableList(new ArrayList(functionMappers));
33 | }
34 |
35 | @Override
36 | public String mapFunctionName(String functionName, Context context) {
37 | for (FunctionMapper functionMapper : functionMappers) {
38 | String mappedName = functionMapper.mapFunctionName(functionName, context);
39 | if (mappedName != functionName) {
40 | // A translation was explicit in this mapper.
41 | return mappedName;
42 | }
43 | }
44 | return functionName;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/DataConverter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | /**
22 | *
23 | */
24 | public interface DataConverter extends ToNativeConverter, FromNativeConverter {
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/DefaultTypeMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.util.LinkedHashMap;
22 | import java.util.Map;
23 |
24 | /**
25 | *
26 | */
27 | public final class DefaultTypeMapper implements TypeMapper {
28 | private final Map toNativeConverters;
29 | private final Map fromNativeConverters;
30 |
31 | public DefaultTypeMapper() {
32 | toNativeConverters = new LinkedHashMap();
33 | fromNativeConverters = new LinkedHashMap();
34 | }
35 |
36 | public final void put(Class javaClass, DataConverter converter) {
37 | toNativeConverters.put(javaClass, converter);
38 | fromNativeConverters.put(javaClass, converter);
39 | }
40 |
41 | public final void put(Class javaClass, ToNativeConverter converter) {
42 | toNativeConverters.put(javaClass, converter);
43 | }
44 |
45 | public final void put(Class javaClass, FromNativeConverter converter) {
46 | fromNativeConverters.put(javaClass, converter);
47 | }
48 |
49 | public FromNativeConverter getFromNativeConverter(Class type) {
50 | return fromNativeConverters.get(type);
51 | }
52 |
53 | public ToNativeConverter getToNativeConverter(Class type) {
54 | return toNativeConverters.get(type);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/FromNativeContext.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.lang.annotation.Annotation;
22 | import java.util.Collection;
23 |
24 | /**
25 | * Context for conversion from native to Java.
26 | */
27 | public interface FromNativeContext {
28 | /**
29 | * Gets a sorted list of annotations
30 | *
31 | * @return a sorted list of annotations for this native type
32 | */
33 | public abstract Collection getAnnotations();
34 |
35 | /**
36 | * Gets the Runtime used for the conversion.
37 | *
38 | * @return The runtime used for the conversion.
39 | */
40 | public jnr.ffi.Runtime getRuntime();
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/FromNativeConverter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * Converts data from a native type to a java type
28 | */
29 | public interface FromNativeConverter {
30 | public J fromNative(N nativeValue, FromNativeContext context);
31 | public Class nativeType();
32 |
33 | @Retention(RetentionPolicy.RUNTIME)
34 | @Target({ElementType.TYPE, ElementType.METHOD})
35 | public static @interface NoContext {
36 | }
37 |
38 | @Retention(RetentionPolicy.RUNTIME)
39 | @Target(ElementType.TYPE)
40 | public static @interface Cacheable {
41 | }
42 |
43 | @Retention(RetentionPolicy.RUNTIME)
44 | @Target(ElementType.METHOD)
45 | @interface FromNative {
46 | Class nativeType();
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/FromNativeType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | *
28 | */
29 | public interface FromNativeType {
30 | public FromNativeConverter getFromNativeConverter();
31 |
32 | @Retention(RetentionPolicy.RUNTIME)
33 | @Target(ElementType.TYPE)
34 | public static @interface Cacheable {
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/FromNativeTypes.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | /**
22 | *
23 | */
24 | public final class FromNativeTypes {
25 |
26 | public static FromNativeType create(FromNativeConverter converter) {
27 | if (converter == null) {
28 | return null;
29 | }
30 | return converter.getClass().isAnnotationPresent(FromNativeConverter.Cacheable.class)
31 | ? new Cacheable(converter) : new UnCacheable(converter);
32 | }
33 |
34 | @FromNativeType.Cacheable
35 | static class Cacheable extends AbstractFromNativeType {
36 | public Cacheable(FromNativeConverter converter) {
37 | super(converter);
38 | }
39 | }
40 |
41 | static class UnCacheable extends AbstractFromNativeType {
42 | public UnCacheable(FromNativeConverter converter) {
43 | super(converter);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/MethodResultContext.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.lang.annotation.Annotation;
22 | import java.lang.reflect.Method;
23 | import java.util.Collection;
24 |
25 | import static jnr.ffi.util.Annotations.sortedAnnotationCollection;
26 |
27 | /**
28 | *
29 | */
30 | public class MethodResultContext implements FromNativeContext {
31 | private final jnr.ffi.Runtime runtime;
32 | private final Method method;
33 | private Collection annotations;
34 |
35 | public MethodResultContext(jnr.ffi.Runtime runtime, Method method) {
36 | this.runtime = runtime;
37 | this.method = method;
38 | }
39 |
40 | public Method getMethod() {
41 | return method;
42 | }
43 |
44 | public Collection getAnnotations() {
45 | return annotations != null ? annotations : (annotations = sortedAnnotationCollection(method.getAnnotations()));
46 | }
47 |
48 | public jnr.ffi.Runtime getRuntime() {
49 | return runtime;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/SignatureType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.lang.annotation.Annotation;
22 | import java.util.Collection;
23 |
24 | /**
25 | *
26 | */
27 | public interface SignatureType {
28 | public Class getDeclaredType();
29 | public Collection getAnnotations();
30 | public java.lang.reflect.Type getGenericType();
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/SignatureTypeMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | /**
22 |
23 | */
24 | public interface SignatureTypeMapper {
25 | public FromNativeType getFromNativeType(SignatureType type, FromNativeContext context);
26 | public ToNativeType getToNativeType(SignatureType type, ToNativeContext context);
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/SignatureTypeMapperAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | /**
22 | * Adapts a {@link jnr.ffi.mapper.TypeMapper} to a SignatureTypeMapper
23 | */
24 | public class SignatureTypeMapperAdapter implements SignatureTypeMapper {
25 | private final TypeMapper typeMapper;
26 |
27 | public SignatureTypeMapperAdapter(TypeMapper typeMapper) {
28 | this.typeMapper = typeMapper;
29 | }
30 |
31 | @Override
32 | public FromNativeType getFromNativeType(SignatureType type, FromNativeContext context) {
33 | return FromNativeTypes.create(typeMapper.getFromNativeConverter(type.getDeclaredType()));
34 | }
35 |
36 | @Override
37 | public ToNativeType getToNativeType(SignatureType type, ToNativeContext context) {
38 | return ToNativeTypes.create(typeMapper.getToNativeConverter(type.getDeclaredType()));
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/SimpleFunctionMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.util.Collections;
22 | import java.util.HashMap;
23 | import java.util.Map;
24 |
25 | class SimpleFunctionMapper implements FunctionMapper {
26 | private final Map functionNameMap;
27 |
28 | SimpleFunctionMapper(Map map) {
29 | functionNameMap = Collections.unmodifiableMap(new HashMap(map));
30 | }
31 |
32 | public String mapFunctionName(String functionName, Context context) {
33 | String nativeFunction = functionNameMap.get(functionName);
34 | return nativeFunction != null ? nativeFunction : functionName;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/SimpleTypeMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.util.Collections;
22 | import java.util.IdentityHashMap;
23 | import java.util.Map;
24 |
25 | final class SimpleTypeMapper implements TypeMapper {
26 | private final Map> toNativeConverters;
27 | private final Map> fromNativeConverters;
28 |
29 | public SimpleTypeMapper(Map> toNativeConverters, Map> fromNativeConverters) {
30 | this.toNativeConverters = Collections.unmodifiableMap(new IdentityHashMap>(toNativeConverters));
31 | this.fromNativeConverters = Collections.unmodifiableMap(new IdentityHashMap>(fromNativeConverters));
32 | }
33 |
34 | @Override
35 | public FromNativeConverter getFromNativeConverter(Class type) {
36 | return fromNativeConverters.get(type);
37 | }
38 |
39 | @Override
40 | public ToNativeConverter getToNativeConverter(Class type) {
41 | return toNativeConverters.get(type);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/ToNativeContext.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.lang.annotation.Annotation;
22 | import java.util.Collection;
23 |
24 | public interface ToNativeContext {
25 | /**
26 | * Gets a sorted list of annotations
27 | *
28 | * @return a sorted list of annotations for this native type
29 | */
30 | public abstract Collection getAnnotations();
31 |
32 | /**
33 | * Gets the Runtime used for the conversion.
34 | *
35 | * @return The runtime used for the conversion.
36 | */
37 | public jnr.ffi.Runtime getRuntime();
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/ToNativeConverter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | public interface ToNativeConverter {
27 | public N toNative(J value, ToNativeContext context);
28 | public Class nativeType();
29 |
30 | /**
31 | * Used to reload a parameter converted to a native type via a custom {@link jnr.ffi.mapper.ToNativeConverter}
32 | */
33 | public static interface PostInvocation extends ToNativeConverter {
34 | public void postInvoke(J j, N n, ToNativeContext context);
35 | }
36 |
37 | @Retention(RetentionPolicy.RUNTIME)
38 | @Target({ElementType.TYPE, ElementType.METHOD})
39 | public static @interface NoContext {
40 | }
41 |
42 | @Retention(RetentionPolicy.RUNTIME)
43 | @Target(ElementType.TYPE)
44 | public static @interface Cacheable {
45 | }
46 |
47 | @Retention(RetentionPolicy.RUNTIME)
48 | @Target(ElementType.METHOD)
49 | @interface ToNative {
50 | Class nativeType();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/ToNativeType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | *
28 | */
29 | public interface ToNativeType {
30 | public ToNativeConverter getToNativeConverter();
31 |
32 | @Retention(RetentionPolicy.RUNTIME)
33 | @Target(ElementType.TYPE)
34 | public static @interface Cacheable {
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/ToNativeTypes.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | /**
22 | *
23 | */
24 | public final class ToNativeTypes {
25 |
26 | public static ToNativeType create(ToNativeConverter converter) {
27 | if (converter == null) {
28 | return null;
29 | }
30 | return converter.getClass().isAnnotationPresent(ToNativeConverter.Cacheable.class)
31 | ? new Cacheable(converter) : new UnCacheable(converter);
32 | }
33 |
34 | @ToNativeType.Cacheable
35 | static class Cacheable extends AbstractToNativeType {
36 | public Cacheable(ToNativeConverter converter) {
37 | super(converter);
38 | }
39 | }
40 |
41 | static class UnCacheable extends AbstractToNativeType {
42 | public UnCacheable(ToNativeConverter converter) {
43 | super(converter);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/mapper/Util.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.mapper;
20 |
21 | class Util {
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/provider/BadType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.provider;
20 |
21 | import jnr.ffi.NativeType;
22 |
23 | /**
24 | *
25 | */
26 | public final class BadType extends jnr.ffi.Type {
27 |
28 | private final String typeName;
29 |
30 | public BadType(String typeName) {
31 | this.typeName = typeName;
32 | }
33 |
34 | public final int alignment() {
35 | throw new RuntimeException("invalid type: " + typeName);
36 | }
37 |
38 | public final int size() {
39 | throw new RuntimeException("invalid type: " + typeName);
40 | }
41 |
42 | public NativeType getNativeType() {
43 | throw new RuntimeException("invalid type: " + typeName);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/provider/ClosureManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.provider;
20 |
21 | /**
22 | *
23 | */
24 | public interface ClosureManager {
25 | public abstract T newClosure(Class extends T> closureClass, T instance);
26 | public abstract jnr.ffi.Pointer getClosurePointer(Class extends T> closureClass, T instance);
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/provider/DelegatingMemoryIO.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.provider;
20 |
21 | import jnr.ffi.Pointer;
22 |
23 | public interface DelegatingMemoryIO {
24 | public Pointer getDelegatedMemoryIO();
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/provider/FromNativeType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.provider;
20 |
21 | import jnr.ffi.NativeType;
22 | import jnr.ffi.mapper.FromNativeContext;
23 | import jnr.ffi.mapper.FromNativeConverter;
24 |
25 | import java.lang.annotation.Annotation;
26 | import java.util.Collection;
27 |
28 | /**
29 | *
30 | */
31 | public class FromNativeType extends SigType implements jnr.ffi.mapper.FromNativeType {
32 | private final FromNativeConverter fromNativeConverter;
33 | private final FromNativeContext fromNativeContext;
34 |
35 | public FromNativeType(Class javaType, NativeType nativeType, Collection annotations,
36 | FromNativeConverter fromNativeConverter, FromNativeContext fromNativeContext) {
37 | super(javaType, nativeType, annotations, fromNativeConverter != null ? fromNativeConverter.nativeType() : javaType);
38 | this.fromNativeConverter = fromNativeConverter;
39 | this.fromNativeContext = fromNativeContext;
40 | }
41 |
42 | @Override
43 | public FromNativeConverter getFromNativeConverter() {
44 | return fromNativeConverter;
45 | }
46 |
47 | public FromNativeContext getFromNativeContext() {
48 | return fromNativeContext;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/provider/IdentityFunctionMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.provider;
20 |
21 | import jnr.ffi.mapper.FunctionMapper;
22 |
23 | /**
24 | * An implementation of {@link FunctionMapper} that just returns the same name as input
25 | */
26 | public class IdentityFunctionMapper implements FunctionMapper {
27 | private static final class SingletonHolder {
28 | public static final FunctionMapper INSTANCE = new IdentityFunctionMapper();
29 | }
30 |
31 | public static FunctionMapper getInstance() {
32 | return SingletonHolder.INSTANCE;
33 | }
34 |
35 | public String mapFunctionName(String functionName, Context context) {
36 | return functionName;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/provider/IntPointer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.provider;
20 |
21 | import jnr.ffi.Pointer;
22 |
23 | /**
24 | *
25 | */
26 | public final class IntPointer extends InAccessibleMemoryIO {
27 | public IntPointer(jnr.ffi.Runtime runtime, long address) {
28 | super(runtime, address, true);
29 | }
30 |
31 | public IntPointer(jnr.ffi.Runtime runtime, int address) {
32 | super(runtime, address & 0xffffffffL, true);
33 | }
34 |
35 | public long size() {
36 | return 0;
37 | }
38 |
39 | @Override
40 | public int hashCode() {
41 | return (int) address();
42 | }
43 |
44 | @Override
45 | public boolean equals(Object obj) {
46 | return obj instanceof Pointer && ((Pointer) obj).address() == address();
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/provider/InvalidProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.provider;
20 |
21 | import jnr.ffi.LibraryLoader;
22 | import jnr.ffi.LibraryOption;
23 | import jnr.ffi.Runtime;
24 |
25 | import java.util.Collection;
26 | import java.util.Map;
27 |
28 | final class InvalidProvider extends FFIProvider {
29 | private final String message;
30 | private final Throwable cause;
31 | private final jnr.ffi.Runtime runtime;
32 |
33 | InvalidProvider(String message, Throwable cause) {
34 | this.message = message;
35 | this.cause = cause;
36 | this.runtime = new InvalidRuntime(message, cause);
37 | }
38 |
39 | @Override
40 | public Runtime getRuntime() {
41 | return runtime;
42 | }
43 |
44 | @Override
45 | public LibraryLoader createLibraryLoader(Class interfaceClass) {
46 | return new LibraryLoader(interfaceClass) {
47 | @Override
48 | protected T loadLibrary(Class interfaceClass, Collection libraryNames, Collection searchPaths, Map options, boolean failImmediately) {
49 | UnsatisfiedLinkError error = new UnsatisfiedLinkError(message);
50 | error.initCause(cause);
51 | throw error;
52 | }
53 | };
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/jnr/ffi/provider/InvocationSession.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008-2010 Wayne Meissner
3 | *
4 | * This file is part of the JNR project.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | package jnr.ffi.provider;
20 |
21 | import java.util.ArrayList;
22 |
23 | /**
24 | * Holds information for each invocation of a native function
25 | */
26 | public class InvocationSession {
27 | private ArrayList list;
28 | private ArrayList