Skip to content

Commit

Permalink
FC-5315: add create and free wrapper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawnW858 committed Dec 8, 2020
1 parent fe21f22 commit 7945c67
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ static void CoapGroupRequestFilter_Destroy(void *_args) {
COAP_GROUP_REQUEST_FILTER_CLASS_NAME,
filterWrapper->java_object);

env->DeleteGlobalRef(filterWrapper->java_object);

GG_CoapGroupRequestFilter_Destroy((GG_CoapGroupRequestFilter*)filterWrapper->pointer);
GG_FreeMemory(filterWrapper);

freeNativeReferenceWrapper(env, filterWrapper);
}

JNIEXPORT jlong JNICALL
Expand All @@ -69,11 +68,7 @@ Java_com_fitbit_goldengate_bindings_coap_CoapGroupRequestFilter_create(
return create_result;
}

NativeReferenceWrapper* wrapper = (NativeReferenceWrapper*) GG_AllocateMemory(sizeof(NativeReferenceWrapper));
wrapper->pointer = create_args.filter;
wrapper->java_object = env->NewGlobalRef(thiz);

return (jlong) (intptr_t) wrapper;
return (jlong) (intptr_t) createNativeReferenceWrapper(env, create_args.filter, thiz);
}

JNIEXPORT void JNICALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ static void CoapEndpoint_Destroy(void *_args) {
COAP_ENDPOINT_CLASS_NAME,
endpointWrapper->java_object);

env->DeleteGlobalRef(endpointWrapper->java_object);

GG_CoapEndpoint_Destroy((GG_CoapEndpoint*)endpointWrapper->pointer);
GG_FreeMemory(endpointWrapper);

freeNativeReferenceWrapper(env, endpointWrapper);
}

JNIEXPORT jlong JNICALL
Expand All @@ -116,10 +115,7 @@ Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_create(
return create_result;
}

NativeReferenceWrapper* wrapper = (NativeReferenceWrapper*) GG_AllocateMemory(sizeof(NativeReferenceWrapper));
wrapper->pointer = create_args.endpoint;
wrapper->java_object = env->NewGlobalRef(thiz);
return (jlong) (intptr_t) wrapper;
return (jlong) (intptr_t) createNativeReferenceWrapper(env, create_args.endpoint, thiz);
}

JNIEXPORT void JNICALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@

#include <util/jni_gg_native_reference.h>
#include <xp/common/gg_port.h>
#include <xp/common/gg_memory.h>

NativeReferenceWrapper* createNativeReferenceWrapper(
JNIEnv* env,
void* pointer,
jobject java_object
) {
NativeReferenceWrapper* wrapper = (NativeReferenceWrapper*) GG_AllocateMemory(sizeof(NativeReferenceWrapper));
wrapper->pointer = pointer;
wrapper->java_object = env->NewGlobalRef(java_object);
return wrapper;
}

void callJavaObjectOnFreeMethod(
JNIEnv* env,
Expand All @@ -20,3 +32,9 @@ void callJavaObjectOnFreeMethod(

env->CallVoidMethod(java_object, on_free_method_id);
}

void freeNativeReferenceWrapper(JNIEnv* env, NativeReferenceWrapper* wrapper) {
env->DeleteGlobalRef(wrapper->java_object);
GG_FreeMemory(wrapper);
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,44 @@ typedef struct {
jobject java_object;
} NativeReferenceWrapper;

/**
* Build a NativeReferenceWrapper struct for a native memory reference
* @param env A JNIEnv that can be called from the current thread.
* @param pointer a native memory reference
* @param java_object the java object which stores the native reference
* @return
*/
NativeReferenceWrapper*
createNativeReferenceWrapper(
JNIEnv* env,
void* pointer,
jobject java_object
);

void callJavaObjectOnFreeMethod(
/**
* Call onFree callback inside java class object which implements NativeReferenceWithCallback interface
* ino order to notify that the native memory is going to be freed
*
* @param env A JNIEnv that can be called from the current thread.
* @param class_name the java class name which implements NativeReferenceWithCallback interface
* @param java_object the java class instance associated with class_name
*/
void
callJavaObjectOnFreeMethod(
JNIEnv* env,
const char* class_name,
jobject java_object);
jobject java_object
);

/**
* Free the NativeReferenceWrapper struct
* @param env A JNIEnv that can be called from the current thread.
* @param wrapper the memory pointer to NativeReferenceWrapper struct
*/
void
freeNativeReferenceWrapper(
JNIEnv* env,
NativeReferenceWrapper* wrapper
);

#endif //GOLDENGATELIB_JNI_GG_NATIVE_REFERENCE_H

0 comments on commit 7945c67

Please sign in to comment.