/ NSGenericDeallocHandler.m
NSGenericDeallocHandler.m
 1  //
 2  //  NSGenericDeallocHandler.m
 3  //  CoreFoundation
 4  //
 5  //  Copyright (c) 2014 Apportable. All rights reserved.
 6  //
 7  
 8  #import "NSGenericDeallocHandler.h"
 9  #import <objc/runtime.h>
10  #import <Block.h>
11  
12  @implementation __NSGenericDeallocHandler
13  
14  void _NSSetDeallocHandler(id object, void (^block)(void))
15  {
16      static void *deallocHandlerKey = &deallocHandlerKey;
17      __NSGenericDeallocHandler *handler = class_createInstance(objc_getClass("__NSGenericDeallocHandler"), 0);
18      handler->_block = Block_copy(block);
19      objc_setAssociatedObject(object, &deallocHandlerKey, handler, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
20  }
21  
22  + (void)initialize
23  {
24  
25  }
26  
27  - (void)release
28  {
29      if (_block != nil)
30      {
31          _block();
32          Block_release(_block);
33          object_dispose(self);
34      }
35      else
36      {
37          abort();
38      }
39  }
40  
41  - (NSUInteger)retainCount
42  {
43      return 1;
44  }
45  
46  - (id)retain
47  {
48      return self;
49  }
50  
51  @end