mirror of
https://github.com/lax1dude/unsafe-memcpy-for-java.git
synced 2024-09-07 11:55:58 -07:00
upload source
This commit is contained in:
commit
f4917d435a
24
LICENSE
Normal file
24
LICENSE
Normal file
|
@ -0,0 +1,24 @@
|
|||
|
||||
Copyright (c) 2024 lax1dude
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
7
README.md
Normal file
7
README.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
### Unsafe Memcpy for Java
|
||||
|
||||
Fast JNI library to copy arbitrary data from native code into and out of primitive arrays in Java.
|
||||
|
||||
You probably don't need this.
|
||||
|
||||
Compiling instructions coming later.
|
BIN
UnsafeMemcpy.dll
Normal file
BIN
UnsafeMemcpy.dll
Normal file
Binary file not shown.
BIN
UnsafeMemcpy.jar
Normal file
BIN
UnsafeMemcpy.jar
Normal file
Binary file not shown.
70
java/net/lax1dude/unsafememcpy/UnsafeMalloc.java
Normal file
70
java/net/lax1dude/unsafememcpy/UnsafeMalloc.java
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package net.lax1dude.unsafememcpy;
|
||||
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class UnsafeMalloc {
|
||||
|
||||
static {
|
||||
UnsafeUtils.loadNatives();
|
||||
}
|
||||
|
||||
public static native long malloc(int size);
|
||||
|
||||
public static native long malloc(long size);
|
||||
|
||||
public static native long calloc(int num, int size);
|
||||
|
||||
public static native long calloc(long num, long size);
|
||||
|
||||
public static native long realloc(long oldPtr, int size);
|
||||
|
||||
public static native long realloc(long oldPtr, long size);
|
||||
|
||||
public static native void free(long ptr);
|
||||
|
||||
public static native ByteBuffer mallocBuffer(int size);
|
||||
|
||||
public static native ByteBuffer mallocBuffer(long size);
|
||||
|
||||
public static native ByteBuffer callocBuffer(int num, int size);
|
||||
|
||||
public static native ByteBuffer callocBuffer(long num, long size);
|
||||
|
||||
public static native ByteBuffer reallocBuffer(long oldPtr, int size);
|
||||
|
||||
public static native ByteBuffer reallocBuffer(long oldPtr, long size);
|
||||
|
||||
public static native ByteBuffer reallocBuffer(Buffer oldBuffer, int size);
|
||||
|
||||
public static native ByteBuffer reallocBuffer(Buffer oldBuffer, long size);
|
||||
|
||||
public static native void freeBuffer(Buffer buffer);
|
||||
|
||||
}
|
361
java/net/lax1dude/unsafememcpy/UnsafeMemcpy.java
Normal file
361
java/net/lax1dude/unsafememcpy/UnsafeMemcpy.java
Normal file
|
@ -0,0 +1,361 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package net.lax1dude.unsafememcpy;
|
||||
|
||||
import java.nio.Buffer;
|
||||
|
||||
public class UnsafeMemcpy {
|
||||
|
||||
static {
|
||||
UnsafeUtils.loadNatives();
|
||||
}
|
||||
|
||||
public static native void memcpy(long dstAddress, long srcAddress, int byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, long srcAddress, long byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, int dstOffset, long srcAddress, int byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, long dstOffset, long srcAddress, long byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, int dstOffset, long srcAddress, int byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, long dstOffset, long srcAddress, long byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, int dstOffset, long srcAddress, int byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, long dstOffset, long srcAddress, long byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, int dstOffset, long srcAddress, int byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, long dstOffset, long srcAddress, long byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, int dstOffset, long srcAddress, int byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, long dstOffset, long srcAddress, long byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, int dstOffset, long srcAddress, int byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, long dstOffset, long srcAddress, long byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, int dstOffset, long srcAddress, int byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, long dstOffset, long srcAddress, long byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, int dstOffset, long srcAddress, int byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, long dstOffset, long srcAddress, long byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, Buffer srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, Buffer srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, int dstOffset, Buffer srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, long dstOffset, Buffer srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, int dstOffset, Buffer srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, long dstOffset, Buffer srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, int dstOffset, Buffer srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, long dstOffset, Buffer srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, int dstOffset, Buffer srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, long dstOffset, Buffer srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, int dstOffset, Buffer srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, long dstOffset, Buffer srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, int dstOffset, Buffer srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, long dstOffset, Buffer srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, int dstOffset, Buffer srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, long dstOffset, Buffer srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, int dstOffset, Buffer srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, long dstOffset, Buffer srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, byte[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, byte[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, int dstOffset, byte[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, long dstOffset, byte[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, int dstOffset, byte[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, long dstOffset, byte[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, int dstOffset, byte[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, long dstOffset, byte[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, int dstOffset, byte[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, long dstOffset, byte[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, int dstOffset, byte[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, long dstOffset, byte[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, int dstOffset, byte[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, long dstOffset, byte[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, int dstOffset, byte[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, long dstOffset, byte[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, int dstOffset, byte[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, long dstOffset, byte[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, short[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, short[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, int dstOffset, short[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, long dstOffset, short[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, int dstOffset, short[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, long dstOffset, short[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, int dstOffset, short[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, long dstOffset, short[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, int dstOffset, short[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, long dstOffset, short[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, int dstOffset, short[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, long dstOffset, short[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, int dstOffset, short[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, long dstOffset, short[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, int dstOffset, short[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, long dstOffset, short[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, int dstOffset, short[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, long dstOffset, short[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, char[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, char[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, int dstOffset, char[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, long dstOffset, char[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, int dstOffset, char[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, long dstOffset, char[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, int dstOffset, char[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, long dstOffset, char[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, int dstOffset, char[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, long dstOffset, char[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, int dstOffset, char[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, long dstOffset, char[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, int dstOffset, char[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, long dstOffset, char[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, int dstOffset, char[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, long dstOffset, char[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, int dstOffset, char[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, long dstOffset, char[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, int[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, int[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, int dstOffset, int[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, long dstOffset, int[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, int dstOffset, int[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, long dstOffset, int[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, int dstOffset, int[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, long dstOffset, int[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, int dstOffset, int[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, long dstOffset, int[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, int dstOffset, int[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, long dstOffset, int[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, int dstOffset, int[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, long dstOffset, int[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, int dstOffset, int[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, long dstOffset, int[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, int dstOffset, int[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, long dstOffset, int[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, long[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, long[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, int dstOffset, long[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, long dstOffset, long[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, int dstOffset, long[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, long dstOffset, long[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, int dstOffset, long[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, long dstOffset, long[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, int dstOffset, long[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, long dstOffset, long[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, int dstOffset, long[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, long dstOffset, long[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, int dstOffset, long[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, long dstOffset, long[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, int dstOffset, long[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, long dstOffset, long[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, int dstOffset, long[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, long dstOffset, long[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, double[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, double[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, int dstOffset, double[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, long dstOffset, double[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, int dstOffset, double[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, long dstOffset, double[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, int dstOffset, double[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, long dstOffset, double[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, int dstOffset, double[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, long dstOffset, double[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, int dstOffset, double[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, long dstOffset, double[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, int dstOffset, double[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, long dstOffset, double[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, int dstOffset, double[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, long dstOffset, double[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, int dstOffset, double[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, long dstOffset, double[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, float[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long dstAddress, float[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, int dstOffset, float[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(Buffer dstAddress, long dstOffset, float[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, int dstOffset, float[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(byte[] dstAddress, long dstOffset, float[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, int dstOffset, float[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(short[] dstAddress, long dstOffset, float[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, int dstOffset, float[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(char[] dstAddress, long dstOffset, float[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, int dstOffset, float[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(int[] dstAddress, long dstOffset, float[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, int dstOffset, float[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(long[] dstAddress, long dstOffset, float[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, int dstOffset, float[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(double[] dstAddress, long dstOffset, float[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, int dstOffset, float[] srcAddress, int srcOffset, int byteLength);
|
||||
|
||||
public static native void memcpy(float[] dstAddress, long dstOffset, float[] srcAddress, long srcOffset, long byteLength);
|
||||
|
||||
}
|
73
java/net/lax1dude/unsafememcpy/UnsafeMemset.java
Normal file
73
java/net/lax1dude/unsafememcpy/UnsafeMemset.java
Normal file
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package net.lax1dude.unsafememcpy;
|
||||
|
||||
import java.nio.Buffer;
|
||||
|
||||
public class UnsafeMemset {
|
||||
|
||||
static {
|
||||
UnsafeUtils.loadNatives();
|
||||
}
|
||||
|
||||
public static native void memset(long address, int value, int byteLength);
|
||||
|
||||
public static native void memset(long address, int value, long byteLength);
|
||||
|
||||
public static native void memset(Buffer address, int offset, int value, int byteLength);
|
||||
|
||||
public static native void memset(Buffer address, long offset, int value, long byteLength);
|
||||
|
||||
public static native void memset(byte[] address, int offset, int value, int byteLength);
|
||||
|
||||
public static native void memset(byte[] address, long offset, int value, long byteLength);
|
||||
|
||||
public static native void memset(short[] address, int offset, int value, int byteLength);
|
||||
|
||||
public static native void memset(short[] address, long offset, int value, long byteLength);
|
||||
|
||||
public static native void memset(char[] address, int offset, int value, int byteLength);
|
||||
|
||||
public static native void memset(char[] address, long offset, int value, long byteLength);
|
||||
|
||||
public static native void memset(int[] address, int offset, int value, int byteLength);
|
||||
|
||||
public static native void memset(int[] address, long offset, int value, long byteLength);
|
||||
|
||||
public static native void memset(long[] address, int offset, int value, int byteLength);
|
||||
|
||||
public static native void memset(long[] address, long offset, int value, long byteLength);
|
||||
|
||||
public static native void memset(double[] address, int offset, int value, int byteLength);
|
||||
|
||||
public static native void memset(double[] address, long offset, int value, long byteLength);
|
||||
|
||||
public static native void memset(float[] address, int offset, int value, int byteLength);
|
||||
|
||||
public static native void memset(float[] address, long offset, int value, long byteLength);
|
||||
|
||||
}
|
85
java/net/lax1dude/unsafememcpy/UnsafeUtils.java
Normal file
85
java/net/lax1dude/unsafememcpy/UnsafeUtils.java
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package net.lax1dude.unsafememcpy;
|
||||
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class UnsafeUtils {
|
||||
|
||||
private static boolean hasLoadedNatives = false;
|
||||
|
||||
static {
|
||||
loadNatives();
|
||||
}
|
||||
|
||||
public static void loadNatives() {
|
||||
if(!hasLoadedNatives) {
|
||||
hasLoadedNatives = true;
|
||||
System.loadLibrary("UnsafeMemcpy");
|
||||
}
|
||||
}
|
||||
|
||||
public static native int getVersionMajor();
|
||||
|
||||
public static native int getVersionMinor();
|
||||
|
||||
public static native ByteBuffer makeDirectBuffer(long address, int size);
|
||||
|
||||
public static native ByteBuffer makeDirectBuffer(long address, long size);
|
||||
|
||||
public static native long getAddress(Buffer buffer);
|
||||
|
||||
public static native byte getMemByte(long address);
|
||||
|
||||
public static native short getMemShort(long address);
|
||||
|
||||
public static native char getMemChar(long address);
|
||||
|
||||
public static native int getMemInt(long address);
|
||||
|
||||
public static native long getMemLong(long address);
|
||||
|
||||
public static native double getMemDouble(long address);
|
||||
|
||||
public static native float getMemFloat(long address);
|
||||
|
||||
public static native void setMemByte(long address, byte value);
|
||||
|
||||
public static native void setMemShort(long address, short value);
|
||||
|
||||
public static native void setMemChar(long address, char value);
|
||||
|
||||
public static native void setMemInt(long address, int value);
|
||||
|
||||
public static native void setMemLong(long address, long value);
|
||||
|
||||
public static native void setMemDouble(long address, double value);
|
||||
|
||||
public static native void setMemFloat(long address, float value);
|
||||
|
||||
}
|
234
native/UnsafeMalloc.c
Normal file
234
native/UnsafeMalloc.c
Normal file
|
@ -0,0 +1,234 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "UnsafeMallocImpl.h"
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: malloc
|
||||
* Signature: (I)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_malloc__I
|
||||
(JNIEnv * jniHandle, jclass jniClass, jint size) {
|
||||
return (jlong)UNSAFE_MALLOC_IMPL(size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: malloc
|
||||
* Signature: (J)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_malloc__J
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong size) {
|
||||
return (jlong)UNSAFE_MALLOC_IMPL(size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: calloc
|
||||
* Signature: (II)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_calloc__II
|
||||
(JNIEnv * jniHandle, jclass jniClass, jint num, jint size) {
|
||||
return (jlong)UNSAFE_CALLOC_IMPL(num, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: calloc
|
||||
* Signature: (JJ)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_calloc__JJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong num, jlong size) {
|
||||
return (jlong)UNSAFE_CALLOC_IMPL(num, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: realloc
|
||||
* Signature: (JI)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_realloc__JI
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jint size) {
|
||||
return (jlong)UNSAFE_REALLOC_IMPL(address, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: realloc
|
||||
* Signature: (JJ)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_realloc__JJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jlong size) {
|
||||
return (jlong)UNSAFE_REALLOC_IMPL(address, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: free
|
||||
* Signature: (J)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_free
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address) {
|
||||
UNSAFE_FREE_IMPL(address);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: mallocBuffer
|
||||
* Signature: (I)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_mallocBuffer__I
|
||||
(JNIEnv * jniHandle, jclass jniClass, jint size) {
|
||||
void* ret = UNSAFE_MALLOC_IMPL(size);
|
||||
if(!ret) {
|
||||
return NULL;
|
||||
}
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, ret, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: mallocBuffer
|
||||
* Signature: (J)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_mallocBuffer__J
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong size) {
|
||||
void* ret = UNSAFE_MALLOC_IMPL(size);
|
||||
if(!ret) {
|
||||
return NULL;
|
||||
}
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, ret, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: callocBuffer
|
||||
* Signature: (II)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_callocBuffer__II
|
||||
(JNIEnv * jniHandle, jclass jniClass, jint num, jint size) {
|
||||
void* ret = UNSAFE_CALLOC_IMPL(num, size);
|
||||
if(!ret) {
|
||||
return NULL;
|
||||
}
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, ret, num * size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: callocBuffer
|
||||
* Signature: (JJ)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_callocBuffer__JJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong num, jlong size) {
|
||||
void* ret = UNSAFE_CALLOC_IMPL(num, size);
|
||||
if(!ret) {
|
||||
return NULL;
|
||||
}
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, ret, num * size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: reallocBuffer
|
||||
* Signature: (JI)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_reallocBuffer__JI
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jint size) {
|
||||
void* ret = UNSAFE_REALLOC_IMPL(address, size);
|
||||
if(!ret) {
|
||||
return NULL;
|
||||
}
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, ret, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: reallocBuffer
|
||||
* Signature: (JJ)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_reallocBuffer__JJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jlong size) {
|
||||
void* ret = UNSAFE_REALLOC_IMPL(address, size);
|
||||
if(!ret) {
|
||||
return NULL;
|
||||
}
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, ret, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: reallocBuffer
|
||||
* Signature: (Ljava/nio/Buffer;I)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_reallocBuffer__Ljava_nio_Buffer_2I
|
||||
(JNIEnv * jniHandle, jclass jniClass, jobject buffer, jint size) {
|
||||
void* srcBuffer = (*jniHandle)->GetDirectBufferAddress(jniHandle, buffer);
|
||||
if(srcBuffer == NULL) {
|
||||
unsafeThrowNotDirectBuffer(jniHandle);
|
||||
return NULL;
|
||||
}
|
||||
void* ret = UNSAFE_REALLOC_IMPL(srcBuffer, size);
|
||||
if(!ret) {
|
||||
return NULL;
|
||||
}
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, ret, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: reallocBuffer
|
||||
* Signature: (Ljava/nio/Buffer;J)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_reallocBuffer__Ljava_nio_Buffer_2J
|
||||
(JNIEnv * jniHandle, jclass jniClass, jobject buffer, jlong size) {
|
||||
void* srcBuffer = (*jniHandle)->GetDirectBufferAddress(jniHandle, buffer);
|
||||
if(srcBuffer == NULL) {
|
||||
unsafeThrowNotDirectBuffer(jniHandle);
|
||||
return NULL;
|
||||
}
|
||||
void* ret = UNSAFE_REALLOC_IMPL(srcBuffer, size);
|
||||
if(!ret) {
|
||||
return NULL;
|
||||
}
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, ret, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMalloc
|
||||
* Method: freeBuffer
|
||||
* Signature: (Ljava/nio/Buffer;)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMalloc_freeBuffer
|
||||
(JNIEnv * jniHandle, jclass jniClass, jobject buffer) {
|
||||
void* srcBuffer = (*jniHandle)->GetDirectBufferAddress(jniHandle, buffer);
|
||||
if(srcBuffer == NULL) {
|
||||
unsafeThrowNotDirectBuffer(jniHandle);
|
||||
return;
|
||||
}
|
||||
UNSAFE_FREE_IMPL(srcBuffer);
|
||||
}
|
1648
native/UnsafeMemcpy.c
Normal file
1648
native/UnsafeMemcpy.c
Normal file
File diff suppressed because it is too large
Load Diff
126
native/UnsafeMemcpyImpl.h
Normal file
126
native/UnsafeMemcpyImpl.h
Normal file
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#define UNSAFE_MEMCPY_IMPL(dst, src, len) memcpy((void*)(dst), (const void*)(src), (size_t)(len))
|
||||
|
||||
#define UNSAFE_MEMCPY_ARRAY_TO_NATIVE_IMPL(jniHandle, dstAddress, srcAddress, srcOffset, byteLength)\
|
||||
uint64_t srcArray = (uint64_t)(*jniHandle)->GetPrimitiveArrayCritical(jniHandle, srcAddress, NULL);\
|
||||
if(!srcArray) {\
|
||||
unsafeThrowOutOfMemory(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMCPY_IMPL(dstAddress, srcArray + srcOffset, byteLength);\
|
||||
(*jniHandle)->ReleasePrimitiveArrayCritical(jniHandle, srcAddress, (void*)srcArray, JNI_ABORT);
|
||||
|
||||
#define UNSAFE_MEMCPY_BUFFER_TO_NATIVE_IMPL(jniHandle, dstAddress, srcAddress, srcOffset, byteLength)\
|
||||
uint64_t srcBuffer = (uint64_t)(*jniHandle)->GetDirectBufferAddress(jniHandle, srcAddress);\
|
||||
if(!srcBuffer) {\
|
||||
unsafeThrowNotDirectBuffer(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMCPY_IMPL(dstAddress, srcBuffer + srcOffset, byteLength);
|
||||
|
||||
#define UNSAFE_MEMCPY_NATIVE_TO_BUFFER_IMPL(jniHandle, dstAddress, dstOffset, srcAddress, byteLength)\
|
||||
uint64_t dstBuffer = (uint64_t)(*jniHandle)->GetDirectBufferAddress(jniHandle, dstAddress);\
|
||||
if(!dstBuffer) {\
|
||||
unsafeThrowNotDirectBuffer(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMCPY_IMPL(dstBuffer + dstOffset, srcAddress, byteLength);
|
||||
|
||||
#define UNSAFE_MEMCPY_BUFFER_TO_BUFFER_IMPL(jniHandle, dstAddress, dstOffset, srcAddress, srcOffset, byteLength)\
|
||||
uint64_t dstBuffer = (uint64_t)(*jniHandle)->GetDirectBufferAddress(jniHandle, dstAddress);\
|
||||
if(!dstBuffer) {\
|
||||
unsafeThrowNotDirectBuffer(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
uint64_t srcBuffer = (uint64_t)(*jniHandle)->GetDirectBufferAddress(jniHandle, srcAddress);\
|
||||
if(!srcBuffer) {\
|
||||
unsafeThrowNotDirectBuffer(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMCPY_IMPL(dstBuffer + dstOffset, srcBuffer + srcOffset, byteLength);
|
||||
|
||||
#define UNSAFE_MEMCPY_ARRAY_TO_BUFFER_IMPL(jniHandle, dstAddress, dstOffset, srcAddress, srcOffset, byteLength)\
|
||||
uint64_t dstBuffer = (uint64_t)(*jniHandle)->GetDirectBufferAddress(jniHandle, dstAddress);\
|
||||
if(!dstBuffer) {\
|
||||
unsafeThrowNotDirectBuffer(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
uint64_t srcArray = (uint64_t)(*jniHandle)->GetPrimitiveArrayCritical(jniHandle, srcAddress, NULL);\
|
||||
if(!srcArray) {\
|
||||
unsafeThrowOutOfMemory(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMCPY_IMPL(dstBuffer + dstOffset, srcArray + srcOffset, byteLength);\
|
||||
(*jniHandle)->ReleasePrimitiveArrayCritical(jniHandle, srcAddress, (void*)srcArray, JNI_ABORT);
|
||||
|
||||
#define UNSAFE_MEMCPY_NATIVE_TO_ARRAY_IMPL(jniHandle, dstAddress, dstOffset, srcAddress, byteLength)\
|
||||
uint64_t dstArray = (uint64_t)(*jniHandle)->GetPrimitiveArrayCritical(jniHandle, dstAddress, NULL);\
|
||||
if(!dstArray) {\
|
||||
unsafeThrowOutOfMemory(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMCPY_IMPL(dstArray + dstOffset, srcAddress, byteLength);\
|
||||
(*jniHandle)->ReleasePrimitiveArrayCritical(jniHandle, dstAddress, (void*)dstArray, JNI_COMMIT);
|
||||
|
||||
#define UNSAFE_MEMCPY_BUFFER_TO_ARRAY_IMPL(jniHandle, dstAddress, dstOffset, srcAddress, srcOffset, byteLength)\
|
||||
uint64_t srcBuffer = (uint64_t)(*jniHandle)->GetDirectBufferAddress(jniHandle, srcAddress);\
|
||||
if(!srcBuffer) {\
|
||||
unsafeThrowNotDirectBuffer(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
uint64_t dstArray = (uint64_t)(*jniHandle)->GetPrimitiveArrayCritical(jniHandle, dstAddress, NULL);\
|
||||
if(!dstArray) {\
|
||||
unsafeThrowOutOfMemory(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMCPY_IMPL(dstArray + dstOffset, srcBuffer + srcOffset, byteLength);\
|
||||
(*jniHandle)->ReleasePrimitiveArrayCritical(jniHandle, dstAddress, (void*)dstArray, JNI_COMMIT);
|
||||
|
||||
#define UNSAFE_MEMCPY_ARRAY_TO_ARRAY_IMPL(jniHandle, dstAddress, dstOffset, srcAddress, srcOffset, byteLength)\
|
||||
uint64_t srcArray = (uint64_t)(*jniHandle)->GetPrimitiveArrayCritical(jniHandle, srcAddress, NULL);\
|
||||
if(!srcArray) {\
|
||||
unsafeThrowOutOfMemory(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
uint64_t dstArray = (uint64_t)(*jniHandle)->GetPrimitiveArrayCritical(jniHandle, dstAddress, NULL);\
|
||||
if(!dstArray) {\
|
||||
(*jniHandle)->ReleasePrimitiveArrayCritical(jniHandle, srcAddress, (void*)srcArray, JNI_ABORT);\
|
||||
unsafeThrowOutOfMemory(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMCPY_IMPL(dstArray + dstOffset, srcArray + srcOffset, byteLength);\
|
||||
(*jniHandle)->ReleasePrimitiveArrayCritical(jniHandle, dstAddress, (void*)dstArray, JNI_COMMIT);\
|
||||
(*jniHandle)->ReleasePrimitiveArrayCritical(jniHandle, srcAddress, (void*)srcArray, JNI_ABORT);
|
208
native/UnsafeMemset.c
Normal file
208
native/UnsafeMemset.c
Normal file
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "UnsafeMemsetImpl.h"
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: (JII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset__JII
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jint value, jint byteLength) {
|
||||
UNSAFE_MEMSET_IMPL(address, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: (JIJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset__JIJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jint value, jlong byteLength) {
|
||||
UNSAFE_MEMSET_IMPL(address, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: (Ljava/nio/Buffer;III)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset__Ljava_nio_Buffer_2III
|
||||
(JNIEnv * jniHandle, jclass jniClass, jobject address, jint offset, jint value, jint byteLength) {
|
||||
UNSAFE_MEMSET_BUFFER_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: (Ljava/nio/Buffer;JIJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset__Ljava_nio_Buffer_2JIJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jobject address, jlong offset, jint value, jlong byteLength) {
|
||||
UNSAFE_MEMSET_BUFFER_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([BIII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3BIII
|
||||
(JNIEnv * jniHandle, jclass jniClass, jbyteArray address, jint offset, jint value, jint byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([BJIJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3BJIJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jbyteArray address, jlong offset, jint value, jlong byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([SIII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3SIII
|
||||
(JNIEnv * jniHandle, jclass jniClass, jshortArray address, jint offset, jint value, jint byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([SJIJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3SJIJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jshortArray address, jlong offset, jint value, jlong byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([CIII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3CIII
|
||||
(JNIEnv * jniHandle, jclass jniClass, jcharArray address, jint offset, jint value, jint byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([CJIJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3CJIJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jcharArray address, jlong offset, jint value, jlong byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([IIII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3IIII
|
||||
(JNIEnv * jniHandle, jclass jniClass, jintArray address, jint offset, jint value, jint byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([IJIJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3IJIJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jintArray address, jlong offset, jint value, jlong byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([JIII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3JIII
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlongArray address, jint offset, jint value, jint byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([JJIJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3JJIJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlongArray address, jlong offset, jint value, jlong byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([DIII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3DIII
|
||||
(JNIEnv * jniHandle, jclass jniClass, jdoubleArray address, jint offset, jint value, jint byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([DJIJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3DJIJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jdoubleArray address, jlong offset, jint value, jlong byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([FIII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3FIII
|
||||
(JNIEnv * jniHandle, jclass jniClass, jfloatArray address, jint offset, jint value, jint byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeMemset
|
||||
* Method: memset
|
||||
* Signature: ([FJIJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeMemset_memset___3FJIJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jfloatArray address, jlong offset, jint value, jlong byteLength) {
|
||||
UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength);
|
||||
}
|
||||
|
53
native/UnsafeMemsetImpl.h
Normal file
53
native/UnsafeMemsetImpl.h
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#define UNSAFE_MEMSET_IMPL(address, value, byteLength)\
|
||||
memset((void*)(address), (int)(value), (size_t)(byteLength));
|
||||
|
||||
#define UNSAFE_MEMSET_BUFFER_IMPL(jniHandle, address, offset, value, byteLength)\
|
||||
uint64_t dstBuffer = (uint64_t)(*jniHandle)->GetDirectBufferAddress(jniHandle, address);\
|
||||
if(!dstBuffer) {\
|
||||
unsafeThrowNotDirectBuffer(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMSET_IMPL(dstBuffer + offset, value, byteLength);
|
||||
|
||||
#define UNSAFE_MEMSET_ARRAY_IMPL(jniHandle, address, offset, value, byteLength)\
|
||||
uint64_t dstArray = (uint64_t)(*jniHandle)->GetPrimitiveArrayCritical(jniHandle, address, NULL);\
|
||||
if(!dstArray) {\
|
||||
unsafeThrowOutOfMemory(jniHandle);\
|
||||
return;\
|
||||
}\
|
||||
UNSAFE_MEMSET_IMPL(dstArray + offset, value, byteLength);\
|
||||
(*jniHandle)->ReleasePrimitiveArrayCritical(jniHandle, address, (void*)dstArray, JNI_COMMIT);
|
224
native/UnsafeUtils.c
Normal file
224
native/UnsafeUtils.c
Normal file
|
@ -0,0 +1,224 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getVersionMajor
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getVersionMajor
|
||||
(JNIEnv * jniHandle, jclass jniClass) {
|
||||
return NATIVE_LIBRARY_VERSION_MAJOR;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getVersionMinor
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getVersionMinor
|
||||
(JNIEnv * jniHandle, jclass jniClass) {
|
||||
return NATIVE_LIBRARY_VERSION_MINOR;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: makeDirectBuffer
|
||||
* Signature: (JI)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_makeDirectBuffer__JI
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jint size) {
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, (void*)address, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: makeDirectBuffer
|
||||
* Signature: (JJ)Ljava/nio/ByteBuffer;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_makeDirectBuffer__JJ
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jlong size) {
|
||||
return (*jniHandle)->NewDirectByteBuffer(jniHandle, (void*)address, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getAddress
|
||||
* Signature: (Ljava/nio/Buffer;)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getAddress
|
||||
(JNIEnv * jniHandle, jclass jniClass, jobject buffer) {
|
||||
void* srcBuffer = (*jniHandle)->GetDirectBufferAddress(jniHandle, buffer);
|
||||
if(srcBuffer == NULL) {
|
||||
unsafeThrowNotDirectBuffer(jniHandle);
|
||||
return 0;
|
||||
}
|
||||
return (jlong)srcBuffer;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getMemByte
|
||||
* Signature: (J)B
|
||||
*/
|
||||
JNIEXPORT jbyte JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getMemByte
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address) {
|
||||
return *(jbyte*)address;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getMemShort
|
||||
* Signature: (J)S
|
||||
*/
|
||||
JNIEXPORT jshort JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getMemShort
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address) {
|
||||
return *(jshort*)address;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getMemChar
|
||||
* Signature: (J)C
|
||||
*/
|
||||
JNIEXPORT jchar JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getMemChar
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address) {
|
||||
return *(jchar*)address;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getMemInt
|
||||
* Signature: (J)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getMemInt
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address) {
|
||||
return *(jint*)address;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getMemLong
|
||||
* Signature: (J)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getMemLong
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address) {
|
||||
return *(jlong*)address;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getMemDouble
|
||||
* Signature: (J)D
|
||||
*/
|
||||
JNIEXPORT jdouble JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getMemDouble
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address) {
|
||||
return *(jdouble*)address;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: getMemFloat
|
||||
* Signature: (J)F
|
||||
*/
|
||||
JNIEXPORT jfloat JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_getMemFloat
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address) {
|
||||
return *(jfloat*)address;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: setMemByte
|
||||
* Signature: (JB)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_setMemByte
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jbyte value) {
|
||||
*(jbyte*)address = value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: setMemShort
|
||||
* Signature: (JS)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_setMemShort
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jshort value) {
|
||||
*(jshort*)address = value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: setMemChar
|
||||
* Signature: (JC)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_setMemChar
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jchar value) {
|
||||
*(jchar*)address = value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: setMemInt
|
||||
* Signature: (JI)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_setMemInt
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jint value) {
|
||||
*(jint*)address = value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: setMemLong
|
||||
* Signature: (JJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_setMemLong
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jlong value) {
|
||||
*(jlong*)address = value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: setMemDouble
|
||||
* Signature: (JD)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_setMemDouble
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jdouble value) {
|
||||
*(jdouble*)address = value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: net_lax1dude_unsafememcpy_UnsafeUtils
|
||||
* Method: setMemFloat
|
||||
* Signature: (JF)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_lax1dude_unsafememcpy_UnsafeUtils_setMemFloat
|
||||
(JNIEnv * jniHandle, jclass jniClass, jlong address, jfloat value) {
|
||||
*(jfloat*)address = value;
|
||||
}
|
43
native/utils.c
Normal file
43
native/utils.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
void unsafeThrowNotDirectBuffer(JNIEnv* jniHandle) {
|
||||
jclass exceptionClass = (*jniHandle)->FindClass(jniHandle, "java/lang/IllegalArgumentException");
|
||||
if (!exceptionClass) {
|
||||
return; // should not happen ever
|
||||
}
|
||||
(*jniHandle)->ThrowNew(jniHandle, exceptionClass, "NIO Buffer object is not direct!");
|
||||
}
|
||||
|
||||
void unsafeThrowOutOfMemory(JNIEnv* jniHandle) {
|
||||
jclass exceptionClass = (*jniHandle)->FindClass(jniHandle, "java/lang/OutOfMemoryError");
|
||||
if (!exceptionClass) {
|
||||
return;
|
||||
}
|
||||
(*jniHandle)->ThrowNew(jniHandle, exceptionClass, "Not enough memory available to access the array!");
|
||||
}
|
35
native/utils.h
Normal file
35
native/utils.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 2024 lax1dude
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#define NATIVE_LIBRARY_VERSION_MAJOR 1
|
||||
#define NATIVE_LIBRARY_VERSION_MINOR 0
|
||||
|
||||
void unsafeThrowNotDirectBuffer(JNIEnv* jniHandle);
|
||||
void unsafeThrowOutOfMemory(JNIEnv* jniHandle);
|
Loading…
Reference in New Issue
Block a user