9bc68a404e6bffb3ebd38f0920d145d0ca4c1fe5
[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 4921 2006-05-15 14:24:36Z twisti $
33
34 */
35
36
37 #include "config.h"
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "vm/types.h"
43
44 #include "mm/boehm.h"
45 #include "mm/memory.h"
46 #include "toolbox/logging.h"
47 #include "native/jni.h"
48 #include "native/native.h"
49 #include "native/include/java_lang_Class.h"
50 #include "native/include/java_lang_Cloneable.h"
51 #include "native/include/java_lang_Object.h"
52
53 #if defined(ENABLE_THREADS)
54 # include "threads/native/threads.h"
55 #endif
56
57 #include "vm/builtin.h"
58 #include "vm/exceptions.h"
59 #include "vm/loader.h"
60 #include "vm/options.h"
61 #include "vm/stringlocal.h"
62
63
64 /*
65  * Class:     java/lang/VMObject
66  * Method:    getClass
67  * Signature: (Ljava/lang/Object;)Ljava/lang/Class;
68  */
69 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMObject_getClass(JNIEnv *env, jclass clazz, java_lang_Object *obj)
70 {
71         classinfo *c;
72
73         if (!obj)
74                 return NULL;
75
76         c = ((java_objectheader *) obj)->vftbl->class;
77
78         return (java_lang_Class *) c;
79 }
80
81
82 /*
83  * Class:     java/lang/VMObject
84  * Method:    clone
85  * Signature: (Ljava/lang/Cloneable;)Ljava/lang/Object;
86  */
87 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_VMObject_clone(JNIEnv *env, jclass clazz, java_lang_Cloneable *this)
88 {
89         classinfo         *c;
90         java_lang_Object  *new;
91         arraydescriptor   *desc;
92
93         /* we are cloning an array */
94
95         if ((desc = this->header.vftbl->arraydesc) != NULL) {
96         
97                 u4 size = desc->dataoffset + desc->componentsize * ((java_arrayheader *) this)->size;
98         
99                 new = (java_lang_Object *)
100                         heap_allocate(size, (desc->arraytype == ARRAYTYPE_OBJECT), NULL);
101
102                 if (new == NULL)
103                         return NULL;
104
105                 MCOPY(new, this, u1, size);
106
107 #if defined(ENABLE_THREADS)
108                 lock_init_object_lock((java_objectheader *) new);
109 #endif
110         
111                 return new;
112         }
113     
114     /* we are cloning a non-array */
115
116     if (!builtin_instanceof((java_objectheader *) this, class_java_lang_Cloneable)) {
117         *exceptionptr =
118                         new_exception(string_java_lang_CloneNotSupportedException);
119         return NULL;
120     }
121
122     /* XXX should use vftbl */
123     c = this->header.vftbl->class;
124     new = (java_lang_Object *) builtin_new(c);
125
126     if (new == NULL)
127         return NULL;
128
129     MCOPY(new, this, u1, c->instancesize);
130
131 #if defined(ENABLE_THREADS)
132         lock_init_object_lock((java_objectheader *) new);
133 #endif
134
135     return new;
136 }
137
138
139 /*
140  * Class:     java/lang/VMObject
141  * Method:    notify
142  * Signature: ()V
143  */
144 JNIEXPORT void JNICALL Java_java_lang_VMObject_notify(JNIEnv *env, jclass clazz, java_lang_Object *this)
145 {
146 #if defined(ENABLE_THREADS)
147         lock_notify_object(&this->header);
148 #endif
149 }
150
151
152 /*
153  * Class:     java/lang/VMObject
154  * Method:    notifyAll
155  * Signature: ()V
156  */
157 JNIEXPORT void JNICALL Java_java_lang_VMObject_notifyAll(JNIEnv *env, jclass clazz, java_lang_Object *this)
158 {
159 #if defined(ENABLE_THREADS)
160         lock_notify_all_object(&this->header);
161 #endif
162 }
163
164
165 /*
166  * Class:     java/lang/VMObject
167  * Method:    wait
168  * Signature: (Ljava/lang/Object;JI)V
169  */
170 JNIEXPORT void JNICALL Java_java_lang_VMObject_wait(JNIEnv *env, jclass clazz, java_lang_Object *o, s8 ms, s4 ns)
171 {
172 #if defined(ENABLE_THREADS)
173         lock_wait_for_object(&o->header, ms, ns);
174 #endif
175 }
176
177
178 /*
179  * These are local overrides for various environment variables in Emacs.
180  * Please do not remove this and leave it at the end of the file, where
181  * Emacs will automagically detect them.
182  * ---------------------------------------------------------------------
183  * Local variables:
184  * mode: c
185  * indent-tabs-mode: t
186  * c-basic-offset: 4
187  * tab-width: 4
188  * End:
189  */