Skip to content

Commit

Permalink
Mach ports back to 32-bit ints
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Sep 17, 2019
1 parent 6c9a8c0 commit 114fca1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 318 deletions.
21 changes: 10 additions & 11 deletions contrib/platform/src/com/sun/jna/platform/mac/IOKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import com.sun.jna.platform.mac.CoreFoundation.CFMutableDictionaryRef;
import com.sun.jna.platform.mac.CoreFoundation.CFStringRef;
import com.sun.jna.platform.mac.CoreFoundation.CFTypeRef;
import com.sun.jna.platform.mac.SystemB.MachPort;
import com.sun.jna.platform.mac.SystemB.TaskPort;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.ptr.PointerByReference;
Expand Down Expand Up @@ -159,16 +157,17 @@ public IOConnect(Pointer p) {
* Returns the mach port used to initiate communication with IOKit.
*
* @param bootstrapPort
* Pass {@link SystemB#MACH_PORT_NULL} for the default.
* @param masterPort
* Pass 0 for the default.
* @param port
* A pointer to the master port is returned. Multiple calls to
* IOMasterPort will not result in leaking ports (each call to
* IOMasterPort adds another send right to the port) but it is
* considered good programming practice to deallocate the port when
* you are finished with it using {@link #IOObjectRelease}
* you are finished with it using
* {@link SystemB#mach_port_deallocate}.
* @return 0 if successful, otherwise a {@code kern_return_t} error code.
*/
int IOMasterPort(MachPort bootstrapPort, PointerByReference masterPort);
int IOMasterPort(int bootstrapPort, IntByReference port);

/**
* Create a matching dictionary that specifies an {@code IOService} class match.
Expand Down Expand Up @@ -219,7 +218,7 @@ public IOConnect(Pointer p) {
* otherwise it should be released with {@link CoreFoundation#CFRelease}
* by the caller.
*/
CFMutableDictionaryRef IOBSDNameMatching(MachPort masterPort, int options, String bsdName);
CFMutableDictionaryRef IOBSDNameMatching(int masterPort, int options, String bsdName);

/**
* Look up a registered IOService object that matches a matching dictionary.
Expand All @@ -236,7 +235,7 @@ public IOConnect(Pointer p) {
* <p>
* The service must be released by the caller.
*/
IOService IOServiceGetMatchingService(MachPort masterPort, CFDictionaryRef matchingDictionary);
IOService IOServiceGetMatchingService(int masterPort, CFDictionaryRef matchingDictionary);

/**
* Look up registered IOService objects that match a matching dictionary.
Expand All @@ -254,7 +253,7 @@ public IOConnect(Pointer p) {
* by the caller when the iteration is finished.
* @return 0 if successful, otherwise a {@code kern_return_t} error code.
*/
int IOServiceGetMatchingServices(MachPort masterPort, CFDictionaryRef matchingDictionary,
int IOServiceGetMatchingServices(int masterPort, CFDictionaryRef matchingDictionary,
PointerByReference iterator);

/**
Expand Down Expand Up @@ -404,7 +403,7 @@ CFTypeRef IORegistryEntrySearchCFProperty(IORegistryEntry entry, String plane, C
* @return A handle to the IORegistryEntry root instance, to be released with
* {@link #IOObjectRelease} by the caller, or 0 on failure.
*/
IORegistryEntry IORegistryGetRootEntry(MachPort masterPort);
IORegistryEntry IORegistryGetRootEntry(int masterPort);

/**
* Performs an OSDynamicCast operation on an IOKit object.
Expand Down Expand Up @@ -444,7 +443,7 @@ CFTypeRef IORegistryEntrySearchCFProperty(IORegistryEntry entry, String plane, C
* {@link IOServiceClose}.
* @return A return code generated by {@code IOService::newUserClient}.
*/
int IOServiceOpen(IOService service, TaskPort owningTask, int type, PointerByReference connect);
int IOServiceOpen(IOService service, int owningTask, int type, PointerByReference connect);

/**
* Returns the busyState of an IOService.
Expand Down
29 changes: 15 additions & 14 deletions contrib/platform/src/com/sun/jna/platform/mac/IOKitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.sun.jna.platform.mac.IOKit.IOIterator;
import com.sun.jna.platform.mac.IOKit.IORegistryEntry;
import com.sun.jna.platform.mac.IOKit.IOService;
import com.sun.jna.platform.mac.SystemB.MachPort;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;

/**
Expand All @@ -44,6 +44,7 @@
public class IOKitUtil {
private static final IOKit IO = IOKit.INSTANCE;
private static final CoreFoundation CF = CoreFoundation.INSTANCE;
private static final SystemB SYS = SystemB.INSTANCE;

private IOKitUtil() {
}
Expand All @@ -57,12 +58,12 @@ private IOKitUtil() {
* ports (each call to {@link IOKit#IOMasterPort} adds another send
* right to the port) but it is considered good programming practice to
* deallocate the port when you are finished with it, using
* {@link SystemB#mach_port_deallocate}.
* {@link IOKit#IOObjectRelease}.
*/
public static MachPort getMasterPort() {
PointerByReference port = new PointerByReference();
IO.IOMasterPort(SystemB.MACH_PORT_NULL, port);
return new MachPort(port.getValue());
public static int getMasterPort() {
IntByReference port = new IntByReference();
IO.IOMasterPort(0, port);
return port.getValue();
}

/**
Expand All @@ -72,9 +73,9 @@ public static MachPort getMasterPort() {
* {@link IOKit#IOObjectRelease}.
*/
public static IORegistryEntry getRoot() {
MachPort masterPort = getMasterPort();
int masterPort = getMasterPort();
IORegistryEntry root = IO.IORegistryGetRootEntry(masterPort);
masterPort.deallocate();
SYS.mach_port_deallocate(SYS.mach_task_self(), masterPort);
return root;
}

Expand Down Expand Up @@ -106,9 +107,9 @@ public static IOService getMatchingService(String serviceName) {
* {@link IOKit#IOObjectRelease}.
*/
public static IOService getMatchingService(CFDictionaryRef matchingDictionary) {
MachPort masterPort = getMasterPort();
int masterPort = getMasterPort();
IOService service = IO.IOServiceGetMatchingService(masterPort, matchingDictionary);
masterPort.deallocate();
SYS.mach_port_deallocate(SYS.mach_task_self(), masterPort);
return service;
}

Expand Down Expand Up @@ -140,10 +141,10 @@ public static IOIterator getMatchingServices(String serviceName) {
* {@link IOKit#IOObjectRelease}.
*/
public static IOIterator getMatchingServices(CFDictionaryRef matchingDictionary) {
MachPort masterPort = getMasterPort();
int masterPort = getMasterPort();
PointerByReference serviceIterator = new PointerByReference();
int result = IO.IOServiceGetMatchingServices(masterPort, matchingDictionary, serviceIterator);
masterPort.deallocate();
SYS.mach_port_deallocate(SYS.mach_task_self(), masterPort);
if (result == 0 && serviceIterator.getValue() != null) {
return new IOIterator(serviceIterator.getValue());
}
Expand All @@ -159,9 +160,9 @@ public static IOIterator getMatchingServices(CFDictionaryRef matchingDictionary)
* should release when finished, using {@link IOKit#IOObjectRelease}.
*/
public static CFMutableDictionaryRef getBSDNameMatchingDict(String bsdName) {
MachPort masterPort = getMasterPort();
int masterPort = getMasterPort();
CFMutableDictionaryRef result = IO.IOBSDNameMatching(masterPort, 0, bsdName);
masterPort.deallocate();
SYS.mach_port_deallocate(SYS.mach_task_self(), masterPort);
return result;
}

Expand Down
Loading

0 comments on commit 114fca1

Please sign in to comment.