From 3c8a5831ab3290356772f64d9c853b017a6281eb Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 6 Oct 2020 18:42:38 -0400 Subject: [PATCH] Make slice-vec-len generic --- genbindings.py | 9 +++++++-- src/main/java/org/ldk/impl/bindings.java | 2 +- src/main/jni/bindings.c | 7 ++++++- src/main/jni/org_ldk_impl_bindings.h | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/genbindings.py b/genbindings.py index a5e9ec41..9c647a63 100755 --- a/genbindings.py +++ b/genbindings.py @@ -544,7 +544,7 @@ void __attribute__((destructor)) check_leaks() { public static native void free_heap_ptr(long ptr); public static native byte[] get_u8_slice_bytes(long slice_ptr); public static native long bytes_to_u8_vec(byte[] bytes); - public static native long u8_vec_len(long vec); + public static native long vec_slice_len(long vec); """) out_c.write(""" @@ -576,7 +576,12 @@ JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec (JNIEnv * _ (*_env)->GetByteArrayRegion (_env, bytes, 0, vec->datalen, vec->data); return (long)vec; } -JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_u8_1vec_1len (JNIEnv * env, jclass _a, jlong ptr) { +JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env, jclass _a, jlong ptr) { + // Check offsets of a few Vec types are all consistent as we're meant to be generic across types + _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_SignatureZ, datalen), "Vec<*> needs to be mapped identically"); + _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_MessageSendEventZ, datalen), "Vec<*> needs to be mapped identically"); + _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_EventZ, datalen), "Vec<*> needs to be mapped identically"); + _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_C2Tuple_usizeTransactionZZ, datalen), "Vec<*> needs to be mapped identically"); LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr; return (long)vec->datalen; } diff --git a/src/main/java/org/ldk/impl/bindings.java b/src/main/java/org/ldk/impl/bindings.java index b9d228b0..f4eef6ba 100644 --- a/src/main/java/org/ldk/impl/bindings.java +++ b/src/main/java/org/ldk/impl/bindings.java @@ -13,7 +13,7 @@ public class bindings { public static native void free_heap_ptr(long ptr); public static native byte[] get_u8_slice_bytes(long slice_ptr); public static native long bytes_to_u8_vec(byte[] bytes); - public static native long u8_vec_len(long vec); + public static native long vec_slice_len(long vec); public static native long LDKSecretKey_new(); diff --git a/src/main/jni/bindings.c b/src/main/jni/bindings.c index 732c134c..d8f24ee2 100644 --- a/src/main/jni/bindings.c +++ b/src/main/jni/bindings.c @@ -77,7 +77,12 @@ JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec (JNIEnv * _ (*_env)->GetByteArrayRegion (_env, bytes, 0, vec->datalen, vec->data); return (long)vec; } -JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_u8_1vec_1len (JNIEnv * env, jclass _a, jlong ptr) { +JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env, jclass _a, jlong ptr) { + // Check offsets of a few Vec types are all consistent as we're meant to be generic across types + _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_SignatureZ, datalen), "Vec<*> needs to be mapped identically"); + _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_MessageSendEventZ, datalen), "Vec<*> needs to be mapped identically"); + _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_EventZ, datalen), "Vec<*> needs to be mapped identically"); + _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_C2Tuple_usizeTransactionZZ, datalen), "Vec<*> needs to be mapped identically"); LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr; return (long)vec->datalen; } diff --git a/src/main/jni/org_ldk_impl_bindings.h b/src/main/jni/org_ldk_impl_bindings.h index 44fec81e..557d5443 100644 --- a/src/main/jni/org_ldk_impl_bindings.h +++ b/src/main/jni/org_ldk_impl_bindings.h @@ -57,10 +57,10 @@ JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec /* * Class: org_ldk_impl_bindings - * Method: u8_vec_len + * Method: vec_slice_len * Signature: (J)J */ -JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_u8_1vec_1len +JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv *, jclass, jlong); /* -- 2.39.5