-
Notifications
You must be signed in to change notification settings - Fork 17
/
MPWObjectCache.h
57 lines (41 loc) · 1.4 KB
/
MPWObjectCache.h
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
/* MPWObjectCache.h Copyright (c) 1998-2017 by Marcel Weiher, All Rights Reserved.
*/
#import <MPWFoundation/MPWObject.h>
#import <MPWFoundation/AccessorMacros.h>
#import <pthread.h>
typedef long (*INT_IMP)(id, SEL );
typedef void (*VOID_IMP)(id, SEL, ...);
@interface MPWObjectCache : MPWObject
{
@public
IMP0 getObject;
}
+cacheWithCapacity:(int)newCap class:(Class)newClass;
-initWithCapacity:(int)newCap class:(Class)newClass;
-getObject;
-(void)makeThreadSafeOnDemand;
-(IMP)getObjectIMP;
-(void)clearCache;
-(void)setInitIMP:(IMP0)newImp;
boolAccessor_h( unsafeFastAlloc, setUnsafeFastAlloc )
scalarAccessor_h( SEL, reInitSelector, setReInitSelector )
#define GETOBJECT( cache ) ((cache)->getObject( (cache), @selector(getObject)))
//#define GETOBJECT( cache ) ([(cache) getObject])
#define CACHING_ALLOC( selector,size, unsafe ) \
static pthread_key_t key=0;\
static void __objc_cache_destructor( void *objref ) { [(id)objref release]; }\
+selector\
{\
if ( !key ) {\
pthread_key_create(&key, __objc_cache_destructor);\
}\
MPWObjectCache *cache=pthread_getspecific( key );\
if ( !cache ) {\
/* NSLog(@"will create a local cache for %@ for thread %p",NSStringFromClass(self),[NSThread currentThread]); */\
cache = [[MPWObjectCache alloc] initWithCapacity:size class:self];\
[cache setUnsafeFastAlloc:unsafe];\
pthread_setspecific( key, cache );\
}\
return GETOBJECT(cache);\
}
@end