The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OSMemory" |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 18 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 19 | #include "JNIHelp.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 20 | #include "utils/misc.h" |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 21 | #include "utils/Log.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | #include <sys/mman.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <errno.h> |
| 26 | |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 27 | /* |
| 28 | * Cached dalvik.system.VMRuntime pieces. |
| 29 | */ |
| 30 | static struct { |
| 31 | jmethodID method_trackExternalAllocation; |
| 32 | jmethodID method_trackExternalFree; |
| 33 | |
| 34 | jobject runtimeInstance; |
| 35 | } gIDCache; |
| 36 | |
Elliott Hughes | 3c6ff81 | 2009-10-22 17:25:01 -0700 | [diff] [blame] | 37 | static const int MMAP_READ_ONLY = 1; |
| 38 | static const int MMAP_READ_WRITE = 2; |
| 39 | static const int MMAP_WRITE_COPY = 4; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 43 | * Method: littleEndian |
| 44 | * Signature: ()Z |
| 45 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 46 | static jboolean harmony_nio_littleEndian(JNIEnv*, jclass) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 47 | long l = 0x01020304; |
| 48 | unsigned char* c = (unsigned char*)&l; |
| 49 | return (*c == 0x04) ? JNI_TRUE : JNI_FALSE; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 54 | * Method: getPointerSizeImpl |
| 55 | * Signature: ()I |
| 56 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 57 | static jint harmony_nio_getPointerSizeImpl(JNIEnv*, jclass) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 58 | return sizeof(void *); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 63 | * Method: mallocImpl |
| 64 | * Signature: (I)I |
| 65 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 66 | static jint harmony_nio_mallocImpl(JNIEnv* _env, jobject, jint size) { |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 67 | jboolean allowed = _env->CallBooleanMethod(gIDCache.runtimeInstance, |
| 68 | gIDCache.method_trackExternalAllocation, (jlong) size); |
| 69 | if (!allowed) { |
| 70 | LOGW("External allocation of %d bytes was rejected\n", size); |
| 71 | jniThrowException(_env, "java/lang/OutOfMemoryError", NULL); |
| 72 | return 0; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 73 | } |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 74 | |
| 75 | LOGV("OSMemory alloc %d\n", size); |
| 76 | void *returnValue = malloc(size + sizeof(jlong)); |
| 77 | if (returnValue == NULL) { |
| 78 | jniThrowException(_env, "java/lang/OutOfMemoryError", NULL); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Tuck a copy of the size at the head of the buffer. We need this |
| 84 | * so harmony_nio_freeImpl() knows how much memory is being freed. |
| 85 | */ |
| 86 | jlong* adjptr = (jlong*) returnValue; |
| 87 | *adjptr++ = size; |
| 88 | return (jint)adjptr; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 93 | * Method: freeImpl |
| 94 | * Signature: (I)V |
| 95 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 96 | static void harmony_nio_freeImpl(JNIEnv* _env, jobject, jint pointer) { |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 97 | jlong* adjptr = (jlong*) pointer; |
| 98 | jint size = *--adjptr; |
| 99 | LOGV("OSMemory free %d\n", size); |
| 100 | _env->CallVoidMethod(gIDCache.runtimeInstance, |
| 101 | gIDCache.method_trackExternalFree, (jlong) size); |
| 102 | free((void *)adjptr); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 107 | * Method: memset |
| 108 | * Signature: (IBJ)V |
| 109 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 110 | static void harmony_nio_memset(JNIEnv*, jobject, jint address, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 111 | jbyte value, jlong length) { |
| 112 | memset ((void *) ((jint) address), (jbyte) value, (jlong) length); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 117 | * Method: memmove |
| 118 | * Signature: (IIJ)V |
| 119 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 120 | static void harmony_nio_memmove(JNIEnv*, jobject, jint destAddress, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 121 | jint srcAddress, jlong length) { |
| 122 | memmove ((void *) ((jint) destAddress), (const void *) ((jint) srcAddress), |
| 123 | (jlong) length); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 128 | * Method: getByteImpl |
| 129 | * Signature: (I)B |
| 130 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 131 | static jbyte harmony_nio_getByteImpl(JNIEnv*, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 132 | jint pointer) { |
| 133 | jbyte returnValue = *((jbyte *)pointer); |
| 134 | return returnValue; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 139 | * Method: getBytesImpl |
| 140 | * Signature: (I[BII)V |
| 141 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 142 | static void harmony_nio_getBytesImpl(JNIEnv* _env, jobject, jint pointer, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 143 | jbyteArray dst, jint offset, jint length) { |
Elliott Hughes | de2d83f | 2009-09-09 15:16:59 -0700 | [diff] [blame] | 144 | jbyte* src = reinterpret_cast<jbyte*>(static_cast<uintptr_t>(pointer)); |
| 145 | _env->SetByteArrayRegion(dst, offset, length, src); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 150 | * Method: putByteImpl |
| 151 | * Signature: (IB)V |
| 152 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 153 | static void harmony_nio_putByteImpl(JNIEnv*, jobject, jint pointer, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 154 | jbyte val) { |
| 155 | *((jbyte *)pointer) = val; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 160 | * Method: putBytesImpl |
| 161 | * Signature: (I[BII)V |
| 162 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 163 | static void harmony_nio_putBytesImpl(JNIEnv* _env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 164 | jint pointer, jbyteArray src, jint offset, jint length) { |
Elliott Hughes | de2d83f | 2009-09-09 15:16:59 -0700 | [diff] [blame] | 165 | jbyte* dst = reinterpret_cast<jbyte*>(static_cast<uintptr_t>(pointer)); |
| 166 | _env->GetByteArrayRegion(src, offset, length, dst); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 170 | swapShorts(jshort *shorts, int count) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 171 | jbyte *src = (jbyte *) shorts; |
| 172 | jbyte *dst = src; |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 173 | for (int i = 0; i < count; ++i) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 174 | jbyte b0 = *src++; |
| 175 | jbyte b1 = *src++; |
| 176 | *dst++ = b1; |
| 177 | *dst++ = b0; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static void |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 182 | swapInts(jint *ints, int count) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 183 | jbyte *src = (jbyte *) ints; |
| 184 | jbyte *dst = src; |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 185 | for (int i = 0; i < count; ++i) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 186 | jbyte b0 = *src++; |
| 187 | jbyte b1 = *src++; |
| 188 | jbyte b2 = *src++; |
| 189 | jbyte b3 = *src++; |
| 190 | *dst++ = b3; |
| 191 | *dst++ = b2; |
| 192 | *dst++ = b1; |
| 193 | *dst++ = b0; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Class: org_apache_harmony_luni_platform_OSMemory |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 199 | * Method: setShortArrayImpl |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 200 | * Signature: (I[SIIZ)V |
| 201 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 202 | static void harmony_nio_setShortArrayImpl(JNIEnv* _env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 203 | jint pointer, jshortArray src, jint offset, jint length, jboolean swap) { |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 204 | jshort* dst = reinterpret_cast<jshort*>(static_cast<uintptr_t>(pointer)); |
| 205 | _env->GetShortArrayRegion(src, offset, length, dst); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 206 | if (swap) { |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 207 | swapShorts(dst, length); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 208 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Class: org_apache_harmony_luni_platform_OSMemory |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 213 | * Method: setIntArrayImpl |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 214 | * Signature: (I[IIIZ)V |
| 215 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 216 | static void harmony_nio_setIntArrayImpl(JNIEnv* _env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 217 | jint pointer, jintArray src, jint offset, jint length, jboolean swap) { |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 218 | jint* dst = reinterpret_cast<jint*>(static_cast<uintptr_t>(pointer)); |
| 219 | _env->GetIntArrayRegion(src, offset, length, dst); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 220 | if (swap) { |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 221 | swapInts(dst, length); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 222 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 227 | * Method: getShortImpl |
| 228 | * Signature: (I)S |
| 229 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 230 | static jshort harmony_nio_getShortImpl(JNIEnv*, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 231 | jint pointer) { |
| 232 | if ((pointer & 0x1) == 0) { |
| 233 | jshort returnValue = *((jshort *)pointer); |
| 234 | return returnValue; |
| 235 | } else { |
| 236 | // Handle unaligned memory access one byte at a time |
| 237 | jshort s; |
| 238 | unsigned char *src = (unsigned char *) pointer; |
| 239 | unsigned char *dst = (unsigned char *) &s; |
| 240 | dst[0] = src[0]; |
| 241 | dst[1] = src[1]; |
| 242 | return s; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 248 | * Method: petShortImpl |
| 249 | * Signature: (IS)V |
| 250 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 251 | static void harmony_nio_putShortImpl(JNIEnv*, jobject, jint pointer, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 252 | jshort value) { |
| 253 | if ((pointer & 0x1) == 0) { |
| 254 | *((jshort *)pointer) = value; |
| 255 | } else { |
| 256 | // Handle unaligned memory access one byte at a time |
| 257 | unsigned char *src = (unsigned char *) &value; |
| 258 | unsigned char *dst = (unsigned char *) pointer; |
| 259 | dst[0] = src[0]; |
| 260 | dst[1] = src[1]; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 266 | * Method: getIntImpl |
| 267 | * Signature: (I)I |
| 268 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 269 | static jint harmony_nio_getIntImpl(JNIEnv*, jobject, jint pointer) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 270 | if ((pointer & 0x3) == 0) { |
| 271 | jint returnValue = *((jint *)pointer); |
| 272 | return returnValue; |
| 273 | } else { |
| 274 | // Handle unaligned memory access one byte at a time |
| 275 | jint i; |
| 276 | unsigned char *src = (unsigned char *) pointer; |
| 277 | unsigned char *dst = (unsigned char *) &i; |
| 278 | dst[0] = src[0]; |
| 279 | dst[1] = src[1]; |
| 280 | dst[2] = src[2]; |
| 281 | dst[3] = src[3]; |
| 282 | return i; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 288 | * Method: putIntImpl |
| 289 | * Signature: (II)V |
| 290 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 291 | static void harmony_nio_putIntImpl(JNIEnv*, jobject, jint pointer, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 292 | jint value) { |
| 293 | if ((pointer & 0x3) == 0) { |
| 294 | *((jint *)pointer) = value; |
| 295 | } else { |
| 296 | // Handle unaligned memory access one byte at a time |
| 297 | unsigned char *src = (unsigned char *) &value; |
| 298 | unsigned char *dst = (unsigned char *) pointer; |
| 299 | dst[0] = src[0]; |
| 300 | dst[1] = src[1]; |
| 301 | dst[2] = src[2]; |
| 302 | dst[3] = src[3]; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 308 | * Method: getLongImpl |
| 309 | * Signature: (I)Ljava/lang/Long; |
| 310 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 311 | static jlong harmony_nio_getLongImpl(JNIEnv*, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 312 | jint pointer) { |
| 313 | if ((pointer & 0x7) == 0) { |
| 314 | jlong returnValue = *((jlong *)pointer); |
| 315 | return returnValue; |
| 316 | } else { |
| 317 | // Handle unaligned memory access one byte at a time |
| 318 | jlong l; |
| 319 | memcpy((void *) &l, (void *) pointer, sizeof(jlong)); |
| 320 | return l; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 326 | * Method: putLongImpl |
| 327 | * Signature: (IJ)V |
| 328 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 329 | static void harmony_nio_putLongImpl(JNIEnv*, jobject, jint pointer, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 330 | jlong value) { |
| 331 | if ((pointer & 0x7) == 0) { |
| 332 | *((jlong *)pointer) = value; |
| 333 | } else { |
| 334 | // Handle unaligned memory access one byte at a time |
| 335 | memcpy((void *) pointer, (void *) &value, sizeof(jlong)); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 341 | * Method: getFloatImpl |
| 342 | * Signature: (I)F |
| 343 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 344 | static jfloat harmony_nio_getFloatImpl(JNIEnv*, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 345 | jint pointer) { |
| 346 | if ((pointer & 0x3) == 0) { |
| 347 | jfloat returnValue = *((jfloat *)pointer); |
| 348 | return returnValue; |
| 349 | } else { |
| 350 | // Handle unaligned memory access one byte at a time |
| 351 | jfloat f; |
| 352 | memcpy((void *) &f, (void *) pointer, sizeof(jfloat)); |
| 353 | return f; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 359 | * Method: setFloatImpl |
| 360 | * Signature: (IF)V |
| 361 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 362 | static void harmony_nio_putFloatImpl(JNIEnv*, jobject, jint pointer, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 363 | jfloat value) { |
| 364 | if ((pointer & 0x3) == 0) { |
| 365 | *((jfloat *)pointer) = value; |
| 366 | } else { |
| 367 | // Handle unaligned memory access one byte at a time |
| 368 | memcpy((void *) pointer, (void *) &value, sizeof(jfloat)); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 374 | * Method: getDoubleImpl |
| 375 | * Signature: (I)D |
| 376 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 377 | static jdouble harmony_nio_getDoubleImpl(JNIEnv*, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 378 | jint pointer) { |
| 379 | if ((pointer & 0x7) == 0) { |
| 380 | jdouble returnValue = *((jdouble *)pointer); |
| 381 | return returnValue; |
| 382 | } else { |
| 383 | // Handle unaligned memory access one byte at a time |
| 384 | jdouble d; |
| 385 | memcpy((void *) &d, (void *) pointer, sizeof(jdouble)); |
| 386 | return d; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 392 | * Method: putDoubleImpl |
| 393 | * Signature: (ID)V |
| 394 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 395 | static void harmony_nio_putDoubleImpl(JNIEnv*, jobject, jint pointer, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 396 | jdouble value) { |
| 397 | if ((pointer & 0x7) == 0) { |
| 398 | *((jdouble *)pointer) = value; |
| 399 | } else { |
| 400 | // Handle unaligned memory access one byte at a time |
| 401 | memcpy((void *) pointer, (void *) &value, sizeof(jdouble)); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 407 | * Method: getAddress |
| 408 | * Signature: (I)I |
| 409 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 410 | static jint harmony_nio_getAddress(JNIEnv*, jobject, jint pointer) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 411 | return (jint) * (int *) pointer; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 416 | * Method: setAddress |
| 417 | * Signature: (II)V |
| 418 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 419 | static void harmony_nio_setAddress(JNIEnv*, jobject, jint pointer, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 420 | jint value) { |
| 421 | *(int *) pointer = (int) value; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 426 | * Method: mmapImpl |
| 427 | * Signature: (IJJI)I |
| 428 | */ |
Elliott Hughes | 3c6ff81 | 2009-10-22 17:25:01 -0700 | [diff] [blame] | 429 | static jint harmony_nio_mmapImpl(JNIEnv* env, jobject, jint fd, |
| 430 | jlong offset, jlong size, jint mapMode) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 431 | int prot, flags; |
Elliott Hughes | 3c6ff81 | 2009-10-22 17:25:01 -0700 | [diff] [blame] | 432 | switch (mapMode) { |
| 433 | case MMAP_READ_ONLY: |
| 434 | prot = PROT_READ; |
| 435 | flags = MAP_SHARED; |
| 436 | break; |
| 437 | case MMAP_READ_WRITE: |
| 438 | prot = PROT_READ|PROT_WRITE; |
| 439 | flags = MAP_SHARED; |
| 440 | break; |
| 441 | case MMAP_WRITE_COPY: |
| 442 | prot = PROT_READ|PROT_WRITE; |
| 443 | flags = MAP_PRIVATE; |
| 444 | break; |
| 445 | default: |
| 446 | jniThrowIOException(env, EINVAL); |
| 447 | LOGE("bad mapMode %i", mapMode); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 448 | return -1; |
| 449 | } |
Elliott Hughes | 3c6ff81 | 2009-10-22 17:25:01 -0700 | [diff] [blame] | 450 | |
| 451 | void* mapAddress = mmap(0, size, prot, flags, fd, offset); |
| 452 | if (mapAddress == MAP_FAILED) { |
| 453 | jniThrowIOException(env, errno); |
| 454 | } |
| 455 | return reinterpret_cast<uintptr_t>(mapAddress); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 460 | * Method: unmapImpl |
| 461 | * Signature: (IJ)V |
| 462 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 463 | static void harmony_nio_unmapImpl(JNIEnv*, jobject, jint address, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 464 | jlong size) { |
| 465 | munmap((void *)address, (size_t)size); |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 470 | * Method: loadImpl |
| 471 | * Signature: (IJ)I |
| 472 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 473 | static jint harmony_nio_loadImpl(JNIEnv*, jobject, jint address, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 474 | jlong size) { |
| 475 | |
| 476 | if(mlock((void *)address, (size_t)size)!=-1) { |
| 477 | if(munlock((void *)address, (size_t)size)!=-1) { |
| 478 | return 0; /* normally */ |
| 479 | } |
| 480 | } |
| 481 | else { |
| 482 | /* according to linux sys call, only root can mlock memory. */ |
| 483 | if(errno == EPERM) { |
| 484 | return 0; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | return -1; |
| 489 | } |
| 490 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 491 | /* |
| 492 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 493 | * Method: isLoadedImpl |
| 494 | * Signature: (IJ)Z |
| 495 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 496 | static jboolean harmony_nio_isLoadedImpl(JNIEnv*, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 497 | jint address, jlong size) { |
| 498 | |
Elliott Hughes | 3c6ff81 | 2009-10-22 17:25:01 -0700 | [diff] [blame] | 499 | static int page_size = getpagesize(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 500 | jboolean result = 0; |
| 501 | jint m_addr = (jint)address; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 502 | |
| 503 | int align_offset = m_addr%page_size;// addr should align with the boundary of a page. |
| 504 | m_addr -= align_offset; |
| 505 | size += align_offset; |
Elliott Hughes | 3c6ff81 | 2009-10-22 17:25:01 -0700 | [diff] [blame] | 506 | int page_count = (size+page_size-1)/page_size; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 507 | |
Elliott Hughes | 3c6ff81 | 2009-10-22 17:25:01 -0700 | [diff] [blame] | 508 | unsigned char* vec = (unsigned char *) malloc(page_count*sizeof(char)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 509 | |
| 510 | if (mincore((void *)m_addr, size, (MINCORE_POINTER_TYPE) vec)==0) { |
| 511 | // or else there is error about the mincore and return false; |
| 512 | int i; |
| 513 | for(i=0 ;i<page_count;i++) { |
| 514 | if(vec[i]!=1) { |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | if(i==page_count) { |
| 519 | result = 1; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | free(vec); |
| 524 | |
| 525 | return result; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Class: org_apache_harmony_luni_platform_OSMemory |
| 530 | * Method: flushImpl |
| 531 | * Signature: (IJ)I |
| 532 | */ |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 533 | static jint harmony_nio_flushImpl(JNIEnv *, jobject, jint address, jlong size) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 534 | return msync((void *)address, size, MS_SYNC); |
| 535 | } |
| 536 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 537 | static JNINativeMethod gMethods[] = { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 538 | { "isLittleEndianImpl", "()Z", (void*) harmony_nio_littleEndian }, |
| 539 | { "getPointerSizeImpl", "()I", (void*) harmony_nio_getPointerSizeImpl }, |
| 540 | { "malloc", "(I)I", (void*) harmony_nio_mallocImpl }, |
| 541 | { "free", "(I)V", (void*) harmony_nio_freeImpl }, |
| 542 | { "memset", "(IBJ)V", (void*) harmony_nio_memset }, |
| 543 | { "memmove", "(IIJ)V", (void*) harmony_nio_memmove }, |
| 544 | { "getByteArray", "(I[BII)V",(void*) harmony_nio_getBytesImpl }, |
| 545 | { "setByteArray", "(I[BII)V",(void*) harmony_nio_putBytesImpl }, |
Elliott Hughes | 558383f | 2009-08-26 09:37:48 -0700 | [diff] [blame] | 546 | { "setShortArray", "(I[SIIZ)V",(void*) harmony_nio_setShortArrayImpl }, |
| 547 | { "setIntArray", "(I[IIIZ)V",(void*) harmony_nio_setIntArrayImpl }, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 548 | { "getByte", "(I)B", (void*) harmony_nio_getByteImpl }, |
| 549 | { "setByte", "(IB)V", (void*) harmony_nio_putByteImpl }, |
| 550 | { "getShort", "(I)S", (void*) harmony_nio_getShortImpl }, |
| 551 | { "setShort", "(IS)V", (void*) harmony_nio_putShortImpl }, |
| 552 | { "getInt", "(I)I", (void*) harmony_nio_getIntImpl }, |
| 553 | { "setInt", "(II)V", (void*) harmony_nio_putIntImpl }, |
| 554 | { "getLong", "(I)J", (void*) harmony_nio_getLongImpl }, |
| 555 | { "setLong", "(IJ)V", (void*) harmony_nio_putLongImpl }, |
| 556 | { "getFloat", "(I)F", (void*) harmony_nio_getFloatImpl }, |
| 557 | { "setFloat", "(IF)V", (void*) harmony_nio_putFloatImpl }, |
| 558 | { "getDouble", "(I)D", (void*) harmony_nio_getDoubleImpl }, |
| 559 | { "setDouble", "(ID)V", (void*) harmony_nio_putDoubleImpl }, |
| 560 | { "getAddress", "(I)I", (void*) harmony_nio_getAddress }, |
| 561 | { "setAddress", "(II)V", (void*) harmony_nio_setAddress }, |
| 562 | { "mmapImpl", "(IJJI)I", (void*) harmony_nio_mmapImpl }, |
| 563 | { "unmapImpl", "(IJ)V", (void*) harmony_nio_unmapImpl }, |
| 564 | { "loadImpl", "(IJ)I", (void*) harmony_nio_loadImpl }, |
| 565 | { "isLoadedImpl", "(IJ)Z", (void*) harmony_nio_isLoadedImpl }, |
| 566 | { "flushImpl", "(IJ)I", (void*) harmony_nio_flushImpl } |
| 567 | }; |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 568 | int register_org_apache_harmony_luni_platform_OSMemory(JNIEnv* env) { |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 569 | /* |
| 570 | * We need to call VMRuntime.trackExternal{Allocation,Free}. Cache |
| 571 | * method IDs and a reference to the singleton. |
| 572 | */ |
| 573 | static const char* kVMRuntimeName = "dalvik/system/VMRuntime"; |
| 574 | jmethodID method_getRuntime; |
| 575 | jclass clazz; |
| 576 | |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 577 | clazz = env->FindClass(kVMRuntimeName); |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 578 | if (clazz == NULL) { |
| 579 | LOGE("Unable to find class %s\n", kVMRuntimeName); |
| 580 | return -1; |
| 581 | } |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 582 | gIDCache.method_trackExternalAllocation = env->GetMethodID(clazz, |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 583 | "trackExternalAllocation", "(J)Z"); |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 584 | gIDCache.method_trackExternalFree = env->GetMethodID(clazz, |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 585 | "trackExternalFree", "(J)V"); |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 586 | method_getRuntime = env->GetStaticMethodID(clazz, |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 587 | "getRuntime", "()Ldalvik/system/VMRuntime;"); |
| 588 | |
| 589 | if (gIDCache.method_trackExternalAllocation == NULL || |
| 590 | gIDCache.method_trackExternalFree == NULL || |
| 591 | method_getRuntime == NULL) |
| 592 | { |
| 593 | LOGE("Unable to find VMRuntime methods\n"); |
| 594 | return -1; |
| 595 | } |
| 596 | |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 597 | jobject instance = env->CallStaticObjectMethod(clazz, method_getRuntime); |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 598 | if (instance == NULL) { |
| 599 | LOGE("Unable to obtain VMRuntime instance\n"); |
| 600 | return -1; |
| 601 | } |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 602 | gIDCache.runtimeInstance = env->NewGlobalRef(instance); |
Andy McFadden | 5f327be | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 603 | |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 604 | return jniRegisterNativeMethods(env, "org/apache/harmony/luni/platform/OSMemory", |
| 605 | gMethods, NELEM(gMethods)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 606 | } |