1 /* java_nio_MappedByteBufferImpl.c - Native methods for MappedByteBufferImpl
2 Copyright (C) 2004, 2005, 2006 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
41 #include <jni.h>
42 #include <jcl.h>
43
44 #include "java_nio_MappedByteBufferImpl.h"
45
46 #include <string.h>
47 #include <stdlib.h>
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif /* HAVE_UNISTD_H */
51 #ifdef HAVE_SYS_MMAN_H
52 #include <sys/mman.h>
53 #endif /* HAVE_SYS_MMAN_H */
54
55 #define IO_EXCEPTION "java/io/IOException"
56
57 /* FIXME these are defined in gnu_java_nio_channels_FileChannelImpl
58 too; should be someplace common. */
59 #define ALIGN_DOWN(p,s) ((jpointer)(p) - ((jpointer)(p) % (s)))
60 #define ALIGN_UP(p,s) ((jpointer)(p) + ((s) - ((jpointer)(p) % (s))))
61
62 /**
63 * Returns the memory page size of this platform.
64 *
65 * \return The page size.
66 */
67 static long
get_pagesize(void)68 get_pagesize (void)
69 {
70 /* FIXME can we just try HAVE_SYSCONF? */
71 #if defined(HAVE_GETPAGESIZE)
72 return getpagesize ();
73 #elif defined (HAVE_SYSCONF)
74 return sysconf (_SC_PAGESIZE);
75 #endif /* HAVE_GETPAGESIZE / HAVE_SYSCONF */
76 }
77
78 /**
79 * Retrieve the 'address' and 'cap' (the mapped size) fields of this
80 * buffer.
81 *
82 * This function will align the address down to the nearest page
83 * boundary, and the size up to the nearest page boundary. Thus, it is
84 * safe to use these values in 'mman' functions.
85 *
86 * \param env The JNI environment pointer.
87 * \param this The MappedByteBufferImpl instance.
88 * \param address A pointer to where the actual pointer should be
89 * stored.
90 * \param size A pointer to where the mapped region's size should be
91 * stored
92 */
93 static void
get_raw_values(JNIEnv * env,jobject this,void ** address,size_t * size)94 get_raw_values (JNIEnv *env, jobject this, void **address, size_t *size)
95 {
96 const long pagesize = get_pagesize ();
97 jfieldID MappedByteBufferImpl_address;
98 jfieldID MappedByteBufferImpl_size;
99 jobject MappedByteBufferImpl_address_value = NULL;
100
101 *address = NULL;
102 /* 'address' is declared in java.nio.Buffer */
103 MappedByteBufferImpl_address
104 = (*env)->GetFieldID (env, (*env)->GetObjectClass (env, this),
105 "address", "Lgnu/classpath/Pointer;");
106 /* 'cap' -- likewise, the capacity */
107 MappedByteBufferImpl_size
108 = (*env)->GetFieldID (env, (*env)->GetObjectClass (env, this),
109 "cap", "I");
110 if (MappedByteBufferImpl_address != NULL)
111 {
112 MappedByteBufferImpl_address_value =
113 (*env)->GetObjectField (env, this, MappedByteBufferImpl_address);
114 }
115 if ((*env)->ExceptionOccurred (env))
116 return;
117 if (MappedByteBufferImpl_address_value == NULL)
118 {
119 JCL_ThrowException (env, "java/lang/NullPointerException",
120 "mapped address is NULL");
121 return;
122 }
123
124 *address = (void *)
125 ALIGN_DOWN (JCL_GetRawData (env, MappedByteBufferImpl_address_value), pagesize);
126 *size = (size_t)
127 ALIGN_UP ((*env)->GetIntField (env, this, MappedByteBufferImpl_size),
128 pagesize);
129 }
130
131 JNIEXPORT void JNICALL
Java_java_nio_MappedByteBufferImpl_unmapImpl(JNIEnv * env,jobject this)132 Java_java_nio_MappedByteBufferImpl_unmapImpl (JNIEnv *env, jobject this)
133 {
134 #ifdef HAVE_MUNMAP
135 void *address;
136 size_t size;
137
138 get_raw_values (env, this, &address, &size);
139
140 if (address == NULL)
141 return;
142
143 if (munmap (address, size) != 0)
144 {
145 JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
146 return;
147 }
148 #else
149 JCL_ThrowException (env, IO_EXCEPTION,
150 "unmapping files not implemented");
151 #endif /* HAVE_MUNMAP */
152 }
153
154 JNIEXPORT jboolean JNICALL
Java_java_nio_MappedByteBufferImpl_isLoadedImpl(JNIEnv * env,jobject this)155 Java_java_nio_MappedByteBufferImpl_isLoadedImpl (JNIEnv * env, jobject this)
156 {
157 #ifdef HAVE_MINCORE
158 void *address;
159 size_t size;
160 char *vec;
161 size_t count, i;
162 const long pagesize = get_pagesize ();
163
164 /*
165 * FIXME on Darwin this does not work if the mapped region is
166 * exactly one page long; i.e., 'mincore' tells us it isn't loaded.
167 */
168 get_raw_values (env, this, &address, &size);
169 if (address == NULL)
170 return JNI_FALSE;
171 count = (size_t) ((size + pagesize - 1) / pagesize);
172 vec = (char *) malloc (count * sizeof (unsigned char));
173
174 /*
175 * Darwin (and BSD?) define argument 3 of 'mincore' to be 'char *',
176 * while GNU libc defines it to be 'unsigned char *'. Casting the
177 * argument to 'void *' fixes this, but not with C++. So you might
178 * be SOL if you compile this with g++ (!) on GNU with -Werror.
179 */
180 #ifdef __cplusplus
181 if (mincore (address, size, vec) != 0)
182 #else
183 if (mincore (address, size, (void *) vec) != 0)
184 #endif /* __cplusplus */
185 {
186 free (vec);
187 JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
188 return JNI_FALSE;
189 }
190
191 for (i = 0; i < count; i++)
192 {
193 if ((vec[i] & 1) == 0)
194 return JNI_FALSE;
195 }
196 return JNI_TRUE;
197 #else
198 return JNI_FALSE;
199 #endif
200 }
201
202 JNIEXPORT void JNICALL
Java_java_nio_MappedByteBufferImpl_loadImpl(JNIEnv * env,jobject this)203 Java_java_nio_MappedByteBufferImpl_loadImpl (JNIEnv *env, jobject this)
204 {
205 #ifdef HAVE_MADVISE
206 void *address;
207 size_t size;
208
209 get_raw_values (env, this, &address, &size);
210 if (address == NULL)
211 return;
212
213 madvise (address, size, MADV_WILLNEED);
214 #else
215 JCL_ThrowException (env, IO_EXCEPTION,
216 "forcing mapped files into core not implemented");
217 #endif /* HAVE_MADVISE */
218 }
219
220 JNIEXPORT void JNICALL
Java_java_nio_MappedByteBufferImpl_forceImpl(JNIEnv * env,jobject this)221 Java_java_nio_MappedByteBufferImpl_forceImpl (JNIEnv *env, jobject this)
222 {
223 #ifdef HAVE_MSYNC
224 void *address;
225 size_t size;
226
227 get_raw_values (env, this, &address, &size);
228
229 if (address == NULL)
230 return;
231
232 /* FIXME: is using MS_SYNC ok? Should we use MS_INVALIDATE? */
233 if (msync (address, size, MS_SYNC) != 0)
234 {
235 JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
236 }
237 #else
238 JCL_ThrowException (env, IO_EXCEPTION,
239 "forcing mapped files to disk not implemented");
240 #endif /* HAVE_MSYNC */
241 }
242