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