abc6167c74b86363aba620377f00c92ac42894ed
[cacao.git] / src / native / vm / VMObject.c
1 /* nat/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 930 2004-03-02 21:18:23Z jowenn $
32
33 */
34
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include "jni.h"
39 #include "builtin.h"
40 #include "native.h"
41 #include "mm/boehm.h"
42 #include "threads/locks.h"
43 #include "toolbox/loging.h"
44 #include "toolbox/memory.h"
45 #include "java_lang_Cloneable.h"
46 #include "java_lang_Object.h"
47
48
49 /*
50  * Class:     java/lang/Object
51  * Method:    clone
52  * Signature: ()Ljava/lang/Object;
53  */
54 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_VMObject_clone(JNIEnv *env, jclass clazz, java_lang_Cloneable *this)
55 {
56 /*      log_text("Java_java_lang_VMObject_clone called");
57         log_utf(((java_objectheader*)this)->vftbl->class->name);
58         log_text("starting cloning");     */
59         classinfo *c;
60         java_lang_Object *new;
61         arraydescriptor *desc;
62     
63         if ((desc = this->header.vftbl->arraydesc) != NULL) {
64                 /* We are cloning an array */
65         
66                 u4 size = desc->dataoffset + desc->componentsize * ((java_arrayheader *) this)->size;
67         
68                 new = (java_lang_Object *) heap_allocate(size, (desc->arraytype == ARRAYTYPE_OBJECT), NULL);
69                 memcpy(new, this, size);
70         
71                 return new;
72         }
73     
74     /* We are cloning a non-array */
75     if (!builtin_instanceof((java_objectheader *) this, class_java_lang_Cloneable)) {
76         *exceptionptr = native_new_and_init (class_java_lang_CloneNotSupportedException);
77         return NULL;
78     }
79
80     /* XXX should use vftbl */
81     c = this->header.vftbl->class;
82     new = (java_lang_Object *) builtin_new(c);
83     if (!new) {
84         *exceptionptr = proto_java_lang_OutOfMemoryError;
85         return NULL;
86     }
87
88     memcpy(new, this, c->instancesize);
89
90     return new;
91 }
92
93
94 /*
95  * Class:     java/lang/Object
96  * Method:    notify
97  * Signature: ()V
98  */
99 JNIEXPORT void JNICALL Java_java_lang_VMObject_notify(JNIEnv *env, jclass clazz, java_lang_Object *this)
100 {
101         if (runverbose)
102                 log_text("java_lang_Object_notify called");
103
104 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
105         signal_cond_for_object(&this->header);
106 #endif
107 }
108
109
110 /*
111  * Class:     java/lang/Object
112  * Method:    notifyAll
113  * Signature: ()V
114  */
115 JNIEXPORT void JNICALL Java_java_lang_VMObject_notifyAll(JNIEnv *env, jclass clazz, java_lang_Object *this)
116 {
117         if (runverbose)
118                 log_text("java_lang_Object_notifyAll called");
119
120 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
121         broadcast_cond_for_object(&this->header);
122 #endif
123 }
124
125
126 /*
127  * Class:     java/lang/Object
128  * Method:    wait
129  * Signature: (J)V
130  */
131 JNIEXPORT void JNICALL Java_java_lang_VMObject_wait(JNIEnv *env, jclass clazz, java_lang_Object *this, s8 time, s4 par3)
132 {
133         if (runverbose)
134                 log_text("java_lang_VMObject_wait called");
135
136 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
137         wait_cond_for_object(&this->header, time);
138 #endif
139 }
140
141
142 /*
143  * These are local overrides for various environment variables in Emacs.
144  * Please do not remove this and leave it at the end of the file, where
145  * Emacs will automagically detect them.
146  * ---------------------------------------------------------------------
147  * Local variables:
148  * mode: c
149  * indent-tabs-mode: t
150  * c-basic-offset: 4
151  * tab-width: 4
152  * End:
153  */