From a3c156361280600366639152087fe4e660dd8192 Mon Sep 17 00:00:00 2001 From: Felix Schwarz Date: Sat, 26 Oct 2024 23:02:51 +0200 Subject: [PATCH] - move memory configuration from OCCoreManager to OCPlatform - effectively disable caching in minimum memory configuration for OCCore's thumbnail cache and OCResourceManager's OCResource cache --- ownCloudSDK/Bookmark/OCBookmark+DataItem.m | 4 +-- ownCloudSDK/Connection/OCConnection+Upload.m | 6 ++-- ownCloudSDK/Core/ItemList/OCCore+ItemList.m | 4 +-- ownCloudSDK/Core/OCCore.h | 11 ++----- ownCloudSDK/Core/OCCore.m | 12 +++---- .../Resources/Manager/OCResourceManager.h | 2 +- .../Resources/Manager/OCResourceManager.m | 8 ++--- ownCloudSDK/Platforms/OCPlatform.h | 10 ++++++ ownCloudSDK/Platforms/OCPlatform.m | 31 +++++++++++++++++++ .../Resource Management/OCCoreManager.h | 1 - .../Resource Management/OCCoreManager.m | 23 -------------- ownCloudSDK/Vaults/Database/OCDatabase.m | 10 +++--- .../Vaults/Database/SQLite/OCSQLiteDB.m | 8 ++++- 13 files changed, 74 insertions(+), 56 deletions(-) diff --git a/ownCloudSDK/Bookmark/OCBookmark+DataItem.m b/ownCloudSDK/Bookmark/OCBookmark+DataItem.m index 6c0f88d0..97ddea7a 100644 --- a/ownCloudSDK/Bookmark/OCBookmark+DataItem.m +++ b/ownCloudSDK/Bookmark/OCBookmark+DataItem.m @@ -20,7 +20,7 @@ #import "OCDataTypes.h" #import "OCResource.h" #import "OCMacros.h" -#import "OCCoreManager.h" +#import "OCPlatform.h" @implementation OCBookmark (DataItem) @@ -36,7 +36,7 @@ - (OCDataItemReference)dataItemReference - (OCDataItemVersion)dataItemVersion { - if (OCCoreManager.sharedCoreManager.memoryConfiguration != OCCoreMemoryConfigurationMinimum) + if (OCPlatform.current.memoryConfiguration != OCPlatformMemoryConfigurationMinimum) { OCResource *avatarResource = OCTypedCast(self.avatar, OCResource); NSString *avatarVersion = ((avatarResource != nil) ? avatarResource.version : @""); diff --git a/ownCloudSDK/Connection/OCConnection+Upload.m b/ownCloudSDK/Connection/OCConnection+Upload.m index abe6cf91..1b0f992d 100644 --- a/ownCloudSDK/Connection/OCConnection+Upload.m +++ b/ownCloudSDK/Connection/OCConnection+Upload.m @@ -27,7 +27,7 @@ #import "OCHTTPResponse+DAVError.h" #import "NSProgress+OCExtensions.h" #import "OCCore+SyncEngine.h" -#import "OCCoreManager.h" +#import "OCPlatform.h" typedef NSString* OCUploadInfoKey; typedef NSString* OCUploadInfoTask; @@ -241,7 +241,7 @@ - (OCProgress *)_tusUploadFileFromURL:(NSURL *)sourceURL withName:(NSString *)fi { NSNumber *capabilitiesTusMaxChunkSize; - if (OCCoreManager.sharedCoreManager.memoryConfiguration != OCCoreMemoryConfigurationMinimum) + if (OCPlatform.current.memoryConfiguration != OCPlatformMemoryConfigurationMinimum) { // Memory configuration is NOT minimum, so avoid splitting up files into chunks if // possible, which requires additional memory and could mean going over a tight memory @@ -296,7 +296,7 @@ - (OCProgress *)_continueTusJob:(OCTUSJob *)tusJob lastTask:(NSString *)lastTask return (nil); } - if (OCCoreManager.sharedCoreManager.memoryConfiguration == OCCoreMemoryConfigurationMinimum) + if (OCPlatform.current.memoryConfiguration == OCPlatformMemoryConfigurationMinimum) { // Memory configuration is minimum, so use just Creation instead of Creation-With-Upload // to avoid splitting up files into chunks if possible, which requires additional memory diff --git a/ownCloudSDK/Core/ItemList/OCCore+ItemList.m b/ownCloudSDK/Core/ItemList/OCCore+ItemList.m index f78ebe9a..463aaa69 100644 --- a/ownCloudSDK/Core/ItemList/OCCore+ItemList.m +++ b/ownCloudSDK/Core/ItemList/OCCore+ItemList.m @@ -51,11 +51,11 @@ - (NSUInteger)parallelItemListTaskCount { switch (self.memoryConfiguration) { - case OCCoreMemoryConfigurationMinimum: + case OCPlatformMemoryConfigurationMinimum: return (1); break; - case OCCoreMemoryConfigurationDefault: + case OCPlatformMemoryConfigurationDefault: default: return (2); break; diff --git a/ownCloudSDK/Core/OCCore.h b/ownCloudSDK/Core/OCCore.h index 741d1002..63c09221 100644 --- a/ownCloudSDK/Core/OCCore.h +++ b/ownCloudSDK/Core/OCCore.h @@ -43,6 +43,7 @@ #import "OCDataSourceComposition.h" #import "OCDataItemPresentable.h" #import "OCShareRole.h" +#import "OCPlatform.h" @class OCCore; @class OCItem; @@ -95,12 +96,6 @@ typedef NS_ENUM(NSUInteger, OCCoreConnectionStatusSignalState) OCCoreConnectionStatusSignalStateForceTrue //!< Signal state is force true (overriding any false states) } __attribute__((enum_extensibility(closed))); -typedef NS_ENUM(NSUInteger, OCCoreMemoryConfiguration) -{ - OCCoreMemoryConfigurationDefault, //!< Default memory configuration - OCCoreMemoryConfigurationMinimum //!< Try using only the minimum amount of memory needed -} __attribute__((enum_extensibility(closed))); - typedef NS_ENUM(NSUInteger,OCCoreAvailableOfflineCoverage) { OCCoreAvailableOfflineCoverageNone, //!< Item is not targeted by available offline item policy @@ -156,7 +151,7 @@ typedef id OCCoreItemTracking; OCConnection *_connection; BOOL _attemptConnect; - OCCoreMemoryConfiguration _memoryConfiguration; + OCPlatformMemoryConfiguration _memoryConfiguration; NSMutableArray *_queries; @@ -310,7 +305,7 @@ typedef id OCCoreItemTracking; @property(readonly) OCVault *vault; //!< Vault managing storage and database access for this core. @property(readonly) OCConnection *connection; //!< Connection used by the core to make requests to the server. -@property(assign,nonatomic) OCCoreMemoryConfiguration memoryConfiguration; +@property(assign,nonatomic) OCPlatformMemoryConfiguration memoryConfiguration; @property(readonly,nonatomic) OCCoreState state; @property(copy) OCCoreStateChangedHandler stateChangedHandler; diff --git a/ownCloudSDK/Core/OCCore.m b/ownCloudSDK/Core/OCCore.m index a8911836..60f08e8c 100644 --- a/ownCloudSDK/Core/OCCore.m +++ b/ownCloudSDK/Core/OCCore.m @@ -410,7 +410,7 @@ - (instancetype)initWithBookmark:(OCBookmark *)bookmark [self addSignalProvider:_connectingStatusSignalProvider]; [self addSignalProvider:_connectionStatusSignalProvider]; - self.memoryConfiguration = OCCoreManager.sharedCoreManager.memoryConfiguration; + self.memoryConfiguration = OCPlatform.current.memoryConfiguration; [self startIPCObservation]; } @@ -755,7 +755,7 @@ - (void)__attemptConnect } // If app provider is available and enabled - if (OCCoreManager.sharedCoreManager.memoryConfiguration != OCCoreMemoryConfigurationMinimum) // only load app providers in memory configurations other than minimum + if (self.memoryConfiguration != OCPlatformMemoryConfigurationMinimum) // only load app providers in memory configurations other than minimum { OCAppProvider *latestSupportedAppProvider = self.connection.capabilities.latestSupportedAppProvider; @@ -1043,7 +1043,7 @@ - (OCMessageQueue *)messageQueue } #pragma mark - Memory configuration -- (void)setMemoryConfiguration:(OCCoreMemoryConfiguration)memoryConfiguration +- (void)setMemoryConfiguration:(OCPlatformMemoryConfiguration)memoryConfiguration { _memoryConfiguration = memoryConfiguration; @@ -1051,12 +1051,12 @@ - (void)setMemoryConfiguration:(OCCoreMemoryConfiguration)memoryConfiguration switch (_memoryConfiguration) { - case OCCoreMemoryConfigurationDefault: + case OCPlatformMemoryConfigurationDefault: _thumbnailCache.countLimit = OCCacheLimitNone; break; - case OCCoreMemoryConfigurationMinimum: - _thumbnailCache.countLimit = 1; + case OCPlatformMemoryConfigurationMinimum: + _thumbnailCache.countLimit = 0; break; } } diff --git a/ownCloudSDK/Core/Resources/Manager/OCResourceManager.h b/ownCloudSDK/Core/Resources/Manager/OCResourceManager.h index 28cb49b4..75d3237d 100644 --- a/ownCloudSDK/Core/Resources/Manager/OCResourceManager.h +++ b/ownCloudSDK/Core/Resources/Manager/OCResourceManager.h @@ -40,7 +40,7 @@ typedef void(^OCResourceRetrieveCompletionHandler)(NSError * _Nullable error, OC @property(weak,nullable) id storage; @property(weak,nullable) OCCore *core; -@property(assign,nonatomic) OCCoreMemoryConfiguration memoryConfiguration; +@property(assign,nonatomic) OCPlatformMemoryConfiguration memoryConfiguration; // @property(assign) NSUInteger maximumConcurrentJobs; //!< Maximum number of jobs to work on in parallel. A value of 0 indicates no limit. diff --git a/ownCloudSDK/Core/Resources/Manager/OCResourceManager.m b/ownCloudSDK/Core/Resources/Manager/OCResourceManager.m index 7c681073..cb287952 100644 --- a/ownCloudSDK/Core/Resources/Manager/OCResourceManager.m +++ b/ownCloudSDK/Core/Resources/Manager/OCResourceManager.m @@ -56,18 +56,18 @@ - (instancetype)initWithStorage:(id)storage return (self); } -- (void)setMemoryConfiguration:(OCCoreMemoryConfiguration)memoryConfiguration +- (void)setMemoryConfiguration:(OCPlatformMemoryConfiguration)memoryConfiguration { _memoryConfiguration = memoryConfiguration; switch (_memoryConfiguration) { - case OCCoreMemoryConfigurationDefault: + case OCPlatformMemoryConfigurationDefault: _cache.countLimit = OCCacheLimitNone; break; - case OCCoreMemoryConfigurationMinimum: - _cache.countLimit = 1; + case OCPlatformMemoryConfigurationMinimum: + _cache.countLimit = 0; break; } } diff --git a/ownCloudSDK/Platforms/OCPlatform.h b/ownCloudSDK/Platforms/OCPlatform.h index 8918ccb6..ddab8dd7 100644 --- a/ownCloudSDK/Platforms/OCPlatform.h +++ b/ownCloudSDK/Platforms/OCPlatform.h @@ -34,10 +34,20 @@ #define OCView NSView #endif /* TARGET_OS_OSX */ +typedef NS_ENUM(NSUInteger, OCPlatformMemoryConfiguration) +{ + OCPlatformMemoryConfigurationDefault, //!< Default memory configuration + OCPlatformMemoryConfigurationMinimum //!< Try using only the minimum amount of memory needed +} __attribute__((enum_extensibility(closed))); + NS_ASSUME_NONNULL_BEGIN @interface OCPlatform : NSObject +@property(readonly,strong,nonatomic,class) OCPlatform *current; + +@property(assign) OCPlatformMemoryConfiguration memoryConfiguration; + @end NS_ASSUME_NONNULL_END diff --git a/ownCloudSDK/Platforms/OCPlatform.m b/ownCloudSDK/Platforms/OCPlatform.m index 07026d0c..a0b60f8c 100644 --- a/ownCloudSDK/Platforms/OCPlatform.m +++ b/ownCloudSDK/Platforms/OCPlatform.m @@ -17,7 +17,38 @@ */ #import "OCPlatform.h" +#import "OCAppIdentity.h" +#import "OCSQLiteDB.h" @implementation OCPlatform ++ (OCPlatform *)current +{ + static OCPlatform *currentPlatform; + + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + currentPlatform = [OCPlatform new]; + }); + + return (currentPlatform); +} + +- (instancetype)init +{ + if ((self = [super init]) != nil) + { + _memoryConfiguration = OCPlatformMemoryConfigurationDefault; + + if ([OCAppIdentity.sharedAppIdentity.componentIdentifier isEqual:OCAppComponentIdentifierFileProviderExtension] || + [OCAppIdentity.sharedAppIdentity.componentIdentifier isEqual:OCAppComponentIdentifierFileProviderUIExtension] || + [OCAppIdentity.sharedAppIdentity.componentIdentifier isEqual:OCAppComponentIdentifierShareExtension]) + { + _memoryConfiguration = OCPlatformMemoryConfigurationMinimum; + } + } + + return (self); +} + @end diff --git a/ownCloudSDK/Resource Management/OCCoreManager.h b/ownCloudSDK/Resource Management/OCCoreManager.h index 935571b1..b117e1dc 100644 --- a/ownCloudSDK/Resource Management/OCCoreManager.h +++ b/ownCloudSDK/Resource Management/OCCoreManager.h @@ -42,7 +42,6 @@ typedef void(^OCCoreManagerOfflineOperation)(OCBookmark *bookmark, dispatch_bloc @property(class, readonly, strong, nonatomic) OCCoreManager *sharedCoreManager; @property(assign) BOOL postFileProviderNotifications; -@property(assign,nonatomic) OCCoreMemoryConfiguration memoryConfiguration; @property(readonly, nonatomic, strong) NSArray *activeRunIdentifiers; diff --git a/ownCloudSDK/Resource Management/OCCoreManager.m b/ownCloudSDK/Resource Management/OCCoreManager.m index 3993fef6..19bf476f 100644 --- a/ownCloudSDK/Resource Management/OCCoreManager.m +++ b/ownCloudSDK/Resource Management/OCCoreManager.m @@ -411,29 +411,6 @@ - (void)_runNextOfflineOperationForBookmark:(OCBookmark *)bookmark return (nil); } -#pragma mark - Memory configuration -- (void)setMemoryConfiguration:(OCCoreMemoryConfiguration)memoryConfiguration -{ - @synchronized (self) - { - _memoryConfiguration = memoryConfiguration; - - [_coresByUUID enumerateKeysAndObjectsUsingBlock:^(NSUUID * _Nonnull key, OCCore * _Nonnull core, BOOL * _Nonnull stop) { - core.memoryConfiguration = memoryConfiguration; - }]; - - switch (memoryConfiguration) - { - case OCCoreMemoryConfigurationMinimum: - [OCSQLiteDB setMemoryLimit:(1 * 1024 * 512)]; // Set 0.5 MB memory limit for SQLite; - break; - - default: break; - } - - } -} - #pragma mark - Active run identifiers - (NSArray *)activeRunIdentifiers { diff --git a/ownCloudSDK/Vaults/Database/OCDatabase.m b/ownCloudSDK/Vaults/Database/OCDatabase.m index 4d0e8dd3..1ca09d69 100644 --- a/ownCloudSDK/Vaults/Database/OCDatabase.m +++ b/ownCloudSDK/Vaults/Database/OCDatabase.m @@ -36,7 +36,7 @@ #import "OCAsyncSequentialQueue.h" #import "NSString+OCSQLTools.h" #import "OCItemPolicy.h" -#import "OCCoreManager.h" +#import "OCPlatform.h" #import "NSArray+OCSegmentedProcessing.h" #import "OCSQLiteDB+Internal.h" @@ -50,7 +50,7 @@ @interface OCDatabase () OCAsyncSequentialQueue *_openQueue; NSInteger _openCount; - OCCoreMemoryConfiguration _memoryConfiguration; + OCPlatformMemoryConfiguration _memoryConfiguration; NSMutableSet *_knownInvalidSyncRecordIDs; } @@ -79,7 +79,7 @@ - (instancetype)initWithURL:(NSURL *)databaseURL _selectItemRowsSQLQueryPrefix = @"SELECT mdID, mdTimestamp, syncAnchor, itemData"; - _memoryConfiguration = OCCoreManager.sharedCoreManager.memoryConfiguration; + _memoryConfiguration = OCPlatform.current.memoryConfiguration; _progressBySyncRecordID = [NSMutableDictionary new]; _ephermalParametersBySyncRecordID = [NSMutableDictionary new]; @@ -336,7 +336,7 @@ - (void)addCacheItems:(NSArray *)items syncAnchor:(OCSyncAnchor)syncA completionHandler(self, error); } }]]; - } segmentSize:((_memoryConfiguration == OCCoreMemoryConfigurationMinimum) ? 10 : 200)]; + } segmentSize:((_memoryConfiguration == OCPlatformMemoryConfigurationMinimum) ? 10 : 200)]; } - (void)updateCacheItems:(NSArray *)items syncAnchor:(OCSyncAnchor)syncAnchor completionHandler:(OCDatabaseCompletionHandler)completionHandler @@ -452,7 +452,7 @@ - (void)updateCacheItems:(NSArray *)items syncAnchor:(OCSyncAnchor)sy completionHandler(self, error); } }]]; - } segmentSize:((_memoryConfiguration == OCCoreMemoryConfigurationMinimum) ? 10 : 200)]; + } segmentSize:((_memoryConfiguration == OCPlatformMemoryConfigurationMinimum) ? 10 : 200)]; } - (void)removeCacheItems:(NSArray *)items syncAnchor:(OCSyncAnchor)syncAnchor completionHandler:(OCDatabaseCompletionHandler)completionHandler diff --git a/ownCloudSDK/Vaults/Database/SQLite/OCSQLiteDB.m b/ownCloudSDK/Vaults/Database/SQLite/OCSQLiteDB.m index d79ca037..64771193 100644 --- a/ownCloudSDK/Vaults/Database/SQLite/OCSQLiteDB.m +++ b/ownCloudSDK/Vaults/Database/SQLite/OCSQLiteDB.m @@ -28,6 +28,7 @@ #import "NSProgress+OCExtensions.h" #import "OCCoreManager.h" #import "OCSQLiteCollationLocalized.h" +#import "OCPlatform.h" #import "OCExtension+License.h" @@ -49,6 +50,11 @@ @implementation OCSQLiteDB + (void)load { [[OCExtensionManager sharedExtensionManager] addExtension:[OCExtension licenseExtensionWithIdentifier:@"license.ISRunLoopThread" bundleOfClass:[OCRunLoopThread class] title:@"ISRunLoopThread" resourceName:@"ISRunLoopThread" fileExtension:@"LICENSE"]]; + + if (OCPlatform.current.memoryConfiguration == OCPlatformMemoryConfigurationMinimum) + { + [OCSQLiteDB setMemoryLimit:(1 * 1024 * 512)]; // Set 0.5 MB memory limit for SQLite; + } } + (BOOL)allowConcurrentFileAccess @@ -72,7 +78,7 @@ - (instancetype)init _journalMode = OCSQLiteJournalModeDelete; // (SQLite default) - self.cacheStatements = (OCCoreManager.sharedCoreManager.memoryConfiguration != OCCoreMemoryConfigurationMinimum); + self.cacheStatements = (OCPlatform.current.memoryConfiguration != OCPlatformMemoryConfigurationMinimum); #if TARGET_OS_IOS [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shrinkMemory) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];