From 8cde1870d77702f73072bcb1f71393346004d24e Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Thu, 9 Mar 2023 14:28:25 -0800 Subject: [PATCH] Use zstd --- examples/rules_foreign_cc/WORKSPACE | 4 +-- examples/rules_foreign_cc/java/com/app/BUILD | 4 ++- .../rules_foreign_cc/java/com/app/Jni.java | 12 ++++----- .../java/com/app/MainActivity.java | 10 ++++---- examples/rules_foreign_cc/java/com/app/jni.cc | 14 +++++------ .../rules_foreign_cc/java/com/app/jni_dep.cc | 25 ++++++++++++++----- .../rules_foreign_cc/java/com/app/jni_dep.h | 12 +++++---- 7 files changed, 48 insertions(+), 33 deletions(-) diff --git a/examples/rules_foreign_cc/WORKSPACE b/examples/rules_foreign_cc/WORKSPACE index 154aa08..85f0cb1 100644 --- a/examples/rules_foreign_cc/WORKSPACE +++ b/examples/rules_foreign_cc/WORKSPACE @@ -17,8 +17,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "rules_foreign_cc", - strip_prefix = "rules_foreign_cc-e5c7d6df6132ed88bb3a67f05dad188f4754474c", - url = "https://github.com/bazelbuild/rules_foreign_cc/archive/e5c7d6df6132ed88bb3a67f05dad188f4754474c.tar.gz", + strip_prefix = "rules_foreign_cc-d33d862abb4ce3ba178547ef58c9fcb24cec38ca", + url = "https://github.com/bazelbuild/rules_foreign_cc/archive/d33d862abb4ce3ba178547ef58c9fcb24cec38ca.tar.gz", ) load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies") diff --git a/examples/rules_foreign_cc/java/com/app/BUILD b/examples/rules_foreign_cc/java/com/app/BUILD index 41b6930..f025168 100644 --- a/examples/rules_foreign_cc/java/com/app/BUILD +++ b/examples/rules_foreign_cc/java/com/app/BUILD @@ -24,7 +24,6 @@ cc_library( srcs = ["jni.cc"], deps = [ ":jni_dep", - "@com_github_facebook_zstd//:zstd", ], ) @@ -32,4 +31,7 @@ cc_library( name = "jni_dep", srcs = ["jni_dep.cc"], hdrs = ["jni_dep.h"], + deps = [ + "@com_github_facebook_zstd//:zstd", + ], ) diff --git a/examples/rules_foreign_cc/java/com/app/Jni.java b/examples/rules_foreign_cc/java/com/app/Jni.java index 072a5fb..9bc7d37 100644 --- a/examples/rules_foreign_cc/java/com/app/Jni.java +++ b/examples/rules_foreign_cc/java/com/app/Jni.java @@ -1,21 +1,19 @@ -// Copyright 2022 The Bazel Authors. All rights reserved. -// +// Copyright 2023 The Bazel Authors. All rights reserved. +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - package com.app; public class Jni { - public static native int getValue(int a); + public static native String getString(); } - diff --git a/examples/rules_foreign_cc/java/com/app/MainActivity.java b/examples/rules_foreign_cc/java/com/app/MainActivity.java index fe9a65f..1b8e981 100644 --- a/examples/rules_foreign_cc/java/com/app/MainActivity.java +++ b/examples/rules_foreign_cc/java/com/app/MainActivity.java @@ -1,11 +1,11 @@ -// Copyright 2022 The Bazel Authors. All rights reserved. -// +// Copyright 2023 The Bazel Authors. All rights reserved. +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -30,7 +30,7 @@ public void onCreate(Bundle savedInstanceState) { System.loadLibrary("app"); - int valueFromNative = Jni.getValue(2); + String valueFromNative = Jni.getString(); ((TextView) findViewById(R.id.text_from_native)) .setText("Result from JNI: " + valueFromNative); diff --git a/examples/rules_foreign_cc/java/com/app/jni.cc b/examples/rules_foreign_cc/java/com/app/jni.cc index f2aba83..86a22ec 100644 --- a/examples/rules_foreign_cc/java/com/app/jni.cc +++ b/examples/rules_foreign_cc/java/com/app/jni.cc @@ -1,11 +1,11 @@ -// Copyright 2022 The Bazel Authors. All rights reserved. -// +// Copyright 2023 The Bazel Authors. All rights reserved. +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,7 +17,7 @@ #include "java/com/app/jni_dep.h" -extern "C" JNIEXPORT int JNICALL -Java_com_app_Jni_getValue(JNIEnv *env, jclass clazz, jint a) { - return calculate((int)a, 2); +extern "C" JNIEXPORT jstring JNICALL +Java_com_app_Jni_getString(JNIEnv *env, jclass clazz, jint a) { + return env->NewStringUTF(get_string().c_str()); } diff --git a/examples/rules_foreign_cc/java/com/app/jni_dep.cc b/examples/rules_foreign_cc/java/com/app/jni_dep.cc index 366c4c0..ac03e2f 100644 --- a/examples/rules_foreign_cc/java/com/app/jni_dep.cc +++ b/examples/rules_foreign_cc/java/com/app/jni_dep.cc @@ -1,19 +1,32 @@ -// Copyright 2022 The Bazel Authors. All rights reserved. -// +// Copyright 2023 The Bazel Authors. All rights reserved. +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +#include +#include "zstd.h" #include "java/com/app/jni_dep.h" -int calculate(int a, int b) { - return a + b * 20; +std::string get_string() { + std::vector input_buffer = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!'}; + std::vector compressed_buffer(ZSTD_compressBound(input_buffer.size())); + + // Compress the input buffer into the compressed buffer + size_t compressed_size = ZSTD_compress(compressed_buffer.data(), compressed_buffer.size(), + input_buffer.data(), input_buffer.size(), 1); + + std::vector decompressed_buffer(input_buffer.size()); + size_t decompressed_size = ZSTD_decompress(decompressed_buffer.data(), decompressed_buffer.size(), + compressed_buffer.data(), compressed_size); + + return std::string(decompressed_buffer.data(), decompressed_size); } diff --git a/examples/rules_foreign_cc/java/com/app/jni_dep.h b/examples/rules_foreign_cc/java/com/app/jni_dep.h index 342551c..00de7b5 100644 --- a/examples/rules_foreign_cc/java/com/app/jni_dep.h +++ b/examples/rules_foreign_cc/java/com/app/jni_dep.h @@ -1,11 +1,11 @@ -// Copyright 2022 The Bazel Authors. All rights reserved. -// +// Copyright 2023 The Bazel Authors. All rights reserved. +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -14,4 +14,6 @@ #pragma once -int calculate(int a, int b); +#include + +std::string get_string();