Skip to content

Commit

Permalink
Use zstd
Browse files Browse the repository at this point in the history
  • Loading branch information
keith committed Mar 9, 2023
1 parent f8faeb1 commit 8cde187
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 33 deletions.
4 changes: 2 additions & 2 deletions examples/rules_foreign_cc/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion examples/rules_foreign_cc/java/com/app/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ cc_library(
srcs = ["jni.cc"],
deps = [
":jni_dep",
"@com_github_facebook_zstd//:zstd",
],
)

cc_library(
name = "jni_dep",
srcs = ["jni_dep.cc"],
hdrs = ["jni_dep.h"],
deps = [
"@com_github_facebook_zstd//:zstd",
],
)
12 changes: 5 additions & 7 deletions examples/rules_foreign_cc/java/com/app/Jni.java
Original file line number Diff line number Diff line change
@@ -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();
}

10 changes: 5 additions & 5 deletions examples/rules_foreign_cc/java/com/app/MainActivity.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions examples/rules_foreign_cc/java/com/app/jni.cc
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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());
}
25 changes: 19 additions & 6 deletions examples/rules_foreign_cc/java/com/app/jni_dep.cc
Original file line number Diff line number Diff line change
@@ -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 <vector>
#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<char> input_buffer = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!'};
std::vector<char> 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<char> 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);
}
12 changes: 7 additions & 5 deletions examples/rules_foreign_cc/java/com/app/jni_dep.h
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,4 +14,6 @@

#pragma once

int calculate(int a, int b);
#include <string>

std::string get_string();

0 comments on commit 8cde187

Please sign in to comment.