@@ -49,7 +49,7 @@ @interface FICImageTable () {
49
49
size_t _chunkLength;
50
50
NSInteger _chunkCount;
51
51
52
- CFMutableDictionaryRef _chunkDictionary ;
52
+ NSMapTable *_chunkMapTable ;
53
53
NSMutableArray *_recentChunks;
54
54
NSRecursiveLock *_lock;
55
55
@@ -104,9 +104,9 @@ + (NSString *)directoryPath {
104
104
static dispatch_once_t onceToken;
105
105
dispatch_once (&onceToken, ^{
106
106
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
107
- __directoryPath = [[[ paths objectAtIndex: 0 ] stringByAppendingPathComponent: @" ImageTables" ] retain ];
107
+ __directoryPath = [[paths objectAtIndex: 0 ] stringByAppendingPathComponent: @" ImageTables" ];
108
108
109
- NSFileManager *fileManager = [[[ NSFileManager alloc ] init ] autorelease ];
109
+ NSFileManager *fileManager = [[NSFileManager alloc ] init ];
110
110
BOOL directoryExists = [fileManager fileExistsAtPath: __directoryPath];
111
111
if (directoryExists == NO ) {
112
112
[fileManager createDirectoryAtPath: __directoryPath withIntermediateDirectories: YES attributes: nil error: nil ];
@@ -128,15 +128,15 @@ - (instancetype)initWithFormat:(FICImageFormat *)imageFormat {
128
128
129
129
_lock = [[NSRecursiveLock alloc ] init ];
130
130
_imageFormat = [imageFormat copy ];
131
- _imageFormatDictionary = [[ imageFormat dictionaryRepresentation ] retain ];
131
+ _imageFormatDictionary = [imageFormat dictionaryRepresentation ];
132
132
133
133
_screenScale = [[UIScreen mainScreen ] scale ];
134
134
135
135
int bytesPerPixel = 4 ;
136
136
_imageRowLength = FICByteAlignForCoreAnimation ([_imageFormat pixelSize ].width * bytesPerPixel);
137
137
_imageLength = _imageRowLength * (NSInteger )[_imageFormat pixelSize ].height ;
138
138
139
- _chunkDictionary = CFDictionaryCreateMutable ( kCFAllocatorDefault , 0 , NULL , NULL ); // Non-retained keys and values
139
+ _chunkMapTable = [ NSMapTable mapTableWithKeyOptions: NSMapTableStrongMemory valueOptions: NSMapTableWeakMemory ];
140
140
141
141
_indexMap = [[NSMutableDictionary alloc ] init ];
142
142
_occupiedIndexes = [[NSMutableIndexSet alloc ] init ];
@@ -176,8 +176,7 @@ - (instancetype)initWithFormat:(FICImageFormat *)imageFormat {
176
176
// If something goes wrong and we can't open the image table file, then we have no choice but to release and nil self.
177
177
NSString *message = [NSString stringWithFormat: @" *** FIC Error: %s could not open the image table file at path %@ . The image table was not created." , __PRETTY_FUNCTION__, _filePath];
178
178
[[FICImageCache sharedImageCache ] _logMessage: message];
179
-
180
- [self release ];
179
+
181
180
self = nil ;
182
181
}
183
182
}
@@ -190,54 +189,32 @@ - (instancetype)init {
190
189
}
191
190
192
191
- (void )dealloc {
193
- [_imageFormat release ];
194
- [_filePath release ];
195
-
196
- CFRelease (_chunkDictionary);
197
-
198
- [_indexMap release ];
199
- [_occupiedIndexes release ];
200
- [_MRUEntries release ];
201
- [_sourceImageMap release ];
202
- [_imageFormatDictionary release ];
203
- [_recentChunks release ];
204
-
205
192
if (_fileDescriptor >= 0 ) {
206
193
close (_fileDescriptor);
207
194
}
208
-
209
- [_lock release ];
210
-
211
- [super dealloc ];
212
195
}
213
196
214
197
#pragma mark - Working with Chunks
215
198
216
199
- (FICImageTableChunk *)_cachedChunkAtIndex : (NSInteger )index {
217
- return (FICImageTableChunk *)CFDictionaryGetValue (_chunkDictionary, (const void *)index );
200
+ FICImageTableChunk *cachedChunk = [_chunkMapTable objectForKey: @(index )];
201
+
202
+ return cachedChunk;
218
203
}
219
204
220
205
- (void )_setChunk : (FICImageTableChunk *)chunk index : (NSInteger )index {
221
206
if (chunk != nil ) {
222
- CFDictionarySetValue (_chunkDictionary, ( const void *) index , ( const void *)chunk) ;
207
+ [_chunkMapTable setObject: chunk forKey: @( index )] ;
223
208
} else {
224
- CFDictionaryRemoveValue (_chunkDictionary, ( const void *) index );
209
+ [_chunkMapTable removeObjectForKey: @( index )] ;
225
210
}
226
211
}
227
212
228
- - (void )_cleanupRecentChunks {
229
- [_lock lock ];
230
-
231
- [_recentChunks removeAllObjects ];
232
-
233
- [_lock unlock ];
234
- }
235
-
236
213
- (FICImageTableChunk *)_chunkAtIndex : (NSInteger )index {
237
214
FICImageTableChunk *chunk = nil ;
238
215
239
216
if (index < _chunkCount) {
240
- chunk = [[ self _cachedChunkAtIndex: index ] retain ];
217
+ chunk = [self _cachedChunkAtIndex: index ];
241
218
242
219
if (chunk == nil ) {
243
220
size_t chunkLength = _chunkLength;
@@ -246,7 +223,7 @@ - (FICImageTableChunk *)_chunkAtIndex:(NSInteger)index {
246
223
chunkLength = _fileLength - chunkOffset;
247
224
}
248
225
249
- chunk = [[FICImageTableChunk alloc ] initWithImageTable: self fileDescriptor : _fileDescriptor index :index length: chunkLength];
226
+ chunk = [[FICImageTableChunk alloc ] initWithFileDescriptor : _fileDescriptor index :index length: chunkLength];
250
227
[self _setChunk: chunk index :index ];
251
228
}
252
229
@@ -260,15 +237,7 @@ - (FICImageTableChunk *)_chunkAtIndex:(NSInteger)index {
260
237
}
261
238
}
262
239
263
- return [chunk autorelease ];
264
- }
265
-
266
- - (void )_chunkWillBeDeallocated : (FICImageTableChunk *)chunk {
267
- [_lock lock ];
268
-
269
- [self _setChunk: nil index :[chunk index ]];
270
-
271
- [_lock unlock ];
240
+ return chunk;
272
241
}
273
242
274
243
#pragma mark - Storing, Retrieving, and Deleting Entries
@@ -351,11 +320,9 @@ - (UIImage *)newImageForEntityUUID:(NSString *)entityUUID sourceImageUUID:(NSStr
351
320
} else {
352
321
[self _entryWasAccessedWithEntityUUID: entityUUID];
353
322
354
- [entryData retain ]; // Released by _FICReleaseImageData
355
-
356
323
// Create CGImageRef whose backing store *is* the mapped image table entry. We avoid a memcpy this way.
357
324
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
358
- CGDataProviderRef dataProvider = CGDataProviderCreateWithData ((void *)entryData, [entryData bytes ], [entryData imageLength ], _FICReleaseImageData);
325
+ CGDataProviderRef dataProvider = CGDataProviderCreateWithData ((__bridge_retained void *)entryData, [entryData bytes ], [entryData imageLength ], _FICReleaseImageData);
359
326
360
327
CGSize pixelSize = [_imageFormat pixelSize ];
361
328
CGBitmapInfo bitmapInfo;
@@ -386,8 +353,7 @@ - (UIImage *)newImageForEntityUUID:(NSString *)entityUUID sourceImageUUID:(NSStr
386
353
}
387
354
388
355
static void _FICReleaseImageData (void *info, const void *data, size_t size) {
389
- FICImageTableEntry *entryData = (FICImageTableEntry *)info;
390
- [entryData release ];
356
+ CFRelease (info);
391
357
}
392
358
393
359
- (void )deleteEntryForEntityUUID : (NSString *)entityUUID {
@@ -452,7 +418,7 @@ - (void)_setEntryCount:(NSInteger)entryCount {
452
418
} else {
453
419
_fileLength = fileLength;
454
420
_entryCount = entryCount;
455
- _chunkCount = ( _entryCount + _entriesPerChunk - 1 ) / _entriesPerChunk;
421
+ _chunkCount = _entriesPerChunk > 0 ? (( _entryCount + _entriesPerChunk - 1 ) / _entriesPerChunk) : 0 ;
456
422
}
457
423
}
458
424
}
@@ -478,7 +444,7 @@ - (FICImageTableEntry *)_entryDataAtIndex:(NSInteger)index {
478
444
479
445
[_lock unlock ];
480
446
481
- return [ entryData autorelease ] ;
447
+ return entryData;
482
448
}
483
449
484
450
- (NSInteger )_nextEntryIndex {
@@ -489,7 +455,6 @@ - (NSInteger)_nextEntryIndex {
489
455
if (index == NSNotFound ) {
490
456
index = _entryCount;
491
457
}
492
- [unoccupiedIndexes release ];
493
458
494
459
if (index >= [_imageFormat maximumCount ] && [_MRUEntries count ]) {
495
460
// Evict the oldest/least-recently accessed entry here
@@ -533,10 +498,8 @@ - (void)_entryWasAccessedWithEntityUUID:(NSString *)entityUUID {
533
498
if (index == NSNotFound ) {
534
499
[_MRUEntries insertObject: entityUUID atIndex: 0 ];
535
500
} else if (index != 0 ) {
536
- [entityUUID retain ];
537
501
[_MRUEntries removeObjectAtIndex: index ];
538
502
[_MRUEntries insertObject: entityUUID atIndex: 0 ];
539
- [entityUUID release ];
540
503
}
541
504
}
542
505
0 commit comments