summaryrefslogtreecommitdiffstats
path: root/java/jni/JNIMem.cxx
blob: 992b1a956cfdc3ce3d262fc7e320ff5bf2ae6336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Author: Petre Eftime <petre.p.eftime@intel.com>
 * Copyright (c) 2016 Intel Corporation.
 *
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <cstdio>

#include "JNIMem.hpp"

// #define VERBOSE_ON 1
#ifdef VERBOSE_ON
    #define DBG_PRINT(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr); }
#else
    #define DBG_PRINT(...)
#endif

JavaVM* vm;
thread_local JNIEnvContainer jni_env;

jint JNI_OnLoad(JavaVM *initVM, void *reserved) {
    (void)reserved; // warning
    vm = initVM;
    return JNI_VERSION_1_8;
}

JNIEnv *JNIEnvContainer::operator*() {
    attach();
    return env;
}

JNIEnv *JNIEnvContainer::operator->() {
    attach();
    return env;
}

JNIEnvContainer::JNIEnvContainer() {}

JNIEnvContainer::~JNIEnvContainer() {
    detach();
}

void JNIEnvContainer::attach() {
    if (env != nullptr) {
        return;
    }
    JNIEnv *newEnv = nullptr;
    int envRes;

    envRes = vm->GetEnv((void **) &env, JNI_VERSION_1_8) ;
    if( JNI_EDETACHED == envRes ) {
        envRes = vm->AttachCurrentThreadAsDaemon((void**) &newEnv, NULL);
        if( JNI_OK != envRes ) {
            throw direct_bt::RuntimeException("Attach to VM failed", E_FILE_LINE);
        }
        env = newEnv;
    } else if( JNI_OK != envRes ) {
        throw direct_bt::RuntimeException("GetEnv of VM failed", E_FILE_LINE);
    }
    if (env==NULL) {
        throw direct_bt::RuntimeException("GetEnv of VM is NULL", E_FILE_LINE);
    }
    needsDetach = NULL != newEnv;
}

void JNIEnvContainer::detach() {
    if (env == nullptr) {
        return;
    }
    if( needsDetach ) {
        vm->DetachCurrentThread();
    }
    env = nullptr;
    needsDetach = false;
}

JNIGlobalRef::JNIGlobalRef(jobject object) {
    if( nullptr == object ) {
        throw direct_bt::RuntimeException("JNIGlobalRef ctor null jobject", E_FILE_LINE);
    }
    this->object = jni_env->NewGlobalRef(object);
    DBG_PRINT("JNIGlobalRef::def_ctor %p -> %p", object, this->object);
}

JNIGlobalRef::JNIGlobalRef(const JNIGlobalRef &o) {
    if( nullptr == o.object ) {
        throw direct_bt::RuntimeException("Other JNIGlobalRef jobject is null", E_FILE_LINE);
    }
    object = jni_env->NewGlobalRef(o.object);
    DBG_PRINT("JNIGlobalRef::copy_ctor %p -> %p", o.object, object);
}
JNIGlobalRef::JNIGlobalRef(JNIGlobalRef &&o)
: object(o.object) {
    DBG_PRINT("JNIGlobalRef::move_ctor %p (nulled) -> %p", o.object, object);
    o.object = nullptr;
}
JNIGlobalRef& JNIGlobalRef::operator=(const JNIGlobalRef &o) {
    if( &o == this ) {
        return *this;
    }
    JNIEnv * env = *jni_env;
    if( nullptr != object ) { // always
        env->DeleteGlobalRef(object);
    }
    if( nullptr == o.object ) {
        throw direct_bt::RuntimeException("Other JNIGlobalRef jobject is null", E_FILE_LINE);
    }
    object = jni_env->NewGlobalRef(o.object);
    DBG_PRINT("JNIGlobalRef::copy_assign %p -> %p", o.object, object);
    return *this;
}
JNIGlobalRef& JNIGlobalRef::operator=(JNIGlobalRef &&o) {
    object = o.object;
    DBG_PRINT("JNIGlobalRef::move_assign %p (nulled) -> %p", o.object, object);
    o.object = nullptr;
    return *this;
}

JNIGlobalRef::~JNIGlobalRef() {
    try {
        JNIEnv * env = *jni_env;
        if( nullptr == env ) {
            throw direct_bt::RuntimeException("JNIGlobalRef dtor null JNIEnv", E_FILE_LINE);
        }
        DBG_PRINT("JNIGlobalRef::dtor %p", object);
        if( nullptr != object ) {
            // due to move ctor and assignment, we accept nullptr object
            env->DeleteGlobalRef(object);
        }
    } catch (std::exception &e) {
        fprintf(stderr, "JNIGlobalRef dtor: Caught %s\n", e.what());
    }
}

bool JNIGlobalRef::operator==(const JNIGlobalRef& rhs) const {
    if( &rhs == this ) {
        DBG_PRINT("JNIGlobalRef::== true: %p == %p (ptr)", object, rhs.object);
        return true;
    }
    bool res = JNI_TRUE == jni_env->IsSameObject(object, rhs.object);
    DBG_PRINT("JNIGlobalRef::== %d: %p == %p (IsSameObject)", res, object, rhs.object);
    return res;
}