1 |
774 |
jeremybenn |
/* java_nio_VMDirectByteBuffer.c - Native methods for VMDirectByteBuffer
|
2 |
|
|
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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.
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
#include <config.h>
|
39 |
|
|
#include <errno.h>
|
40 |
|
|
#include <stdlib.h>
|
41 |
|
|
#include <string.h>
|
42 |
|
|
|
43 |
|
|
#include <jni.h>
|
44 |
|
|
#include <jcl.h>
|
45 |
|
|
|
46 |
|
|
#include "java_nio_VMDirectByteBuffer.h"
|
47 |
|
|
|
48 |
|
|
JNIEXPORT jobject JNICALL
|
49 |
|
|
Java_java_nio_VMDirectByteBuffer_allocate
|
50 |
|
|
(JNIEnv * env, jclass clazz __attribute__ ((__unused__)), jint capacity)
|
51 |
|
|
{
|
52 |
|
|
void *buffer;
|
53 |
|
|
|
54 |
|
|
buffer = malloc (capacity);
|
55 |
|
|
|
56 |
|
|
if (buffer == NULL)
|
57 |
|
|
{
|
58 |
|
|
JCL_ThrowException (env, "java/lang/OutOfMemoryError",
|
59 |
|
|
"unable to allocate memory for direct byte buffer");
|
60 |
|
|
return 0;
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
memset (buffer, 0, capacity);
|
64 |
|
|
|
65 |
|
|
return JCL_NewRawDataObject (env, buffer);
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
JNIEXPORT void JNICALL
|
69 |
|
|
Java_java_nio_VMDirectByteBuffer_free
|
70 |
|
|
(JNIEnv * env, jclass clazz __attribute__ ((__unused__)), jobject address)
|
71 |
|
|
{
|
72 |
|
|
free (JCL_GetRawData (env, address));
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
JNIEXPORT jbyte JNICALL
|
76 |
|
|
Java_java_nio_VMDirectByteBuffer_get__Lgnu_classpath_Pointer_2I
|
77 |
|
|
(JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
|
78 |
|
|
jobject address, jint index)
|
79 |
|
|
{
|
80 |
|
|
return ((jbyte *) JCL_GetRawData (env, address))[index];
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
JNIEXPORT void JNICALL
|
84 |
|
|
Java_java_nio_VMDirectByteBuffer_put__Lgnu_classpath_Pointer_2IB
|
85 |
|
|
(JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
|
86 |
|
|
jobject address, jint index, jbyte value)
|
87 |
|
|
{
|
88 |
|
|
jbyte *pointer = (jbyte *) JCL_GetRawData (env, address) + index;
|
89 |
|
|
*pointer = value;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
JNIEXPORT void JNICALL
|
93 |
|
|
Java_java_nio_VMDirectByteBuffer_get__Lgnu_classpath_Pointer_2I_3BII
|
94 |
|
|
(JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
|
95 |
|
|
jobject address, jint index, jbyteArray dst, jint dst_offset, jint dst_len)
|
96 |
|
|
{
|
97 |
|
|
jbyte *src = (jbyte *) JCL_GetRawData (env, address) + index;
|
98 |
|
|
jbyte *_dst = (*env)->GetByteArrayElements (env, dst, NULL);
|
99 |
|
|
memcpy (_dst + dst_offset, src, dst_len);
|
100 |
|
|
(*env)->ReleaseByteArrayElements (env, dst, _dst, 0);
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
JNIEXPORT void JNICALL
|
104 |
|
|
Java_java_nio_VMDirectByteBuffer_put__Lgnu_classpath_Pointer_2I_3BII
|
105 |
|
|
(JNIEnv *env, jclass clazz __attribute__ ((__unused__)),
|
106 |
|
|
jobject address, jint index, jbyteArray src, jint src_offset, jint src_len)
|
107 |
|
|
{
|
108 |
|
|
jbyte *_src = (*env)->GetByteArrayElements (env, src, NULL);
|
109 |
|
|
jbyte *dst = (jbyte *)JCL_GetRawData (env, address);
|
110 |
|
|
memcpy (dst + index, _src + src_offset, src_len);
|
111 |
|
|
(*env)->ReleaseByteArrayElements (env, src, _src, 0);
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
JNIEXPORT void JNICALL
|
115 |
|
|
Java_java_nio_VMDirectByteBuffer_shiftDown
|
116 |
|
|
(JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
|
117 |
|
|
jobject address, jint dst_offset, jint src_offset, jint count)
|
118 |
|
|
{
|
119 |
|
|
jbyte *dst = (jbyte *) JCL_GetRawData (env, address) + dst_offset;
|
120 |
|
|
jbyte *src = (jbyte *) JCL_GetRawData (env, address) + src_offset;
|
121 |
|
|
memmove (dst, src, count);
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
JNIEXPORT jobject JNICALL
|
125 |
|
|
Java_java_nio_VMDirectByteBuffer_adjustAddress
|
126 |
|
|
(JNIEnv * env, jclass clazz __attribute__ ((__unused__)),
|
127 |
|
|
jobject address, jint offset)
|
128 |
|
|
{
|
129 |
|
|
return JCL_NewRawDataObject (env, (jbyte *) JCL_GetRawData (env, address) + offset);
|
130 |
|
|
}
|