forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EMInMemoryImageCache.m
163 lines (136 loc) · 5.01 KB
/
EMInMemoryImageCache.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#import "EMInMemoryImageCache.h"
#import <pthread.h>
#import "EMImageData.h"
@interface EMInMemoryResidentImage : NSObject
@property (nonatomic, strong, readonly) id<NSCopying> key;
@property (nonatomic, strong, readonly) NSImage *image;
@property (nonatomic) NSUInteger accessIndex;
@end
@implementation EMInMemoryResidentImage
- (instancetype)initWithKey:(id<NSCopying>)key image:(NSImage *)image accessIndex:(NSUInteger)accessIndex
{
self = [super init];
if (self != nil)
{
_key = key;
_image = image;
_accessIndex = accessIndex;
}
return self;
}
@end
@interface EMInMemoryImageCache ()
{
pthread_mutex_t _lock;
NSMutableDictionary *_dict;
NSMutableDictionary *_residentImages;
NSUInteger _maxResidentSize;
NSUInteger _residentImagesTotalSize;
NSUInteger _nextAccessIndex;
}
@end
@implementation EMInMemoryImageCache
- (instancetype)init
{
return [self initWithMaxResidentSize:1 * 1024 * 1024];
}
- (instancetype)initWithMaxResidentSize:(NSUInteger)maxResidentSize
{
self = [super init];
if (self != nil)
{
pthread_mutex_init(&_lock, NULL);
_dict = [[NSMutableDictionary alloc] init];
_residentImages = [[NSMutableDictionary alloc] init];
_maxResidentSize = maxResidentSize;
_nextAccessIndex = 1;
}
return self;
}
- (void)setImageDataWithSize:(CGSize)size generator:(void (^)(uint8_t *memory, NSUInteger bytesPerRow))generator forKey:(id<NSCopying>)key
{
if (key != nil)
{
NSImage *image = nil;
EMImageData *imageData = [[EMImageData alloc] initWithSize:size generator:generator image:&image];
pthread_mutex_lock(&_lock);
_dict[key] = imageData;
[self _addResidentImage:image forKey:key];
pthread_mutex_unlock(&_lock);
}
}
- (void)_addResidentImage:(NSImage *)image forKey:(id<NSCopying>)key
{
NSUInteger imageSize = (NSUInteger)(image.size.width * image.size.height * 1 * 4);
if (_residentImagesTotalSize + imageSize > _maxResidentSize)
{
NSInteger sizeToRemove = ((NSInteger)_residentImagesTotalSize) - (((NSInteger)_maxResidentSize) - ((NSInteger)imageSize));
NSArray *sortedImages = [[_residentImages allValues] sortedArrayUsingComparator:^NSComparisonResult(EMInMemoryResidentImage *image1, EMInMemoryResidentImage *image2)
{
if (image1.accessIndex < image2.accessIndex)
return NSOrderedDescending;
else if (image1.accessIndex > image2.accessIndex)
return NSOrderedAscending;
return NSOrderedSame;
}];
NSMutableArray *removedKeys = [[NSMutableArray alloc] init];
NSInteger removedSize = 0;
for (NSInteger i = ((NSInteger)sortedImages.count) - 1; i >= 0 && removedSize < sizeToRemove; i--)
{
EMInMemoryResidentImage *currentImage = sortedImages[i];
NSInteger currentImageSize = (NSInteger)(currentImage.image.size.width * currentImage.image.size.height * 1 * 4);
removedSize += currentImageSize;
[removedKeys addObject:currentImage.key];
}
[_residentImages removeObjectsForKeys:removedKeys];
_residentImagesTotalSize = MAX(0, ((NSInteger)_residentImagesTotalSize) - removedSize);
}
_residentImagesTotalSize += imageSize;
_residentImages[key] = [[EMInMemoryResidentImage alloc] initWithKey:key image:image accessIndex:_nextAccessIndex++];
}
- (NSImage *)imageForKey:(id<NSCopying>)key
{
if (key == nil)
return nil;
NSImage *image = nil;
pthread_mutex_lock(&_lock);
EMInMemoryResidentImage *residentImage = _residentImages[key];
if (residentImage != nil)
{
EMInMemoryResidentImage *residentImage = _residentImages[key];
residentImage.accessIndex = _nextAccessIndex++;
image = residentImage.image;
}
else
{
EMImageData *imageData = _dict[key];
if (imageData != nil)
{
image = [imageData image];
if (image == nil)
[_dict removeObjectForKey:key];
else
[self _addResidentImage:image forKey:key];
}
}
pthread_mutex_unlock(&_lock);
return image;
}
- (void)_debugPrintStats
{
pthread_mutex_lock(&_lock);
__block NSUInteger residentSize = 0;
[_residentImages enumerateKeysAndObjectsUsingBlock:^(__unused id key, EMInMemoryResidentImage *image, __unused BOOL *stop)
{
residentSize += (NSUInteger)(image.image.size.width * image.image.size.height * 4);
}];
__block NSUInteger cachedSize = 0;
[_dict enumerateKeysAndObjectsUsingBlock:^(id key, EMImageData *imageData, __unused BOOL *stop)
{
if (![imageData isDiscarded])
cachedSize += (NSUInteger)(imageData.size.width * imageData.size.height * 4);
}];
NSLog(@"(EMInMemoryImageCache residentSize: %.02fMB, cachedSize: %.02fMB)", residentSize / (1024.0f * 1024.0f), cachedSize / (1024.0f * 1024.0f));
pthread_mutex_unlock(&_lock);
}
@end