use uninitialized byte buffers

This commit is contained in:
2024-08-23 22:51:34 +02:00
parent dca507ec4b
commit 848ab14f8e
11 changed files with 170 additions and 40 deletions

View File

@@ -84,5 +84,10 @@
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_SIZE/@EntryValue" value="4" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CONTINUOUS_LINE_INDENT/@EntryValue" value="Double" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TAB_WIDTH/@EntryValue" value="8" type="long" />
<option name="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003Ajni_002Eh_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FJava_003FJavaVirtualMachines_003Fzulu_002D21_002Ejdk_003FContents_003FHome_003Finclude_003Fjni_002Eh/@EntryIndexedValue" value="ExplicitlyExcluded" type="string" />
<option name="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003A_005Fmalloc_002Eh_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FApplications_003FXcode_002Eapp_003FContents_003FDeveloper_003FPlatforms_003FMacOSX_002Eplatform_003FDeveloper_003FSDKs_003FMacOSX14_002E5_002Esdk_003Fusr_003Finclude_003Fmalloc_003F_005Fmalloc_002Eh/@EntryIndexedValue" value="ExplicitlyExcluded" type="string" />
<option name="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003A_005Fmalloc_002Eh_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FApplications_003FXcode_002Eapp_003FContents_003FDeveloper_003FPlatforms_003FMacOSX_002Eplatform_003FDeveloper_003FSDKs_003FMacOSX14_002E5_002Esdk_003Fusr_003Finclude_003Fmalloc_003F_005Fmalloc_002Eh/@EntryIndexRemoved" />
<option name="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003Ajni_005Fmd_002Eh_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FJava_003FJavaVirtualMachines_003Fzulu_002D21_002Ejdk_003FContents_003FHome_003Finclude_003Fdarwin_003Fjni_005Fmd_002Eh/@EntryIndexedValue" value="ExplicitlyExcluded" type="string" />
<option name="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003Ajni_005Fmd_002Eh_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FJava_003FJavaVirtualMachines_003Fzulu_002D21_002Ejdk_003FContents_003FHome_003Finclude_003Fdarwin_003Fjni_005Fmd_002Eh/@EntryIndexRemoved" />
</component>
</project>

View File

@@ -1,9 +1,61 @@
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cassert>
#include <unistd.h>
#include "com_usatiuk_dhfs_supportlib_DhfsSupportNative.h"
long get_page_size() {
static const long PAGE_SIZE = sysconf(_SC_PAGESIZE);
return PAGE_SIZE;
}
constexpr uintptr_t align_up(uintptr_t what, size_t alignment) {
assert(__builtin_popcount(alignment) == 1);
const uintptr_t mask = alignment - 1;
if (what & mask) {
return (what & ~mask) + alignment;
}
return what;
}
extern "C" {
void Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_hello(JNIEnv* env, jclass klass) {
JNIEXPORT void JNICALL Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_hello(JNIEnv* env, jclass klass) {
printf("Hello, World!\n");
}
JNIEXPORT jlong JNICALL Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_allocateUninitializedByteBuffer
(JNIEnv* env, jclass klass, jobjectArray bb, jint size) {
void* buf;
if (size < get_page_size())
buf = malloc(size);
else
buf = std::aligned_alloc(get_page_size(), align_up(size, get_page_size()));
if (buf == nullptr) {
env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), "Buffer memory allocation failed");
return 0;
}
env->SetObjectArrayElement(bb, 0, env->NewDirectByteBuffer(buf, size));
jlong token = static_cast<jlong>((uintptr_t) buf);
assert(token == (uintptr_t)buf);
return token;
}
JNIEXPORT void JNICALL Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_dropByteBuffer
(JNIEnv* env, jclass klass, jlong token) {
uintptr_t addr = static_cast<uintptr_t>(token);
assert(addr == token);
if (addr == 0) {
env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), "Trying to free null pointer");
return;
}
free((void*) addr);
}
}

View File

@@ -0,0 +1,9 @@
package com.usatiuk.dhfs.supportlib;
import java.nio.file.Path;
class DhfsNativeLibFinder {
static Path getLibPath() {
return null;
}
}