Skip to content

Commit

Permalink
override VZVirtualMachine object
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Jan 22, 2023
1 parent 7d2aa2f commit 3a1111d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
4 changes: 0 additions & 4 deletions virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package vz
*/
import "C"
import (
"log"
"runtime/cgo"
"sync"
"unsafe"
Expand Down Expand Up @@ -130,8 +129,6 @@ func NewVirtualMachine(config *VirtualMachineConfiguration) (*VirtualMachine, er

func (v *VirtualMachine) finalize() {
v.finalizeOnce.Do(func() {
v.stateHandle.Delete()
// deleteStateHandler(unsafe.Pointer(&v.stateHandle))
objc.ReleaseDispatch(v.dispatchQueue)
objc.Release(v)
})
Expand Down Expand Up @@ -163,7 +160,6 @@ func changeStateOnObserver(newStateRaw C.int, cgoHandlerPtr unsafe.Pointer) {
v, _ := stateHandler.Value().(*machineState)
v.mu.Lock()
newState := VirtualMachineState(newStateRaw)
log.Println(newState.String())
v.state = newState
// for non-blocking
go func() { v.stateNotify <- newState }()
Expand Down
8 changes: 8 additions & 0 deletions virtualization_11.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
void connectionHandler(void *connection, void *err, void *cgoHandlerPtr);
void changeStateOnObserver(int state, void *cgoHandler);
bool shouldAcceptNewConnectionHandler(void *cgoHandler, void *connection, void *socketDevice);
void deleteStateHandler(void *cgoHandlerPtr);

@interface Observer : NSObject
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
@end

@interface ObservableVZVirtualMachine : VZVirtualMachine
- (instancetype)initWithConfiguration:(VZVirtualMachineConfiguration *)configuration
queue:(dispatch_queue_t)queue
statusHandler:(void *)statusHandler;
- (void)dealloc;
@end

/* VZVirtioSocketListener */
@interface VZVirtioSocketListenerDelegateImpl : NSObject <VZVirtioSocketListenerDelegate>
- (instancetype)initWithHandler:(void *)cgoHandler;
Expand Down
38 changes: 29 additions & 9 deletions virtualization_11.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}
@end

@implementation ObservableVZVirtualMachine {
Observer *_observer;
void *_stateHandler;
};
- (instancetype)initWithConfiguration:(VZVirtualMachineConfiguration *)configuration
queue:(dispatch_queue_t)queue
statusHandler:(void *)statusHandler
{
self = [super initWithConfiguration:configuration queue:queue];
_observer = [[Observer alloc] init];
[self addObserver:_observer
forKeyPath:@"state"
options:NSKeyValueObservingOptionNew
context:statusHandler];
return self;
}

- (void)dealloc
{
[self removeObserver:_observer forKeyPath:@"state"];
deleteStateHandler(_stateHandler);
[_observer release];
[super dealloc];
}
@end

@implementation VZVirtioSocketListenerDelegateImpl {
void *_cgoHandler;
}
Expand Down Expand Up @@ -663,16 +689,10 @@ VZVirtioSocketConnectionFlat convertVZVirtioSocketConnection2Flat(void *connecti
void *newVZVirtualMachineWithDispatchQueue(void *config, void *queue, void *statusHandler)
{
if (@available(macOS 11, *)) {
VZVirtualMachine *vm = [[VZVirtualMachine alloc]
ObservableVZVirtualMachine *vm = [[ObservableVZVirtualMachine alloc]
initWithConfiguration:(VZVirtualMachineConfiguration *)config
queue:(dispatch_queue_t)queue];
@autoreleasepool {
Observer *o = [[Observer alloc] init];
[vm addObserver:o
forKeyPath:@"state"
options:NSKeyValueObservingOptionNew
context:statusHandler];
}
queue:(dispatch_queue_t)queue
statusHandler:statusHandler];
return vm;
}

Expand Down
12 changes: 6 additions & 6 deletions virtualization_12.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ void setStreamsVZVirtioSoundDeviceConfiguration(void *audioDeviceConfiguration,
@param error If not nil, assigned with the error if the initialization failed.
@return A VZDiskImageStorageDeviceAttachment on success. Nil otherwise and the error parameter is populated if set.
*/
void *newVZDiskImageStorageDeviceAttachmentWithCacheAndSyncMode(const char *diskPath, bool readOnly, int cacheMode, int syncMode, void **error)
void *newVZDiskImageStorageDeviceAttachmentWithCacheAndSyncMode(const char *diskPath, bool readOnly, int cacheMode, int syncMode, void **error)
{
if (@available(macOS 12, *)) {
NSString *diskPathNSString = [NSString stringWithUTF8String:diskPath];
NSURL *diskURL = [NSURL fileURLWithPath:diskPathNSString];
return [[VZDiskImageStorageDeviceAttachment alloc]
initWithURL:diskURL
readOnly:(BOOL)readOnly
cachingMode:(VZDiskImageCachingMode)cacheMode
synchronizationMode: (VZDiskImageSynchronizationMode)syncMode
error:(NSError *_Nullable *_Nullable)error];
initWithURL:diskURL
readOnly:(BOOL)readOnly
cachingMode:(VZDiskImageCachingMode)cacheMode
synchronizationMode:(VZDiskImageSynchronizationMode)syncMode
error:(NSError *_Nullable *_Nullable)error];
}

RAISE_UNSUPPORTED_MACOS_EXCEPTION();
Expand Down

0 comments on commit 3a1111d

Please sign in to comment.