-
Notifications
You must be signed in to change notification settings - Fork 17
/
MPWObjectCache.m
295 lines (256 loc) · 7.57 KB
/
MPWObjectCache.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* MPWObjectCache.m Copyright (c) 1998-2017 by Marcel Weiher, All Rights Reserved.
*/
#import "MPWObjectCache.h"
#import "NSInvocationAdditions_lookup.h"
#import <Foundation/Foundation.h>
#import <stdlib.h>
@interface NSObject(removeFromCache)
-(void)removeFromCache:(MPWObjectCache*)aCache;
@end
@implementation MPWObjectCache
{
MPWObject **objs;
Class objClass;
SEL allocSel,initSel;
IMP0 allocImp,initImp,retainImp,autoreleaseImp;
INT_IMP retainCountImp;
VOID_IMP removeFromCacheImp;
int cacheSize;
int objIndex;
void *cachelock;
BOOL unsafeFastAlloc;
VOID_IMP releaseImp;
SEL reInitSelector;
IMP0 reInitImp;
}
/*"
Provides a local, circular buffer for re-cycling objects. Needs objects that can be
re-initialized. Can be made thread-safe if necessary, default is non-thread-safe
because local allocators can be made thread-specific and thus avoid the locking
overhead.
The objects returned are usually retained+autoreleased and will not be re-used
unless all external retains have been released. This can be disabled for
objects with known, limited lifetimes and caches large enough to avoid reallocation
of the object while it is still in use.
"*/
-(void)setInitIMP:(IMP0)newImp
{
initImp=newImp;
}
-initWithCapacity:(int)newCap class:(Class)newClass allocSel:(SEL)aSel initSel:(SEL)iSel
{
self = [super init];
allocSel=aSel;
initSel=iSel;
objClass = newClass;
allocImp = (IMP0)[newClass methodForSelector:allocSel];
NSAssert( allocImp != NULL , @"allocImp");
[self setReInitSelector:initSel];
// NSAssert( initImp != NULL , @"initImp");
retainImp = (IMP0)[newClass instanceMethodForSelector:@selector(retain)];
NSAssert( retainImp != NULL , @"retainImp");
autoreleaseImp = (IMP0)[newClass instanceMethodForSelector:@selector(autorelease)];
NSAssert( autoreleaseImp != NULL , @"autoreleaseImp");
releaseImp = (VOID_IMP)[newClass instanceMethodForSelector:@selector(release)];
NSAssert( releaseImp != NULL , @"releaseImp");
retainCountImp = (INT_IMP)[newClass instanceMethodForSelector:@selector(retainCount)];
NSAssert( retainCountImp != NULL , @"retainCountImp");
if ( [newClass instancesRespondToSelector:@selector(removeFromCache:)] ) {
removeFromCacheImp=(VOID_IMP)[newClass instanceMethodForSelector:@selector(removeFromCache:)];
} else {
removeFromCacheImp=NULL;
}
objs = calloc( newCap+2, sizeof *objs);
cacheSize = newCap;
getObject = (IMP0)[self getObjectIMP];
return self;
}
+cacheWithCapacity:(int)newCap class:(Class)newClass
{
return [[[self alloc] initWithCapacity:newCap class:newClass] autorelease];
}
-initWithCapacity:(int)newCap class:(Class)newClass
{
return [self initWithCapacity:newCap class:newClass allocSel:@selector(alloc)
initSel:@selector(init)];
}
-(void)makeThreadSafeNow
/*"
Make the object cache thread-safe by providing locks around getObject calls.
"*/
{
// cachelock=mutex_alloc( );
}
-(void)makeThreadSafeOnDemand
/*"
Make the object cache thread-safe if the process becomes multi-threaded.
"*/
{
[(NSNotificationCenter*)[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(makeThreadSafeNow)
name:NSWillBecomeMultiThreadedNotification
object:nil];
}
-(IMP)getObjectIMP
/*"
Return a pointer to the method getObject for fast calls to the allocator.
"*/
{
// [self getObject];
return [self methodForSelector:@selector(getObject)];
}
boolAccessor( unsafeFastAlloc, setUnsafeFastAlloc )
scalarAccessor( SEL, reInitSelector, _setReInitSelector )
-(void)setReInitSelector:(SEL)newSelector
{
[self _setReInitSelector:newSelector];
reInitImp = (IMP0)[objClass instanceMethodForSelector:newSelector];
NSAssert( reInitImp != NULL , @"reInitImp");
}
#if SLOW_SAMPLE_IMPLEMENTATION_WANTED
-getObject
{
id obj;
objIndex++;
if ( objIndex >= cacheSize ) {
objIndex=0;
}
obj=objs[objIndex];
if ( obj==nil || [obj retainCount] > 1 ) {
if ( obj!=nil ) {
[obj release];
//--- removeFromCache
}
obj = [[objClass alloc] init];
objs[objIndex]=obj;
}
return [[obj retain] autorelease];
}
#else
static int misses=0,hits=0;
intAccessor( misses, setMisses)
intAccessor( hits, setHits )
-getObject
/*"
Returns an object as if freshly allocated.
"*/
{
{
int i,maxIndex;
MPWObject *obj=nil;
// look back just a bit, a lot can be stacking order
objIndex++;
if ( cachelock ) {
// mutex_lock( (mutex_t)cachelock );
}
if ( objIndex >= cacheSize ) {
objIndex=0;
}
for (i=objIndex,maxIndex=MIN(objIndex+4,cacheSize);i<maxIndex;i++) {
obj=objs[i];
if ( obj != nil ) {
if ((int)retainCountImp( obj, @selector(retainCount)) <= 1 ) {
break;
} else {
obj=nil;
}
}
}
if ( obj == nil ) {
obj=objs[objIndex];
misses++;
if ( obj!=nil ) {
releaseImp( objs[objIndex] ,@selector(release));
if ( removeFromCacheImp ) {
// NSLog(@"remove from cache");
removeFromCacheImp( objs[objIndex] ,@selector(removeFromCache:), self );
}
}
// lazy fetch of initImp needed for some class clusters
// that substitute instances of a different classs
// (so IMP from original class won't work for actual instance
// created).
id allocated = allocImp( objClass, allocSel );
if (!initImp) {
allocated=((IMP0)objc_msgSend)(allocated, reInitSelector);
initImp = (IMP0)[allocated methodForSelector:initSel];
} else {
allocated=initImp(allocated ,initSel);
}
objs[objIndex] = allocated;
obj=objs[objIndex];
} else {
hits++;
// NSLog(@"found after %d probes",i-objIndex);
objIndex=i;
}
if ( cachelock ) {
// mutex_unlock( (mutex_t)cachelock );
}
if ( unsafeFastAlloc &&!cachelock) {
return obj;
// return initImp( obj, initSel);
} else {
return autoreleaseImp( retainImp(obj,@selector(retain)), @selector(autorelease) );
}
}
}
#endif
-new
{
return [self getObject];
}
-(void)clearCache
{
int i;
if ( objs ) {
for (i=0;i<cacheSize;i++) {
if ( objs[i] ) {
if ( removeFromCacheImp && [objs[objIndex] retainCount] > 1 ) {
// NSLog(@"remove from cache");
removeFromCacheImp( objs[objIndex] ,@selector(removeFromCache:), self );
}
releaseImp( objs[i] ,@selector(release));
objs[i]=nil;
}
}
}
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]
removeObserver:self];
if ( objs ) {
[self clearCache];
free(objs);
}
if (cachelock) {
// mutex_free( (mutex_t)cachelock );
}
[super dealloc];
}
-description
{
return [NSString stringWithFormat:@"<%@:%p: class: %@ getObject: %p/%p >",
[self class],self,objClass,[self getObjectIMP],getObject];
}
@end
#import "DebugMacros.h"
@implementation MPWObjectCache(testing)
+(void)testCanCreateMutableDictionary
{
MPWObjectCache *cache=[self cacheWithCapacity:2 class:[NSMutableDictionary class]];
NSMutableDictionary *d=GETOBJECT(cache);
INTEXPECT(d.count, 0, @"count of dict after create");
d[@"hi"]=@"there";
INTEXPECT(d.count, 1, @"count of dict after set");
IDEXPECT(d[@"hi"], @"there",@"value we put in");
}
+(NSArray<NSString*>*)testSelectors
{
return @[
@"testCanCreateMutableDictionary",
];
}
@end