c47b9f440d6a20ee8e20dc477698e2272883c914
[cacao.git] / src / native / vm / VMObject.c
1 /* native/vm/VMObject.c - java/lang/Object
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30
31    $Id: VMObject.c 1621 2004-11-30 13:06:55Z twisti $
32
33 */
34
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "mm/boehm.h"
40 #include "mm/memory.h"
41 #include "toolbox/logging.h"
42 #include "native/jni.h"
43 #include "native/native.h"
44 #include "native/include/java_lang_Cloneable.h"
45 #include "native/include/java_lang_Object.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/loader.h"
49 #include "vm/options.h"
50
51 #if defined(USE_THREADS)
52 # if defined(NATIVE_THREADS)
53 #  include "threads/native/threads.h"
54 # else
55 #  include "threads/green/threads.h"
56 #  include "threads/green/locks.h"
57 # endif
58 #endif
59
60
61 /*
62  * Class:     java/lang/Object
63  * Method:    clone
64  * Signature: ()Ljava/lang/Object;
65  */
66 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_VMObject_clone(JNIEnv *env, jclass clazz, java_lang_Cloneable *this)
67 {
68         classinfo *c;
69         java_lang_Object *new;
70         arraydescriptor *desc;
71     
72         if ((desc = this->header.vftbl->arraydesc) != NULL) {
73                 /* We are cloning an array */
74         
75                 u4 size = desc->dataoffset + desc->componentsize * ((java_arrayheader *) this)->size;
76         
77                 new = (java_lang_Object *) heap_allocate(size, (desc->arraytype == ARRAYTYPE_OBJECT), NULL);
78                 memcpy(new, this, size);
79 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
80                 initObjectLock((java_objectheader *) new);
81 #endif
82         
83                 return new;
84         }
85     
86     /* We are cloning a non-array */
87     if (!builtin_instanceof((java_objectheader *) this, class_java_lang_Cloneable)) {
88         *exceptionptr =
89                         new_exception(string_java_lang_CloneNotSupportedException);
90         return NULL;
91     }
92
93     /* XXX should use vftbl */
94     c = this->header.vftbl->class;
95     new = (java_lang_Object *) builtin_new(c);
96
97     if (!new)
98         return NULL;
99
100     memcpy(new, this, c->instancesize);
101 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
102         initObjectLock((java_objectheader *) new);
103 #endif
104
105     return new;
106 }
107
108
109 /*
110  * Class:     java/lang/Object
111  * Method:    notify
112  * Signature: ()V
113  */
114 JNIEXPORT void JNICALL Java_java_lang_VMObject_notify(JNIEnv *env, jclass clazz, java_lang_Object *this)
115 {
116         if (runverbose)
117                 log_text("java_lang_Object_notify called");
118
119 #if defined(USE_THREADS)
120         signal_cond_for_object(&this->header);
121 #endif
122 }
123
124
125 /*
126  * Class:     java/lang/Object
127  * Method:    notifyAll
128  * Signature: ()V
129  */
130 JNIEXPORT void JNICALL Java_java_lang_VMObject_notifyAll(JNIEnv *env, jclass clazz, java_lang_Object *this)
131 {
132         if (runverbose)
133                 log_text("java_lang_Object_notifyAll called");
134
135 #if defined(USE_THREADS)
136         broadcast_cond_for_object(&this->header);
137 #endif
138 }
139
140
141 /*
142  * Class:     java/lang/Object
143  * Method:    wait
144  * Signature: (J)V
145  */
146 JNIEXPORT void JNICALL Java_java_lang_VMObject_wait(JNIEnv *env, jclass clazz, java_lang_Object *this, s8 time, s4 par3)
147 {
148         if (runverbose)
149                 log_text("java_lang_VMObject_wait called");
150
151 #if defined(USE_THREADS)
152         wait_cond_for_object(&this->header, time, par3);
153 #endif
154 }
155
156
157 /*
158  * These are local overrides for various environment variables in Emacs.
159  * Please do not remove this and leave it at the end of the file, where
160  * Emacs will automagically detect them.
161  * ---------------------------------------------------------------------
162  * Local variables:
163  * mode: c
164  * indent-tabs-mode: t
165  * c-basic-offset: 4
166  * tab-width: 4
167  * End:
168  */