From 864c39a52e6a3d3f56495e617b67c39663b10528 Mon Sep 17 00:00:00 2001 From: Kevin Renskers Date: Sat, 24 Sep 2022 06:29:30 +0200 Subject: [PATCH] ref: Add typealias for bytes (#2209) --- Sentry.xcodeproj/project.pbxproj | 4 ++ Sources/Sentry/SentryClient.m | 44 +++++++++---------- Sources/Sentry/SentryCrashIntegration.m | 4 +- Sources/Sentry/SentryCrashWrapper.m | 10 ++--- Sources/Sentry/include/SentryCrashWrapper.h | 7 +-- .../Sentry/include/SentryInternalDefines.h | 1 + .../Monitors/SentryCrashMonitorContext.h | 4 +- .../Monitors/SentryCrashMonitor_System.h | 5 ++- .../Monitors/SentryCrashMonitor_System.m | 36 +++++++-------- Sources/SentryCrash/Recording/SentryCrash.m | 4 +- .../SentryCrash/Recording/SentryCrashReport.c | 4 +- Tests/SentryTests/SentryClientTests.swift | 6 +-- .../SentryCrash/TestSentryCrashWrapper.h | 6 +-- .../SentryCrash/TestSentryCrashWrapper.m | 18 ++++---- 14 files changed, 80 insertions(+), 73 deletions(-) create mode 100644 Sources/Sentry/include/SentryInternalDefines.h diff --git a/Sentry.xcodeproj/project.pbxproj b/Sentry.xcodeproj/project.pbxproj index a2cb585965c..8ddefc31526 100644 --- a/Sentry.xcodeproj/project.pbxproj +++ b/Sentry.xcodeproj/project.pbxproj @@ -55,6 +55,7 @@ 0A9BF4E428A114B50068D266 /* SentryViewHierarchyIntegration.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A9BF4E328A114B50068D266 /* SentryViewHierarchyIntegration.h */; }; 0A9BF4E928A125390068D266 /* TestSentryViewHierarchy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A9BF4E628A123270068D266 /* TestSentryViewHierarchy.swift */; }; 0A9BF4EB28A127120068D266 /* SentryViewHierarchyIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A9BF4EA28A127120068D266 /* SentryViewHierarchyIntegrationTests.swift */; }; + 0A9E917128DC7E7000FB4182 /* SentryInternalDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A9E917028DC7E7000FB4182 /* SentryInternalDefines.h */; }; 0AABE2ED2885924A0057ED69 /* SentryPermissionsObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 0AABE2EC2885924A0057ED69 /* SentryPermissionsObserver.m */; }; 0ACBA10128A6406400D711F7 /* UIView+Sentry.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ACBA10028A6406400D711F7 /* UIView+Sentry.m */; }; 0ACBA10328A6407200D711F7 /* UIView+Sentry.h in Headers */ = {isa = PBXBuildFile; fileRef = 0ACBA10228A6407200D711F7 /* UIView+Sentry.h */; }; @@ -759,6 +760,7 @@ 0A9BF4E328A114B50068D266 /* SentryViewHierarchyIntegration.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryViewHierarchyIntegration.h; path = include/SentryViewHierarchyIntegration.h; sourceTree = ""; }; 0A9BF4E628A123270068D266 /* TestSentryViewHierarchy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSentryViewHierarchy.swift; sourceTree = ""; }; 0A9BF4EA28A127120068D266 /* SentryViewHierarchyIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryViewHierarchyIntegrationTests.swift; sourceTree = ""; }; + 0A9E917028DC7E7000FB4182 /* SentryInternalDefines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryInternalDefines.h; path = include/SentryInternalDefines.h; sourceTree = ""; }; 0AABE2EC2885924A0057ED69 /* SentryPermissionsObserver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryPermissionsObserver.m; sourceTree = ""; }; 0AABE2EE288592750057ED69 /* SentryPermissionsObserver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryPermissionsObserver.h; path = include/SentryPermissionsObserver.h; sourceTree = ""; }; 0AABE2EF2885C2120057ED69 /* TestSentryPermissionsObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSentryPermissionsObserver.swift; sourceTree = ""; }; @@ -1813,6 +1815,7 @@ 7B2A70DC27D6083D008B0D15 /* SentryThreadWrapper.m */, 7B18DE3F28D9F748004845C6 /* SentryNSNotificationCenterWrapper.h */, 7B18DE4128D9F794004845C6 /* SentryNSNotificationCenterWrapper.m */, + 0A9E917028DC7E7000FB4182 /* SentryInternalDefines.h */, ); name = Helper; sourceTree = ""; @@ -2964,6 +2967,7 @@ D8479328278873A100BE8E99 /* SentryByteCountFormatter.h in Headers */, 63AA76981EB9C1C200D153DE /* SentryClient.h in Headers */, 63AA76971EB9C1C200D153DE /* Sentry.h in Headers */, + 0A9E917128DC7E7000FB4182 /* SentryInternalDefines.h in Headers */, 63FE711F20DA4C1000CDBAE8 /* SentryCrashObjC.h in Headers */, 7BC3936825B1AB3E004F03D3 /* SentryLevelMapper.h in Headers */, 8E4E7C6E25DAAAFE006AB9E2 /* SentrySpan.h in Headers */, diff --git a/Sources/Sentry/SentryClient.m b/Sources/Sentry/SentryClient.m index 7daef95bb01..55b04360815 100644 --- a/Sources/Sentry/SentryClient.m +++ b/Sources/Sentry/SentryClient.m @@ -768,35 +768,35 @@ - (void)applyCultureContextToEvent:(SentryEvent *)event - (void)applyExtraDeviceContextToEvent:(SentryEvent *)event { - [self modifyContext:event - key:@"device" - block:^(NSMutableDictionary *device) { - device[SentryDeviceContextFreeMemoryKey] = @(self.crashWrapper.freeMemory); - device[@"free_storage"] = @(self.crashWrapper.freeStorage); + [self + modifyContext:event + key:@"device" + block:^(NSMutableDictionary *device) { + device[SentryDeviceContextFreeMemoryKey] = @(self.crashWrapper.freeMemorySize); + device[@"free_storage"] = @(self.crashWrapper.freeStorageSize); #if TARGET_OS_IOS - if (self.deviceWrapper.orientation != UIDeviceOrientationUnknown) { - device[@"orientation"] - = UIDeviceOrientationIsPortrait(self.deviceWrapper.orientation) - ? @"portrait" - : @"landscape"; - } - - if (self.deviceWrapper.isBatteryMonitoringEnabled) { - device[@"charging"] - = self.deviceWrapper.batteryState == UIDeviceBatteryStateCharging - ? @(YES) - : @(NO); - device[@"battery_level"] = - @((int)(self.deviceWrapper.batteryLevel * 100)); - } + if (self.deviceWrapper.orientation != UIDeviceOrientationUnknown) { + device[@"orientation"] + = UIDeviceOrientationIsPortrait(self.deviceWrapper.orientation) + ? @"portrait" + : @"landscape"; + } + + if (self.deviceWrapper.isBatteryMonitoringEnabled) { + device[@"charging"] + = self.deviceWrapper.batteryState == UIDeviceBatteryStateCharging + ? @(YES) + : @(NO); + device[@"battery_level"] = @((int)(self.deviceWrapper.batteryLevel * 100)); + } #endif - }]; + }]; [self modifyContext:event key:@"app" block:^(NSMutableDictionary *app) { - app[SentryDeviceContextAppMemoryKey] = @(self.crashWrapper.appMemory); + app[SentryDeviceContextAppMemoryKey] = @(self.crashWrapper.appMemorySize); }]; } diff --git a/Sources/Sentry/SentryCrashIntegration.m b/Sources/Sentry/SentryCrashIntegration.m index 0c53d0ed554..7bcd76b394b 100644 --- a/Sources/Sentry/SentryCrashIntegration.m +++ b/Sources/Sentry/SentryCrashIntegration.m @@ -249,8 +249,8 @@ + (void)enrichScope:(SentryScope *)scope crashWrapper:(SentryCrashWrapper *)cras [deviceData setValue:systemInfo[@"cpuArchitecture"] forKey:@"arch"]; [deviceData setValue:systemInfo[@"machine"] forKey:@"model"]; [deviceData setValue:systemInfo[@"model"] forKey:@"model_id"]; - [deviceData setValue:systemInfo[@"freeMemory"] forKey:SentryDeviceContextFreeMemoryKey]; - [deviceData setValue:systemInfo[@"usableMemory"] forKey:@"usable_memory"]; + [deviceData setValue:systemInfo[@"freeMemorySize"] forKey:SentryDeviceContextFreeMemoryKey]; + [deviceData setValue:systemInfo[@"usableMemorySize"] forKey:@"usable_memory"]; [deviceData setValue:systemInfo[@"memorySize"] forKey:@"memory_size"]; [deviceData setValue:systemInfo[@"totalStorageSize"] forKey:@"storage_size"]; [deviceData setValue:systemInfo[@"freeStorageSize"] forKey:@"free_storage"]; diff --git a/Sources/Sentry/SentryCrashWrapper.m b/Sources/Sentry/SentryCrashWrapper.m index bc3643dffd5..db599881906 100644 --- a/Sources/Sentry/SentryCrashWrapper.m +++ b/Sources/Sentry/SentryCrashWrapper.m @@ -71,17 +71,17 @@ - (NSDictionary *)systemInfo return sharedInfo; } -- (uint64_t)freeMemory +- (bytes)freeMemorySize { - return sentrycrashcm_system_freememory(); + return sentrycrashcm_system_freememory_size(); } -- (uint64_t)freeStorage +- (bytes)freeStorageSize { - return sentrycrashcm_system_freestorage(); + return sentrycrashcm_system_freestorage_size(); } -- (uint64_t)appMemory +- (bytes)appMemorySize { task_vm_info_data_t info; mach_msg_type_number_t size = TASK_VM_INFO_COUNT; diff --git a/Sources/Sentry/include/SentryCrashWrapper.h b/Sources/Sentry/include/SentryCrashWrapper.h index 3e15fc9dba5..e069ad73c65 100644 --- a/Sources/Sentry/include/SentryCrashWrapper.h +++ b/Sources/Sentry/include/SentryCrashWrapper.h @@ -1,4 +1,5 @@ #import "SentryDefines.h" +#import "SentryInternalDefines.h" #import NS_ASSUME_NONNULL_BEGIN @@ -30,11 +31,11 @@ SENTRY_NO_INIT - (NSDictionary *)systemInfo; -- (uint64_t)freeMemory; +- (bytes)freeMemorySize; -- (uint64_t)appMemory; +- (bytes)appMemorySize; -- (uint64_t)freeStorage; +- (bytes)freeStorageSize; @end diff --git a/Sources/Sentry/include/SentryInternalDefines.h b/Sources/Sentry/include/SentryInternalDefines.h new file mode 100644 index 00000000000..47c21f81fb4 --- /dev/null +++ b/Sources/Sentry/include/SentryInternalDefines.h @@ -0,0 +1 @@ +typedef unsigned long long bytes; diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitorContext.h b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitorContext.h index 90d83fe2e23..1ada1e2f6bd 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitorContext.h +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitorContext.h @@ -204,8 +204,8 @@ typedef struct SentryCrash_MonitorContext { uint64_t totalStorageSize; uint64_t freeStorageSize; uint64_t memorySize; - uint64_t freeMemory; - uint64_t usableMemory; + uint64_t freeMemorySize; + uint64_t usableMemorySize; } System; struct { diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.h b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.h index ee3429c7001..468c4fcc859 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.h +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.h @@ -32,6 +32,7 @@ extern "C" { #endif #include "SentryCrashMonitor.h" +#import "SentryInternalDefines.h" /** Access the Monitor API. */ @@ -39,8 +40,8 @@ SentryCrashMonitorAPI *sentrycrashcm_system_getAPI(void); bool sentrycrash_isSimulatorBuild(void); -uint64_t sentrycrashcm_system_freememory(void); -uint64_t sentrycrashcm_system_freestorage(void); +bytes sentrycrashcm_system_freememory_size(void); +bytes sentrycrashcm_system_freestorage_size(void); #ifdef __cplusplus } diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.m b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.m index 12268e166ac..3d43b1d79fc 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.m +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.m @@ -71,9 +71,9 @@ int parentProcessID; const char *deviceAppHash; const char *buildType; - uint64_t totalStorageSize; - uint64_t freeStorageSize; - uint64_t memorySize; + bytes totalStorageSize; + bytes freeStorageSize; + bytes memorySize; } SystemData; static SystemData g_systemData; @@ -188,30 +188,30 @@ return true; } -static uint64_t -freeMemory(void) +static bytes +freeMemorySize(void) { vm_statistics_data_t vmStats; vm_size_t pageSize; if (VMStats(&vmStats, &pageSize)) { - return ((uint64_t)pageSize) * vmStats.free_count; + return ((bytes)pageSize) * vmStats.free_count; } return 0; } -uint64_t -sentrycrashcm_system_freememory(void) +bytes +sentrycrashcm_system_freememory_size(void) { - return freeMemory(); + return freeMemorySize(); } -static uint64_t -usableMemory(void) +static bytes +usableMemorySize(void) { vm_statistics_data_t vmStats; vm_size_t pageSize; if (VMStats(&vmStats, &pageSize)) { - return ((uint64_t)pageSize) + return ((bytes)pageSize) * (vmStats.active_count + vmStats.inactive_count + vmStats.wire_count + vmStats.free_count); } @@ -486,7 +486,7 @@ return "unknown"; } -static uint64_t +static bytes getTotalStorageSize() { NSNumber *storageSize = [[[NSFileManager defaultManager] @@ -495,7 +495,7 @@ return storageSize.unsignedLongLongValue; } -static uint64_t +static bytes getFreeStorageSize() { NSNumber *storageSize = [[[NSFileManager defaultManager] @@ -504,8 +504,8 @@ return storageSize.unsignedLongLongValue; } -uint64_t -sentrycrashcm_system_freestorage(void) +bytes +sentrycrashcm_system_freestorage_size(void) { return getFreeStorageSize(); } @@ -642,8 +642,8 @@ COPY_REFERENCE(totalStorageSize); COPY_REFERENCE(freeStorageSize); COPY_REFERENCE(memorySize); - eventContext->System.freeMemory = freeMemory(); - eventContext->System.usableMemory = usableMemory(); + eventContext->System.freeMemorySize = freeMemorySize(); + eventContext->System.usableMemorySize = usableMemorySize(); } } diff --git a/Sources/SentryCrash/Recording/SentryCrash.m b/Sources/SentryCrash/Recording/SentryCrash.m index cebf7309dc0..36b51320c8b 100644 --- a/Sources/SentryCrash/Recording/SentryCrash.m +++ b/Sources/SentryCrash/Recording/SentryCrash.m @@ -261,8 +261,8 @@ - (NSDictionary *)systemInfo COPY_PRIMITIVE(totalStorageSize); COPY_PRIMITIVE(freeStorageSize); COPY_PRIMITIVE(memorySize); - COPY_PRIMITIVE(freeMemory); - COPY_PRIMITIVE(usableMemory); + COPY_PRIMITIVE(freeMemorySize); + COPY_PRIMITIVE(usableMemorySize); return dict; } diff --git a/Sources/SentryCrash/Recording/SentryCrashReport.c b/Sources/SentryCrash/Recording/SentryCrashReport.c index b5849b788cd..79693f07837 100644 --- a/Sources/SentryCrash/Recording/SentryCrashReport.c +++ b/Sources/SentryCrash/Recording/SentryCrashReport.c @@ -1252,9 +1252,9 @@ writeMemoryInfo(const SentryCrashReportWriter *const writer, const char *const k writer->addUIntegerElement( writer, SentryCrashField_Size, monitorContext->System.memorySize); writer->addUIntegerElement( - writer, SentryCrashField_Usable, monitorContext->System.usableMemory); + writer, SentryCrashField_Usable, monitorContext->System.usableMemorySize); writer->addUIntegerElement( - writer, SentryCrashField_Free, monitorContext->System.freeMemory); + writer, SentryCrashField_Free, monitorContext->System.freeMemorySize); } writer->endContainer(writer); } diff --git a/Tests/SentryTests/SentryClientTests.swift b/Tests/SentryTests/SentryClientTests.swift index 33a6a5017a3..8f169d9a579 100644 --- a/Tests/SentryTests/SentryClientTests.swift +++ b/Tests/SentryTests/SentryClientTests.swift @@ -561,9 +561,9 @@ class SentryClientTest: XCTestCase { event.threads = nil event.debugMeta = nil - fixture.crashWrapper.internalFreeMemory = 123_456 - fixture.crashWrapper.internalAppMemory = 234_567 - fixture.crashWrapper.internalFreeStorage = 345_678 + fixture.crashWrapper.internalFreeMemorySize = 123_456 + fixture.crashWrapper.internalAppMemorySize = 234_567 + fixture.crashWrapper.internalFreeStorageSize = 345_678 fixture.getSut().captureCrash(event, with: fixture.scope) assertLastSentEventWithAttachment { actual in diff --git a/Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.h b/Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.h index 53c8d800736..e2ba87fe97b 100644 --- a/Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.h +++ b/Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.h @@ -25,11 +25,11 @@ SENTRY_NO_INIT @property (nonatomic, assign) BOOL closeCalled; -@property (nonatomic, assign) uint64_t internalFreeMemory; +@property (nonatomic, assign) uint64_t internalFreeMemorySize; -@property (nonatomic, assign) uint64_t internalAppMemory; +@property (nonatomic, assign) uint64_t internalAppMemorySize; -@property (nonatomic, assign) uint64_t internalFreeStorage; +@property (nonatomic, assign) uint64_t internalFreeStorageSize; @end diff --git a/Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.m b/Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.m index 28eb052b078..90c17aff8fb 100644 --- a/Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.m +++ b/Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.m @@ -14,9 +14,9 @@ + (instancetype)sharedInstance instance.internalIsApplicationInForeground = YES; instance.installAsyncHooksCalled = NO; instance.closeCalled = NO; - instance.internalFreeMemory = 0; - instance.internalAppMemory = 0; - instance.internalFreeStorage = 0; + instance.internalFreeMemorySize = 0; + instance.internalAppMemorySize = 0; + instance.internalFreeStorageSize = 0; return instance; } @@ -60,19 +60,19 @@ - (NSDictionary *)systemInfo return @{}; } -- (uint64_t)freeMemory +- (bytes)freeMemorySize { - return self.internalFreeMemory; + return self.internalFreeMemorySize; } -- (uint64_t)appMemory +- (bytes)appMemorySize { - return self.internalAppMemory; + return self.internalAppMemorySize; } -- (uint64_t)freeStorage +- (bytes)freeStorageSize { - return self.internalFreeStorage; + return self.internalFreeStorageSize; } @end