1 | // natSystem.cc - Native code implementing System class.
|
---|
2 |
|
---|
3 | /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
|
---|
4 |
|
---|
5 | This file is part of libgcj.
|
---|
6 |
|
---|
7 | This software is copyrighted work licensed under the terms of the
|
---|
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
9 | details. */
|
---|
10 |
|
---|
11 | #include <config.h>
|
---|
12 | #include <platform.h>
|
---|
13 |
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <string.h>
|
---|
16 | #include <stdlib.h>
|
---|
17 |
|
---|
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>
|
---|
27 |
|
---|
28 | |
---|
29 |
|
---|
30 |
|
---|
31 | void
|
---|
32 | java::lang::System::setErr0 (java::io::PrintStream *newErr)
|
---|
33 | {
|
---|
34 | err = newErr;
|
---|
35 | }
|
---|
36 |
|
---|
37 | void
|
---|
38 | java::lang::System::setIn0 (java::io::InputStream *newIn)
|
---|
39 | {
|
---|
40 | in = newIn;
|
---|
41 | }
|
---|
42 |
|
---|
43 | void
|
---|
44 | java::lang::System::setOut0 (java::io::PrintStream *newOut)
|
---|
45 | {
|
---|
46 | out = newOut;
|
---|
47 | }
|
---|
48 |
|
---|
49 | void
|
---|
50 | java::lang::System::arraycopy (jobject src, jint src_offset,
|
---|
51 | jobject dst, jint dst_offset,
|
---|
52 | jint count)
|
---|
53 | {
|
---|
54 | if (! src || ! dst)
|
---|
55 | throw new NullPointerException;
|
---|
56 |
|
---|
57 | jclass src_c = src->getClass();
|
---|
58 | jclass dst_c = dst->getClass();
|
---|
59 | jclass src_comp = src_c->getComponentType();
|
---|
60 | jclass dst_comp = dst_c->getComponentType();
|
---|
61 |
|
---|
62 | if (! src_c->isArray() || ! dst_c->isArray()
|
---|
63 | || src_comp->isPrimitive() != dst_comp->isPrimitive()
|
---|
64 | || (src_comp->isPrimitive() && src_comp != dst_comp))
|
---|
65 | throw new ArrayStoreException;
|
---|
66 |
|
---|
67 | __JArray *src_a = (__JArray *) src;
|
---|
68 | __JArray *dst_a = (__JArray *) dst;
|
---|
69 | if (src_offset < 0 || dst_offset < 0 || count < 0
|
---|
70 | || src_offset + count > src_a->length
|
---|
71 | || dst_offset + count > dst_a->length)
|
---|
72 | throw new ArrayIndexOutOfBoundsException;
|
---|
73 |
|
---|
74 | // Do-nothing cases.
|
---|
75 | if ((src == dst && src_offset == dst_offset)
|
---|
76 | || ! count)
|
---|
77 | return;
|
---|
78 |
|
---|
79 | // If both are primitive, we can optimize trivially. If DST
|
---|
80 | // components are always assignable from SRC components, then we
|
---|
81 | // will never need to raise an error, and thus can do the
|
---|
82 | // optimization. If source and destinations are the same, then we
|
---|
83 | // know that the assignability premise always holds.
|
---|
84 | const bool prim = src_comp->isPrimitive();
|
---|
85 | if (prim || dst_comp->isAssignableFrom(src_comp) || src == dst)
|
---|
86 | {
|
---|
87 | const size_t size = (prim ? src_comp->size()
|
---|
88 | : sizeof elements((jobjectArray)src)[0]);
|
---|
89 |
|
---|
90 | char *src_elts = _Jv_GetArrayElementFromElementType (src, src_comp);
|
---|
91 | src_elts += size * src_offset;
|
---|
92 |
|
---|
93 | char *dst_elts = _Jv_GetArrayElementFromElementType (dst, dst_comp);
|
---|
94 | dst_elts += size * dst_offset;
|
---|
95 |
|
---|
96 | #if HAVE_MEMMOVE
|
---|
97 | // We don't bother trying memcpy. It can't be worth the cost of
|
---|
98 | // the check.
|
---|
99 | // Don't cast to (void*), as memmove may expect (char*)
|
---|
100 | memmove (dst_elts, src_elts, count * size);
|
---|
101 | #else
|
---|
102 | bcopy (src_elts, dst_elts, count * size);
|
---|
103 | #endif
|
---|
104 | }
|
---|
105 | else
|
---|
106 | {
|
---|
107 | jobject *src_elts = elements ((jobjectArray) src_a) + src_offset;
|
---|
108 | jobject *dst_elts = elements ((jobjectArray) dst_a) + dst_offset;
|
---|
109 |
|
---|
110 | for (int i = 0; i < count; ++i)
|
---|
111 | {
|
---|
112 | if (*src_elts
|
---|
113 | && ! dst_comp->isAssignableFrom((*src_elts)->getClass()))
|
---|
114 | throw new ArrayStoreException;
|
---|
115 | *dst_elts++ = *src_elts++;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | jlong
|
---|
121 | java::lang::System::currentTimeMillis (void)
|
---|
122 | {
|
---|
123 | return _Jv_platform_gettimeofday ();
|
---|
124 | }
|
---|
125 |
|
---|
126 | jint
|
---|
127 | java::lang::System::identityHashCode (jobject obj)
|
---|
128 | {
|
---|
129 | return _Jv_HashCode (obj);
|
---|
130 | }
|
---|
131 |
|
---|
132 | jboolean
|
---|
133 | java::lang::System::isWordsBigEndian (void)
|
---|
134 | {
|
---|
135 | union
|
---|
136 | {
|
---|
137 | long lval;
|
---|
138 | char cval;
|
---|
139 | } u;
|
---|
140 |
|
---|
141 | u.lval = 1;
|
---|
142 | return u.cval == 0;
|
---|
143 | }
|
---|