java_nio_MappedByteBufferImpl.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* java_nio_MappedByteBufferImpl.c - Native methods for MappedByteBufferImpl
  2. Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include <config.h>
  32. #include <errno.h>
  33. #include <jni.h>
  34. #include <jcl.h>
  35. #include "java_nio_MappedByteBufferImpl.h"
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif /* HAVE_UNISTD_H */
  41. #ifdef HAVE_SYS_MMAN_H
  42. #include <sys/mman.h>
  43. #endif /* HAVE_SYS_MMAN_H */
  44. #define IO_EXCEPTION "java/io/IOException"
  45. /* FIXME these are defined in gnu_java_nio_channels_FileChannelImpl
  46. too; should be someplace common. */
  47. #define ALIGN_DOWN(p,s) ((jpointer)(p) - ((jpointer)(p) % (s)))
  48. #define ALIGN_UP(p,s) ((jpointer)(p) + ((s) - ((jpointer)(p) % (s))))
  49. /**
  50. * Returns the memory page size of this platform.
  51. *
  52. * \return The page size.
  53. */
  54. static long
  55. get_pagesize (void)
  56. {
  57. /* FIXME can we just try HAVE_SYSCONF? */
  58. #if defined(HAVE_GETPAGESIZE)
  59. return getpagesize ();
  60. #elif defined (HAVE_SYSCONF)
  61. return sysconf (_SC_PAGESIZE);
  62. #endif /* HAVE_GETPAGESIZE / HAVE_SYSCONF */
  63. }
  64. /**
  65. * Retrieve the 'address' and 'cap' (the mapped size) fields of this
  66. * buffer.
  67. *
  68. * This function will align the address down to the nearest page
  69. * boundary, and the size up to the nearest page boundary. Thus, it is
  70. * safe to use these values in 'mman' functions.
  71. *
  72. * \param env The JNI environment pointer.
  73. * \param this The MappedByteBufferImpl instance.
  74. * \param address A pointer to where the actual pointer should be
  75. * stored.
  76. * \param size A pointer to where the mapped region's size should be
  77. * stored
  78. */
  79. static void
  80. get_raw_values (JNIEnv *env, jobject this, void **address, size_t *size)
  81. {
  82. const long pagesize = get_pagesize ();
  83. jfieldID MappedByteBufferImpl_address;
  84. jfieldID MappedByteBufferImpl_size;
  85. jobject MappedByteBufferImpl_address_value = NULL;
  86. *address = NULL;
  87. /* 'address' is declared in java.nio.Buffer */
  88. MappedByteBufferImpl_address
  89. = (*env)->GetFieldID (env, (*env)->GetObjectClass (env, this),
  90. "address", "Lgnu/classpath/Pointer;");
  91. /* 'cap' -- likewise, the capacity */
  92. MappedByteBufferImpl_size
  93. = (*env)->GetFieldID (env, (*env)->GetObjectClass (env, this),
  94. "cap", "I");
  95. if (MappedByteBufferImpl_address != NULL)
  96. {
  97. MappedByteBufferImpl_address_value =
  98. (*env)->GetObjectField (env, this, MappedByteBufferImpl_address);
  99. }
  100. if ((*env)->ExceptionOccurred (env))
  101. return;
  102. if (MappedByteBufferImpl_address_value == NULL)
  103. {
  104. JCL_ThrowException (env, "java/lang/NullPointerException",
  105. "mapped address is NULL");
  106. return;
  107. }
  108. *address = (void *)
  109. ALIGN_DOWN (JCL_GetRawData (env, MappedByteBufferImpl_address_value), pagesize);
  110. *size = (size_t)
  111. ALIGN_UP ((*env)->GetIntField (env, this, MappedByteBufferImpl_size),
  112. pagesize);
  113. }
  114. JNIEXPORT void JNICALL
  115. Java_java_nio_MappedByteBufferImpl_unmapImpl (JNIEnv *env, jobject this)
  116. {
  117. #ifdef HAVE_MUNMAP
  118. void *address;
  119. size_t size;
  120. get_raw_values (env, this, &address, &size);
  121. if (address == NULL)
  122. return;
  123. if (munmap (address, size) != 0)
  124. {
  125. JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
  126. return;
  127. }
  128. #else
  129. JCL_ThrowException (env, IO_EXCEPTION,
  130. "unmapping files not implemented");
  131. #endif /* HAVE_MUNMAP */
  132. }
  133. JNIEXPORT jboolean JNICALL
  134. Java_java_nio_MappedByteBufferImpl_isLoadedImpl (JNIEnv * env, jobject this)
  135. {
  136. #ifdef HAVE_MINCORE
  137. void *address;
  138. size_t size;
  139. char *vec;
  140. size_t count, i;
  141. const long pagesize = get_pagesize ();
  142. /*
  143. * FIXME on Darwin this does not work if the mapped region is
  144. * exactly one page long; i.e., 'mincore' tells us it isn't loaded.
  145. */
  146. get_raw_values (env, this, &address, &size);
  147. if (address == NULL)
  148. return JNI_FALSE;
  149. count = (size_t) ((size + pagesize - 1) / pagesize);
  150. vec = (char *) malloc (count * sizeof (unsigned char));
  151. /*
  152. * Darwin (and BSD?) define argument 3 of 'mincore' to be 'char *',
  153. * while GNU libc defines it to be 'unsigned char *'. Casting the
  154. * argument to 'void *' fixes this, but not with C++. So you might
  155. * be SOL if you compile this with g++ (!) on GNU with -Werror.
  156. */
  157. #ifdef __cplusplus
  158. if (mincore (address, size, vec) != 0)
  159. #else
  160. if (mincore (address, size, (void *) vec) != 0)
  161. #endif /* __cplusplus */
  162. {
  163. free (vec);
  164. JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
  165. return JNI_FALSE;
  166. }
  167. for (i = 0; i < count; i++)
  168. {
  169. if ((vec[i] & 1) == 0)
  170. return JNI_FALSE;
  171. }
  172. return JNI_TRUE;
  173. #else
  174. return JNI_FALSE;
  175. #endif
  176. }
  177. JNIEXPORT void JNICALL
  178. Java_java_nio_MappedByteBufferImpl_loadImpl (JNIEnv *env, jobject this)
  179. {
  180. #ifdef HAVE_MADVISE
  181. void *address;
  182. size_t size;
  183. get_raw_values (env, this, &address, &size);
  184. if (address == NULL)
  185. return;
  186. madvise (address, size, MADV_WILLNEED);
  187. #else
  188. JCL_ThrowException (env, IO_EXCEPTION,
  189. "forcing mapped files into core not implemented");
  190. #endif /* HAVE_MADVISE */
  191. }
  192. JNIEXPORT void JNICALL
  193. Java_java_nio_MappedByteBufferImpl_forceImpl (JNIEnv *env, jobject this)
  194. {
  195. #ifdef HAVE_MSYNC
  196. void *address;
  197. size_t size;
  198. get_raw_values (env, this, &address, &size);
  199. if (address == NULL)
  200. return;
  201. /* FIXME: is using MS_SYNC ok? Should we use MS_INVALIDATE? */
  202. if (msync (address, size, MS_SYNC) != 0)
  203. {
  204. JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
  205. }
  206. #else
  207. JCL_ThrowException (env, IO_EXCEPTION,
  208. "forcing mapped files to disk not implemented");
  209. #endif /* HAVE_MSYNC */
  210. }