@@ -302,8 +302,8 @@ def java_c_types(fn_arg, ret_arr_len):
302302 arr_ty = "uint64_t"
303303 fn_arg = fn_arg [8 :].strip ()
304304 else :
305- java_ty = consts .ptr_native_ty
306- c_ty = consts .ptr_c_ty
305+ java_ty = consts .usize_native_ty
306+ c_ty = consts .usize_c_ty
307307 arr_ty = "uintptr_t"
308308 rust_obj = "uintptr_t"
309309 fn_arg = fn_arg [9 :].strip ()
@@ -434,7 +434,66 @@ def java_c_types(fn_arg, ret_arr_len):
434434
435435# Define some manual clones...
436436clone_fns .add ("ThirtyTwoBytes_clone" )
437- write_c ("static inline struct LDKThirtyTwoBytes ThirtyTwoBytes_clone(const struct LDKThirtyTwoBytes *orig) { struct LDKThirtyTwoBytes ret; memcpy(ret.data, orig->data, 32); return ret; }\n " )
437+ write_c ("static inline struct LDKThirtyTwoBytes ThirtyTwoBytes_clone(const struct LDKThirtyTwoBytes *orig) { struct LDKThirtyTwoBytes ret; memcpy(ret.data, orig->data, 32); return ret; }\n \n " )
438+
439+
440+ write_c ("static inline void* untag_ptr(uint64_t ptr) {\n " )
441+ write_c ("\t if (ptr < 4096) return (void*)ptr;\n " )
442+ write_c ("\t if (sizeof(void*) == 4) {\n " )
443+ write_c ("\t \t // For 32-bit systems, store pointers as 64-bit ints and use the 31st bit\n " )
444+ write_c ("\t \t return (void*)(uintptr_t)ptr;\n " )
445+ write_c ("\t } else {\n " )
446+ write_c ("\t \t // For 64-bit systems, assume the top byte is used for tagging, then\n " )
447+ write_c ("\t \t // use bit 9 ^ bit 10.\n " )
448+ write_c ("\t \t uint64_t tenth_bit = (((uintptr_t)ptr) & (1ULL << 54)) >> 54;\n " )
449+ write_c ("\t \t uintptr_t p = (ptr & ~(1ULL << 55)) | (tenth_bit << 55);\n " )
450+ write_c ("#ifdef LDK_DEBUG_BUILD\n " )
451+ write_c ("\t \t // On debug builds we also use the 11th bit as a debug flag\n " )
452+ write_c ("\t \t uintptr_t eleventh_bit = (((uintptr_t)ptr) & (1ULL << 53)) >> 53;\n " )
453+ write_c ("\t \t CHECK(tenth_bit != eleventh_bit);\n " )
454+ write_c ("\t \t p ^= 1ULL << 53;\n " )
455+ write_c ("#endif\n " )
456+ write_c ("\t \t return (void*)p;\n " )
457+ write_c ("\t }\n " )
458+ write_c ("}\n " )
459+
460+ write_c ("static inline bool ptr_is_owned(uint64_t ptr) {\n " )
461+ write_c ("\t if(ptr < 4096) return true;\n " )
462+ write_c ("\t if (sizeof(void*) == 4) {\n " )
463+ write_c ("\t \t return ptr & (1ULL << 32);\n " )
464+ write_c ("\t } else {\n " )
465+ write_c ("\t \t uintptr_t ninth_bit = (((uintptr_t)ptr) & (1ULL << 55)) >> 55;\n " )
466+ write_c ("\t \t uintptr_t tenth_bit = (((uintptr_t)ptr) & (1ULL << 54)) >> 54;\n " )
467+ write_c ("#ifdef LDK_DEBUG_BUILD\n " )
468+ write_c ("\t \t // On debug builds we also use the 11th bit as a debug flag\n " )
469+ write_c ("\t \t uintptr_t eleventh_bit = (((uintptr_t)ptr) & (1ULL << 53)) >> 53;\n " )
470+ write_c ("\t \t CHECK(tenth_bit != eleventh_bit);\n " )
471+ write_c ("#endif\n " )
472+ write_c ("\t \t return (ninth_bit ^ tenth_bit) ? true : false;\n " )
473+ write_c ("\t }\n " )
474+ write_c ("}\n " )
475+
476+ write_c ("static inline uint64_t tag_ptr(const void* ptr, bool is_owned) {\n " )
477+ write_c ("\t if ((uintptr_t)ptr < 4096) return (uint64_t)ptr;\n " )
478+ write_c ("\t if (sizeof(void*) == 4) {\n " )
479+ write_c ("\t \t return (((uint64_t)ptr) | ((is_owned ? 1ULL : 0) << 32));\n " )
480+ write_c ("\t } else {\n " )
481+ write_c ("\t \t CHECK(sizeof(uintptr_t) == 8);\n " )
482+ write_c ("\t \t uintptr_t tenth_bit = (((uintptr_t)ptr) & (1ULL << 54)) >> 54;\n " )
483+ write_c ("\t \t uintptr_t t = (((uintptr_t)ptr) | (((is_owned ? 1ULL : 0ULL) ^ tenth_bit) << 55));\n " )
484+ write_c ("#ifdef LDK_DEBUG_BUILD\n " )
485+ write_c ("\t \t uintptr_t ninth_bit = (((uintptr_t)ptr) & (1ULL << 55)) >> 55;\n " )
486+ write_c ("\t \t uintptr_t eleventh_bit = (((uintptr_t)ptr) & (1ULL << 53)) >> 53;\n " )
487+ write_c ("\t \t CHECK(ninth_bit == tenth_bit);\n " )
488+ write_c ("\t \t CHECK(ninth_bit == eleventh_bit);\n " )
489+ write_c ("\t \t t ^= 1ULL << 53;\n " )
490+ write_c ("#endif\n " )
491+ write_c ("\t \t CHECK(ptr_is_owned(t) == is_owned);\n " )
492+ write_c ("\t \t CHECK(untag_ptr(t) == ptr);\n " )
493+ #write_c("\t\tCHECK(untag_ptr((uintptr_t)untag_ptr(t)) == ptr);\n")
494+ write_c ("\t \t return t;\n " )
495+ write_c ("\t }\n " )
496+ write_c ("}\n \n " )
438497
439498java_c_types_none_allowed = False # C structs created by cbindgen are declared in dependency order
440499
@@ -480,13 +539,13 @@ def map_fn_with_ref_option(line, re_match, ret_arr_len, c_call_string, doc_comme
480539 return_type_info = type_mapping_generator .map_type (method_return_type .strip () + " ret" , True , ret_arr_len , False , force_holds_ref )
481540
482541 if method_name .endswith ("_clone" ) and expected_struct not in unitary_enums :
483- meth_line = "uintptr_t " + expected_struct .replace ("LDK" , "" ) + "_clone_ptr(" + expected_struct + " *NONNULL_PTR arg)"
542+ meth_line = "uint64_t " + expected_struct .replace ("LDK" , "" ) + "_clone_ptr(" + expected_struct + " *NONNULL_PTR arg)"
484543 write_c ("static inline " + meth_line + " {\n " )
485544 write_c ("\t " + return_type_info .ret_conv [0 ].replace ("\n " , "\n \t " ))
486545 write_c (method_name + "(arg)" )
487- write_c (return_type_info .ret_conv [1 ])
546+ write_c (return_type_info .ret_conv [1 ]. replace ( " \n " , " \n \t " ) )
488547 write_c ("\n \t return " + return_type_info .ret_conv_name + ";\n }\n " )
489- map_fn (meth_line + ";\n " , re .compile ("(uintptr_t ) ([A-Za-z_0-9]*)\((.*)\)" ).match (meth_line ), None , None , None )
548+ map_fn (meth_line + ";\n " , re .compile ("(uint64_t ) ([A-Za-z_0-9]*)\((.*)\)" ).match (meth_line ), None , None , None )
490549
491550 argument_types = []
492551 default_constructor_args = {}
@@ -575,7 +634,7 @@ def map_fn_with_ref_option(line, re_match, ret_arr_len, c_call_string, doc_comme
575634 assert return_type_info .c_ty == "void"
576635 write_c (consts .c_fn_ty_pfx + "void " + consts .c_fn_name_define_pfx (method_name , True ) + argument_types [0 ].c_ty + " " + argument_types [0 ].ty_info .var_name + ") {\n " )
577636 if argument_types [0 ].ty_info .passed_as_ptr and not argument_types [0 ].ty_info .rust_obj in opaque_structs :
578- write_c ("\t if ((" + argument_types [0 ].ty_info .var_name + " & 1) != 0 ) return;\n " )
637+ write_c ("\t if (!ptr_is_owned (" + argument_types [0 ].ty_info .var_name + ") ) return;\n " )
579638
580639 for info in argument_types :
581640 if info .arg_conv is not None :
0 commit comments