mirror of
https://github.com/usatiuk/dhfs.git
synced 2025-10-29 04:57:48 +01:00
a little cleanup
This commit is contained in:
@@ -3,12 +3,47 @@ project(libdhfs_support CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
if (NOT SANITIZE)
|
||||
set(SANITIZE YES)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
include(CheckCXXCompilerFlag)
|
||||
|
||||
if (SANITIZE STREQUAL "YES")
|
||||
message(WARNING "Enabling sanitizers!")
|
||||
add_compile_options(-Werror -Wall -Wextra -pedantic -Wshadow -Wformat=2 -Wfloat-equal -D_GLIBCXX_DEBUG -Wconversion)
|
||||
check_cxx_compiler_flag(-fsanitize-trap=all CAN_TRAP)
|
||||
if (CAN_TRAP)
|
||||
add_compile_options(-fsanitize=undefined -fsanitize-trap=all -fno-sanitize-recover)
|
||||
add_link_options(-fsanitize=undefined -fsanitize-trap=all -fno-sanitize-recover)
|
||||
else ()
|
||||
message(WARNING "Sanitizers not supported!")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
add_compile_options(-flto)
|
||||
add_link_options(-flto)
|
||||
endif ()
|
||||
|
||||
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
add_compile_options(-O3)
|
||||
add_link_options(-O3)
|
||||
endif ()
|
||||
|
||||
add_compile_options(-Wno-unused-parameter)
|
||||
|
||||
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
|
||||
|
||||
find_package(JNI REQUIRED COMPONENTS JVM)
|
||||
find_package(Java REQUIRED)
|
||||
include(UseJava)
|
||||
|
||||
add_jar(DhfsSupportNative
|
||||
"${PROJECT_SOURCE_DIR}/../dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportNative.java"
|
||||
"${PROJECT_SOURCE_DIR}/LibPathDummy.java"
|
||||
GENERATE_NATIVE_HEADERS DhfsSupportNative-native)
|
||||
|
||||
add_library(dhfs_support SHARED DhfsSupportNative.cpp)
|
||||
|
||||
@@ -7,18 +7,40 @@
|
||||
|
||||
#include "com_usatiuk_dhfs_supportlib_DhfsSupportNative.h"
|
||||
|
||||
long get_page_size() {
|
||||
static const long PAGE_SIZE = sysconf(_SC_PAGESIZE);
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
#pragma GCC diagnostic ignored "-Wsign-compare"
|
||||
|
||||
template<typename To, typename From>
|
||||
constexpr To checked_cast(const From& f) {
|
||||
To result = static_cast<To>(f);
|
||||
assert(f == result);
|
||||
return result;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
unsigned int get_page_size() {
|
||||
static const auto PAGE_SIZE = checked_cast<unsigned int>(sysconf(_SC_PAGESIZE));
|
||||
return PAGE_SIZE;
|
||||
}
|
||||
|
||||
constexpr uintptr_t align_up(uintptr_t what, size_t alignment) {
|
||||
template<typename T, typename A>
|
||||
T align_up(T what, A alignment) {
|
||||
assert(__builtin_popcount(alignment) == 1);
|
||||
const uintptr_t mask = alignment - 1;
|
||||
if (what & mask) {
|
||||
return (what & ~mask) + alignment;
|
||||
}
|
||||
return what;
|
||||
|
||||
const T mask = checked_cast<T>(alignment - 1);
|
||||
|
||||
T ret;
|
||||
|
||||
if (what & mask)
|
||||
ret = (what + mask) & ~mask;
|
||||
else
|
||||
ret = what;
|
||||
|
||||
assert((ret & mask) == 0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
@@ -28,28 +50,33 @@ JNIEXPORT void JNICALL Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_hello(
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_allocateUninitializedByteBuffer
|
||||
(JNIEnv* env, jclass klass, jobjectArray bb, jint size) {
|
||||
if (size < 0) {
|
||||
env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), "Size less than 0?");
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t checked_size = checked_cast<size_t>(size);
|
||||
|
||||
void* buf;
|
||||
if (size < get_page_size())
|
||||
buf = malloc(size);
|
||||
if (checked_size < get_page_size())
|
||||
buf = malloc(checked_size);
|
||||
else
|
||||
buf = std::aligned_alloc(get_page_size(), align_up(size, get_page_size()));
|
||||
buf = std::aligned_alloc(get_page_size(), align_up(checked_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));
|
||||
env->SetObjectArrayElement(bb, 0, env->NewDirectByteBuffer(buf, checked_cast<jlong>(checked_size)));
|
||||
|
||||
jlong token = static_cast<jlong>((uintptr_t) buf);
|
||||
assert(token == (uintptr_t)buf);
|
||||
jlong token = checked_cast<jlong>((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);
|
||||
const auto addr = checked_cast<uintptr_t>(token);
|
||||
|
||||
if (addr == 0) {
|
||||
env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), "Trying to free null pointer");
|
||||
@@ -58,4 +85,9 @@ JNIEXPORT void JNICALL Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_dropBy
|
||||
|
||||
free((void*) addr);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_getPageSize
|
||||
(JNIEnv*, jclass) {
|
||||
return checked_cast<jint>(get_page_size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
CMAKE_ARGS="${CMAKE_ARGS:--DCMAKE_BUILD_TYPE=Debug}"
|
||||
|
||||
export SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
if [[ -z "${INSIDE_DOCKER_ALREADY}" ]]; then
|
||||
if [[ -z "${DO_LOCAL_BUILD}" ]]; then
|
||||
if [[ "$(uname)" == "Linux" ]]; then
|
||||
if [[ -z "${CROSS_PLATFORM}" ]]; then
|
||||
echo "Already on linux"
|
||||
@@ -26,7 +28,7 @@ CONFIGURE_DIR="$2"
|
||||
INSTALL_DIR="$3"
|
||||
|
||||
function configure() {
|
||||
cmake -B"$CONFIGURE_DIR" -S"$PROJECT_DIR" -DDHFS_LIB_INSTALL="$INSTALL_DIR"
|
||||
cmake -B"$CONFIGURE_DIR" -S"$PROJECT_DIR" -DDHFS_LIB_INSTALL="$INSTALL_DIR" $CMAKE_ARGS
|
||||
}
|
||||
|
||||
function build() {
|
||||
|
||||
@@ -14,5 +14,5 @@ docker build $PLATFORM_ARG --iidfile "$DOCKER_IMG_FILE" .
|
||||
|
||||
ROOT_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")"
|
||||
|
||||
docker run $PLATFORM_ARG --rm -v "$ROOT_DIR:$ROOT_DIR" -e INSIDE_DOCKER_ALREADY=TRUE "$(cat "$DOCKER_IMG_FILE")" \
|
||||
docker run $PLATFORM_ARG --rm -v "$ROOT_DIR:$ROOT_DIR" -e DO_LOCAL_BUILD=TRUE "$(cat "$DOCKER_IMG_FILE")" \
|
||||
"$SCRIPT_DIR/cross-build.sh" "$@"
|
||||
|
||||
Reference in New Issue
Block a user