@@ -15,42 +15,18 @@ extern void go_notify_warning_unset_bridge(void* user_data, btck_Warning warning
15
15
extern void go_notify_flush_error_bridge(void* user_data, const char* message, size_t message_len);
16
16
extern void go_notify_fatal_error_bridge(void* user_data, const char* message, size_t message_len);
17
17
extern void go_validation_interface_block_checked_bridge(void* user_data, const btck_BlockPointer* block, const btck_BlockValidationState* state);
18
-
19
- // Wrapper function: C helper to set notifications with Go callbacks
20
- // Converts Handle ID to void* and passes to C library
21
- static inline void set_notifications_wrapper(btck_ContextOptions* opts, uintptr_t handle) {
22
- btck_NotificationInterfaceCallbacks callbacks = {
23
- .user_data = (void*)handle,
24
- .block_tip = (btck_NotifyBlockTip)go_notify_block_tip_bridge,
25
- .header_tip = (btck_NotifyHeaderTip)go_notify_header_tip_bridge,
26
- .progress = (btck_NotifyProgress)go_notify_progress_bridge,
27
- .warning_set = (btck_NotifyWarningSet)go_notify_warning_set_bridge,
28
- .warning_unset = (btck_NotifyWarningUnset)go_notify_warning_unset_bridge,
29
- .flush_error = (btck_NotifyFlushError)go_notify_flush_error_bridge,
30
- .fatal_error = (btck_NotifyFatalError)go_notify_fatal_error_bridge,
31
- };
32
- btck_context_options_set_notifications(opts, callbacks);
33
- }
34
-
35
- // Wrapper function: C helper to set validation interface with Go callbacks
36
- // Converts Handle ID to void* and passes to C library
37
- static inline void set_validation_interface_wrapper(btck_ContextOptions* opts, uintptr_t handle) {
38
- btck_ValidationInterfaceCallbacks callbacks = {
39
- .user_data = (void*)handle,
40
- .block_checked = (btck_ValidationInterfaceBlockChecked)go_validation_interface_block_checked_bridge,
41
- };
42
- btck_context_options_set_validation_interface(opts, callbacks);
43
- }
44
18
*/
45
19
import "C"
46
20
import (
47
21
"runtime"
48
22
"runtime/cgo"
23
+ "unsafe"
49
24
)
50
25
51
26
var _ cManagedResource = & ContextOptions {}
52
27
53
- // ContextOptions wraps the C btck_ContextOptions
28
+ // ContextOptions wraps the C btck_ContextOptions.
29
+ // Once the options is set on a context, the context is responsible for its lifetime and should not be destroyed manually.
54
30
type ContextOptions struct {
55
31
ptr * C.btck_ContextOptions
56
32
notificationHandle cgo.Handle // Prevents notification callbacks GC until Destroy() called
@@ -87,15 +63,29 @@ func (opts *ContextOptions) SetNotifications(callbacks *NotificationCallbacks) e
87
63
return ErrNilNotificationCallbacks
88
64
}
89
65
66
+ // Clean up existing handle if present
67
+ if opts .notificationHandle != 0 {
68
+ opts .notificationHandle .Delete ()
69
+ opts .notificationHandle = 0
70
+ }
71
+
90
72
// Create a handle for the callbacks - this prevents garbage collection
91
73
// and provides a stable ID that can be passed through C code safely
92
- handle := cgo .NewHandle (callbacks )
93
-
94
- // Call the C wrapper function to set all notification callbacks
95
- C .set_notifications_wrapper (opts .ptr , C .uintptr_t (handle ))
96
-
97
- // Store the handle to prevent GC and allow cleanup
98
- opts .notificationHandle = handle
74
+ opts .notificationHandle = cgo .NewHandle (callbacks )
75
+
76
+ // Create notification callbacks struct and call C library directly
77
+ notificationCallbacks := C.btck_NotificationInterfaceCallbacks {
78
+ user_data : unsafe .Pointer (opts .notificationHandle ),
79
+ user_data_destroy : nil , // Go handles memory management via cgo.Handle
80
+ block_tip : C .btck_NotifyBlockTip (C .go_notify_block_tip_bridge ),
81
+ header_tip : C .btck_NotifyHeaderTip (C .go_notify_header_tip_bridge ),
82
+ progress : C .btck_NotifyProgress (C .go_notify_progress_bridge ),
83
+ warning_set : C .btck_NotifyWarningSet (C .go_notify_warning_set_bridge ),
84
+ warning_unset : C .btck_NotifyWarningUnset (C .go_notify_warning_unset_bridge ),
85
+ flush_error : C .btck_NotifyFlushError (C .go_notify_flush_error_bridge ),
86
+ fatal_error : C .btck_NotifyFatalError (C .go_notify_fatal_error_bridge ),
87
+ }
88
+ C .btck_context_options_set_notifications (opts .ptr , notificationCallbacks )
99
89
return nil
100
90
}
101
91
@@ -107,15 +97,23 @@ func (opts *ContextOptions) SetValidationInterface(callbacks *ValidationInterfac
107
97
return ErrNilValidationInterfaceCallbacks
108
98
}
109
99
100
+ // Clean up existing handle if present
101
+ if opts .validationHandle != 0 {
102
+ opts .validationHandle .Delete ()
103
+ opts .validationHandle = 0
104
+ }
105
+
110
106
// Create a handle for the callbacks - this prevents garbage collection
111
107
// and provides a stable ID that can be passed through C code safely
112
- handle : = cgo .NewHandle (callbacks )
108
+ opts . validationHandle = cgo .NewHandle (callbacks )
113
109
114
- // Call the C wrapper function to set all validation interface callbacks
115
- C .set_validation_interface_wrapper (opts .ptr , C .uintptr_t (handle ))
116
-
117
- // Store the handle to prevent GC and allow cleanup
118
- opts .validationHandle = handle
110
+ // Create validation callbacks struct and call C library directly
111
+ validationCallbacks := C.btck_ValidationInterfaceCallbacks {
112
+ user_data : unsafe .Pointer (opts .validationHandle ),
113
+ user_data_destroy : nil , // Go handles memory management via cgo.Handle
114
+ block_checked : C .btck_ValidationInterfaceBlockChecked (C .go_validation_interface_block_checked_bridge ),
115
+ }
116
+ C .btck_context_options_set_validation_interface (opts .ptr , validationCallbacks )
119
117
return nil
120
118
}
121
119
0 commit comments