GBDeviceInfo_Common.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // GBDeviceInfoTypes_Common.m
  3. // GBDeviceInfo
  4. //
  5. // Created by Luka Mirosevic on 20/02/2015.
  6. // Copyright (c) 2015 Luka Mirosevic. All rights reserved.
  7. //
  8. #import "GBDeviceInfo_Common.h"
  9. #import "GBDeviceInfo_Subclass.h"
  10. #if TARGET_OS_IPHONE
  11. #import "GBDeviceInfo_iOS.h"
  12. #else
  13. #import "GBDeviceInfo_OSX.h"
  14. #endif
  15. #import <stdlib.h>
  16. #import <stdio.h>
  17. #import <sys/types.h>
  18. #import <sys/sysctl.h>
  19. #import <sys/utsname.h>
  20. static NSString * const kHardwareCPUFrequencyKey = @"hw.cpufrequency";
  21. static NSString * const kHardwareNumberOfCoresKey = @"hw.ncpu";
  22. static NSString * const kHardwareByteOrderKey = @"hw.byteorder";
  23. static NSString * const kHardwareL2CacheSizeKey = @"hw.l2cachesize";
  24. @implementation GBDeviceInfo_Common
  25. #pragma mark - Public
  26. + (instancetype)deviceInfo {
  27. static id _shared;
  28. static dispatch_once_t onceToken;
  29. dispatch_once(&onceToken, ^{
  30. _shared = [self new];
  31. });
  32. return _shared;
  33. }
  34. - (BOOL)isOperatingSystemAtLeastOSVersion:(GBOSVersion)version {
  35. GBOSVersion currentVersion = [GBDeviceInfo deviceInfo].osVersion;
  36. // major bigger
  37. if (currentVersion.major > version.major) {
  38. return YES;
  39. }
  40. // major equal
  41. else if (currentVersion.major == version.major) {
  42. // minor bigger
  43. if (currentVersion.minor > version.minor) {
  44. return YES;
  45. }
  46. // minor equal
  47. else if (currentVersion.minor == version.minor) {
  48. // patch bigger
  49. if (currentVersion.patch > version.patch) {
  50. return YES;
  51. }
  52. // patch equal
  53. else if (currentVersion.patch == version.patch) {
  54. return YES;
  55. }
  56. // patch smaller
  57. else {
  58. return NO;
  59. }
  60. }
  61. // minor smaller
  62. else {
  63. return NO;
  64. }
  65. }
  66. // major smaller
  67. else {
  68. return NO;
  69. }
  70. }
  71. - (BOOL)isOperatingSystemAtLeastVersion:(NSString *)versionString {
  72. return [self isOperatingSystemAtLeastOSVersion:GBOSVersionFromString(versionString)];
  73. }
  74. #pragma mark - Private
  75. + (NSString *)_sysctlStringForKey:(NSString *)key {
  76. const char *keyCString = [key UTF8String];
  77. NSString *answer = @"";
  78. size_t length;
  79. sysctlbyname(keyCString, NULL, &length, NULL, 0);
  80. if (length) {
  81. char *answerCString = malloc(length * sizeof(char));
  82. sysctlbyname(keyCString, answerCString, &length, NULL, 0);
  83. answer = [NSString stringWithCString:answerCString encoding:NSUTF8StringEncoding];
  84. free(answerCString);
  85. }
  86. return answer;
  87. }
  88. + (CGFloat)_sysctlCGFloatForKey:(NSString *)key {
  89. const char *keyCString = [key UTF8String];
  90. CGFloat answerFloat = 0;
  91. size_t length = 0;
  92. sysctlbyname(keyCString, NULL, &length, NULL, 0);
  93. if (length) {
  94. char *answerRaw = malloc(length * sizeof(char));
  95. sysctlbyname(keyCString, answerRaw, &length, NULL, 0);
  96. switch (length) {
  97. case 8: {
  98. answerFloat = (CGFloat)*(int64_t *)answerRaw;
  99. } break;
  100. case 4: {
  101. answerFloat = (CGFloat)*(int32_t *)answerRaw;
  102. } break;
  103. default: {
  104. answerFloat = 0.;
  105. } break;
  106. }
  107. free(answerRaw);
  108. }
  109. return answerFloat;
  110. }
  111. + (GBCPUInfo)_cpuInfo {
  112. return GBCPUInfoMake(
  113. [self _sysctlCGFloatForKey:kHardwareCPUFrequencyKey] / 1000000000., //giga
  114. (NSUInteger)[self _sysctlCGFloatForKey:kHardwareNumberOfCoresKey],
  115. [self _sysctlCGFloatForKey:kHardwareL2CacheSizeKey] / 1024 //kibi
  116. );
  117. }
  118. + (CGFloat)_physicalMemory {
  119. return [[NSProcessInfo processInfo] physicalMemory] / 1073741824.; //gibi
  120. }
  121. + (GBByteOrder)_systemByteOrder {
  122. NSString *byteOrderString = [self _sysctlStringForKey:kHardwareByteOrderKey];
  123. if ([byteOrderString isEqualToString:@"1234"]) {
  124. return GBByteOrderLittleEndian;
  125. }
  126. else {
  127. return GBByteOrderBigEndian;
  128. }
  129. }
  130. @end