1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * This source file is part of SableVM.                            *
3  *                                                                 *
4  * See the file "LICENSE" for the copyright information and for    *
5  * the terms and conditions for copying, distribution and          *
6  * modification of this source file.                               *
7  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
8 
9 /*
10 ----------------------------------------------------------------------
11 _svmh_new_native_global
12 ----------------------------------------------------------------------
13 */
14 
15 svm_static jint
_svmh_new_native_global(_svmt_JNIEnv * env,jobject * obj)16 _svmh_new_native_global (_svmt_JNIEnv *env, jobject *obj)
17 {
18   _svmt_JavaVM *vm = env->vm;
19   _svmt_native_ref *native_global = NULL;
20   jint status = JNI_OK;
21 
22   _svmm_mutex_lock (vm->global_mutex);
23 
24   /* do we already have a free reference available? */
25   if (vm->native_globals.free_list != NULL)
26     {
27       native_global = vm->native_globals.free_list;
28 
29       vm->native_globals.free_list = native_global->next;
30       if (vm->native_globals.free_list != NULL)
31 	{
32 	  assert (vm->native_globals.free_list->previous == native_global);
33 
34 	  vm->native_globals.free_list->previous = NULL;
35 	}
36     }
37   else
38     {
39       status = _svmm_gzalloc_native_ref_no_exception (native_global);
40     }
41 
42   _svmm_mutex_unlock ();
43 
44   if (status != JNI_OK)
45     {
46       _svmf_error_OutOfMemoryError (env);
47       return JNI_ERR;
48     }
49 
50   assert (native_global != NULL);
51   assert (native_global->previous == NULL);
52 
53   native_global->next = vm->native_globals.list;
54   vm->native_globals.list = native_global;
55   if (native_global->next != NULL)
56     {
57       assert (native_global->next->previous == NULL);
58 
59       native_global->next->previous = native_global;
60     }
61 
62   *obj = _svmf_cast_jobject_nref (native_global);
63   return JNI_OK;
64 }
65 
66 /*
67 ----------------------------------------------------------------------
68 _svmh_free_native_global
69 ----------------------------------------------------------------------
70 */
71 
72 svm_static void
_svmh_free_native_global(_svmt_JNIEnv * env,jobject * pglobal_ref)73 _svmh_free_native_global (_svmt_JNIEnv *env, jobject *pglobal_ref)
74 {
75   _svmt_JavaVM *vm = env->vm;
76   _svmt_native_ref *native_global = _svmf_cast_nref_jobject (*pglobal_ref);
77 
78   _svmm_mutex_lock (vm->global_mutex);
79 
80   if (native_global->next != NULL)
81     {
82       native_global->next->previous = native_global->previous;
83     }
84 
85   if (native_global->previous != NULL)
86     {
87       native_global->previous->next = native_global->next;
88     }
89   else
90     {
91       vm->native_globals.list = native_global->next;
92     }
93 
94   native_global->ref = NULL;
95   native_global->previous = NULL;
96   native_global->next = vm->native_globals.free_list;
97   vm->native_globals.free_list = native_global;
98   if (native_global->next != NULL)
99     {
100       assert (native_global->next->previous == NULL);
101 
102       native_global->next->previous = native_global;
103     }
104 
105   *pglobal_ref = NULL;
106 
107   _svmm_mutex_unlock ();
108 }
109