Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmetic fixes in JNI code #64

Merged
merged 4 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,28 @@ javadoc {
options.overview = "${project.rootDir}/docs/javadoc/overview.html"
options.memberLevel = System.getProperty("visibility") == "private" ? JavadocMemberLevel.PRIVATE : JavadocMemberLevel.PUBLIC
}

task clangFormat {
description = 'Formats all .h and .cpp files using clang-format'

doLast {
def hFiles = fileTree(dir: 'roc_jni', include: '**/*.h')
def cppFiles = fileTree(dir: 'roc_jni', include: '**/*.cpp')
def excludedFiles = fileTree(dir: 'roc_jni', include: '**/org_rocstreaming_roctoolkit_*')

hFiles = hFiles.minus(excludedFiles)
cppFiles = cppFiles.minus(excludedFiles)

hFiles.each { file ->
exec {
commandLine 'clang-format', '-i', file
}
}

cppFiles.each { file ->
exec {
commandLine 'clang-format', '-i', file
}
}
}
}
12 changes: 12 additions & 0 deletions roc_jni/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
#
---
BasedOnStyle: WebKit # http://www.webkit.org/coding/coding-style.html

UseTab: Never
IndentWidth: 4
ColumnLimit: 100

AllowShortIfStatementsOnASingleLine: Always
BreakBeforeBraces: Attach
SpaceAfterCStyleCast: true
2 changes: 1 addition & 1 deletion roc_jni/src/main/cpp/channel_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <roc/config.h>

