blob: 8c8a864936ebdaff6ea65528d493723722f7bda5 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
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 McFadden5f327be2009-06-04 14:34:14 -070017#define LOG_TAG "OSMemory"
Elliott Hughesc08f9fb2010-04-16 17:44:12 -070018
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080019#include "JNIHelp.h"
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080020#include "utils/misc.h"
Andy McFadden5f327be2009-06-04 14:34:14 -070021#include "utils/Log.h"
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080022#include <sys/mman.h>
23#include <stdlib.h>
24#include <string.h>
25#include <errno.h>
26
Andy McFadden5f327be2009-06-04 14:34:14 -070027/*
28 * Cached dalvik.system.VMRuntime pieces.
29 */
30static struct {
31 jmethodID method_trackExternalAllocation;
32 jmethodID method_trackExternalFree;
33
34 jobject runtimeInstance;
35} gIDCache;
36
Elliott Hughes3c6ff812009-10-22 17:25:01 -070037static const int MMAP_READ_ONLY = 1;
38static const int MMAP_READ_WRITE = 2;
39static const int MMAP_WRITE_COPY = 4;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080040
41/*
42 * Class: org_apache_harmony_luni_platform_OSMemory
43 * Method: littleEndian
44 * Signature: ()Z
45 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -070046static jboolean harmony_nio_littleEndian(JNIEnv*, jclass) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080047 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 Carlstrom44e0e562010-05-06 23:44:16 -070057static jint harmony_nio_getPointerSizeImpl(JNIEnv*, jclass) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080058 return sizeof(void *);
59}
60
61/*
62 * Class: org_apache_harmony_luni_platform_OSMemory
63 * Method: mallocImpl
64 * Signature: (I)I
65 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -070066static jint harmony_nio_mallocImpl(JNIEnv* _env, jobject, jint size) {
Andy McFadden5f327be2009-06-04 14:34:14 -070067 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 Projectadc854b2009-03-03 19:28:47 -080073 }
Andy McFadden5f327be2009-06-04 14:34:14 -070074
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 Projectadc854b2009-03-03 19:28:47 -080089}
90
91/*
92 * Class: org_apache_harmony_luni_platform_OSMemory
93 * Method: freeImpl
94 * Signature: (I)V
95 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -070096static void harmony_nio_freeImpl(JNIEnv* _env, jobject, jint pointer) {
Andy McFadden5f327be2009-06-04 14:34:14 -070097 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 Projectadc854b2009-03-03 19:28:47 -0800103}
104
105/*
106 * Class: org_apache_harmony_luni_platform_OSMemory
107 * Method: memset
108 * Signature: (IBJ)V
109 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -0700110static void harmony_nio_memset(JNIEnv*, jobject, jint address,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800111 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 Carlstrom44e0e562010-05-06 23:44:16 -0700120static void harmony_nio_memmove(JNIEnv*, jobject, jint destAddress,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800121 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 Carlstrom44e0e562010-05-06 23:44:16 -0700131static jbyte harmony_nio_getByteImpl(JNIEnv*, jobject,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800132 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 Carlstrom44e0e562010-05-06 23:44:16 -0700142static void harmony_nio_getBytesImpl(JNIEnv* _env, jobject, jint pointer,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800143 jbyteArray dst, jint offset, jint length) {
Elliott Hughesde2d83f2009-09-09 15:16:59 -0700144 jbyte* src = reinterpret_cast<jbyte*>(static_cast<uintptr_t>(pointer));
145 _env->SetByteArrayRegion(dst, offset, length, src);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800146}
147
148/*
149 * Class: org_apache_harmony_luni_platform_OSMemory
150 * Method: putByteImpl
151 * Signature: (IB)V
152 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -0700153static void harmony_nio_putByteImpl(JNIEnv*, jobject, jint pointer,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800154 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 Carlstrom44e0e562010-05-06 23:44:16 -0700163static void harmony_nio_putBytesImpl(JNIEnv* _env, jobject,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800164 jint pointer, jbyteArray src, jint offset, jint length) {
Elliott Hughesde2d83f2009-09-09 15:16:59 -0700165 jbyte* dst = reinterpret_cast<jbyte*>(static_cast<uintptr_t>(pointer));
166 _env->GetByteArrayRegion(src, offset, length, dst);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800167}
168
169static void
Elliott Hughes558383f2009-08-26 09:37:48 -0700170swapShorts(jshort *shorts, int count) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800171 jbyte *src = (jbyte *) shorts;
172 jbyte *dst = src;
Elliott Hughes558383f2009-08-26 09:37:48 -0700173 for (int i = 0; i < count; ++i) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800174 jbyte b0 = *src++;
175 jbyte b1 = *src++;
176 *dst++ = b1;
177 *dst++ = b0;
178 }
179}
180
181static void
Elliott Hughes558383f2009-08-26 09:37:48 -0700182swapInts(jint *ints, int count) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800183 jbyte *src = (jbyte *) ints;
184 jbyte *dst = src;
Elliott Hughes558383f2009-08-26 09:37:48 -0700185 for (int i = 0; i < count; ++i) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800186 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 Hughes558383f2009-08-26 09:37:48 -0700199 * Method: setShortArrayImpl
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800200 * Signature: (I[SIIZ)V
201 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -0700202static void harmony_nio_setShortArrayImpl(JNIEnv* _env, jobject,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800203 jint pointer, jshortArray src, jint offset, jint length, jboolean swap) {
Elliott Hughes558383f2009-08-26 09:37:48 -0700204 jshort* dst = reinterpret_cast<jshort*>(static_cast<uintptr_t>(pointer));
205 _env->GetShortArrayRegion(src, offset, length, dst);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800206 if (swap) {
Elliott Hughes558383f2009-08-26 09:37:48 -0700207 swapShorts(dst, length);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800208 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800209}
210
211/*
212 * Class: org_apache_harmony_luni_platform_OSMemory
Elliott Hughes558383f2009-08-26 09:37:48 -0700213 * Method: setIntArrayImpl
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800214 * Signature: (I[IIIZ)V
215 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -0700216static void harmony_nio_setIntArrayImpl(JNIEnv* _env, jobject,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800217 jint pointer, jintArray src, jint offset, jint length, jboolean swap) {
Elliott Hughes558383f2009-08-26 09:37:48 -0700218 jint* dst = reinterpret_cast<jint*>(static_cast<uintptr_t>(pointer));
219 _env->GetIntArrayRegion(src, offset, length, dst);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800220 if (swap) {
Elliott Hughes558383f2009-08-26 09:37:48 -0700221 swapInts(dst, length);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800222 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800223}
224
225/*
226 * Class: org_apache_harmony_luni_platform_OSMemory
227 * Method: getShortImpl
228 * Signature: (I)S
229 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -0700230static jshort harmony_nio_getShortImpl(JNIEnv*, jobject,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800231 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 Carlstrom44e0e562010-05-06 23:44:16 -0700251static void harmony_nio_putShortImpl(JNIEnv*, jobject, jint pointer,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800252 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 Carlstrom44e0e562010-05-06 23:44:16 -0700269static jint harmony_nio_getIntImpl(JNIEnv*, jobject, jint pointer) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800270 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 Carlstrom44e0e562010-05-06 23:44:16 -0700291static void harmony_nio_putIntImpl(JNIEnv*, jobject, jint pointer,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800292 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 Carlstrom44e0e562010-05-06 23:44:16 -0700311static jlong harmony_nio_getLongImpl(JNIEnv*, jobject,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800312 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 Carlstrom44e0e562010-05-06 23:44:16 -0700329static void harmony_nio_putLongImpl(JNIEnv*, jobject, jint pointer,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800330 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 Carlstrom44e0e562010-05-06 23:44:16 -0700344static jfloat harmony_nio_getFloatImpl(JNIEnv*, jobject,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800345 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 Carlstrom44e0e562010-05-06 23:44:16 -0700362static void harmony_nio_putFloatImpl(JNIEnv*, jobject, jint pointer,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800363 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 Carlstrom44e0e562010-05-06 23:44:16 -0700377static jdouble harmony_nio_getDoubleImpl(JNIEnv*, jobject,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800378 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 Carlstrom44e0e562010-05-06 23:44:16 -0700395static void harmony_nio_putDoubleImpl(JNIEnv*, jobject, jint pointer,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800396 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 Carlstrom44e0e562010-05-06 23:44:16 -0700410static jint harmony_nio_getAddress(JNIEnv*, jobject, jint pointer) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800411 return (jint) * (int *) pointer;
412}
413
414/*
415 * Class: org_apache_harmony_luni_platform_OSMemory
416 * Method: setAddress
417 * Signature: (II)V
418 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -0700419static void harmony_nio_setAddress(JNIEnv*, jobject, jint pointer,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800420 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 Hughes3c6ff812009-10-22 17:25:01 -0700429static jint harmony_nio_mmapImpl(JNIEnv* env, jobject, jint fd,
430 jlong offset, jlong size, jint mapMode) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800431 int prot, flags;
Elliott Hughes3c6ff812009-10-22 17:25:01 -0700432 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 Projectadc854b2009-03-03 19:28:47 -0800448 return -1;
449 }
Elliott Hughes3c6ff812009-10-22 17:25:01 -0700450
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 Projectadc854b2009-03-03 19:28:47 -0800456}
457
458/*
459 * Class: org_apache_harmony_luni_platform_OSMemory
460 * Method: unmapImpl
461 * Signature: (IJ)V
462 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -0700463static void harmony_nio_unmapImpl(JNIEnv*, jobject, jint address,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800464 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 Carlstrom44e0e562010-05-06 23:44:16 -0700473static jint harmony_nio_loadImpl(JNIEnv*, jobject, jint address,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800474 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 Projectadc854b2009-03-03 19:28:47 -0800491/*
492 * Class: org_apache_harmony_luni_platform_OSMemory
493 * Method: isLoadedImpl
494 * Signature: (IJ)Z
495 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -0700496static jboolean harmony_nio_isLoadedImpl(JNIEnv*, jobject,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800497 jint address, jlong size) {
498
Elliott Hughes3c6ff812009-10-22 17:25:01 -0700499 static int page_size = getpagesize();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800500 jboolean result = 0;
501 jint m_addr = (jint)address;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800502
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 Hughes3c6ff812009-10-22 17:25:01 -0700506 int page_count = (size+page_size-1)/page_size;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800507
Elliott Hughes3c6ff812009-10-22 17:25:01 -0700508 unsigned char* vec = (unsigned char *) malloc(page_count*sizeof(char));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800509
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 Carlstrom44e0e562010-05-06 23:44:16 -0700533static jint harmony_nio_flushImpl(JNIEnv *, jobject, jint address, jlong size) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800534 return msync((void *)address, size, MS_SYNC);
535}
536
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800537static JNINativeMethod gMethods[] = {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800538 { "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 Hughes558383f2009-08-26 09:37:48 -0700546 { "setShortArray", "(I[SIIZ)V",(void*) harmony_nio_setShortArrayImpl },
547 { "setIntArray", "(I[IIIZ)V",(void*) harmony_nio_setIntArrayImpl },
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800548 { "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 Hughesc08f9fb2010-04-16 17:44:12 -0700568int register_org_apache_harmony_luni_platform_OSMemory(JNIEnv* env) {
Andy McFadden5f327be2009-06-04 14:34:14 -0700569 /*
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 Hughesc08f9fb2010-04-16 17:44:12 -0700577 clazz = env->FindClass(kVMRuntimeName);
Andy McFadden5f327be2009-06-04 14:34:14 -0700578 if (clazz == NULL) {
579 LOGE("Unable to find class %s\n", kVMRuntimeName);
580 return -1;
581 }
Elliott Hughesc08f9fb2010-04-16 17:44:12 -0700582 gIDCache.method_trackExternalAllocation = env->GetMethodID(clazz,
Andy McFadden5f327be2009-06-04 14:34:14 -0700583 "trackExternalAllocation", "(J)Z");
Elliott Hughesc08f9fb2010-04-16 17:44:12 -0700584 gIDCache.method_trackExternalFree = env->GetMethodID(clazz,
Andy McFadden5f327be2009-06-04 14:34:14 -0700585 "trackExternalFree", "(J)V");
Elliott Hughesc08f9fb2010-04-16 17:44:12 -0700586 method_getRuntime = env->GetStaticMethodID(clazz,
Andy McFadden5f327be2009-06-04 14:34:14 -0700587 "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 Hughesc08f9fb2010-04-16 17:44:12 -0700597 jobject instance = env->CallStaticObjectMethod(clazz, method_getRuntime);
Andy McFadden5f327be2009-06-04 14:34:14 -0700598 if (instance == NULL) {
599 LOGE("Unable to obtain VMRuntime instance\n");
600 return -1;
601 }
Elliott Hughesc08f9fb2010-04-16 17:44:12 -0700602 gIDCache.runtimeInstance = env->NewGlobalRef(instance);
Andy McFadden5f327be2009-06-04 14:34:14 -0700603
Elliott Hughesc08f9fb2010-04-16 17:44:12 -0700604 return jniRegisterNativeMethods(env, "org/apache/harmony/luni/platform/OSMemory",
605 gMethods, NELEM(gMethods));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800606}