natSystem.cc 4 KB
Newer Older
Tom Tromey committed
1 2
// natSystem.cc - Native code implementing System class.

3
/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2006 Free Software Foundation
Tom Tromey committed
4 5 6 7 8 9 10 11

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <config.h>
12
#include <platform.h>
Tom Tromey committed
13

14
#include <stdio.h>
Tom Tromey committed
15 16 17
#include <string.h>
#include <stdlib.h>

Tom Tromey committed
18
#include <gcj/cni.h>
Tom Tromey committed
19 20 21 22 23 24 25 26 27 28 29 30
#include <jvm.h>
#include <java/lang/System.h>
#include <java/lang/Class.h>
#include <java/lang/ArrayStoreException.h>
#include <java/lang/ArrayIndexOutOfBoundsException.h>
#include <java/lang/NullPointerException.h>
#include <java/io/PrintStream.h>
#include <java/io/InputStream.h>



void
31
java::lang::System::setErr0 (java::io::PrintStream *newErr)
Tom Tromey committed
32 33 34 35 36
{
  err = newErr;
}

void
37
java::lang::System::setIn0 (java::io::InputStream *newIn)
Tom Tromey committed
38 39 40 41 42
{
  in = newIn;
}

void
43
java::lang::System::setOut0 (java::io::PrintStream *newOut)
Tom Tromey committed
44 45 46 47 48 49 50 51 52 53
{
  out = newOut;
}

void
java::lang::System::arraycopy (jobject src, jint src_offset,
			       jobject dst, jint dst_offset,
			       jint count)
{
  if (! src || ! dst)
54
    throw new NullPointerException;
Tom Tromey committed
55 56 57 58 59 60 61 62 63

  jclass src_c = src->getClass();
  jclass dst_c = dst->getClass();
  jclass src_comp = src_c->getComponentType();
  jclass dst_comp = dst_c->getComponentType();

  if (! src_c->isArray() || ! dst_c->isArray()
      || src_comp->isPrimitive() != dst_comp->isPrimitive()
      || (src_comp->isPrimitive() && src_comp != dst_comp))
64
    throw new ArrayStoreException;
Tom Tromey committed
65 66 67 68

  __JArray *src_a = (__JArray *) src;
  __JArray *dst_a = (__JArray *) dst;
  if (src_offset < 0 || dst_offset < 0 || count < 0
69 70 71 72
      || (unsigned jint) src_offset > (unsigned jint) src_a->length
      || (unsigned jint) (src_offset + count) > (unsigned jint) src_a->length
      || (unsigned jint) dst_offset > (unsigned jint) dst_a->length
      || (unsigned jint) (dst_offset + count) > (unsigned jint) dst_a->length)
73
    throw new ArrayIndexOutOfBoundsException;
Tom Tromey committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87

  // Do-nothing cases.
  if ((src == dst && src_offset == dst_offset)
      || ! count)
    return;

  // If both are primitive, we can optimize trivially.  If DST
  // components are always assignable from SRC components, then we
  // will never need to raise an error, and thus can do the
  // optimization.  If source and destinations are the same, then we
  // know that the assignability premise always holds.
  const bool prim = src_comp->isPrimitive();
  if (prim || dst_comp->isAssignableFrom(src_comp) || src == dst)
    {
88 89 90
      const size_t size = (prim ? src_comp->size()
			   : sizeof elements((jobjectArray)src)[0]);

91
      char *src_elts = _Jv_GetArrayElementFromElementType (src, src_comp);
92 93
      src_elts += size * src_offset;

94
      char *dst_elts = _Jv_GetArrayElementFromElementType (dst, dst_comp);
95 96
      dst_elts += size * dst_offset;

97
#if HAVE_MEMMOVE
Tom Tromey committed
98 99
      // We don't bother trying memcpy.  It can't be worth the cost of
      // the check.
100 101 102 103 104
      // Don't cast to (void*), as memmove may expect (char*)
      memmove (dst_elts, src_elts, count * size);
#else
      bcopy (src_elts, dst_elts, count * size);
#endif
Tom Tromey committed
105 106 107 108 109 110 111 112 113 114
    }
  else
    {
      jobject *src_elts = elements ((jobjectArray) src_a) + src_offset;
      jobject *dst_elts = elements ((jobjectArray) dst_a) + dst_offset;

      for (int i = 0; i < count; ++i)
	{
	  if (*src_elts
	      && ! dst_comp->isAssignableFrom((*src_elts)->getClass()))
115
	    throw new ArrayStoreException;
Tom Tromey committed
116 117 118 119 120 121 122 123
	  *dst_elts++ = *src_elts++;
	}
    }
}

jlong
java::lang::System::currentTimeMillis (void)
{
124
  return _Jv_platform_gettimeofday ();
Tom Tromey committed
125 126
}

127 128 129 130 131 132
jlong
java::lang::System::nanoTime ()
{
  return _Jv_platform_nanotime ();
}

Tom Tromey committed
133 134 135 136 137 138
jint
java::lang::System::identityHashCode (jobject obj)
{
  return _Jv_HashCode (obj);
}

139 140 141 142 143 144 145 146 147 148
jstring
java::lang::System::getenv0 (jstring name)
{
  jint len = _Jv_GetStringUTFLength (name);
  char buf[len + 1];
  jsize total = JvGetStringUTFRegion (name, 0, name->length(), buf);
  buf[total] = '\0';
  const char *value = ::getenv (buf);
  if (value == NULL)
    return NULL;
149
  return JvNewStringUTF (value);
150
}