Path: blob/master/src/java.base/unix/native/libnio/MappedMemoryUtils.c
41119 views
/*1* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jvm.h"28#include "jlong.h"29#include "java_nio_MappedMemoryUtils.h"30#include <assert.h>31#include <sys/mman.h>32#include <stddef.h>33#include <stdlib.h>3435#ifdef _AIX36#include <unistd.h>37#endif3839/* Output type for mincore(2) */40#ifdef __linux__41typedef unsigned char mincore_vec_t;42#else43typedef char mincore_vec_t;44#endif4546#ifdef _AIX47static long calculate_number_of_pages_in_range(void* address, size_t len, size_t pagesize) {48uintptr_t address_unaligned = (uintptr_t) address;49uintptr_t address_aligned = address_unaligned & (~(pagesize - 1));50size_t len2 = len + (address_unaligned - address_aligned);51long numPages = (len2 + pagesize - 1) / pagesize;52return numPages;53}54#endif5556JNIEXPORT jboolean JNICALL57Java_java_nio_MappedMemoryUtils_isLoaded0(JNIEnv *env, jobject obj, jlong address,58jlong len, jlong numPages)59{60jboolean loaded = JNI_TRUE;61int result = 0;62long i = 0;63void *a = (void *) jlong_to_ptr(address);64mincore_vec_t* vec = NULL;6566#ifdef _AIX67/* See JDK-8186665 */68size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);69if ((long)pagesize == -1) {70return JNI_FALSE;71}72numPages = (jlong) calculate_number_of_pages_in_range(a, len, pagesize);73#endif7475/* Include space for one sentinel byte at the end of the buffer76* to catch overflows. */77vec = (mincore_vec_t*) malloc(numPages + 1);7879if (vec == NULL) {80JNU_ThrowOutOfMemoryError(env, NULL);81return JNI_FALSE;82}8384vec[numPages] = '\x7f'; /* Write sentinel. */85result = mincore(a, (size_t)len, vec);86assert(vec[numPages] == '\x7f'); /* Check sentinel. */8788if (result == -1) {89JNU_ThrowIOExceptionWithLastError(env, "mincore failed");90free(vec);91return JNI_FALSE;92}9394for (i=0; i<numPages; i++) {95if (vec[i] == 0) {96loaded = JNI_FALSE;97break;98}99}100free(vec);101return loaded;102}103104105JNIEXPORT void JNICALL106Java_java_nio_MappedMemoryUtils_load0(JNIEnv *env, jobject obj, jlong address,107jlong len)108{109char *a = (char *)jlong_to_ptr(address);110int result = madvise((caddr_t)a, (size_t)len, MADV_WILLNEED);111if (result == -1) {112JNU_ThrowIOExceptionWithLastError(env, "madvise failed");113}114}115116JNIEXPORT void JNICALL117Java_java_nio_MappedMemoryUtils_unload0(JNIEnv *env, jobject obj, jlong address,118jlong len)119{120char *a = (char *)jlong_to_ptr(address);121int result = madvise((caddr_t)a, (size_t)len, MADV_DONTNEED);122if (result == -1) {123JNU_ThrowIOExceptionWithLastError(env, "madvise failed");124}125}126127JNIEXPORT void JNICALL128Java_java_nio_MappedMemoryUtils_force0(JNIEnv *env, jobject obj, jobject fdo,129jlong address, jlong len)130{131void* a = (void *)jlong_to_ptr(address);132int result = msync(a, (size_t)len, MS_SYNC);133if (result == -1) {134JNU_ThrowIOExceptionWithLastError(env, "msync failed");135}136}137138139