natSystem.cc 4.0 KB

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