]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fastlz/native/jniWrapper.c
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.fastlz / native / jniWrapper.c
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 #include "fastlz.h"
13 #include "lz4.h"
14 #include <stdio.h>
15 #include "jni.h"
16 #include <string.h>
17 #include <stdlib.h>
18
19 JNIEXPORT jint JNICALL Java_org_simantics_fastlz_FastLZ_compress(JNIEnv* env, jclass clazz, 
20                                                                  jobject input, jint inputOffset, jint length, 
21                                                                  jobject output, jint outputOffset) {
22     void* inputAddress = (char*)(*env)->GetDirectBufferAddress(env, input) + inputOffset;
23     void* outputAddress = (char*)(*env)->GetDirectBufferAddress(env, output) + outputOffset;
24     return fastlz_compress(inputAddress, length, outputAddress);
25 }
26
27 JNIEXPORT jint JNICALL Java_org_simantics_fastlz_FastLZ_decompress(JNIEnv* env, jclass clazz, 
28                                                                    jobject input, jint inputOffset, jint length, 
29                                                                    jobject output, jint outputOffset, jint maxout) {
30     void* inputAddress = (char*)(*env)->GetDirectBufferAddress(env, input) + inputOffset;
31     void* outputAddress = (char*)(*env)->GetDirectBufferAddress(env, output) + outputOffset;
32     return fastlz_decompress(inputAddress, length, outputAddress, maxout);
33 }
34
35 #define INITIAL_SIZE 2000000
36
37 JNIEXPORT jint JNICALL Java_org_simantics_fastlz_FastLZ_decompressCluster(JNIEnv* env, jclass clazz, jobject deflated, jint deflatedSize, jint inflatedSize, jobjectArray arrays) {
38
39         static char *inflateBuffer = 0;
40         static int inflateBufferSize = 0;
41
42         int ll, il, bl;
43
44         jlongArray longs;
45         jintArray ints;
46         jbyteArray bytes;
47
48         char *input = (char*)(*env)->GetDirectBufferAddress(env, deflated);
49         char *address;
50
51         if(inflateBufferSize < inflatedSize) {
52                 if(!inflateBuffer) {
53                         if(inflatedSize < INITIAL_SIZE) inflatedSize = INITIAL_SIZE;
54                         inflateBuffer = malloc(inflatedSize);
55                         inflateBufferSize = inflatedSize;
56                 } else {
57                         if(inflateBuffer) free(inflateBuffer);
58                         inflateBuffer = malloc(inflatedSize);
59                         inflateBufferSize = inflatedSize;
60                 }
61         }
62
63         address = inflateBuffer;
64
65         fastlz_decompress(input, deflatedSize, inflateBuffer, inflateBufferSize);
66
67         ll = *(int *)address;
68         longs = (*env)->NewLongArray(env, ll);
69         (*env)->SetLongArrayRegion(env, longs, 0, ll, (const jlong *)(address + 4));
70         (*env)->SetObjectArrayElement(env, arrays, 0, longs);
71
72         address += 4 + 8 * ll;
73
74         il = *(int *)address;
75         ints = (*env)->NewIntArray(env, il);
76         (*env)->SetIntArrayRegion(env, ints, 0, il, (const jint *)(address + 4));
77         (*env)->SetObjectArrayElement(env, arrays, 1, ints);
78
79         address += 4 * il + 4;
80
81         bl = *(int *)address;
82         bytes = (*env)->NewByteArray(env, bl);
83         (*env)->SetByteArrayRegion(env, bytes, 0, bl, (const jbyte *)(address + 4));
84         (*env)->SetObjectArrayElement(env, arrays, 2, bytes);
85
86         return 0;
87
88 }
89