Merge from mainline
[official-gcc.git] / libjava / classpath / native / jni / java-nio / java_nio.c
bloba25f38a6d2ac7d1df4082b3848c30355e5ec90e1
1 /* java_nio.c - Native methods for gnu.java.nio.FileChannelImpl class
2 Copyright (C) 2002 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 /* do not move; needed here because of some macro definitions */
39 #include <config.h>
41 #ifdef HAVE_MMAP
42 #include <sys/mman.h>
43 #endif
45 #include <jni.h>
46 #include <jcl.h>
48 #include "target_native.h"
49 #ifndef WITHOUT_NETWORK
50 #include "target_native_network.h"
51 #endif /* WITHOUT_NETWORK */
53 #include "java_nio_channels_FileChannelImpl.h"
55 #include "javaio.h"
57 #define NIO_DEBUG(X) /* no debug */
58 //#define NIO_DEBUG(X) X
60 /***************************************
62 * File Channel implementation
64 *************/
67 static char *
68 compare (int i, int lim, char *buffer)
70 sprintf (buffer, "(%d >= %d)", i, lim);
71 return buffer;
74 static inline int
75 convert_Int (int X)
77 unsigned char *a = (unsigned char *) &X;
78 int res =
79 (((int) a[0]) << 24) +
80 (((int) a[1]) << 16) + (((int) a[2]) << 8) + (((int) a[3]) << 0);
81 return res;
84 static inline jlong
85 convert_Long (jlong X)
87 unsigned char *a = (unsigned char *) &X;
88 int res1 =
89 (((int) a[0]) << 24) +
90 (((int) a[1]) << 16) + (((int) a[2]) << 8) + (((int) a[3]) << 0);
91 int res2;
92 a += 4;
93 res2 =
94 (((int) a[0]) << 24) +
95 (((int) a[1]) << 16) + (((int) a[2]) << 8) + (((int) a[3]) << 0);
96 return ((jlong) res1) | ((jlong) res2) << 32LL;
99 static inline short
100 convert_Short (short X)
102 unsigned char *a = (unsigned char *) &X;
103 int res = (((int) a[2]) << 8) + (((int) a[3]) << 0);
104 return res;
106 static inline short
107 convert_Char (short X)
109 unsigned char *a = (unsigned char *) &X;
110 int res = (((int) a[2]) << 8) + (((int) a[3]) << 0);
111 return res;
114 static inline unsigned char
115 convert_Byte (unsigned char X)
117 return X;
120 static inline float
121 convert_Float (float X)
123 return X;
126 static inline double
127 convert_Double (double X)
129 return X;
133 // Note: do to many get()'s on a buffer and you should throw a BufferUnderflowException
134 // Alas, I only found this out during testing....
136 #define READ_WRITE_MMAPED_FILE(TYPE,ELT) \
138 ELT Java_gnu_java_nio_MappedByteFileBuffer_nio_1read_1 ## TYPE ## _1file_1channel(JNIEnv *env, \
139 jclass c, jobject b, \
140 int index, int limit, jlong jaddress) \
142 char *address = *(void **) &jaddress; char buffer[128]; \
143 if (index >= limit) JCL_ThrowException(env, "java/nio/BufferUnderflowException", compare(index,limit, buffer)); \
144 NIO_DEBUG( fprintf(stderr, "READ:index = %d [0]=%c [1]=%c\n", index, address[0],address[1]); ) \
145 address += index; \
146 return convert_ ## TYPE (*(ELT *) address); \
149 void Java_gnu_java_nio_MappedByteFileBuffer_nio_1write_1 ## TYPE ## _1file_1channel(JNIEnv *env, \
150 jclass c, jobject b, \
151 int index, int limit, ELT value, jlong jaddress) \
154 char *address = *(void **) &jaddress; char buffer[128]; \
155 if (index >= limit) JCL_ThrowException(env, "java/nio/BufferUnderflowException", compare(index,limit, buffer)); \
156 NIO_DEBUG( fprintf(stderr, "WRITE:index = %d [0]=%c [1]=%c\n", index, address[0],address[1]); ) \
157 address += index; \
158 *(ELT *) address = value; \
161 ELT Java_gnu_java_nio_MappedByteFileBuffer_nio_1get_1 ## TYPE(JNIEnv *env, jclass c, jobject b, \
162 int index, int limit, jlong jaddress) \
164 fprintf(stderr, "unimplemented\n"); return 0; \
167 void Java_gnu_java_nio_MappedByteFileBuffer_nio_1put_1 ## TYPE(JNIEnv *env, jclass c, jobject b, \
168 int index, int limit, \
169 ELT value, jlong jaddress) \
171 fprintf(stderr, "unimplemented\n"); \
174 READ_WRITE_MMAPED_FILE (Byte, u_int8_t);
175 READ_WRITE_MMAPED_FILE (Char, u_int16_t);
176 READ_WRITE_MMAPED_FILE (Short, u_int16_t);
177 READ_WRITE_MMAPED_FILE (Int, u_int32_t);
178 READ_WRITE_MMAPED_FILE (Long, u_int64_t);
179 READ_WRITE_MMAPED_FILE (Float, float);
180 READ_WRITE_MMAPED_FILE (Double, double);
182 u_int64_t
183 nio_mmap_file (jint fd, jlong pos, jint size, jint jflags)
185 #ifdef HAVE_MMAP
186 u_int64_t ret = 0;
187 void *address;
189 int flags = (jflags != 2) ? MAP_SHARED : MAP_PRIVATE;
190 int prot = PROT_READ;
192 if (jflags == 1)
193 prot |= PROT_WRITE;
195 // fprintf(stderr, "mapping file: %d\n", fd);
197 address = mmap (0, size, prot, flags, fd, pos);
199 if (address == (void *) -1)
201 perror ("mapping file failed");
202 return 0;
205 // fprintf(stderr, "address = %p, fd = %d, pos=%lld, size=%d\n", address, fd, pos, size);
207 *(void **) &ret = address;
209 return ret;
210 #else /* not HAVE_MMAP */
211 return (TARGET_NATIVE_MATH_INT_INT64_CONST_0);
212 #endif /* not HAVE_MMAP */
216 void
217 nio_msync (int fd, jlong jaddress, int size)
219 #ifdef HAVE_MMAP
220 int res;
221 char *address = *(void **) &jaddress;
223 // fprintf(stderr, "synchronizing with file (%p -> %d bytes (%s))\n", address, size, address);
225 res = msync (address, size, MS_SYNC | MS_INVALIDATE);
227 if (res == -1)
229 perror ("synchronize with file failed");
231 #else /* not HAVE_MMAP */
232 #endif /* not HAVE_MMAP */
235 void
236 nio_unmmap_file (int fd, jlong jaddress, int size)
238 #ifdef HAVE_MMAP
239 int res = 0;
240 char *address = *(void **) &jaddress;
242 // nio_msync(fd, jaddress, size);
244 // fprintf(stderr, "unmapping (%p -> %d bytes)\n", address, size);
246 res = munmap (address, size);
247 if (res == -1)
249 perror ("un-mapping file failed");
251 #else /* not HAVE_MMAP */
252 #endif /* not HAVE_MMAP */
256 /***************************************
258 * Socket Channel implementation
260 *************/
262 /*************************************************************************/
265 * Returns a 32 bit Internet address for the passed in InetAddress object
266 * Ronald: This is a verbatim copy from javanet.c.
267 * It's a copy to avoid a link error in orp.
270 static int
271 socket_channel_get_net_addr (JNIEnv * env, jobject addr)
273 #ifndef WITHOUT_NETWORK
274 jclass cls = 0;
275 jmethodID mid;
276 jarray arr = 0;
277 jbyte *octets;
278 int netaddr, len;
281 ("socket_channel_get_net_addr(): Entered socket_channel_get_net_addr\n");
283 /* Call the getAddress method on the object to retrieve the IP address */
284 cls = (*env)->GetObjectClass (env, addr);
285 if (cls == NULL)
286 return (0);
288 mid = (*env)->GetMethodID (env, cls, "getAddress", "()[B");
289 if (mid == NULL)
290 return (0);
292 DBG ("socket_channel_get_net_addr(): Got getAddress method\n");
294 arr = (*env)->CallObjectMethod (env, addr, mid);
295 if (arr == NULL)
296 return (0);
298 DBG ("socket_channel_get_net_addr(): Got the address\n");
300 /* Turn the IP address into a 32 bit Internet address in network byte order */
301 len = (*env)->GetArrayLength (env, arr);
302 if (len != 4)
304 JCL_ThrowException (env, "java/io/IOException",
305 "Internal Error: invalid byte array length");
306 return (0);
308 DBG ("socket_channel_get_net_addr(): Length ok\n");
310 octets = (*env)->GetByteArrayElements (env, arr, 0);
311 if (octets == NULL)
312 return (0);
314 DBG ("socket_channel_get_net_addr(): Grabbed bytes\n");
316 TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT (octets[0],
317 octets[1],
318 octets[2],
319 octets[3], netaddr);
321 (*env)->ReleaseByteArrayElements (env, arr, octets, 0);
322 DBG ("socket_channel_get_net_addr(): Done getting addr\n");
324 return netaddr;
325 #else /* not WITHOUT_NETWORK */
326 return (0);
327 #endif /* not WITHOUT_NETWORK */