comparison java/jdc/org_dyncall_DC.c @ 22:4ee8d6aa7721

- java binding cleanup, exposed free functions (not very oo, this entire binding) - java binding examples
author cslag
date Sat, 09 Apr 2016 18:18:34 -0500
parents 40a2c4198016
children 87fd9f34eaa0
comparison
equal deleted inserted replaced
21:40a2c4198016 22:4ee8d6aa7721
1 #include <stdlib.h>
1 #include "org_dyncall_DC.h" 2 #include "org_dyncall_DC.h"
2 #include "dyncall.h" 3 #include "dyncall.h"
3 #include "dynload.h" 4 #include "dynload.h"
4 5
6 // Bookkeping to clean up on reset.
7 static int gc_snum = 0;
8 static jobject* gc_jstr = NULL;
9 static const char** gc_cstr = NULL;
10 static void cleanupHeldStrings(JNIEnv *pEnv)
11 {
12 for(int i=0; i<gc_snum; ++i)
13 (*pEnv)->ReleaseStringUTFChars(pEnv, gc_jstr[i], gc_cstr[i]);
14
15 free(gc_jstr); gc_jstr = NULL;
16 free(gc_cstr); gc_cstr = NULL;
17 gc_snum = 0;
18
19 }
20
21
5 jlong JNICALL Java_org_dyncall_DC_newCallVM(JNIEnv *pEnv, jclass clazz, jint size) 22 jlong JNICALL Java_org_dyncall_DC_newCallVM(JNIEnv *pEnv, jclass clazz, jint size)
6 { 23 {
7 return (jlong)dcNewCallVM(size); 24 return (jlong)dcNewCallVM(size);
8 //@@@ free 25 }
9 } 26
10 27 void JNICALL Java_org_dyncall_DC_freeCallVM(JNIEnv *pEnv, jclass clazz, jlong vm)
11 jlong JNICALL Java_org_dyncall_DC_load(JNIEnv *pEnv, jclass clazz, jstring s) 28 {
29 cleanupHeldStrings(pEnv);
30 dcFree((DCCallVM*)vm);
31 }
32
33 jlong JNICALL Java_org_dyncall_DC_loadLibrary(JNIEnv *pEnv, jclass clazz, jstring s)
12 { 34 {
13 jlong l = 0; 35 jlong l = 0;
14 const char *sz = (*pEnv)->GetStringUTFChars(pEnv, s, NULL); 36 const char *sz = (*pEnv)->GetStringUTFChars(pEnv, s, NULL);
15 if(sz != NULL) { 37 if(sz != NULL) {
16 l = (jlong)dlLoadLibrary(sz); 38 l = (jlong)dlLoadLibrary(sz);
17 (*pEnv)->ReleaseStringUTFChars(pEnv, s, sz); 39 (*pEnv)->ReleaseStringUTFChars(pEnv, s, sz);
18 //@@@ free
19 } 40 }
20 return l; 41 return l;
42 }
43
44 void JNICALL Java_org_dyncall_DC_freeLibrary(JNIEnv *pEnv, jclass clazz, jlong libhandle)
45 {
46 dlFreeLibrary((DLLib*)libhandle);
21 } 47 }
22 48
23 jlong JNICALL Java_org_dyncall_DC_find(JNIEnv *pEnv, jclass clazz, jlong libhandle, jstring s) 49 jlong JNICALL Java_org_dyncall_DC_find(JNIEnv *pEnv, jclass clazz, jlong libhandle, jstring s)
24 { 50 {
25 jlong l = 0; 51 jlong l = 0;
41 // return dlSymsName((DLSyms*)symshandle, i); 67 // return dlSymsName((DLSyms*)symshandle, i);
42 //} 68 //}
43 69
44 void JNICALL Java_org_dyncall_DC_mode(JNIEnv *pEnv, jclass clazz, jlong vm, jint i) 70 void JNICALL Java_org_dyncall_DC_mode(JNIEnv *pEnv, jclass clazz, jlong vm, jint i)
45 { 71 {
46 dcMode((DCCallVM*)vm, i);//@@@test 72 dcMode((DCCallVM*)vm, i);
47 } 73 }
48 74
49 void JNICALL Java_org_dyncall_DC_reset(JNIEnv *pEnv, jclass clazz, jlong vm) 75 void JNICALL Java_org_dyncall_DC_reset(JNIEnv *pEnv, jclass clazz, jlong vm)
50 { 76 {
77 cleanupHeldStrings(pEnv);
51 dcReset((DCCallVM*)vm); 78 dcReset((DCCallVM*)vm);
52 //@@@add cleanup code here for temporary memory held by jni
53 } 79 }
54 80
55 void JNICALL Java_org_dyncall_DC_argBool(JNIEnv *pEnv, jclass clazz, jlong vm, jboolean b) 81 void JNICALL Java_org_dyncall_DC_argBool(JNIEnv *pEnv, jclass clazz, jlong vm, jboolean b)
56 { 82 {
57 dcArgBool((DCCallVM*)vm, b);//@@@test 83 dcArgBool((DCCallVM*)vm, b);//@@@test
67 dcArgShort((DCCallVM*)vm, s);//@@@test 93 dcArgShort((DCCallVM*)vm, s);//@@@test
68 } 94 }
69 95
70 void JNICALL Java_org_dyncall_DC_argInt(JNIEnv *pEnv, jclass clazz, jlong vm, jint i) 96 void JNICALL Java_org_dyncall_DC_argInt(JNIEnv *pEnv, jclass clazz, jlong vm, jint i)
71 { 97 {
72 dcArgInt((DCCallVM*)vm, i);//@@@test 98 dcArgInt((DCCallVM*)vm, i);
73 } 99 }
74 100
75 void JNICALL Java_org_dyncall_DC_argLong(JNIEnv *pEnv, jclass clazz, jlong vm, jlong l) 101 void JNICALL Java_org_dyncall_DC_argLong(JNIEnv *pEnv, jclass clazz, jlong vm, jlong l)
76 { 102 {
77 dcArgLong((DCCallVM*)vm, l);//@@@test 103 dcArgLong((DCCallVM*)vm, l);//@@@test
105 void JNICALL Java_org_dyncall_DC_argString(JNIEnv *pEnv, jclass clazz, jlong vm, jstring s) 131 void JNICALL Java_org_dyncall_DC_argString(JNIEnv *pEnv, jclass clazz, jlong vm, jstring s)
106 { 132 {
107 const char *sz = (*pEnv)->GetStringUTFChars(pEnv, s, NULL); 133 const char *sz = (*pEnv)->GetStringUTFChars(pEnv, s, NULL);
108 if(sz != NULL) { 134 if(sz != NULL) {
109 dcArgPointer((DCCallVM*)vm, (DCpointer)sz); 135 dcArgPointer((DCCallVM*)vm, (DCpointer)sz);
110 //(*pEnv)->ReleaseStringUTFChars(pEnv, s, sz); 136
111 //@@@ free this string when vm is destroyed, reset 137 // Bookkeeping, to later release on reset or destruction of vm.
138 gc_jstr = realloc(gc_jstr, (gc_snum+1)*sizeof(jobject));
139 gc_cstr = realloc(gc_cstr, (gc_snum+1)*sizeof(const char*));
140 gc_jstr[gc_snum] = s;
141 gc_cstr[gc_snum] = sz;
142 ++gc_snum;
112 } 143 }
113 } 144 }
114 145
115 void JNICALL Java_org_dyncall_DC_callVoid (JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) 146 void JNICALL Java_org_dyncall_DC_callVoid (JNIEnv *pEnv, jclass clazz, jlong vm, jlong target)
116 { 147 {
132 return dcCallShort((DCCallVM*)vm, (DCpointer)target);//@@@test 163 return dcCallShort((DCCallVM*)vm, (DCpointer)target);//@@@test
133 } 164 }
134 165
135 jint JNICALL Java_org_dyncall_DC_callInt(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) 166 jint JNICALL Java_org_dyncall_DC_callInt(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target)
136 { 167 {
137 return dcCallInt((DCCallVM*)vm, (DCpointer)target);//@@@test 168 return dcCallInt((DCCallVM*)vm, (DCpointer)target);
138 } 169 }
139 170
140 jlong JNICALL Java_org_dyncall_DC_callLong(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) 171 jlong JNICALL Java_org_dyncall_DC_callLong(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target)
141 { 172 {
142 return dcCallLong((DCCallVM*)vm, (DCpointer)target);//@@@test 173 return dcCallLong((DCCallVM*)vm, (DCpointer)target);//@@@test
157 return dcCallDouble((DCCallVM*)vm, (DCpointer)target); 188 return dcCallDouble((DCCallVM*)vm, (DCpointer)target);
158 } 189 }
159 190
160 jlong JNICALL Java_org_dyncall_DC_callPointer(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) 191 jlong JNICALL Java_org_dyncall_DC_callPointer(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target)
161 { 192 {
162 return (jlong)dcCallPointer((DCCallVM*)vm, (DCpointer)target);//@@@test 193 return (jlong)dcCallPointer((DCCallVM*)vm, (DCpointer)target);
163 } 194 }
164 195
165 jstring JNICALL Java_org_dyncall_DC_callString(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) 196 jstring JNICALL Java_org_dyncall_DC_callString(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target)
166 { 197 {
167 return (*pEnv)->NewStringUTF(pEnv, dcCallPointer((DCCallVM*)vm, (DCpointer)target));//@@@test 198 return (*pEnv)->NewStringUTF(pEnv, dcCallPointer((DCCallVM*)vm, (DCpointer)target));
168 } 199 }
169 200
170 jint JNICALL Java_org_dyncall_DC_getError(JNIEnv *pEnv, jclass clazz, jlong vm) 201 jint JNICALL Java_org_dyncall_DC_getError(JNIEnv *pEnv, jclass clazz, jlong vm)
171 { 202 {
172 return dcGetError((DCCallVM*)vm);//@@@test 203 return dcGetError((DCCallVM*)vm);
173 } 204 }
174 205