java_nio_VMDirectByteBuffer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* java_nio_VMDirectByteBuffer.c - Native methods for VMDirectByteBuffer
  2. Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include <config.h>
  32. #include <errno.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <jni.h>
  36. #include <jcl.h>
  37. #include "java_nio_VMDirectByteBuffer.h"
  38. JNIEXPORT jobject JNICALL
  39. Java_java_nio_VMDirectByteBuffer_allocate
  40. (JNIEnv * env, jclass clazz __attribute__ ((__unused__)), jint capacity)
  41. {
  42. void *buffer;
  43. if (capacity < 0)
  44. {
  45. JCL_ThrowException (env, "java/lang/IllegalArgumentException",
  46. "negative capacity");
  47. return 0;
  48. }
  49. buffer = malloc (capacity);
  50. if (buffer == NULL)
  51. {
  52. JCL_ThrowException (env, "java/lang/OutOfMemoryError",
  53. "unable to allocate memory for direct byte buffer");
  54. return 0;
  55. }
  56. memset (buffer, 0, capacity);
  57. return JCL_NewRawDataObject (env, buffer);
  58. }
  59. JNIEXPORT void JNICALL
  60. Java_java_nio_VMDirectByteBuffer_free
  61. (JNIEnv * env, jclass clazz __attribute__ ((__unused__)), jobject address)
  62. {
  63. free (JCL_GetRawData (env, address));
  64. }
  65. JNIEXPORT jbyte JNICALL
  66. Java_java_nio_VMDirectByteBuffer_get__Lgnu_classpath_Pointer_2I
  67. (JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
  68. jobject address, jint index)
  69. {
  70. return ((jbyte *) JCL_GetRawData (env, address))[index];
  71. }
  72. JNIEXPORT void JNICALL
  73. Java_java_nio_VMDirectByteBuffer_put__Lgnu_classpath_Pointer_2IB
  74. (JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
  75. jobject address, jint index, jbyte value)
  76. {
  77. jbyte *pointer = (jbyte *) JCL_GetRawData (env, address) + index;
  78. *pointer = value;
  79. }
  80. JNIEXPORT void JNICALL
  81. Java_java_nio_VMDirectByteBuffer_get__Lgnu_classpath_Pointer_2I_3BII
  82. (JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
  83. jobject address, jint index, jbyteArray dst, jint dst_offset, jint dst_len)
  84. {
  85. jbyte *src = (jbyte *) JCL_GetRawData (env, address) + index;
  86. jbyte *_dst = (*env)->GetByteArrayElements (env, dst, NULL);
  87. memcpy (_dst + dst_offset, src, dst_len);
  88. (*env)->ReleaseByteArrayElements (env, dst, _dst, 0);
  89. }
  90. JNIEXPORT void JNICALL
  91. Java_java_nio_VMDirectByteBuffer_put__Lgnu_classpath_Pointer_2I_3BII
  92. (JNIEnv *env, jclass clazz __attribute__ ((__unused__)),
  93. jobject address, jint index, jbyteArray src, jint src_offset, jint src_len)
  94. {
  95. jbyte *_src = (*env)->GetByteArrayElements (env, src, NULL);
  96. jbyte *dst = (jbyte *)JCL_GetRawData (env, address);
  97. memcpy (dst + index, _src + src_offset, src_len);
  98. (*env)->ReleaseByteArrayElements (env, src, _src, 0);
  99. }
  100. JNIEXPORT void JNICALL
  101. Java_java_nio_VMDirectByteBuffer_shiftDown
  102. (JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
  103. jobject address, jint dst_offset, jint src_offset, jint count)
  104. {
  105. jbyte *dst = (jbyte *) JCL_GetRawData (env, address) + dst_offset;
  106. jbyte *src = (jbyte *) JCL_GetRawData (env, address) + src_offset;
  107. memmove (dst, src, count);
  108. }
  109. JNIEXPORT jobject JNICALL
  110. Java_java_nio_VMDirectByteBuffer_adjustAddress
  111. (JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
  112. jobject address, jint offset)
  113. {
  114. return JCL_NewRawDataObject (env, (jbyte *) JCL_GetRawData (env, address) + offset);
  115. }