diff --git a/Makefile b/Makefile index 19b6354..909f54c 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ $(SNAPPY_OUT)/%.o : $(BITSHUFFLE_SRC_DIR)/%.c @mkdir -p $(@D) $(CXX) $(CXXFLAGS) -c $< -o $@ -SNAPPY_OBJ:=$(addprefix $(SNAPPY_OUT)/,$(patsubst %.cc,%.o,$(SNAPPY_CC)) $(patsubst %.c,%.o,$(BITSHUFFLE_C)) SnappyNative.o) +SNAPPY_OBJ:=$(addprefix $(SNAPPY_OUT)/,$(patsubst %.cc,%.o,$(SNAPPY_CC)) $(patsubst %.c,%.o,$(BITSHUFFLE_C)) SnappyNative.o BitShuffleNative.o) ifndef UNIVERSAL_BITSHUFFLE # Undefined macros to generate a platform-independent binary @@ -102,8 +102,11 @@ $(SNAPPY_OUT)/%.o : $(SNAPPY_SRC_DIR)/%.cc @mkdir -p $(@D) $(CXX) $(CXXFLAGS) -c $< -o $@ -#$(SNAPPY_OUT)/SnappyNative.o : $(SRC)/org/xerial/snappy/SnappyNative.cpp $(SRC)/org/xerial/snappy/SnappyNative.h $(SRC)/org/xerial/snappy/BitShufflenative.h -$(SNAPPY_OUT)/SnappyNative.o : $(SRC)/org/xerial/snappy/SnappyNative.cpp jni-header +$(SNAPPY_OUT)/SnappyNative.o: $(SRC)/org/xerial/snappy/SnappyNative.cpp $(SRC)/org/xerial/snappy/SnappyNative.h + @mkdir -p $(@D) + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(SNAPPY_OUT)/BitShuffleNative.o: $(SRC)/org/xerial/snappy/BitShuffleNative.cpp $(SRC)/org/xerial/snappy/BitShuffleNative.h @mkdir -p $(@D) $(CXX) $(CXXFLAGS) -c $< -o $@ diff --git a/src/main/java/org/xerial/snappy/BitShuffleNative.cpp b/src/main/java/org/xerial/snappy/BitShuffleNative.cpp new file mode 100755 index 0000000..eb79a16 --- /dev/null +++ b/src/main/java/org/xerial/snappy/BitShuffleNative.cpp @@ -0,0 +1,91 @@ +/*-------------------------------------------------------------------------- + * Copyright 2011 Taro L. Saito + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *--------------------------------------------------------------------------*/ +#include +#include "BitShuffleNative.h" + +inline void throw_exception(JNIEnv *env, jobject self, int errorCode) +{ + jclass c = env->FindClass("org/xerial/snappy/SnappyNative"); + if(c==0) + return; + jmethodID mth_throwex = env->GetMethodID(c, "throw_error", "(I)V"); + if(mth_throwex == 0) + return; + env->CallVoidMethod(self, mth_throwex, (jint) errorCode); +} + +/* + * Class: org_xerial_snappy_SnappyNative + * Method: bitShuffle + * Signature: (Ljava/lang/Object;IIILjava/lang/Object;I)I + */ +JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitShuffle + (JNIEnv * env, jobject self, jobject input, jint inputOffset, jint typeSize, jint length, jobject output, jint outputOffset) +{ + char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); + char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0); + if(in == 0 || out == 0) { + // out of memory + if(in != 0) { + env->ReleasePrimitiveArrayCritical((jarray) input, in, 0); + } + if(out != 0) { + env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); + } + throw_exception(env, self, 4); + return 0; + } + + int64_t processedBytes = bshuf_bitshuffle( + in + inputOffset, out + outputOffset, (size_t) (length / typeSize), (size_t) typeSize, 0); + + env->ReleasePrimitiveArrayCritical((jarray) input, in, 0); + env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); + + return (jint) processedBytes; +} + +/* + * Class: org_xerial_snappy_SnappyNative + * Method: bitUnShuffle + * Signature: (Ljava/lang/Object;IIILjava/lang/Object;I)I + */ +JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitUnShuffle + (JNIEnv * env, jobject self, jobject input, jint inputOffset, jint typeSize, jint length, jobject output, jint outputOffset) +{ + char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); + char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0); + if(in == 0 || out == 0) { + // out of memory + if(in != 0) { + env->ReleasePrimitiveArrayCritical((jarray) input, in, 0); + } + if(out != 0) { + env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); + } + throw_exception(env, self, 4); + return 0; + } + + int64_t processedBytes = bshuf_bitunshuffle( + in + inputOffset, out + outputOffset, (size_t) (length / typeSize), (size_t) typeSize, 0); + + env->ReleasePrimitiveArrayCritical((jarray) input, in, 0); + env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); + + return (jint) processedBytes; +} + diff --git a/src/main/java/org/xerial/snappy/SnappyNative.cpp b/src/main/java/org/xerial/snappy/SnappyNative.cpp index 75de90d..a1863a8 100755 --- a/src/main/java/org/xerial/snappy/SnappyNative.cpp +++ b/src/main/java/org/xerial/snappy/SnappyNative.cpp @@ -15,22 +15,18 @@ *--------------------------------------------------------------------------*/ #include #include -#include #include -#include - #include "SnappyNative.h" -#include "BitShuffleNative.h" -void throw_exception(JNIEnv *env, jobject self, int errorCode) +inline void throw_exception(JNIEnv *env, jobject self, int errorCode) { jclass c = env->FindClass("org/xerial/snappy/SnappyNative"); if(c==0) return; - jmethodID mth_throwex = env->GetMethodID(c, "throw_error", "(I)V"); - if(mth_throwex == 0) - return; - env->CallVoidMethod(self, mth_throwex, (jint) errorCode); + jmethodID mth_throwex = env->GetMethodID(c, "throw_error", "(I)V"); + if(mth_throwex == 0) + return; + env->CallVoidMethod(self, mth_throwex, (jint) errorCode); } JNIEXPORT jstring JNICALL Java_org_xerial_snappy_SnappyNative_nativeLibraryVersion @@ -300,65 +296,3 @@ JNIEXPORT void JNICALL Java_org_xerial_snappy_SnappyNative_arrayCopy env->ReleasePrimitiveArrayCritical((jarray) output, dest, 0); } -/* - * Class: org_xerial_snappy_SnappyNative - * Method: bitShuffle - * Signature: (Ljava/lang/Object;IIILjava/lang/Object;I)I - */ -JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitShuffle - (JNIEnv * env, jobject self, jobject input, jint inputOffset, jint typeSize, jint length, jobject output, jint outputOffset) -{ - char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); - char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0); - if(in == 0 || out == 0) { - // out of memory - if(in != 0) { - env->ReleasePrimitiveArrayCritical((jarray) input, in, 0); - } - if(out != 0) { - env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); - } - throw_exception(env, self, 4); - return 0; - } - - int64_t processedBytes = bshuf_bitshuffle( - in + inputOffset, out + outputOffset, (size_t) (length / typeSize), (size_t) typeSize, 0); - - env->ReleasePrimitiveArrayCritical((jarray) input, in, 0); - env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); - - return (jint) processedBytes; -} - -/* - * Class: org_xerial_snappy_SnappyNative - * Method: bitUnShuffle - * Signature: (Ljava/lang/Object;IIILjava/lang/Object;I)I - */ -JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitUnShuffle - (JNIEnv * env, jobject self, jobject input, jint inputOffset, jint typeSize, jint length, jobject output, jint outputOffset) -{ - char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); - char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0); - if(in == 0 || out == 0) { - // out of memory - if(in != 0) { - env->ReleasePrimitiveArrayCritical((jarray) input, in, 0); - } - if(out != 0) { - env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); - } - throw_exception(env, self, 4); - return 0; - } - - int64_t processedBytes = bshuf_bitunshuffle( - in + inputOffset, out + outputOffset, (size_t) (length / typeSize), (size_t) typeSize, 0); - - env->ReleasePrimitiveArrayCritical((jarray) input, in, 0); - env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); - - return (jint) processedBytes; -} -