roc_channel_set get_channel_set(JNIEnv *env, jobject jchannel_set) {
roc_channel_set get_channel_set(JNIEnv* env, jobject jchannel_set) {
jclass channelSetClass = NULL;

channelSetClass = env->FindClass(CHANNEL_SET_CLASS);
Expand Down
2 changes: 1 addition & 1 deletion roc_jni/src/main/cpp/clock_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <roc/config.h>

roc_clock_source get_clock_source(JNIEnv *env, jobject jclock_source) {
roc_clock_source get_clock_source(JNIEnv* env, jobject jclock_source) {
jclass clockSourceClass = NULL;

clockSourceClass = env->FindClass(CLOCK_SOURCE_CLASS);
Expand Down
20 changes: 13 additions & 7 deletions roc_jni/src/main/cpp/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#include <limits.h>

int get_boolean_field_value(JNIEnv *env, jclass clazz, jobject obj, const char* attr_name, char* error) {
int get_boolean_field_value(
JNIEnv* env, jclass clazz, jobject obj, const char* attr_name, char* error) {
jfieldID attrId = env->GetFieldID(clazz, attr_name, "Z");
assert(attrId != NULL);
return env->GetBooleanField(obj, attrId) == JNI_TRUE;
}

int get_int_field_value(JNIEnv *env, jclass clazz, jobject obj, const char* attr_name, char* error) {
int get_int_field_value(
JNIEnv* env, jclass clazz, jobject obj, const char* attr_name, char* error) {
jfieldID attrId = env->GetFieldID(clazz, attr_name, "I");
assert(attrId != NULL);
jint ret = env->GetIntField(obj, attrId);
Expand All @@ -19,7 +21,8 @@ int get_int_field_value(JNIEnv *env, jclass clazz, jobject obj, const char* attr
return (int) ret;
}

unsigned int get_uint_field_value(JNIEnv *env, jclass clazz, jobject obj, const char* attr_name, char* error) {
unsigned int get_uint_field_value(
JNIEnv* env, jclass clazz, jobject obj, const char* attr_name, char* error) {
jfieldID attrId = env->GetFieldID(clazz, attr_name, "I");
assert(attrId != NULL);
jint ret = env->GetIntField(obj, attrId);
Expand All @@ -30,13 +33,15 @@ unsigned int get_uint_field_value(JNIEnv *env, jclass clazz, jobject obj, const
return (unsigned int) ret;
}

long long get_llong_field_value(JNIEnv *env, jclass clazz, jobject obj, const char* attr_name, char* error) {
long long get_llong_field_value(
JNIEnv* env, jclass clazz, jobject obj, const char* attr_name, char* error) {
jfieldID attrId = env->GetFieldID(clazz, attr_name, "J");
assert(attrId != NULL);
jlong ret = env->GetLongField(obj, attrId);
return (long long) ret; // always safe (sizeof(long long) == sizeof(jlong))
}
unsigned long long get_ullong_field_value(JNIEnv *env, jclass clazz, jobject obj, const char* attr_name, char* error) {
unsigned long long get_ullong_field_value(
JNIEnv* env, jclass clazz, jobject obj, const char* attr_name, char* error) {
jfieldID attrId = env->GetFieldID(clazz, attr_name, "J");
assert(attrId != NULL);
jlong ret = env->GetLongField(obj, attrId);
Expand All @@ -47,15 +52,16 @@ unsigned long long get_ullong_field_value(JNIEnv *env, jclass clazz, jobject obj
return (unsigned long long) ret;
}

int get_enum_value(JNIEnv *env, jclass clazz, jobject enumObj) {
int get_enum_value(JNIEnv* env, jclass clazz, jobject enumObj) {
if (enumObj != NULL) {
jfieldID attrId = env->GetFieldID(clazz, "value", "I");
return env->GetIntField(enumObj, attrId);
}
return 0;
}

jobject get_object_field(JNIEnv *env, jclass clazz, jobject obj, const char* attr_name, const char* attr_class_name) {
jobject get_object_field(
JNIEnv* env, jclass clazz, jobject obj, const char* attr_name, const char* attr_class_name) {
jfieldID attrId = env->GetFieldID(clazz, attr_name, attr_class_name);
assert(attrId != NULL);
return env->GetObjectField(obj, attrId);
Expand Down
21 changes: 13 additions & 8 deletions roc_jni/src/main/cpp/context.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "org_rocstreaming_roctoolkit_Context.h"

#include "common.h"

#include <roc/context.h>

#define CONTEXT_CLASS PACKAGE_BASE_NAME "/Context"
#define CONTEXT_CONFIG_CLASS PACKAGE_BASE_NAME "/ContextConfig"
#define CONTEXT_CLASS PACKAGE_BASE_NAME "/Context"
#define CONTEXT_CONFIG_CLASS PACKAGE_BASE_NAME "/ContextConfig"

char context_config_unmarshal(JNIEnv *env, roc_context_config* conf, jobject jconfig) {
char context_config_unmarshal(JNIEnv* env, roc_context_config* conf, jobject jconfig) {
jclass contextConfigClass = NULL;
char err = 0;

Expand All @@ -15,14 +16,17 @@ char context_config_unmarshal(JNIEnv *env, roc_context_config* conf, jobject jco

memset(conf, 0, sizeof(roc_context_config));

conf->max_packet_size = get_uint_field_value(env, contextConfigClass, jconfig, "maxPacketSize", &err);
conf->max_packet_size
= get_uint_field_value(env, contextConfigClass, jconfig, "maxPacketSize", &err);
if (err) return err;
conf->max_frame_size = get_uint_field_value(env, contextConfigClass, jconfig, "maxFrameSize", &err);
conf->max_frame_size
= get_uint_field_value(env, contextConfigClass, jconfig, "maxFrameSize", &err);
return err;
}

JNIEXPORT jlong JNICALL Java_org_rocstreaming_roctoolkit_Context_open(JNIEnv *env, jclass contextClass, jobject config) {
roc_context* context = NULL;
JNIEXPORT jlong JNICALL Java_org_rocstreaming_roctoolkit_Context_open(
JNIEnv* env, jclass contextClass, jobject config) {
roc_context* context = NULL;
roc_context_config context_config = {};

if (context_config_unmarshal(env, &context_config, config) != 0) {
Expand All @@ -40,7 +44,8 @@ JNIEXPORT jlong JNICALL Java_org_rocstreaming_roctoolkit_Context_open(JNIEnv *en
return (jlong) context;
}

JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Context_close(JNIEnv *env, jclass contextClass, jlong nativePtr) {
JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Context_close(
JNIEnv* env, jclass contextClass, jlong nativePtr) {

roc_context* context = (roc_context*) nativePtr;

Expand Down
107 changes: 56 additions & 51 deletions roc_jni/src/main/cpp/endpoint.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
#include <stdlib.h>

#include "org_rocstreaming_roctoolkit_Endpoint.h"

#include "common.h"
#include "endpoint.h"
#include "protocol.h"

#include <roc/config.h>

#define ENDPOINT_CLASS PACKAGE_BASE_NAME "/Endpoint"
#include <stdlib.h>

#define ENDPOINT_CLASS PACKAGE_BASE_NAME "/Endpoint"

int endpoint_unmarshal(JNIEnv *env, roc_endpoint** endpoint, jobject jendpoint) {
jclass endpointClass = NULL;
jobject tempObject = NULL;
jstring jstr = NULL;
roc_protocol protocol = (roc_protocol)0;
const char* host = NULL;
int port = 0;
const char* resource = NULL;
char err = 0;
int endpoint_unmarshal(JNIEnv* env, roc_endpoint** endpoint, jobject jendpoint) {
jclass endpointClass = NULL;
jobject tempObject = NULL;
jstring jstr = NULL;
roc_protocol protocol = (roc_protocol) 0;
const char* host = NULL;
int port = 0;
const char* resource = NULL;
char err = 0;

assert(*endpoint == NULL);
if (jendpoint == NULL)
return -1;
if (jendpoint == NULL) return -1;

endpointClass = env->FindClass(ENDPOINT_CLASS);
assert(endpointClass != NULL);

tempObject = get_object_field(env, endpointClass, jendpoint, "protocol", "L" PROTOCOL_CLASS ";");
tempObject
= get_object_field(env, endpointClass, jendpoint, "protocol", "L" PROTOCOL_CLASS ";");
protocol = get_protocol(env, tempObject);

if ((err = roc_endpoint_allocate(endpoint)) != 0) return err;
Expand Down Expand Up @@ -59,7 +60,8 @@ int endpoint_unmarshal(JNIEnv *env, roc_endpoint** endpoint, jobject jendpoint)
return err;
}

jstr = (jstring) get_object_field(env, endpointClass, jendpoint, "resource", "Ljava/lang/String;");
jstr = (jstring) get_object_field(
env, endpointClass, jendpoint, "resource", "Ljava/lang/String;");
if (jstr != NULL) {
resource = env->GetStringUTFChars(jstr, 0);
if ((err = roc_endpoint_set_resource(*endpoint, resource)) != 0) {
Expand All @@ -73,10 +75,10 @@ int endpoint_unmarshal(JNIEnv *env, roc_endpoint** endpoint, jobject jendpoint)
return 0;
}

void endpoint_set_protocol(JNIEnv *env, jobject endpoint, roc_protocol protocol) {
jclass endpointClass = NULL;
jfieldID attrId = NULL;
jobject protocolObj = NULL;
void endpoint_set_protocol(JNIEnv* env, jobject endpoint, roc_protocol protocol) {
jclass endpointClass = NULL;
jfieldID attrId = NULL;
jobject protocolObj = NULL;

endpointClass = env->FindClass(ENDPOINT_CLASS);
assert(endpointClass != NULL);
Expand All @@ -89,9 +91,9 @@ void endpoint_set_protocol(JNIEnv *env, jobject endpoint, roc_protocol protocol)
env->SetObjectField(endpoint, attrId, protocolObj);
}

void endpoint_set_host(JNIEnv *env, jobject endpoint, const char* buf) {
jclass endpointClass = NULL;
jfieldID attrId = NULL;
void endpoint_set_host(JNIEnv* env, jobject endpoint, const char* buf) {
jclass endpointClass = NULL;
jfieldID attrId = NULL;

endpointClass = env->FindClass(ENDPOINT_CLASS);
assert(endpointClass != NULL);
Expand All @@ -101,9 +103,9 @@ void endpoint_set_host(JNIEnv *env, jobject endpoint, const char* buf) {
env->SetObjectField(endpoint, attrId, env->NewStringUTF(buf));
}

void endpoint_set_port(JNIEnv *env, jobject endpoint, int port) {
jclass endpointClass = NULL;
jfieldID attrId = NULL;
void endpoint_set_port(JNIEnv* env, jobject endpoint, int port) {
jclass endpointClass = NULL;
jfieldID attrId = NULL;

endpointClass = env->FindClass(ENDPOINT_CLASS);
assert(endpointClass != NULL);
Expand All @@ -113,9 +115,9 @@ void endpoint_set_port(JNIEnv *env, jobject endpoint, int port) {
env->SetIntField(endpoint, attrId, port);
}

void endpoint_set_resource(JNIEnv *env, jobject endpoint, const char* buf) {
jclass endpointClass = NULL;
jfieldID attrId = NULL;
void endpoint_set_resource(JNIEnv* env, jobject endpoint, const char* buf) {
jclass endpointClass = NULL;
jfieldID attrId = NULL;

endpointClass = env->FindClass(ENDPOINT_CLASS);
assert(endpointClass != NULL);
Expand All @@ -125,22 +127,23 @@ void endpoint_set_resource(JNIEnv *env, jobject endpoint, const char* buf) {
env->SetObjectField(endpoint, attrId, env->NewStringUTF(buf));
}

JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_init(JNIEnv *env, jobject thisObj, jstring juri) {
roc_endpoint* endpoint = NULL;
const char* uri = NULL;
jclass endpointClass = NULL;
roc_protocol protocol = (roc_protocol)0;
int port = 0;
char* buf = NULL;
size_t bufsz = 0;
JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_init(
JNIEnv* env, jobject thisObj, jstring juri) {
roc_endpoint* endpoint = NULL;
const char* uri = NULL;
jclass endpointClass = NULL;
roc_protocol protocol = (roc_protocol) 0;
int port = 0;
char* buf = NULL;
size_t bufsz = 0;

if (juri == NULL) {
jclass exceptionClass = env->FindClass(ILLEGAL_ARGUMENTS_EXCEPTION);
env->ThrowNew(exceptionClass, "Bad uri argument");
return;
}

if (roc_endpoint_allocate(&endpoint) != 0 ) {
if (roc_endpoint_allocate(&endpoint) != 0) {
jclass exceptionClass = env->FindClass(EXCEPTION);
env->ThrowNew(exceptionClass, "Can't allocate roc_endpoint");
return;
Expand Down Expand Up @@ -173,7 +176,7 @@ JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_init(JNIEnv *en
env->ThrowNew(exceptionClass, "Can't get host from endpoint");
return;
}
buf = (char*)malloc(bufsz);
buf = (char*) malloc(bufsz);
if (roc_endpoint_get_host(endpoint, buf, &bufsz) != 0) {
free(buf);
roc_endpoint_deallocate(endpoint);
Expand All @@ -192,7 +195,7 @@ JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_init(JNIEnv *en
}

if (roc_endpoint_get_resource(endpoint, NULL, &bufsz) == 0) {
buf = (char*)malloc(bufsz);
buf = (char*) malloc(bufsz);
if (roc_endpoint_get_resource(endpoint, buf, &bufsz) == 0) {
endpoint_set_resource(env, thisObj, buf);
}
Expand All @@ -202,11 +205,12 @@ JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_init(JNIEnv *en
roc_endpoint_deallocate(endpoint);
}

JNIEXPORT jstring JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_getUri(JNIEnv *env, jobject thisObj) {
roc_endpoint* endpoint = NULL;
jstring jstr = NULL;
char* buf = NULL;
size_t bufsz = 0;
JNIEXPORT jstring JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_getUri(
JNIEnv* env, jobject thisObj) {
roc_endpoint* endpoint = NULL;
jstring jstr = NULL;
char* buf = NULL;
size_t bufsz = 0;

if (endpoint_unmarshal(env, &endpoint, thisObj) != 0) {
jclass exceptionClass = env->FindClass(EXCEPTION);
Expand All @@ -220,7 +224,7 @@ JNIEXPORT jstring JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_getUri(JNIEn
env->ThrowNew(exceptionClass, "Can't get uri");
return NULL;
}
buf = (char*)malloc(bufsz);
buf = (char*) malloc(bufsz);
if (roc_endpoint_get_uri(endpoint, buf, &bufsz) != 0) {
free(buf);
roc_endpoint_deallocate(endpoint);
Expand All @@ -235,10 +239,11 @@ JNIEXPORT jstring JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_getUri(JNIEn
return jstr;
}

JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_validate(JNIEnv *env, jobject thisObj) {
JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_validate(
JNIEnv* env, jobject thisObj) {
roc_endpoint* endpoint = NULL;
char* buf = NULL;
size_t bufsz = 0;
char* buf = NULL;
size_t bufsz = 0;

if (endpoint_unmarshal(env, &endpoint, thisObj) != 0) {
jclass exceptionClass = env->FindClass(EXCEPTION);
Expand All @@ -251,7 +256,7 @@ JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_validate(JNIEnv
env->ThrowNew(exceptionClass, "Invalid roc_endpoint");
return;
}
buf = (char*)malloc(bufsz);
buf = (char*) malloc(bufsz);
if (roc_endpoint_get_uri(endpoint, buf, &bufsz) != 0) {
free(buf);
roc_endpoint_deallocate(endpoint);
Expand All @@ -261,4 +266,4 @@ JNIEXPORT void JNICALL Java_org_rocstreaming_roctoolkit_Endpoint_validate(JNIEnv
}
roc_endpoint_deallocate(endpoint);
free(buf);
}
}
Loading