-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemtc_memManager.cpp
143 lines (120 loc) · 4.41 KB
/
memtc_memManager.cpp
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
#include <jni.h>
#include "memtc_memManager.h"
#include <iostream>
#include <mutex>
#include "MemoryManager.h"
#define MEMTC_NULL_EXCEPTION ((int) 0)
# define _memtc_long_to_addr(_val) ((void *)(_val))
# define _addr_to_memtc_long(_addr) ((long)(_addr))
using namespace std;
using std::string;
using std::cout;
using std::endl;
static mutex __class_LOADER_lock;
static jclass memtc_manager_CLS = NULL;
static jfieldID memtc_manager_PTR = NULL;
static jmethodID memtc_manager_INIT_PTR = NULL;
static void memtc_manager_LOADER(JNIEnv *__env)
{
jclass lcl_class = __env->FindClass("memtc/MemtcJNI");
if (lcl_class==NULL) throw MEMTC_NULL_EXCEPTION;
jclass memtc_class = (jclass) __env->NewGlobalRef(lcl_class);
__env->DeleteLocalRef(lcl_class);
jfieldID memtc_field = __env->GetFieldID(memtc_class,"_native_ptr","J");
if (memtc_field==NULL) throw MEMTC_NULL_EXCEPTION;
__class_LOADER_lock.lock();
if (memtc_manager_CLS == NULL)
{
memtc_manager_PTR = memtc_field;
memtc_manager_CLS = memtc_class;
memtc_class = NULL;
}
__class_LOADER_lock.unlock();
if (memtc_class != NULL)
__env->DeleteGlobalRef(memtc_class);
}
JNIEXPORT jlong JNICALL Java_memtc_MemtcJNI_Native_1create__(JNIEnv *__env, jclass this_class)
{
try {
MemoryManager *self = new MemoryManager();
return ((jlong) _addr_to_memtc_long(self)) | ((jlong) 1);
} catch(std::bad_alloc &)
{ cout << "bad allocation" << endl; }
catch(...) {};
return (jlong) 0;
}
JNIEXPORT void JNICALL Java_memtc_MemtcJNI_Native_1destroy(JNIEnv *__env, jobject _self)
{
if (memtc_manager_CLS==NULL)
memtc_manager_LOADER(__env);
jlong self_ref = __env->GetLongField(_self,memtc_manager_PTR);
if (self_ref == 0) return;
__env->SetLongField(_self,memtc_manager_PTR,0);
if ((self_ref & ((jlong) 1)) != 0)
{
MemoryManager *self = (MemoryManager *) _memtc_long_to_addr(self_ref - ((jlong) 1));
delete self;
}
}
JNIEXPORT void JNICALL Java_memtc_MemtcJNI_Native_1init_1class(JNIEnv *__env, jclass this_class)
{
if (memtc_manager_CLS != NULL) return;
try {
memtc_manager_LOADER(__env);
} catch(...) {};
}
JNIEXPORT void JNICALL Java_memtc_MemtcJNI_TryRelease(JNIEnv *__env, jobject _self, jboolean force){
try {
if (memtc_manager_CLS==NULL)
memtc_manager_LOADER(__env);
MemoryManager *self = (MemoryManager *)
_memtc_long_to_addr(__env->GetLongField(_self,memtc_manager_PTR) & ~((jlong) 1));
// todo if NULL -> throw exception
self->tryRelease(force);
} catch (...)
{cout << "exception 3" << endl; }
}
JNIEXPORT void JNICALL Java_memtc_MemtcJNI_SetMaxBytes(JNIEnv *__env, jobject _self, jlong bytes) {
try {
if (memtc_manager_CLS==NULL)
memtc_manager_LOADER(__env);
MemoryManager *self = (MemoryManager *)
_memtc_long_to_addr(__env->GetLongField(_self,memtc_manager_PTR) & ~((jlong) 1));
// todo if NULL -> throw exception
self->setMaxBytes(bytes);
} catch (...)
{cout << "exception 3" << endl; }
}
JNIEXPORT void JNICALL Java_memtc_MemtcJNI_SetMinBytes(JNIEnv *__env, jobject _self, jlong bytes) {
try {
if (memtc_manager_CLS==NULL)
memtc_manager_LOADER(__env);
MemoryManager *self = (MemoryManager *)
_memtc_long_to_addr(__env->GetLongField(_self,memtc_manager_PTR) & ~((jlong) 1));
// todo if NULL -> throw exception
self->setMinBytes(bytes);
} catch (...)
{cout << "exception 3" << endl; }
}
JNIEXPORT void JNICALL Java_memtc_MemtcJNI_SetCriticalBytes(JNIEnv *__env, jobject _self, jlong bytes) {
try {
if (memtc_manager_CLS==NULL)
memtc_manager_LOADER(__env);
MemoryManager *self = (MemoryManager *)
_memtc_long_to_addr(__env->GetLongField(_self,memtc_manager_PTR) & ~((jlong) 1));
// todo if NULL -> throw exception
self->setCriticalBytes(bytes);
} catch (...)
{cout << "exception 3" << endl; }
}
JNIEXPORT void JNICALL Java_memtc_MemtcJNI_DisplayMemoryInfos(JNIEnv *__env, jobject _self) {
try {
if (memtc_manager_CLS==NULL)
memtc_manager_LOADER(__env);
MemoryManager *self = (MemoryManager *)
_memtc_long_to_addr(__env->GetLongField(_self,memtc_manager_PTR) & ~((jlong) 1));
// todo if NULL -> throw exception
self->displayMemoryInfos();
} catch (...)
{cout << "exception 3" << endl; }
}