diff --git a/shared_directory_arm64.go b/shared_directory_arm64.go index f317051..fe4444f 100644 --- a/shared_directory_arm64.go +++ b/shared_directory_arm64.go @@ -33,10 +33,10 @@ const ( ) //export linuxInstallRosettaWithCompletionHandler -func linuxInstallRosettaWithCompletionHandler(cgoHandlerPtr, errPtr unsafe.Pointer) { - cgoHandler := *(*cgo.Handle)(cgoHandlerPtr) +func linuxInstallRosettaWithCompletionHandler(cgoHandleUintptr C.uintptr_t, errPtr unsafe.Pointer) { + cgoHandle := cgo.Handle(cgoHandleUintptr) - handler := cgoHandler.Value().(func(error)) + handler := cgoHandle.Value().(func(error)) if err := newNSError(errPtr); err != nil { handler(err) @@ -89,10 +89,10 @@ func LinuxRosettaDirectoryShareInstallRosetta() error { return err } errCh := make(chan error, 1) - cgoHandler := cgo.NewHandle(func(err error) { + cgoHandle := cgo.NewHandle(func(err error) { errCh <- err }) - C.linuxInstallRosetta(unsafe.Pointer(&cgoHandler)) + C.linuxInstallRosetta(C.uintptr_t(cgoHandle)) return <-errCh } diff --git a/socket.go b/socket.go index 144fec2..a4b3089 100644 --- a/socket.go +++ b/socket.go @@ -97,17 +97,17 @@ func (v *VirtioSocketDevice) Listen(port uint32) (*VirtioSocketListener, error) ch := make(chan connResults, 1) // should I increase more caps? - handler := cgo.NewHandle(func(conn *VirtioSocketConnection, err error) { + handle := cgo.NewHandle(func(conn *VirtioSocketConnection, err error) { ch <- connResults{conn, err} }) ptr := C.newVZVirtioSocketListener( - unsafe.Pointer(&handler), + C.uintptr_t(handle), ) listener := &VirtioSocketListener{ pointer: objc.NewPointer(ptr), vsockDevice: v, port: port, - handler: handler, + handle: handle, acceptch: ch, } @@ -122,10 +122,10 @@ func (v *VirtioSocketDevice) Listen(port uint32) (*VirtioSocketListener, error) } //export connectionHandler -func connectionHandler(connPtr, errPtr, cgoHandlerPtr unsafe.Pointer) { - cgoHandler := *(*cgo.Handle)(cgoHandlerPtr) - handler := cgoHandler.Value().(func(*VirtioSocketConnection, error)) - defer cgoHandler.Delete() +func connectionHandler(connPtr, errPtr unsafe.Pointer, cgoHandleUintptr C.uintptr_t) { + cgoHandle := cgo.Handle(cgoHandleUintptr) + handler := cgoHandle.Value().(func(*VirtioSocketConnection, error)) + defer cgoHandle.Delete() // see: startHandler if err := newNSError(errPtr); err != nil { handler(nil, err) @@ -144,7 +144,7 @@ func connectionHandler(connPtr, errPtr, cgoHandlerPtr unsafe.Pointer) { // see: https://developer.apple.com/documentation/virtualization/vzvirtiosocketdevice/3656677-connecttoport?language=objc func (v *VirtioSocketDevice) Connect(port uint32) (*VirtioSocketConnection, error) { ch := make(chan connResults, 1) - cgoHandler := cgo.NewHandle(func(conn *VirtioSocketConnection, err error) { + cgoHandle := cgo.NewHandle(func(conn *VirtioSocketConnection, err error) { ch <- connResults{conn, err} close(ch) }) @@ -152,7 +152,7 @@ func (v *VirtioSocketDevice) Connect(port uint32) (*VirtioSocketConnection, erro objc.Ptr(v), v.dispatchQueue, C.uint32_t(port), - unsafe.Pointer(&cgoHandler), + C.uintptr_t(cgoHandle), ) result := <-ch runtime.KeepAlive(v) @@ -170,7 +170,7 @@ type connResults struct { type VirtioSocketListener struct { *pointer vsockDevice *VirtioSocketDevice - handler cgo.Handle + handle cgo.Handle port uint32 acceptch chan connResults closeOnce sync.Once @@ -197,7 +197,7 @@ func (v *VirtioSocketListener) Close() error { v.vsockDevice.dispatchQueue, C.uint32_t(v.port), ) - v.handler.Delete() + v.handle.Delete() }) return nil } @@ -226,9 +226,9 @@ func (a *VirtioSocketListenerAddr) Network() string { return "vsock" } func (a *VirtioSocketListenerAddr) String() string { return fmt.Sprintf("%d:%d", a.CID, a.Port) } //export shouldAcceptNewConnectionHandler -func shouldAcceptNewConnectionHandler(cgoHandlerPtr, connPtr, devicePtr unsafe.Pointer) C.bool { - cgoHandler := *(*cgo.Handle)(cgoHandlerPtr) - handler := cgoHandler.Value().(func(*VirtioSocketConnection, error)) +func shouldAcceptNewConnectionHandler(cgoHandleUintptr C.uintptr_t, connPtr, devicePtr unsafe.Pointer) C.bool { + cgoHandle := cgo.Handle(cgoHandleUintptr) + handler := cgoHandle.Value().(func(*VirtioSocketConnection, error)) // see: startHandler conn, err := newVirtioSocketConnection(connPtr) diff --git a/virtualization.go b/virtualization.go index 00c79cb..97c19fa 100644 --- a/virtualization.go +++ b/virtualization.go @@ -75,7 +75,7 @@ type VirtualMachine struct { *pointer dispatchQueue unsafe.Pointer - stateHandle *machineState + machineState *machineState finalizeOnce sync.Once } @@ -103,28 +103,28 @@ func NewVirtualMachine(config *VirtualMachineConfiguration) (*VirtualMachine, er cs := (*char)(objc.GetUUID()) dispatchQueue := C.makeDispatchQueue(cs.CString()) - stateHandle := &machineState{ + machineState := &machineState{ state: VirtualMachineState(0), stateNotify: infinity.NewChannel[VirtualMachineState](), } - stateHandlePtr := cgo.NewHandle(stateHandle) + stateHandle := cgo.NewHandle(machineState) v := &VirtualMachine{ id: cs.String(), pointer: objc.NewPointer( C.newVZVirtualMachineWithDispatchQueue( objc.Ptr(config), dispatchQueue, - unsafe.Pointer(&stateHandlePtr), + C.uintptr_t(stateHandle), ), ), dispatchQueue: dispatchQueue, - stateHandle: stateHandle, + machineState: machineState, } objc.SetFinalizer(v, func(self *VirtualMachine) { self.finalize() - stateHandlePtr.Delete() + stateHandle.Delete() }) return v, nil } @@ -155,11 +155,11 @@ func (v *VirtualMachine) SocketDevices() []*VirtioSocketDevice { } //export changeStateOnObserver -func changeStateOnObserver(newStateRaw C.int, cgoHandlerPtr unsafe.Pointer) { - stateHandler := *(*cgo.Handle)(cgoHandlerPtr) +func changeStateOnObserver(newStateRaw C.int, cgoHandleUintptr C.uintptr_t) { + stateHandle := cgo.Handle(cgoHandleUintptr) // I expected it will not cause panic. // if caused panic, that's unexpected behavior. - v, _ := stateHandler.Value().(*machineState) + v, _ := stateHandle.Value().(*machineState) v.mu.Lock() newState := VirtualMachineState(newStateRaw) v.state = newState @@ -169,16 +169,16 @@ func changeStateOnObserver(newStateRaw C.int, cgoHandlerPtr unsafe.Pointer) { // State represents execution state of the virtual machine. func (v *VirtualMachine) State() VirtualMachineState { - v.stateHandle.mu.RLock() - defer v.stateHandle.mu.RUnlock() - return v.stateHandle.state + v.machineState.mu.RLock() + defer v.machineState.mu.RUnlock() + return v.machineState.state } // StateChangedNotify gets notification is changed execution state of the virtual machine. func (v *VirtualMachine) StateChangedNotify() <-chan VirtualMachineState { - v.stateHandle.mu.RLock() - defer v.stateHandle.mu.RUnlock() - return v.stateHandle.stateNotify.Out() + v.machineState.mu.RLock() + defer v.machineState.mu.RUnlock() + return v.machineState.stateNotify.Out() } // CanStart returns true if the machine is in a state that can be started. @@ -213,10 +213,10 @@ func (v *VirtualMachine) CanStop() bool { } //export virtualMachineCompletionHandler -func virtualMachineCompletionHandler(cgoHandlerPtr, errPtr unsafe.Pointer) { - cgoHandler := *(*cgo.Handle)(cgoHandlerPtr) +func virtualMachineCompletionHandler(cgoHandleUintptr C.uintptr_t, errPtr unsafe.Pointer) { + cgoHandle := cgo.Handle(cgoHandleUintptr) - handler := cgoHandler.Value().(func(error)) + handler := cgoHandle.Value().(func(error)) if err := newNSError(errPtr); err != nil { handler(err) @@ -255,18 +255,18 @@ func (v *VirtualMachine) Start(opts ...VirtualMachineStartOption) error { } h, errCh := makeHandler() - handler := cgo.NewHandle(h) - defer handler.Delete() + handle := cgo.NewHandle(h) + defer handle.Delete() if o.macOSVirtualMachineStartOptionsPtr != nil { C.startWithOptionsCompletionHandler( objc.Ptr(v), v.dispatchQueue, o.macOSVirtualMachineStartOptionsPtr, - unsafe.Pointer(&handler), + C.uintptr_t(handle), ) } else { - C.startWithCompletionHandler(objc.Ptr(v), v.dispatchQueue, unsafe.Pointer(&handler)) + C.startWithCompletionHandler(objc.Ptr(v), v.dispatchQueue, C.uintptr_t(handle)) } return <-errCh } @@ -276,9 +276,9 @@ func (v *VirtualMachine) Start(opts ...VirtualMachineStartOption) error { // If you want to listen status change events, use the "StateChangedNotify" method. func (v *VirtualMachine) Pause() error { h, errCh := makeHandler() - handler := cgo.NewHandle(h) - defer handler.Delete() - C.pauseWithCompletionHandler(objc.Ptr(v), v.dispatchQueue, unsafe.Pointer(&handler)) + handle := cgo.NewHandle(h) + defer handle.Delete() + C.pauseWithCompletionHandler(objc.Ptr(v), v.dispatchQueue, C.uintptr_t(handle)) return <-errCh } @@ -287,9 +287,9 @@ func (v *VirtualMachine) Pause() error { // If you want to listen status change events, use the "StateChangedNotify" method. func (v *VirtualMachine) Resume() error { h, errCh := makeHandler() - handler := cgo.NewHandle(h) - defer handler.Delete() - C.resumeWithCompletionHandler(objc.Ptr(v), v.dispatchQueue, unsafe.Pointer(&handler)) + handle := cgo.NewHandle(h) + defer handle.Delete() + C.resumeWithCompletionHandler(objc.Ptr(v), v.dispatchQueue, C.uintptr_t(handle)) return <-errCh } @@ -322,9 +322,9 @@ func (v *VirtualMachine) Stop() error { return err } h, errCh := makeHandler() - handler := cgo.NewHandle(h) - defer handler.Delete() - C.stopWithCompletionHandler(objc.Ptr(v), v.dispatchQueue, unsafe.Pointer(&handler)) + handle := cgo.NewHandle(h) + defer handle.Delete() + C.stopWithCompletionHandler(objc.Ptr(v), v.dispatchQueue, C.uintptr_t(handle)) return <-errCh } diff --git a/virtualization_11.h b/virtualization_11.h index c92f827..61b5d79 100644 --- a/virtualization_11.h +++ b/virtualization_11.h @@ -10,9 +10,9 @@ #import /* exported from cgo */ -void connectionHandler(void *connection, void *err, void *cgoHandlerPtr); -void changeStateOnObserver(int state, void *cgoHandler); -bool shouldAcceptNewConnectionHandler(void *cgoHandler, void *connection, void *socketDevice); +void connectionHandler(void *connection, void *err, uintptr_t cgoHandle); +void changeStateOnObserver(int state, uintptr_t cgoHandle); +bool shouldAcceptNewConnectionHandler(uintptr_t cgoHandle, void *connection, void *socketDevice); @interface Observer : NSObject - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; @@ -21,13 +21,13 @@ bool shouldAcceptNewConnectionHandler(void *cgoHandler, void *connection, void * @interface ObservableVZVirtualMachine : VZVirtualMachine - (instancetype)initWithConfiguration:(VZVirtualMachineConfiguration *)configuration queue:(dispatch_queue_t)queue - statusHandler:(void *)statusHandler; + statusUpdateHandle:(uintptr_t)statusUpdateHandle; - (void)dealloc; @end /* VZVirtioSocketListener */ @interface VZVirtioSocketListenerDelegateImpl : NSObject -- (instancetype)initWithHandler:(void *)cgoHandler; +- (instancetype)initWithHandle:(uintptr_t)cgoHandle; - (BOOL)listener:(VZVirtioSocketListener *)listener shouldAcceptNewConnection:(VZVirtioSocketConnection *)connection fromSocketDevice:(VZVirtioSocketDevice *)socketDevice; @end @@ -77,18 +77,18 @@ void *newVZVirtioSocketDeviceConfiguration(); void *newVZMACAddress(const char *macAddress); void *newRandomLocallyAdministeredVZMACAddress(); const char *getVZMACAddressString(void *macAddress); -void *newVZVirtioSocketListener(void *cgoHandlerPtr); +void *newVZVirtioSocketListener(uintptr_t cgoHandle); void *VZVirtualMachine_socketDevices(void *machine); void VZVirtioSocketDevice_setSocketListenerForPort(void *socketDevice, void *vmQueue, void *listener, uint32_t port); void VZVirtioSocketDevice_removeSocketListenerForPort(void *socketDevice, void *vmQueue, uint32_t port); -void VZVirtioSocketDevice_connectToPort(void *socketDevice, void *vmQueue, uint32_t port, void *cgoHandlerPtr); +void VZVirtioSocketDevice_connectToPort(void *socketDevice, void *vmQueue, uint32_t port, uintptr_t cgoHandle); /* VirtualMachine */ -void *newVZVirtualMachineWithDispatchQueue(void *config, void *queue, void *statusHandler); +void *newVZVirtualMachineWithDispatchQueue(void *config, void *queue, uintptr_t cgoHandle); bool requestStopVirtualMachine(void *machine, void *queue, void **error); -void startWithCompletionHandler(void *machine, void *queue, void *completionHandler); -void pauseWithCompletionHandler(void *machine, void *queue, void *completionHandler); -void resumeWithCompletionHandler(void *machine, void *queue, void *completionHandler); +void startWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle); +void pauseWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle); +void resumeWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle); bool vmCanStart(void *machine, void *queue); bool vmCanPause(void *machine, void *queue); bool vmCanResume(void *machine, void *queue); diff --git a/virtualization_11.m b/virtualization_11.m index af7785e..518367d 100644 --- a/virtualization_11.m +++ b/virtualization_11.m @@ -12,25 +12,24 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N if ([keyPath isEqualToString:@"state"]) { int newState = (int)[change[NSKeyValueChangeNewKey] integerValue]; - changeStateOnObserver(newState, context); + changeStateOnObserver(newState, (uintptr_t)context); } } @end @implementation ObservableVZVirtualMachine { Observer *_observer; - void *_stateHandler; }; - (instancetype)initWithConfiguration:(VZVirtualMachineConfiguration *)configuration queue:(dispatch_queue_t)queue - statusHandler:(void *)statusHandler + statusUpdateHandle:(uintptr_t)statusUpdateHandle { self = [super initWithConfiguration:configuration queue:queue]; _observer = [[Observer alloc] init]; [self addObserver:_observer forKeyPath:@"state" options:NSKeyValueObservingOptionNew - context:statusHandler]; + context:(void *)statusUpdateHandle]; return self; } @@ -43,19 +42,19 @@ - (void)dealloc @end @implementation VZVirtioSocketListenerDelegateImpl { - void *_cgoHandler; + uintptr_t _cgoHandle; } -- (instancetype)initWithHandler:(void *)cgoHandler +- (instancetype)initWithHandle:(uintptr_t)cgoHandle { self = [super init]; - _cgoHandler = cgoHandler; + _cgoHandle = cgoHandle; return self; } - (BOOL)listener:(VZVirtioSocketListener *)listener shouldAcceptNewConnection:(VZVirtioSocketConnection *)connection fromSocketDevice:(VZVirtioSocketDevice *)socketDevice; { - return (BOOL)shouldAcceptNewConnectionHandler(_cgoHandler, connection, socketDevice); + return (BOOL)shouldAcceptNewConnectionHandler(_cgoHandle, connection, socketDevice); } @end @@ -583,11 +582,11 @@ void setStorageDevicesVZVirtualMachineConfiguration(void *config, @see VZVirtioSocketDevice @see VZVirtioSocketListenerDelegate */ -void *newVZVirtioSocketListener(void *cgoHandlerPtr) +void *newVZVirtioSocketListener(uintptr_t cgoHandle) { if (@available(macOS 11, *)) { VZVirtioSocketListener *ret = [[VZVirtioSocketListener alloc] init]; - [ret setDelegate:[[VZVirtioSocketListenerDelegateImpl alloc] initWithHandler:cgoHandlerPtr]]; + [ret setDelegate:[[VZVirtioSocketListenerDelegateImpl alloc] initWithHandle:cgoHandle]]; return ret; } @@ -639,13 +638,13 @@ void VZVirtioSocketDevice_removeSocketListenerForPort(void *socketDevice, void * @param completionHandler Block called after the connection has been successfully established or on error. The error parameter passed to the block is nil if the connection was successful. */ -void VZVirtioSocketDevice_connectToPort(void *socketDevice, void *vmQueue, uint32_t port, void *cgoHandlerPtr) +void VZVirtioSocketDevice_connectToPort(void *socketDevice, void *vmQueue, uint32_t port, uintptr_t cgoHandle) { if (@available(macOS 11, *)) { dispatch_async((dispatch_queue_t)vmQueue, ^{ [(VZVirtioSocketDevice *)socketDevice connectToPort:port completionHandler:^(VZVirtioSocketConnection *connection, NSError *err) { - connectionHandler(connection, err, cgoHandlerPtr); + connectionHandler(connection, err, cgoHandle); }]; }); return; @@ -676,13 +675,13 @@ VZVirtioSocketConnectionFlat convertVZVirtioSocketConnection2Flat(void *connecti Every operation on the virtual machine must be done on that queue. The callbacks and delegate methods are invoked on that queue. If the queue is not serial, the behavior is undefined. */ -void *newVZVirtualMachineWithDispatchQueue(void *config, void *queue, void *statusHandler) +void *newVZVirtualMachineWithDispatchQueue(void *config, void *queue, uintptr_t cgoHandle) { if (@available(macOS 11, *)) { ObservableVZVirtualMachine *vm = [[ObservableVZVirtualMachine alloc] initWithConfiguration:(VZVirtualMachineConfiguration *)config queue:(dispatch_queue_t)queue - statusHandler:statusHandler]; + statusUpdateHandle:cgoHandle]; return vm; } @@ -791,10 +790,10 @@ bool requestStopVirtualMachine(void *machine, void *queue, void **error) return queue; } -void startWithCompletionHandler(void *machine, void *queue, void *completionHandler) +void startWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle) { if (@available(macOS 11, *)) { - vm_completion_handler_t handler = makeVMCompletionHandler(completionHandler); + vm_completion_handler_t handler = makeVMCompletionHandler(cgoHandle); dispatch_sync((dispatch_queue_t)queue, ^{ [(VZVirtualMachine *)machine startWithCompletionHandler:handler]; }); @@ -805,10 +804,10 @@ void startWithCompletionHandler(void *machine, void *queue, void *completionHand RAISE_UNSUPPORTED_MACOS_EXCEPTION(); } -void pauseWithCompletionHandler(void *machine, void *queue, void *completionHandler) +void pauseWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle) { if (@available(macOS 11, *)) { - vm_completion_handler_t handler = makeVMCompletionHandler(completionHandler); + vm_completion_handler_t handler = makeVMCompletionHandler(cgoHandle); dispatch_sync((dispatch_queue_t)queue, ^{ [(VZVirtualMachine *)machine pauseWithCompletionHandler:handler]; }); @@ -819,10 +818,10 @@ void pauseWithCompletionHandler(void *machine, void *queue, void *completionHand RAISE_UNSUPPORTED_MACOS_EXCEPTION(); } -void resumeWithCompletionHandler(void *machine, void *queue, void *completionHandler) +void resumeWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle) { if (@available(macOS 11, *)) { - vm_completion_handler_t handler = makeVMCompletionHandler(completionHandler); + vm_completion_handler_t handler = makeVMCompletionHandler(cgoHandle); dispatch_sync((dispatch_queue_t)queue, ^{ [(VZVirtualMachine *)machine resumeWithCompletionHandler:handler]; }); diff --git a/virtualization_12.h b/virtualization_12.h index d94c416..2a62ea5 100644 --- a/virtualization_12.h +++ b/virtualization_12.h @@ -12,7 +12,7 @@ #define NSURLComponents NSURLComponents bool vmCanStop(void *machine, void *queue); -void stopWithCompletionHandler(void *machine, void *queue, void *completionHandler); +void stopWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle); void *newVZGenericPlatformConfiguration(); diff --git a/virtualization_12.m b/virtualization_12.m index 7a0d64f..4bcbcfb 100644 --- a/virtualization_12.m +++ b/virtualization_12.m @@ -19,10 +19,10 @@ bool vmCanStop(void *machine, void *queue) RAISE_UNSUPPORTED_MACOS_EXCEPTION(); } -void stopWithCompletionHandler(void *machine, void *queue, void *completionHandler) +void stopWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle) { if (@available(macOS 12, *)) { - vm_completion_handler_t handler = makeVMCompletionHandler(completionHandler); + vm_completion_handler_t handler = makeVMCompletionHandler(cgoHandle); dispatch_sync((dispatch_queue_t)queue, ^{ [(VZVirtualMachine *)machine stopWithCompletionHandler:handler]; }); diff --git a/virtualization_12_arm64.h b/virtualization_12_arm64.h index 50bf12c..014df9d 100644 --- a/virtualization_12_arm64.h +++ b/virtualization_12_arm64.h @@ -36,9 +36,9 @@ typedef struct VZMacHardwareModelStruct { } VZMacHardwareModelStruct; /* exported from cgo */ -void macOSRestoreImageCompletionHandler(void *cgoHandler, void *restoreImage, void *errPtr); -void macOSInstallCompletionHandler(void *cgoHandler, void *errPtr); -void macOSInstallFractionCompletedHandler(void *cgoHandlerPtr, double completed); +void macOSRestoreImageCompletionHandler(uintptr_t cgoHandle, void *restoreImage, void *errPtr); +void macOSInstallCompletionHandler(uintptr_t cgoHandle, void *errPtr); +void macOSInstallFractionCompletedHandler(uintptr_t cgoHandle, double completed); /* Mac Configurations */ void *newVZMacPlatformConfiguration(); @@ -62,8 +62,8 @@ void *newVZMacMachineIdentifierWithBytes(void *machineIdentifierBytes, int len); nbyteslice getVZMacMachineIdentifierDataRepresentation(void *machineIdentifierPtr); VZMacOSRestoreImageStruct convertVZMacOSRestoreImage2Struct(void *restoreImagePtr); -void fetchLatestSupportedMacOSRestoreImageWithCompletionHandler(void *cgoHandler); -void loadMacOSRestoreImageFile(const char *ipswPath, void *cgoHandler); +void fetchLatestSupportedMacOSRestoreImageWithCompletionHandler(uintptr_t cgoHandle); +void loadMacOSRestoreImageFile(const char *ipswPath, uintptr_t cgoHandle); VZMacOSConfigurationRequirementsStruct convertVZMacOSConfigurationRequirements2Struct(void *requirementsPtr); VZMacHardwareModelStruct convertVZMacHardwareModel2Struct(void *hardwareModelPtr); diff --git a/virtualization_12_arm64.m b/virtualization_12_arm64.m index 4fbaf6c..957e481 100644 --- a/virtualization_12_arm64.m +++ b/virtualization_12_arm64.m @@ -333,12 +333,12 @@ VZMacOSRestoreImageStruct convertVZMacOSRestoreImage2Struct(void *restoreImagePt RAISE_UNSUPPORTED_MACOS_EXCEPTION(); } -void fetchLatestSupportedMacOSRestoreImageWithCompletionHandler(void *cgoHandler) +void fetchLatestSupportedMacOSRestoreImageWithCompletionHandler(uintptr_t cgoHandle) { if (@available(macOS 12, *)) { [VZMacOSRestoreImage fetchLatestSupportedWithCompletionHandler:^(VZMacOSRestoreImage *restoreImage, NSError *error) { VZMacOSRestoreImageStruct restoreImageStruct = convertVZMacOSRestoreImage2Struct(restoreImage); - macOSRestoreImageCompletionHandler(cgoHandler, &restoreImageStruct, error); + macOSRestoreImageCompletionHandler(cgoHandle, &restoreImageStruct, error); }]; return; } @@ -346,7 +346,7 @@ void fetchLatestSupportedMacOSRestoreImageWithCompletionHandler(void *cgoHandler RAISE_UNSUPPORTED_MACOS_EXCEPTION(); } -void loadMacOSRestoreImageFile(const char *ipswPath, void *cgoHandler) +void loadMacOSRestoreImageFile(const char *ipswPath, uintptr_t cgoHandle) { if (@available(macOS 12, *)) { NSString *ipswPathNSString = [NSString stringWithUTF8String:ipswPath]; @@ -354,7 +354,7 @@ void loadMacOSRestoreImageFile(const char *ipswPath, void *cgoHandler) [VZMacOSRestoreImage loadFileURL:ipswURL completionHandler:^(VZMacOSRestoreImage *restoreImage, NSError *error) { VZMacOSRestoreImageStruct restoreImageStruct = convertVZMacOSRestoreImage2Struct(restoreImage); - macOSRestoreImageCompletionHandler(cgoHandler, &restoreImageStruct, error); + macOSRestoreImageCompletionHandler(cgoHandle, &restoreImageStruct, error); }]; return; } diff --git a/virtualization_13.h b/virtualization_13.h index 4c0072d..cf2fb72 100644 --- a/virtualization_13.h +++ b/virtualization_13.h @@ -41,7 +41,7 @@ void *newVZSpiceAgentPortAttachment(); void setSharesClipboardVZSpiceAgentPortAttachment(void *attachment, bool sharesClipboard); const char *getSpiceAgentPortName(); -void startWithOptionsCompletionHandler(void *machine, void *queue, void *options, void *completionHandler); +void startWithOptionsCompletionHandler(void *machine, void *queue, void *options, uintptr_t cgoHandle); const char *getMacOSGuestAutomountTag(); diff --git a/virtualization_13.m b/virtualization_13.m index f6e26a2..cb54591 100644 --- a/virtualization_13.m +++ b/virtualization_13.m @@ -425,11 +425,11 @@ void setSharesClipboardVZSpiceAgentPortAttachment(void *attachment, bool sharesC The error parameter passed to the block is nil if the start was successful. @seealso VZMacOSVirtualMachineStartOptions */ -void startWithOptionsCompletionHandler(void *machine, void *queue, void *options, void *completionHandler) +void startWithOptionsCompletionHandler(void *machine, void *queue, void *options, uintptr_t cgoHandle) { #ifdef INCLUDE_TARGET_OSX_13 if (@available(macOS 13, *)) { - vm_completion_handler_t handler = makeVMCompletionHandler(completionHandler); + vm_completion_handler_t handler = makeVMCompletionHandler(cgoHandle); dispatch_sync((dispatch_queue_t)queue, ^{ [(VZVirtualMachine *)machine startWithOptions:options completionHandler:handler]; }); diff --git a/virtualization_13_arm64.h b/virtualization_13_arm64.h index 4f79596..99701ce 100644 --- a/virtualization_13_arm64.h +++ b/virtualization_13_arm64.h @@ -16,10 +16,10 @@ #import /* exported from cgo */ -void linuxInstallRosettaWithCompletionHandler(void *cgoHandler, void *errPtr); +void linuxInstallRosettaWithCompletionHandler(uintptr_t cgoHandle, void *errPtr); void *newVZLinuxRosettaDirectoryShare(void **error); -void linuxInstallRosetta(void *cgoHandler); +void linuxInstallRosetta(uintptr_t cgoHandle); int availabilityVZLinuxRosettaDirectoryShare(); void *newVZMacOSVirtualMachineStartOptions(bool startUpFromMacOSRecovery); diff --git a/virtualization_13_arm64.m b/virtualization_13_arm64.m index 79c1fdf..52aee1b 100644 --- a/virtualization_13_arm64.m +++ b/virtualization_13_arm64.m @@ -28,12 +28,12 @@ The call prompts the user through the download and install flow for Rosetta. This call is successful if the error is nil. @see +[VZLinuxRosettaDirectoryShare availability] */ -void linuxInstallRosetta(void *cgoHandler) +void linuxInstallRosetta(uintptr_t cgoHandle) { #ifdef INCLUDE_TARGET_OSX_13 if (@available(macOS 13, *)) { [VZLinuxRosettaDirectoryShare installRosettaWithCompletionHandler:^(NSError *error) { - linuxInstallRosettaWithCompletionHandler(cgoHandler, error); + linuxInstallRosettaWithCompletionHandler(cgoHandle, error); }]; return; } diff --git a/virtualization_arm64.go b/virtualization_arm64.go index a0a28e1..97d8680 100644 --- a/virtualization_arm64.go +++ b/virtualization_arm64.go @@ -317,11 +317,11 @@ func (m *MacOSConfigurationRequirements) MinimumSupportedMemorySize() uint64 { type macOSRestoreImageHandler func(restoreImage *MacOSRestoreImage, err error) //export macOSRestoreImageCompletionHandler -func macOSRestoreImageCompletionHandler(cgoHandlerPtr, restoreImagePtr, errPtr unsafe.Pointer) { - cgoHandler := *(*cgo.Handle)(cgoHandlerPtr) +func macOSRestoreImageCompletionHandler(cgoHandleUintptr C.uintptr_t, restoreImagePtr, errPtr unsafe.Pointer) { + cgoHandle := cgo.Handle(cgoHandleUintptr) - handler := cgoHandler.Value().(macOSRestoreImageHandler) - defer cgoHandler.Delete() + handler := cgoHandle.Value().(macOSRestoreImageHandler) + defer cgoHandle.Delete() restoreImageStruct := (*C.VZMacOSRestoreImageStruct)(restoreImagePtr) @@ -412,9 +412,9 @@ func FetchLatestSupportedMacOSRestoreImage(ctx context.Context, destPath string) fetchErr = err defer close(waitCh) }) - cgoHandler := cgo.NewHandle(handler) + cgoHandle := cgo.NewHandle(handler) C.fetchLatestSupportedMacOSRestoreImageWithCompletionHandler( - unsafe.Pointer(&cgoHandler), + C.uintptr_t(cgoHandle), ) <-waitCh if fetchErr != nil { @@ -447,11 +447,11 @@ func LoadMacOSRestoreImageFromPath(imagePath string) (retImage *MacOSRestoreImag retErr = err close(waitCh) }) - cgoHandler := cgo.NewHandle(handler) + cgoHandle := cgo.NewHandle(handler) cs := charWithGoString(imagePath) defer cs.Free() - C.loadMacOSRestoreImageFile(cs.CString(), unsafe.Pointer(&cgoHandler)) + C.loadMacOSRestoreImageFile(cs.CString(), C.uintptr_t(cgoHandle)) <-waitCh return } @@ -504,11 +504,11 @@ func NewMacOSInstaller(vm *VirtualMachine, restoreImageIpsw string) (*MacOSInsta } //export macOSInstallCompletionHandler -func macOSInstallCompletionHandler(cgoHandlerPtr, errPtr unsafe.Pointer) { - cgoHandler := *(*cgo.Handle)(cgoHandlerPtr) +func macOSInstallCompletionHandler(cgoHandleUintptr C.uintptr_t, errPtr unsafe.Pointer) { + cgoHandle := cgo.Handle(cgoHandleUintptr) - handler := cgoHandler.Value().(func(error)) - defer cgoHandler.Delete() + handler := cgoHandle.Value().(func(error)) + defer cgoHandle.Delete() if err := newNSError(errPtr); err != nil { handler(err) @@ -518,10 +518,10 @@ func macOSInstallCompletionHandler(cgoHandlerPtr, errPtr unsafe.Pointer) { } //export macOSInstallFractionCompletedHandler -func macOSInstallFractionCompletedHandler(cgoHandlerPtr unsafe.Pointer, completed C.double) { - cgoHandler := *(*cgo.Handle)(cgoHandlerPtr) +func macOSInstallFractionCompletedHandler(cgoHandleUintptr C.uintptr_t, completed C.double) { + cgoHandle := cgo.Handle(cgoHandleUintptr) - handler := cgoHandler.Value().(func(float64)) + handler := cgoHandle.Value().(func(float64)) handler(float64(completed)) } @@ -549,8 +549,8 @@ func (m *MacOSInstaller) Install(ctx context.Context) error { objc.Ptr(m), m.vm.dispatchQueue, objc.Ptr(m.observerPointer), - unsafe.Pointer(&completionHandler), - unsafe.Pointer(&fractionCompletedHandler), + C.uintptr_t(completionHandler), + C.uintptr_t(fractionCompletedHandler), ) }) diff --git a/virtualization_helper.h b/virtualization_helper.h index 995b408..b85fc48 100644 --- a/virtualization_helper.h +++ b/virtualization_helper.h @@ -47,13 +47,13 @@ typedef struct nbyteslice { } nbyteslice; /* exported from cgo */ -void virtualMachineCompletionHandler(void *cgoHandler, void *errPtr); +void virtualMachineCompletionHandler(uintptr_t cgoHandle, void *errPtr); typedef void (^vm_completion_handler_t)(NSError *); -static inline vm_completion_handler_t makeVMCompletionHandler(void *completionHandler) +static inline vm_completion_handler_t makeVMCompletionHandler(uintptr_t cgoHandle) { return Block_copy(^(NSError *err) { - virtualMachineCompletionHandler(completionHandler, err); + virtualMachineCompletionHandler(cgoHandle, err); }); } diff --git a/vz_export_test.go b/vz_export_test.go index 7c60def..84256ab 100644 --- a/vz_export_test.go +++ b/vz_export_test.go @@ -5,7 +5,7 @@ import ( ) func (v *VirtualMachine) SetMachineStateFinalizer(f func()) { - runtime.SetFinalizer(v.stateHandle, func(self *machineState) { + runtime.SetFinalizer(v.machineState, func(self *machineState) { f() }) }