* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / native / vm / VMObject.c
1 /* src/native/vm/VMObject.c - java/lang/VMObject
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30             Christian Thalinger
31
32    $Id: VMObject.c 4357 2006-01-22 23:33:38Z twisti $
33
34 */
35
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "mm/boehm.h"
41 #include "mm/memory.h"
42 #include "toolbox/logging.h"
43 #include "native/jni.h"
44 #include "native/native.h"
45 #include "native/include/java_lang_Class.h"
46 #include "native/include/java_lang_Cloneable.h"
47 #include "native/include/java_lang_Object.h"
48
49 #if defined(USE_THREADS)
50 # if defined(NATIVE_THREADS)
51 #  include "threads/native/threads.h"
52 # else
53 #  include "threads/green/threads.h"
54 #  include "threads/green/locks.h"
55 # endif
56 #endif
57
58 #include "vm/builtin.h"
59 #include "vm/exceptions.h"
60 #include "vm/loader.h"
61 #include "vm/options.h"
62 #include "vm/stringlocal.h"
63
64
65 /*
66  * Class:     java/lang/VMObject
67  * Method:    getClass
68  * Signature: (Ljava/lang/Object;)Ljava/lang/Class;
69  */
70 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMObject_getClass(JNIEnv *env, jclass clazz, java_lang_Object *obj)
71 {
72         classinfo *c;
73
74         if (!obj)
75                 return NULL;
76
77         c = ((java_objectheader *) obj)->vftbl->class;
78
79         return (java_lang_Class *) c;
80 }
81
82
83 /*
84  * Class:     java/lang/VMObject
85  * Method:    clone
86  * Signature: (Ljava/lang/Cloneable;)Ljava/lang/Object;
87  */
88 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_VMObject_clone(JNIEnv *env, jclass clazz, java_lang_Cloneable *this)
89 {
90         classinfo         *c;
91         java_lang_Object  *new;
92         arraydescriptor   *desc;
93
94         /* we are cloning an array */
95
96         if ((desc = this->header.vftbl->arraydesc) != NULL) {
97         
98                 u4 size = desc->dataoffset + desc->componentsize * ((java_arrayheader *) this)->size;
99         
100                 new = (java_lang_Object *)
101                         heap_allocate(size, (desc->arraytype == ARRAYTYPE_OBJECT), NULL);
102
103                 if (!new)
104                         return NULL;
105
106                 MCOPY(new, this, u1, size);
107
108 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
109                 initObjectLock((java_objectheader *) new);
110 #endif
111         
112                 return new;
113         }
114     
115     /* we are cloning a non-array */
116
117     if (!builtin_instanceof((java_objectheader *) this, class_java_lang_Cloneable)) {
118         *exceptionptr =
119                         new_exception(string_java_lang_CloneNotSupportedException);
120         return NULL;
121     }
122
123     /* XXX should use vftbl */
124     c = this->header.vftbl->class;
125     new = (java_lang_Object *) builtin_new(c);
126
127     if (!new)
128         return NULL;
129
130     MCOPY(new, this, u1, c->instancesize);
131
132 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
133         initObjectLock((java_objectheader *) new);
134 #endif
135
136     return new;
137 }
138
139
140 /*
141  * Class:     java/lang/VMObject
142  * Method:    notify
143  * Signature: ()V
144  */
145 JNIEXPORT void JNICALL Java_java_lang_VMObject_notify(JNIEnv *env, jclass clazz, java_lang_Object *this)
146 {
147         if (runverbose)
148                 log_text("java_lang_Object_notify called");
149
150 #if defined(USE_THREADS)
151         signal_cond_for_object(&this->header);
152 #endif
153 }
154
155
156 /*
157  * Class:     java/lang/VMObject
158  * Method:    notifyAll
159  * Signature: ()V
160  */
161 JNIEXPORT void JNICALL Java_java_lang_VMObject_notifyAll(JNIEnv *env, jclass clazz, java_lang_Object *this)
162 {
163         if (runverbose)
164                 log_text("java_lang_Object_notifyAll called");
165
166 #if defined(USE_THREADS)
167         broadcast_cond_for_object(&this->header);
168 #endif
169 }
170
171
172 /*
173  * Class:     java/lang/VMObject
174  * Method:    wait
175  * Signature: (Ljava/lang/Object;JI)V
176  */
177 JNIEXPORT void JNICALL Java_java_lang_VMObject_wait(JNIEnv *env, jclass clazz, java_lang_Object *o, s8 ms, s4 ns)
178 {
179         if (runverbose)
180                 log_text("java_lang_VMObject_wait called");
181
182 #if defined(USE_THREADS)
183         wait_cond_for_object(&o->header, ms, ns);
184 #endif
185 }
186
187
188 /*
189  * These are local overrides for various environment variables in Emacs.
190  * Please do not remove this and leave it at the end of the file, where
191  * Emacs will automagically detect them.
192  * ---------------------------------------------------------------------
193  * Local variables:
194  * mode: c
195  * indent-tabs-mode: t
196  * c-basic-offset: 4
197  * tab-width: 4
198  * End:
199  */