|
| 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 | +} |
0 commit comments