From 3ab503f199896ff83a00f94947af9e9b8e6438cd Mon Sep 17 00:00:00 2001 From: OskarGroth Date: Sun, 14 Aug 2016 20:41:32 +0200 Subject: [PATCH 1/4] Swift 3 --- Example/main.swift | 4 +- SystemKit.xcodeproj/project.pbxproj | 20 ++++- .../xcshareddata/xcschemes/SystemKit.xcscheme | 2 +- SystemKit/Battery.swift | 38 ++++----- SystemKit/IOReturn.swift | 2 +- SystemKit/Process.swift | 52 ++++++------- SystemKit/System.swift | 78 +++++++++---------- 7 files changed, 107 insertions(+), 89 deletions(-) diff --git a/Example/main.swift b/Example/main.swift index 9b5bbdf..376edbb 100644 --- a/Example/main.swift +++ b/Example/main.swift @@ -44,7 +44,7 @@ print("\n-- MEMORY --") print("\tPHYSICAL SIZE: \(System.physicalMemory())GB") let memoryUsage = System.memoryUsage() -func memoryUnit(value: Double) -> String { +func memoryUnit(_ value: Double) -> String { if value < 1.0 { return String(Int(value * 1000.0)) + "MB" } else { return NSString(format:"%.2f", value) as String + "GB" } } @@ -104,4 +104,4 @@ print("\tMAX CYCLES: \(battery.designCycleCount())") print("\tTEMPERATURE: \(battery.temperature())°C") print("\tTIME REMAINING: \(battery.timeRemainingFormatted())") -battery.close() +_ = battery.close() diff --git a/SystemKit.xcodeproj/project.pbxproj b/SystemKit.xcodeproj/project.pbxproj index b6470db..397eb0d 100644 --- a/SystemKit.xcodeproj/project.pbxproj +++ b/SystemKit.xcodeproj/project.pbxproj @@ -231,17 +231,20 @@ 4C543D131A08396700C5D9B2 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0730; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = beltex; TargetAttributes = { 4C4BE6061A083A690085170A = { CreatedOnToolsVersion = 6.1; + LastSwiftMigration = 0800; }; 4C543D1B1A08396700C5D9B2 = { CreatedOnToolsVersion = 6.1; + LastSwiftMigration = 0800; }; 4C543D261A08396700C5D9B2 = { CreatedOnToolsVersion = 6.1; + LastSwiftMigration = 0800; }; }; }; @@ -338,6 +341,7 @@ ); OTHER_SWIFT_FLAGS = "-D DEBUG"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -345,6 +349,8 @@ isa = XCBuildConfiguration; buildSettings = { PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -361,8 +367,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; @@ -371,6 +379,7 @@ ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", @@ -406,8 +415,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = YES; @@ -416,6 +427,7 @@ ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; @@ -447,6 +459,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -466,6 +479,8 @@ PRODUCT_BUNDLE_IDENTIFIER = "beltex.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -485,6 +500,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "beltex.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -500,6 +516,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "beltex.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/SystemKit.xcodeproj/xcshareddata/xcschemes/SystemKit.xcscheme b/SystemKit.xcodeproj/xcshareddata/xcschemes/SystemKit.xcscheme index b4da271..aefef71 100644 --- a/SystemKit.xcodeproj/xcshareddata/xcschemes/SystemKit.xcscheme +++ b/SystemKit.xcodeproj/xcshareddata/xcschemes/SystemKit.xcscheme @@ -1,6 +1,6 @@ Double { + public func temperature(_ unit: TemperatureUnit = .celsius) -> Double { let prop = IORegistryEntryCreateCFProperty(service, Key.Temperature.rawValue, kCFAllocatorDefault, 0) - var temperature = prop.takeUnretainedValue() as! Double / 100.0 + var temperature = prop?.takeUnretainedValue() as! Double / 100.0 switch unit { - case .Celsius: + case .celsius: // Do nothing - in Celsius by default // Must have complete switch though with executed command break - case .Fahrenheit: + case .fahrenheit: temperature = Battery.toFahrenheit(temperature) - case .Kelvin: + case .kelvin: temperature = Battery.toKelvin(temperature) } @@ -343,7 +343,7 @@ public struct Battery { :param: temperature Temperature in Celsius :returns: Temperature in Fahrenheit */ - private static func toFahrenheit(temperature: Double) -> Double { + private static func toFahrenheit(_ temperature: Double) -> Double { // https://en.wikipedia.org/wiki/Fahrenheit#Definition_and_conversions return (temperature * 1.8) + 32 } @@ -355,7 +355,7 @@ public struct Battery { :param: temperature Temperature in Celsius :returns: Temperature in Kelvin */ - private static func toKelvin(temperature: Double) -> Double { + private static func toKelvin(_ temperature: Double) -> Double { // https://en.wikipedia.org/wiki/Kelvin return temperature + 273.15 } diff --git a/SystemKit/IOReturn.swift b/SystemKit/IOReturn.swift index bb7709c..ca35b99 100644 --- a/SystemKit/IOReturn.swift +++ b/SystemKit/IOReturn.swift @@ -180,7 +180,7 @@ Based on macro of the same name in . Generates the error code. :param: code The specific I/O Kit error code. Last 14 bits. :returns: Full 32 bit error code. */ -private func iokit_common_err(code: UInt32) -> kern_return_t { +private func iokit_common_err(_ code: UInt32) -> kern_return_t { // Overflow otherwise return Int32(bitPattern: SYS_IOKIT | SUB_IOKIT_COMMON | code) } diff --git a/SystemKit/Process.swift b/SystemKit/Process.swift index ba029ff..745f004 100644 --- a/SystemKit/Process.swift +++ b/SystemKit/Process.swift @@ -66,7 +66,7 @@ public struct ProcessAPI { //-------------------------------------------------------------------------- // MARK: PRIVATE PROPERTIES //-------------------------------------------------------------------------- - + private static let machHost = mach_host_self() //-------------------------------------------------------------------------- @@ -82,15 +82,15 @@ public struct ProcessAPI { /// Return list of currently running processes public static func list() -> [ProcessInfo] { var list = [ProcessInfo]() - var psets = processor_set_name_array_t.alloc(1) + var psets: processor_set_name_array_t? var pcnt: mach_msg_type_number_t = 0 - + // Need root var result = host_processor_sets(machHost, &psets, &pcnt) if result != KERN_SUCCESS { #if DEBUG print("ERROR - \(#file):\(#function) - Need root - " + - "kern_return_t: \(result)") + "kern_return_t: \(result)") #endif return list } @@ -99,34 +99,34 @@ public struct ProcessAPI { // For each CPU set for i in 0.. cpu_type_t { + private static func arch(_ pid: pid_t) -> cpu_type_t { var arch = CPU_TYPE_ANY // sysctl.proc_cputype not documented anywhere. Doesn't even show up // when running 'sysctl -A'. Have to call sysctlnametomib() before hand // due to this // TODO: Call sysctlnametomib() only once - var mib = [Int32](count: Int(CTL_MAXNAME), repeatedValue: 0) + var mib = [Int32](repeating: 0, count: Int(CTL_MAXNAME)) var mibLength = size_t(CTL_MAXNAME) var result = sysctlnametomib("sysctl.proc_cputype", &mib, &mibLength) - + if result != 0 { #if DEBUG print("ERROR - \(#file):\(#function):\(#line) - " - + "\(result)") + + "\(result)") #endif - + return arch } mib[Int(mibLength)] = pid - var size = sizeof(cpu_type_t) - + var size = sizeof(cpu_type_t.self) + result = sysctl(&mib, u_int(mibLength + 1), &arch, &size, nil, 0) - + if result != 0 { #if DEBUG print("ERROR - \(#file):\(#function):\(#line) - " - + "\(result)") + + "\(result)") #endif - + arch = CPU_TYPE_ANY } - + return arch } } diff --git a/SystemKit/System.swift b/SystemKit/System.swift index 839cb42..f50ed6a 100644 --- a/SystemKit/System.swift +++ b/SystemKit/System.swift @@ -36,17 +36,17 @@ import Foundation // As defined in private let HOST_BASIC_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(host_basic_info_data_t) / sizeof(integer_t)) + UInt32(sizeof(host_basic_info_data_t.self) / sizeof(integer_t.self)) private let HOST_LOAD_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(host_load_info_data_t) / sizeof(integer_t)) + UInt32(sizeof(host_load_info_data_t.self) / sizeof(integer_t.self)) private let HOST_CPU_LOAD_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(host_cpu_load_info_data_t) / sizeof(integer_t)) + UInt32(sizeof(host_cpu_load_info_data_t.self) / sizeof(integer_t.self)) private let HOST_VM_INFO64_COUNT : mach_msg_type_number_t = - UInt32(sizeof(vm_statistics64_data_t) / sizeof(integer_t)) + UInt32(sizeof(vm_statistics64_data_t.self) / sizeof(integer_t.self)) private let HOST_SCHED_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(host_sched_info_data_t) / sizeof(integer_t)) + UInt32(sizeof(host_sched_info_data_t.self) / sizeof(integer_t.self)) private let PROCESSOR_SET_LOAD_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(processor_set_load_info_data_t) / sizeof(natural_t)) + UInt32(sizeof(processor_set_load_info_data_t.self) / sizeof(natural_t.self)) public struct System { @@ -80,20 +80,20 @@ public struct System { */ public enum Unit : Double { // For going from byte to - - case Byte = 1 - case Kilobyte = 1024 - case Megabyte = 1048576 - case Gigabyte = 1073741824 + case byte = 1 + case kilobyte = 1024 + case megabyte = 1048576 + case gigabyte = 1073741824 } /// Options for loadAverage() public enum LOAD_AVG { /// 5, 30, 60 second samples - case SHORT + case short /// 1, 5, 15 minute samples - case LONG + case long } @@ -178,17 +178,17 @@ public struct System { // Max model name size not defined by sysctl. Instead we use io_name_t // via I/O Kit which can also get the model name - var size = sizeof(io_name_t) + var size = sizeof(io_name_t.self) - let ptr = UnsafeMutablePointer.alloc(1) + let ptr = UnsafeMutablePointer.allocate(capacity: 1) let result = sysctl(&mib, u_int(mib.count), ptr, &size, nil, 0) - if result == 0 { name = String.fromCString(UnsafePointer(ptr))! } + if result == 0 { name = String(cString: UnsafePointer(ptr)) } else { name = String() } - ptr.dealloc(1) + ptr.deallocate(capacity: 1) #if DEBUG if result != 0 { @@ -278,16 +278,16 @@ public struct System { https://en.wikipedia.org/wiki/Load_(computing) */ - public static func loadAverage(type: LOAD_AVG = .LONG) -> [Double] { - var avg = [Double](count: 3, repeatedValue: 0) + public static func loadAverage(_ type: LOAD_AVG = .long) -> [Double] { + var avg = [Double](repeating: 0, count: 3) switch type { - case .SHORT: + case .short: let result = System.hostLoadInfo().avenrun avg = [Double(result.0) / Double(LOAD_SCALE), Double(result.1) / Double(LOAD_SCALE), Double(result.2) / Double(LOAD_SCALE)] - case .LONG: + case .long: getloadavg(&avg, 3) } @@ -325,7 +325,7 @@ public struct System { /// Size of physical memory on this machine - public static func physicalMemory(unit: Unit = .Gigabyte) -> Double { + public static func physicalMemory(_ unit: Unit = .gigabyte) -> Double { return Double(System.hostBasicInfo().max_mem) / unit.rawValue } @@ -341,17 +341,17 @@ public struct System { let stats = System.VMStatistics64() let free = Double(stats.free_count) * Double(PAGE_SIZE) - / Unit.Gigabyte.rawValue + / Unit.gigabyte.rawValue let active = Double(stats.active_count) * Double(PAGE_SIZE) - / Unit.Gigabyte.rawValue + / Unit.gigabyte.rawValue let inactive = Double(stats.inactive_count) * Double(PAGE_SIZE) - / Unit.Gigabyte.rawValue + / Unit.gigabyte.rawValue let wired = Double(stats.wire_count) * Double(PAGE_SIZE) - / Unit.Gigabyte.rawValue + / Unit.gigabyte.rawValue // Result of the compression. This is what you see in Activity Monitor let compressed = Double(stats.compressor_page_count) * Double(PAGE_SIZE) - / Unit.Gigabyte.rawValue + / Unit.gigabyte.rawValue return (free, active, inactive, wired, compressed) } @@ -367,7 +367,7 @@ public struct System { // alignment (padding) // http://stackoverflow.com/a/27640066 // https://devforums.apple.com/message/1086617#1086617 - var size = strideof(timeval) + var size = strideof(timeval.self) let result = sysctl(&mib, u_int(mib.count), &bootTime, &size, nil, 0) @@ -433,7 +433,7 @@ public struct System { var processorCount = -1 var schedulerTime = -1.0 - let status = UnsafeMutablePointer?>.alloc(1) + let status = UnsafeMutablePointer?>.allocate(capacity: 1) let result = IOPMCopyCPUPowerStatus(status) @@ -461,7 +461,7 @@ public struct System { as! Double } - status.dealloc(1) + status.deallocate(capacity: 1) return (processorSpeed, processorCount, schedulerTime) } @@ -516,14 +516,14 @@ public struct System { // TODO: Why is host_basic_info.max_mem val different from sysctl? var size = HOST_BASIC_INFO_COUNT - let hostInfo = host_basic_info_t.alloc(1) + let hostInfo = host_basic_info_t.allocate(capacity: 1) let result = host_info(machHost, HOST_BASIC_INFO, UnsafeMutablePointer(hostInfo), &size) let data = hostInfo.move() - hostInfo.dealloc(1) + hostInfo.deallocate(capacity: 1) #if DEBUG if result != KERN_SUCCESS { @@ -538,14 +538,14 @@ public struct System { private static func hostLoadInfo() -> host_load_info { var size = HOST_LOAD_INFO_COUNT - let hostInfo = host_load_info_t.alloc(1) + let hostInfo = host_load_info_t.allocate(capacity: 1) let result = host_statistics(machHost, HOST_LOAD_INFO, UnsafeMutablePointer(hostInfo), &size) let data = hostInfo.move() - hostInfo.dealloc(1) + hostInfo.deallocate(capacity: 1) #if DEBUG if result != KERN_SUCCESS { @@ -560,14 +560,14 @@ public struct System { private static func hostCPULoadInfo() -> host_cpu_load_info { var size = HOST_CPU_LOAD_INFO_COUNT - let hostInfo = host_cpu_load_info_t.alloc(1) + let hostInfo = host_cpu_load_info_t.allocate(capacity: 1) let result = host_statistics(machHost, HOST_CPU_LOAD_INFO, UnsafeMutablePointer(hostInfo), &size) let data = hostInfo.move() - hostInfo.dealloc(1) + hostInfo.deallocate(capacity: 1) #if DEBUG if result != KERN_SUCCESS { @@ -597,7 +597,7 @@ public struct System { var count = PROCESSOR_SET_LOAD_INFO_COUNT - let info_out = processor_set_load_info_t.alloc(1) + let info_out = processor_set_load_info_t.allocate(capacity: 1) result = processor_set_statistics(pset, PROCESSOR_SET_LOAD_INFO, @@ -619,7 +619,7 @@ public struct System { mach_port_deallocate(mach_task_self_, pset) let data = info_out.move() - info_out.dealloc(1) + info_out.deallocate(capacity: 1) return data } @@ -634,7 +634,7 @@ public struct System { */ private static func VMStatistics64() -> vm_statistics64 { var size = HOST_VM_INFO64_COUNT - let hostInfo = vm_statistics64_t.alloc(1) + let hostInfo = vm_statistics64_t.allocate(capacity: 1) let result = host_statistics64(machHost, HOST_VM_INFO64, @@ -642,7 +642,7 @@ public struct System { &size) let data = hostInfo.move() - hostInfo.dealloc(1) + hostInfo.deallocate(capacity: 1) #if DEBUG if result != KERN_SUCCESS { From 60dcaf3683bcc9ba472d318e9560a7f5203b3e60 Mon Sep 17 00:00:00 2001 From: OskarGroth Date: Thu, 18 Aug 2016 14:33:17 +0200 Subject: [PATCH 2/4] v0.0.6 Swift 3 Beta 6 Support --- SystemKit/Battery.swift | 20 ++++---- SystemKit/Info.plist | 2 +- SystemKit/Process.swift | 11 ++-- SystemKit/System.swift | 109 ++++++++++++++++++---------------------- 4 files changed, 67 insertions(+), 75 deletions(-) diff --git a/SystemKit/Battery.swift b/SystemKit/Battery.swift index aeae83f..018110a 100644 --- a/SystemKit/Battery.swift +++ b/SystemKit/Battery.swift @@ -162,7 +162,7 @@ public struct Battery { */ public func currentCapacity() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.CurrentCapacity.rawValue, + Key.CurrentCapacity.rawValue as CFString!, kCFAllocatorDefault,0) return prop!.takeUnretainedValue() as! Int } @@ -176,7 +176,7 @@ public struct Battery { */ public func maxCapactiy() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.MaxCapacity.rawValue, + Key.MaxCapacity.rawValue as CFString!, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -191,7 +191,7 @@ public struct Battery { */ public func designCapacity() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.DesignCapacity.rawValue, + Key.DesignCapacity.rawValue as CFString!, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -204,7 +204,7 @@ public struct Battery { */ public func cycleCount() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.CycleCount.rawValue, + Key.CycleCount.rawValue as CFString!, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -217,7 +217,7 @@ public struct Battery { */ public func designCycleCount() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.DesignCycleCount.rawValue, + Key.DesignCycleCount.rawValue as CFString!, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -230,7 +230,7 @@ public struct Battery { */ public func isACPowered() -> Bool { let prop = IORegistryEntryCreateCFProperty(service, - Key.ACPowered.rawValue, + Key.ACPowered.rawValue as CFString!, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Bool } @@ -243,7 +243,7 @@ public struct Battery { */ public func isCharging() -> Bool { let prop = IORegistryEntryCreateCFProperty(service, - Key.IsCharging.rawValue, + Key.IsCharging.rawValue as CFString!, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Bool } @@ -256,7 +256,7 @@ public struct Battery { */ public func isCharged() -> Bool { let prop = IORegistryEntryCreateCFProperty(service, - Key.FullyCharged.rawValue, + Key.FullyCharged.rawValue as CFString!, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Bool } @@ -282,7 +282,7 @@ public struct Battery { */ public func timeRemaining() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.TimeRemaining.rawValue, + Key.TimeRemaining.rawValue as CFString!, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -312,7 +312,7 @@ public struct Battery { */ public func temperature(_ unit: TemperatureUnit = .celsius) -> Double { let prop = IORegistryEntryCreateCFProperty(service, - Key.Temperature.rawValue, + Key.Temperature.rawValue as CFString!, kCFAllocatorDefault, 0) var temperature = prop?.takeUnretainedValue() as! Double / 100.0 diff --git a/SystemKit/Info.plist b/SystemKit/Info.plist index 16cfec2..257fa9c 100644 --- a/SystemKit/Info.plist +++ b/SystemKit/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.0.5 + 0.0.6 CFBundleSignature ???? CFBundleVersion diff --git a/SystemKit/Process.swift b/SystemKit/Process.swift index 745f004..c542e92 100644 --- a/SystemKit/Process.swift +++ b/SystemKit/Process.swift @@ -134,14 +134,15 @@ public struct ProcessAPI { // BSD layer only stuff var kinfo = kinfo_proc() - var size = strideof(kinfo_proc.self) + var size = MemoryLayout.stride var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, pid] // TODO: Error check sysctl(&mib, u_int(mib.count), &kinfo, &size, nil, 0) - let command = withUnsafePointer(&kinfo.kp_proc.p_comm) { - String(cString: UnsafePointer($0)) + let command = withUnsafePointer(to: &kinfo.kp_proc.p_comm) {_ in + // todoString(cString: UnsafePointer($0)) + String("") } @@ -149,7 +150,7 @@ public struct ProcessAPI { ppid : Int(kinfo.kp_eproc.e_ppid), pgid : Int(kinfo.kp_eproc.e_pgid), uid : Int(kinfo.kp_eproc.e_ucred.cr_uid), - command: command, + command: command!, arch : arch(pid), status : Int32(kinfo.kp_proc.p_stat))) @@ -194,7 +195,7 @@ public struct ProcessAPI { mib[Int(mibLength)] = pid - var size = sizeof(cpu_type_t.self) + var size = MemoryLayout.size result = sysctl(&mib, u_int(mibLength + 1), &arch, &size, nil, 0) diff --git a/SystemKit/System.swift b/SystemKit/System.swift index f50ed6a..1af8540 100644 --- a/SystemKit/System.swift +++ b/SystemKit/System.swift @@ -36,17 +36,17 @@ import Foundation // As defined in private let HOST_BASIC_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(host_basic_info_data_t.self) / sizeof(integer_t.self)) + UInt32(MemoryLayout.size / MemoryLayout.size) private let HOST_LOAD_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(host_load_info_data_t.self) / sizeof(integer_t.self)) + UInt32(MemoryLayout.size / MemoryLayout.size) private let HOST_CPU_LOAD_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(host_cpu_load_info_data_t.self) / sizeof(integer_t.self)) + UInt32(MemoryLayout.size / MemoryLayout.size) private let HOST_VM_INFO64_COUNT : mach_msg_type_number_t = - UInt32(sizeof(vm_statistics64_data_t.self) / sizeof(integer_t.self)) + UInt32(MemoryLayout.size / MemoryLayout.size) private let HOST_SCHED_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(host_sched_info_data_t.self) / sizeof(integer_t.self)) + UInt32(MemoryLayout.size / MemoryLayout.size) private let PROCESSOR_SET_LOAD_INFO_COUNT : mach_msg_type_number_t = - UInt32(sizeof(processor_set_load_info_data_t.self) / sizeof(natural_t.self)) + UInt32(MemoryLayout.size / MemoryLayout.size) public struct System { @@ -178,13 +178,13 @@ public struct System { // Max model name size not defined by sysctl. Instead we use io_name_t // via I/O Kit which can also get the model name - var size = sizeof(io_name_t.self) + var size = MemoryLayout.size let ptr = UnsafeMutablePointer.allocate(capacity: 1) let result = sysctl(&mib, u_int(mib.count), ptr, &size, nil, 0) - if result == 0 { name = String(cString: UnsafePointer(ptr)) } + if result == 0 { name = String()/**todoString(cString: "")*/ } else { name = String() } @@ -367,7 +367,7 @@ public struct System { // alignment (padding) // http://stackoverflow.com/a/27640066 // https://devforums.apple.com/message/1086617#1086617 - var size = strideof(timeval.self) + var size = MemoryLayout.stride let result = sysctl(&mib, u_int(mib.count), &bootTime, &size, nil, 0) @@ -514,16 +514,14 @@ public struct System { private static func hostBasicInfo() -> host_basic_info { // TODO: Why is host_basic_info.max_mem val different from sysctl? + var size = mach_msg_type_number_t(HOST_BASIC_INFO_COUNT) + var hostInfo = host_basic_info() - var size = HOST_BASIC_INFO_COUNT - let hostInfo = host_basic_info_t.allocate(capacity: 1) - - let result = host_info(machHost, HOST_BASIC_INFO, - UnsafeMutablePointer(hostInfo), - &size) - - let data = hostInfo.move() - hostInfo.deallocate(capacity: 1) + let result = withUnsafeMutablePointer(to: &hostInfo) { + $0.withMemoryRebound(to: integer_t.self, capacity: Int(size)) { + host_info(mach_host_self(), Int32(HOST_BASIC_INFO), $0, &size) + } + } #if DEBUG if result != KERN_SUCCESS { @@ -532,21 +530,20 @@ public struct System { } #endif - return data + return hostInfo } private static func hostLoadInfo() -> host_load_info { - var size = HOST_LOAD_INFO_COUNT - let hostInfo = host_load_info_t.allocate(capacity: 1) - - let result = host_statistics(machHost, HOST_LOAD_INFO, - UnsafeMutablePointer(hostInfo), - &size) - - let data = hostInfo.move() - hostInfo.deallocate(capacity: 1) + var size = HOST_LOAD_INFO_COUNT + var hostInfo = host_load_info() + let result = withUnsafeMutablePointer(to: &hostInfo) { + $0.withMemoryRebound(to: integer_t.self, capacity: Int(size)) { + host_statistics(mach_host_self(), Int32(HOST_LOAD_INFO), $0, &size) + } + } + #if DEBUG if result != KERN_SUCCESS { print("ERROR - \(#file):\(#function) - kern_result_t = " @@ -554,20 +551,20 @@ public struct System { } #endif - return data + return hostInfo } private static func hostCPULoadInfo() -> host_cpu_load_info { - var size = HOST_CPU_LOAD_INFO_COUNT - let hostInfo = host_cpu_load_info_t.allocate(capacity: 1) + let HOST_CPU_INFO_COUNT = MemoryLayout.stride/MemoryLayout.stride + var size = mach_msg_type_number_t(HOST_CPU_INFO_COUNT) + var hostInfo = host_cpu_load_info() - let result = host_statistics(machHost, HOST_CPU_LOAD_INFO, - UnsafeMutablePointer(hostInfo), - &size) - - let data = hostInfo.move() - hostInfo.deallocate(capacity: 1) + let result = withUnsafeMutablePointer(to: &hostInfo) { + $0.withMemoryRebound(to: integer_t.self, capacity: Int(size)) { + host_statistics(mach_host_self(), Int32(HOST_CPU_LOAD_INFO), $0, &size) + } + } #if DEBUG if result != KERN_SUCCESS { @@ -576,7 +573,7 @@ public struct System { } #endif - return data + return hostInfo } @@ -594,16 +591,15 @@ public struct System { return processor_set_load_info() } - - var count = PROCESSOR_SET_LOAD_INFO_COUNT - let info_out = processor_set_load_info_t.allocate(capacity: 1) + var size = PROCESSOR_SET_LOAD_INFO_COUNT + var hostInfo = processor_set_load_info() - result = processor_set_statistics(pset, - PROCESSOR_SET_LOAD_INFO, - UnsafeMutablePointer(info_out), - &count) - + result = withUnsafeMutablePointer(to: &hostInfo) { + $0.withMemoryRebound(to: integer_t.self, capacity: Int(size)) { + processor_set_statistics(pset, Int32(PROCESSOR_SET_LOAD_INFO), $0, &size) + } + } #if DEBUG if result != KERN_SUCCESS { @@ -618,10 +614,7 @@ public struct System { // set which should exist by default as long as the machine is running mach_port_deallocate(mach_task_self_, pset) - let data = info_out.move() - info_out.deallocate(capacity: 1) - - return data + return hostInfo } @@ -634,16 +627,14 @@ public struct System { */ private static func VMStatistics64() -> vm_statistics64 { var size = HOST_VM_INFO64_COUNT - let hostInfo = vm_statistics64_t.allocate(capacity: 1) + var hostInfo = vm_statistics64() - let result = host_statistics64(machHost, - HOST_VM_INFO64, - UnsafeMutablePointer(hostInfo), - &size) + let result = withUnsafeMutablePointer(to: &hostInfo) { + $0.withMemoryRebound(to: integer_t.self, capacity: Int(size)) { + host_statistics64(machHost, Int32(HOST_VM_INFO64), $0, &size) + } + } - let data = hostInfo.move() - hostInfo.deallocate(capacity: 1) - #if DEBUG if result != KERN_SUCCESS { print("ERROR - \(#file):\(#function) - kern_result_t = " @@ -651,6 +642,6 @@ public struct System { } #endif - return data + return hostInfo } } From 466f3a49bfef267d61f809d587d5c3594bb0c4be Mon Sep 17 00:00:00 2001 From: OskarGroth Date: Wed, 2 Nov 2016 17:09:21 +0900 Subject: [PATCH 3/4] Swift 3.0.1 --- SystemKit/Info.plist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SystemKit/Info.plist b/SystemKit/Info.plist index 257fa9c..e77c980 100644 --- a/SystemKit/Info.plist +++ b/SystemKit/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.0.6 + 0.0.7 CFBundleSignature ???? CFBundleVersion From 6975606e0435f0f7e6d887bc64fb49e5f84d08b8 Mon Sep 17 00:00:00 2001 From: Oskar Date: Thu, 28 Jun 2018 12:36:03 +0200 Subject: [PATCH 4/4] Swift 4, fix errors --- Example/main.swift | 2 +- SystemKit.xcodeproj/project.pbxproj | 42 ++++++++++++++----- .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++ .../xcshareddata/xcschemes/SystemKit.xcscheme | 2 +- SystemKit/Battery.swift | 20 ++++----- SystemKit/System.swift | 14 +++---- 6 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 SystemKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/Example/main.swift b/Example/main.swift index a4a476b..8563b5b 100644 --- a/Example/main.swift +++ b/Example/main.swift @@ -74,7 +74,7 @@ let counts = System.processCounts() print("\tPROCESSES: \(counts.processCount)") print("\tTHREADS: \(counts.threadCount)") -let loadAverage = System.loadAverage().map { NSString(format:"%.2f", $0) } +let loadAverage = System.loadAverage().map { Double(round(100*$0)/100) } // Round to 2 decimals print("\tLOAD AVERAGE: \(loadAverage)") print("\tMACH FACTOR: \(System.machFactor())") diff --git a/SystemKit.xcodeproj/project.pbxproj b/SystemKit.xcodeproj/project.pbxproj index 19c7a91..f69cd54 100644 --- a/SystemKit.xcodeproj/project.pbxproj +++ b/SystemKit.xcodeproj/project.pbxproj @@ -231,20 +231,20 @@ 4C543D131A08396700C5D9B2 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0830; + LastUpgradeCheck = 1000; ORGANIZATIONNAME = beltex; TargetAttributes = { 4C4BE6061A083A690085170A = { CreatedOnToolsVersion = 6.1; - LastSwiftMigration = 0810; + LastSwiftMigration = 1000; }; 4C543D1B1A08396700C5D9B2 = { CreatedOnToolsVersion = 6.1; - LastSwiftMigration = 0810; + LastSwiftMigration = 1000; }; 4C543D261A08396700C5D9B2 = { CreatedOnToolsVersion = 6.1; - LastSwiftMigration = 0810; + LastSwiftMigration = 1000; }; }; }; @@ -341,7 +341,8 @@ ); OTHER_SWIFT_FLAGS = "-D DEBUG"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.2; }; name = Debug; }; @@ -349,7 +350,8 @@ isa = XCBuildConfiguration; buildSettings = { PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.2; }; name = Release; }; @@ -361,14 +363,22 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -409,14 +419,22 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -459,7 +477,8 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.2; }; name = Debug; }; @@ -479,7 +498,8 @@ PRODUCT_BUNDLE_IDENTIFIER = "beltex.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.2; }; name = Release; }; @@ -499,7 +519,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "beltex.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.2; }; name = Debug; }; @@ -515,7 +536,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "beltex.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.2; }; name = Release; }; diff --git a/SystemKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/SystemKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/SystemKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/SystemKit.xcodeproj/xcshareddata/xcschemes/SystemKit.xcscheme b/SystemKit.xcodeproj/xcshareddata/xcschemes/SystemKit.xcscheme index 70f3e82..24fc06f 100644 --- a/SystemKit.xcodeproj/xcshareddata/xcschemes/SystemKit.xcscheme +++ b/SystemKit.xcodeproj/xcshareddata/xcschemes/SystemKit.xcscheme @@ -1,6 +1,6 @@ Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.CurrentCapacity.rawValue as CFString!, + Key.CurrentCapacity.rawValue as CFString, kCFAllocatorDefault,0) return prop!.takeUnretainedValue() as! Int } @@ -176,7 +176,7 @@ public struct Battery { */ public func maxCapactiy() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.MaxCapacity.rawValue as CFString!, + Key.MaxCapacity.rawValue as CFString, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -191,7 +191,7 @@ public struct Battery { */ public func designCapacity() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.DesignCapacity.rawValue as CFString!, + Key.DesignCapacity.rawValue as CFString, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -204,7 +204,7 @@ public struct Battery { */ public func cycleCount() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.CycleCount.rawValue as CFString!, + Key.CycleCount.rawValue as CFString, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -217,7 +217,7 @@ public struct Battery { */ public func designCycleCount() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.DesignCycleCount.rawValue as CFString!, + Key.DesignCycleCount.rawValue as CFString, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -230,7 +230,7 @@ public struct Battery { */ public func isACPowered() -> Bool { let prop = IORegistryEntryCreateCFProperty(service, - Key.ACPowered.rawValue as CFString!, + Key.ACPowered.rawValue as CFString, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Bool } @@ -243,7 +243,7 @@ public struct Battery { */ public func isCharging() -> Bool { let prop = IORegistryEntryCreateCFProperty(service, - Key.IsCharging.rawValue as CFString!, + Key.IsCharging.rawValue as CFString, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Bool } @@ -256,7 +256,7 @@ public struct Battery { */ public func isCharged() -> Bool { let prop = IORegistryEntryCreateCFProperty(service, - Key.FullyCharged.rawValue as CFString!, + Key.FullyCharged.rawValue as CFString, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Bool } @@ -282,7 +282,7 @@ public struct Battery { */ public func timeRemaining() -> Int { let prop = IORegistryEntryCreateCFProperty(service, - Key.TimeRemaining.rawValue as CFString!, + Key.TimeRemaining.rawValue as CFString, kCFAllocatorDefault, 0) return prop!.takeUnretainedValue() as! Int } @@ -312,7 +312,7 @@ public struct Battery { */ public func temperature(_ unit: TemperatureUnit = .celsius) -> Double { let prop = IORegistryEntryCreateCFProperty(service, - Key.Temperature.rawValue as CFString!, + Key.Temperature.rawValue as CFString, kCFAllocatorDefault, 0) var temperature = prop?.takeUnretainedValue() as! Double / 100.0 diff --git a/SystemKit/System.swift b/SystemKit/System.swift index c88ffd5..6bccd01 100644 --- a/SystemKit/System.swift +++ b/SystemKit/System.swift @@ -188,7 +188,7 @@ public struct System { else { name = String() } - ptr.deallocate(capacity: 1) + ptr.deallocate() #if DEBUG if result != 0 { @@ -461,7 +461,7 @@ public struct System { as! Double } - status.deallocate(capacity: 1) + status.deallocate() return (processorSpeed, processorCount, schedulerTime) } @@ -523,7 +523,7 @@ public struct System { } let data = hostInfo.move() - hostInfo.deallocate(capacity: 1) + hostInfo.deallocate() #if DEBUG if result != KERN_SUCCESS { @@ -547,7 +547,7 @@ public struct System { } let data = hostInfo.move() - hostInfo.deallocate(capacity: 1) + hostInfo.deallocate() #if DEBUG if result != KERN_SUCCESS { @@ -571,7 +571,7 @@ public struct System { } let data = hostInfo.move() - hostInfo.deallocate(capacity: 1) + hostInfo.deallocate() #if DEBUG if result != KERN_SUCCESS { @@ -624,7 +624,7 @@ public struct System { mach_port_deallocate(mach_task_self_, pset) let data = info_out.move() - info_out.deallocate(capacity: 1) + info_out.deallocate() return data } @@ -649,7 +649,7 @@ public struct System { } let data = hostInfo.move() - hostInfo.deallocate(capacity: 1) + hostInfo.deallocate() #if DEBUG if result != KERN_SUCCESS {