복잡한 객체에 대한 읽기 전용 래퍼 생성을 위해 swig를 사용하고 싶습니다. 래핑하려는 객체는 항상 읽히는 동안 존재하게됩니다. 또한 객체가 존재할 때만 래퍼를 사용하므로 SWIG에서 메모리 관리가 필요하지 않습니다. 꿀꺽 꿀꺽 인터페이스를 다음의 경우swig 래퍼에서 메모리 할당/할당 해제를 없애려면 어떻게해야할까요?
:
%module test
%immutable;
%inline
%{
struct Foo
{
int a;
};
struct Bar
{
int b;
Foo f;
};
%}
내가 생성 인터페이스에 쓰레기를 많이 가지고 내 경우에는 성능이 저하됩니다 쓸모없는 일을하는 래퍼를해야합니다. 바 클래스에 대한
생성 된 자바 래퍼는 다음과 같이 될 것입니다 : 항상 거짓 때문에
public class Bar {
private long swigCPtr;
protected boolean swigCMemOwn;
protected Bar(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}
protected static long getCPtr(Bar obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
protected void finalize() {
delete();
}
public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
testJNI.delete_Bar(swigCPtr);
}
swigCPtr = 0;
}
}
public int getB() {
return testJNI.Bar_b_get(swigCPtr, this);
}
public Foo getF() {
return new Foo(testJNI.Bar_f_get(swigCPtr, this), true);
}
public Bar() {
this(testJNI.new_Bar(), true);
}
}
내 래퍼에 'swigCMemOwn'필드가 필요하지 않습니다. 이 필드와 관련된 모든 코드도 쓸모가 없습니다.
SWIGEXPORT jlong JNICALL Java_some_testJNI_Bar_1f_1get(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
struct Bar *arg1 = (struct Bar *) 0 ;
Foo result;
(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(struct Bar **)&jarg1;
result = ((arg1)->f);
{
Foo * resultptr = (Foo *) malloc(sizeof(Foo));
memmove(resultptr, &result, sizeof(Foo));
*(Foo **)&jresult = resultptr;
}
return jresult;
}
내가 malloc에와 memmove를 이러한 호출을 필요가 없습니다 불필요한 네이티브 코드의 논리가있다
있습니다.
나는이 문제를 해결하기 위해 swig를 강요하고 싶지만 방법은 모른다. 가능한가?