Skip to content

Commit 54a4297

Browse files
committed
[test] Dealt with native memory management for testing
1 parent d09b213 commit 54a4297

File tree

8 files changed

+227
-0
lines changed

8 files changed

+227
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2018 Muhammad Tayyab Akram
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mta.tehreer.internal;
18+
19+
import java.nio.ByteBuffer;
20+
21+
public final class Memory {
22+
static {
23+
TestJNI.loadLibrary();
24+
}
25+
26+
public static native long allocate(long capacity);
27+
public static native void dispose(long pointer);
28+
29+
public static native ByteBuffer buffer(long pointer, long capacity);
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2018 Muhammad Tayyab Akram
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mta.tehreer.internal;
18+
19+
public final class TestJNI {
20+
private static boolean loaded = false;
21+
22+
public static void loadLibrary() {
23+
if (!loaded) {
24+
synchronized (TestJNI.class) {
25+
if (!loaded) {
26+
System.loadLibrary("testjni");
27+
loaded = true;
28+
}
29+
}
30+
}
31+
}
32+
33+
private TestJNI() {
34+
}
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
TEST_PATH := $(call my-dir)
2+
SRC_PATH := $(TEST_PATH)/../..
3+
MAIN_PATH := $(SRC_PATH)/main/jni
4+
5+
include $(MAIN_PATH)/Android.mk
6+
7+
###########################TEST############################
8+
include $(CLEAR_VARS)
9+
10+
LOCAL_PATH := $(TEST_PATH)
11+
LOCAL_MODULE := testjni
12+
13+
FILE_LIST := \
14+
Memory.cpp \
15+
Test.cpp
16+
17+
LOCAL_C_INCLUDES := $(FT_HEADERS_PATH) $(SB_HEADERS_PATH) $(SF_HEADERS_PATH) $(MAIN_PATH)
18+
LOCAL_SRC_FILES := $(FILE_LIST:%=$(LOCAL_PATH)/%)
19+
LOCAL_LDLIBS := -latomic -landroid -ljnigraphics -llog
20+
LOCAL_SHARED_LIBRARIES := tehreerjni
21+
22+
include $(BUILD_SHARED_LIBRARY)
23+
###########################################################
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
APP_PLATFORM := android-15
2+
APP_STL := c++_static
3+
APP_CFLAGS += -std=c89
4+
APP_CPPFLAGS += -std=c++11
5+
NDK_TOOLCHAIN_VERSION := clang
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2018 Muhammad Tayyab Akram
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <cstddef>
18+
#include <cstdlib>
19+
#include <jni.h>
20+
21+
#include "JavaBridge.h"
22+
#include "Memory.h"
23+
24+
using namespace Tehreer;
25+
26+
static jlong allocate(JNIEnv *env, jobject obj, jlong capacity)
27+
{
28+
return (jlong)malloc((size_t)capacity);
29+
}
30+
31+
static void dispose(JNIEnv *env, jobject obj, jlong pointer)
32+
{
33+
free((void *)pointer);
34+
}
35+
36+
static jobject buffer(JNIEnv *env, jobject obj, jlong pointer, jlong capacity)
37+
{
38+
return env->NewDirectByteBuffer((void *)pointer, capacity);
39+
}
40+
41+
static JNINativeMethod JNI_METHODS[] = {
42+
{ "allocate", "(J)J", (void *)allocate },
43+
{ "dispose", "(J)V", (void *)dispose },
44+
{ "buffer", "(JJ)Ljava/nio/ByteBuffer;", (void *)buffer },
45+
};
46+
47+
jint register_com_mta_tehreer_internal_Memory(JNIEnv *env)
48+
{
49+
return JavaBridge::registerClass(env, "com/mta/tehreer/internal/Memory", JNI_METHODS, sizeof(JNI_METHODS) / sizeof(JNI_METHODS[0]));
50+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2018 Muhammad Tayyab Akram
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef _TEHREER__MEMORY_H
18+
#define _TEHREER__MEMORY_H
19+
20+
#include <jni.h>
21+
22+
jint register_com_mta_tehreer_internal_Memory(JNIEnv *env);
23+
24+
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2018 Muhammad Tayyab Akram
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Test.h"
18+
19+
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
20+
JNIEnv *env;
21+
jint result;
22+
23+
if (jvm->GetEnv((void **)&env, JNI_VERSION_1_6)) {
24+
return JNI_ERR;
25+
}
26+
27+
if (env == nullptr) {
28+
return JNI_ERR;
29+
}
30+
31+
result = register_com_mta_tehreer_internal_Memory(env) == JNI_OK;
32+
33+
if (!result) {
34+
return JNI_ERR;
35+
}
36+
37+
return JNI_VERSION_1_6;
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2018 Muhammad Tayyab Akram
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef _TEST_H
18+
#define _TEST_H
19+
20+
#include "Memory.h"
21+
22+
#endif

0 commit comments

Comments
 (0)