2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / lang / natSystem.cc
blob4a08bb138bf90f6a920fc5da4267cd03745cc764
1 // natSystem.cc - Native code implementing System class.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
12 #include <platform.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
18 #include <gcj/cni.h>
19 #include <jvm.h>
20 #include <java/lang/System.h>
21 #include <java/lang/Class.h>
22 #include <java/lang/ArrayStoreException.h>
23 #include <java/lang/ArrayIndexOutOfBoundsException.h>
24 #include <java/lang/NullPointerException.h>
25 #include <java/io/PrintStream.h>
26 #include <java/io/InputStream.h>
30 void
31 java::lang::System::setErr0 (java::io::PrintStream *newErr)
33 err = newErr;
36 void
37 java::lang::System::setIn0 (java::io::InputStream *newIn)
39 in = newIn;
42 void
43 java::lang::System::setOut0 (java::io::PrintStream *newOut)
45 out = newOut;
48 void
49 java::lang::System::arraycopy (jobject src, jint src_offset,
50 jobject dst, jint dst_offset,
51 jint count)
53 if (! src || ! dst)
54 throw new NullPointerException;
56 jclass src_c = src->getClass();
57 jclass dst_c = dst->getClass();
58 jclass src_comp = src_c->getComponentType();
59 jclass dst_comp = dst_c->getComponentType();
61 if (! src_c->isArray() || ! dst_c->isArray()
62 || src_comp->isPrimitive() != dst_comp->isPrimitive()
63 || (src_comp->isPrimitive() && src_comp != dst_comp))
64 throw new ArrayStoreException;
66 __JArray *src_a = (__JArray *) src;
67 __JArray *dst_a = (__JArray *) dst;
68 if (src_offset < 0 || dst_offset < 0 || count < 0
69 || (unsigned jint) src_offset > (unsigned jint) src_a->length
70 || (unsigned jint) (src_offset + count) > (unsigned jint) src_a->length
71 || (unsigned jint) dst_offset > (unsigned jint) dst_a->length
72 || (unsigned jint) (dst_offset + count) > (unsigned jint) dst_a->length)
73 throw new ArrayIndexOutOfBoundsException;
75 // Do-nothing cases.
76 if ((src == dst && src_offset == dst_offset)
77 || ! count)
78 return;
80 // If both are primitive, we can optimize trivially. If DST
81 // components are always assignable from SRC components, then we
82 // will never need to raise an error, and thus can do the
83 // optimization. If source and destinations are the same, then we
84 // know that the assignability premise always holds.
85 const bool prim = src_comp->isPrimitive();
86 if (prim || dst_comp->isAssignableFrom(src_comp) || src == dst)
88 const size_t size = (prim ? src_comp->size()
89 : sizeof elements((jobjectArray)src)[0]);
91 char *src_elts = _Jv_GetArrayElementFromElementType (src, src_comp);
92 src_elts += size * src_offset;
94 char *dst_elts = _Jv_GetArrayElementFromElementType (dst, dst_comp);
95 dst_elts += size * dst_offset;
97 #if HAVE_MEMMOVE
98 // We don't bother trying memcpy. It can't be worth the cost of
99 // the check.
100 // Don't cast to (void*), as memmove may expect (char*)
101 memmove (dst_elts, src_elts, count * size);
102 #else
103 bcopy (src_elts, dst_elts, count * size);
104 #endif
106 else
108 jobject *src_elts = elements ((jobjectArray) src_a) + src_offset;
109 jobject *dst_elts = elements ((jobjectArray) dst_a) + dst_offset;
111 for (int i = 0; i < count; ++i)
113 if (*src_elts
114 && ! dst_comp->isAssignableFrom((*src_elts)->getClass()))
115 throw new ArrayStoreException;
116 *dst_elts++ = *src_elts++;
121 jlong
122 java::lang::System::currentTimeMillis (void)
124 return _Jv_platform_gettimeofday ();
127 jint
128 java::lang::System::identityHashCode (jobject obj)
130 return _Jv_HashCode (obj);
133 jboolean
134 java::lang::System::isWordsBigEndian (void)
136 union
138 long lval;
139 char cval;
140 } u;
142 u.lval = 1;
143 return u.cval == 0;