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