第27月第25天 clang -rewrite-objc main.m

本文深入探讨了Objective-C运行时机制,通过实例解析了消息发送过程,并详细解释了AutoreleasePoolPage的工作原理及其在内存管理中的作用。

1.clang -rewrite-objc main.m

 

#import <objc/runtime.h>

 

#import<objc/message.h>

 

#import <Foundation/Foundation.h>

@interface Person : NSObject
//为了方便查看重写的代码将name改成cjmName
@property (nonatomic, copy) NSString *cjmName;
@property (nonatomic, assign) NSUInteger age;
- (void)showMyself;

@end

@implementation Person

@synthesize cjmName = _cjmName;
@synthesize age = _age;

- (void)showMyself {
    NSLog(@"Name: %@ Age: %ld", self.cjmName, self.age);
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Person *p = [[Person alloc] init];
        
        [p setValue:@"Jiaming Chen" forKey:@"cjmName"];
        [p setValue:@22 forKey:@"age"];
        
        p.cjmName = @"CCCC";
        
        [p showMyself];
    }
    return 0;
}

接着使用clang -rewrite-objc main.m重写为cpp文件,查看main函数重写后的代码如下:

 

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;

        Person *p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));

        ((void (*)(id, SEL, id _Nullable, NSString *))(void *)objc_msgSend)((id)p, sel_registerName("setValue:forKey:"), (id _Nullable)(NSString *)&__NSConstantStringImpl__var_folders_1f_dz4kq57d4b19s4tfmds1mysh0000gn_T_main_080287_mi_1, (NSString *)&__NSConstantStringImpl__var_folders_1f_dz4kq57d4b19s4tfmds1mysh0000gn_T_main_080287_mi_2);
        ((void (*)(id, SEL, id _Nullable, NSString *))(void *)objc_msgSend)((id)p, sel_registerName("setValue:forKey:"), (id _Nullable)((NSNumber *(*)(Class, SEL, int))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithInt:"), 22), (NSString *)&__NSConstantStringImpl__var_folders_1f_dz4kq57d4b19s4tfmds1mysh0000gn_T_main_080287_mi_3);

        ((void (*)(id, SEL, NSString *))(void *)objc_msgSend)((id)p, sel_registerName("setCjmName:"), (NSString *)&__NSConstantStringImpl__var_folders_1f_dz4kq57d4b19s4tfmds1mysh0000gn_T_main_080287_mi_4);

        ((void (*)(id, SEL))(void *)objc_msgSend)((id)p, sel_registerName("showMyself"));
    }
    return 0;
}

 

 

 

https://www.jianshu.com/p/fa941b769606

 

https://www.jianshu.com/p/74369c88da5f

 

2

那这里的 AutoreleasePoolPage 是什么东西呢?其实,autoreleasepool 是没有单独的内存结构的,它是通过以 AutoreleasePoolPage 为结点的双向链表来实现的。我们打开 runtime 的源码工程,在 NSObject.mm 文件的第 438-932 行可以找到 autoreleasepool 的实现源码。通过阅读源码,我们可以知道:

  • 每一个线程的 autoreleasepool 其实就是一个指针的堆栈;

  • 每一个指针代表一个需要 release 的对象或者 POOL_SENTINEL(哨兵对象,代表一个 autoreleasepool 的边界);

  • 一个 pool token 就是这个 pool 所对应的 POOL_SENTINEL 的内存地址。当这个 pool 被 pop 的时候,所有内存地址在 pool token 之后的对象都会被 release ;

  • 这个堆栈被划分成了一个以 page 为结点的双向链表。pages 会在必要的时候动态地增加或删除;

  • Thread-local storage(线程局部存储)指向 hot page ,即最新添加的 autoreleased 对象所在的那个 

 

http://www.cocoachina.com/ios/20150610/12093.html

 

转载于:https://www.cnblogs.com/javastart/p/10173471.html

/eap/.clang-format: line 1: BasedOnStyle:: command not found /eap/.clang-format: line 2: Language:: command not found /eap/.clang-format: line 3: AccessModifierOffset:: command not found /eap/.clang-format: line 4: AlignAfterOpenBracket:: command not found /eap/.clang-format: line 5: AlignConsecutiveAssignments:: command not found /eap/.clang-format: line 6: AlignConsecutiveDeclarations:: command not found /eap/.clang-format: line 7: AlignOperands:: command not found /eap/.clang-format: line 8: AlignTrailingComments:: command not found /eap/.clang-format: line 9: AllowAllArgumentsOnNextLine:: command not found /eap/.clang-format: line 10: AllowAllParametersOfDeclarationOnNextLine:: command not found /eap/.clang-format: line 11: AllowShortFunctionsOnASingleLine:: command not found /eap/.clang-format: line 12: AllowShortIfStatementsOnASingleLine:: command not found /eap/.clang-format: line 13: AlwaysBreakAfterDefinitionReturnType:: command not found /eap/.clang-format: line 14: AlwaysBreakAfterReturnType:: command not found /eap/.clang-format: line 15: AlwaysBreakBeforeMultilineStrings:: command not found /eap/.clang-format: line 16: AlwaysBreakTemplateDeclarations:: command not found /eap/.clang-format: line 17: BinPackArguments:: command not found /eap/.clang-format: line 18: BinPackParameters:: command not found /eap/.clang-format: line 19: $'BraceWrapping:\r': command not found /eap/.clang-format: line 20: AfterCaseLabel:: command not found /eap/.clang-format: line 21: AfterClassKeyword:: command not found /eap/.clang-format: line 22: AfterControlStatement:: command not found /eap/.clang-format: line 23: AfterEnumKeyword:: command not found /eap/.clang-format: line 24: AfterFunctionKeyword:: command not found /eap/.clang-format: line 25: AfterNamespaceKeyword:: command not found /eap/.clang-format: line 26: AfterStructKeyword:: command not found /eap/.clang-format: line 27: AfterUnionKeyword:: command not found /eap/.clang-format: line 28: AfterCXXCatchKeyword:: command not found /eap/.clang-format: line 29: AfterElse:: command not found /eap/.clang-format: line 30: BeforeElse:: command not found /eap/.clang-format: line 31: BeforeWhile:: command not found /eap/.clang-format: line 32: BreakBeforeBinaryOperators:: command not found /eap/.clang-format: line 33: BreakBeforeBraces:: command not found /eap/.clang-format: line 34: BreakBeforeTernaryOperators:: command not found /eap/.clang-format: line 35: BreakConstructorInitializersBeforeComma:: command not found /eap/.clang-format: line 36: BreakInheritanceListBeforeComma:: command not found /eap/.clang-format: line 37: BreakInheritanceListBeforeColon:: command not found /eap/.clang-format: line 38: BreakStringLiterals:: command not found /eap/.clang-format: line 39: ColumnLimit:: command not found /eap/.clang-format: line 40: CommentPragmas:: command not found /eap/.clang-format: line 41: ConstructorInitializerAllOnOneLineOrOnePerLine:: command not found /eap/.clang-format: line 42: ConstructorInitializerIndentWidth:: command not found /eap/.clang-format: line 43: ContinuationIndentWidth:: command not found /eap/.clang-format: line 44: Cpp11BracedListStyle:: command not found /eap/.clang-format: line 45: DerivePointerAlignment:: command not found /eap/.clang-format: line 46: DisableFormat:: command not found /eap/.clang-format: line 47: ExperimentalAutoDetectBinPacking:: command not found /eap/.clang-format: line 48: FixNamespaceComments:: command not found /eap/.clang-format: line 49: $'ForEachMacros:\r': command not found /eap/.clang-format: line 50: -: command not found /eap/.clang-format: line 51: -: command not found /eap/.clang-format: line 52: -: command not found /eap/.clang-format: line 53: $'IncludeCategories:\r': command not found /eap/.clang-format: line 54: -: command not found /eap/.clang-format: line 55: Priority:: command not found /eap/.clang-format: line 56: -: command not found /eap/.clang-format: line 57: Priority:: command not found /eap/.clang-format: line 58: -: command not found /eap/.clang-format: line 59: Priority:: command not found /eap/.clang-format: line 60: IncludeIsMainRegex:: command not found /eap/.clang-format: line 61: IndentCaseLabels:: command not found /eap/.clang-format: line 62: IndentCaseBlocks:: command not found /eap/.clang-format: line 63: IndentGotoLabels:: command not found /eap/.clang-format: line 64: IndentPPDirectives:: command not found /eap/.clang-format: line 65: IndentWidth:: command not found /eap/.clang-format: line 66: IndentWrappedFunctionNames:: command not found /eap/.clang-format: line 67: JavaScriptQuotes:: command not found /eap/.clang-format: line 68: JavaScriptRegex:: command not found /eap/.clang-format: line 69: KeepEmptyLinesAtTheStartOfBlocks:: command not found /eap/.clang-format: line 70: MacroBlockBegin:: command not found /eap/.clang-format: line 71: MacroBlockEnd:: command not found /eap/.clang-format: line 72: MaxEmptyLinesToKeep:: command not found /eap/.clang-format: line 73: NamespaceIndentation:: command not found /eap/.clang-format: line 74: ObjCBlockIndentWidth:: command not found /eap/.clang-format: line 75: ObjCSpaceAfterProperty:: command not found /eap/.clang-format: line 76: ObjCSpaceBeforeProtocolList:: command not found /eap/.clang-format: line 77: PenaltyBreakAssignment:: command not found /eap/.clang-format: line 78: PenaltyBreakBeforeFirstCallParameter:: command not found /eap/.clang-format: line 79: PenaltyBreakComment:: command not found /eap/.clang-format: line 80: PenaltyBreakFirstLessLess:: command not found /eap/.clang-format: line 81: PenaltyBreakString:: command not found /eap/.clang-format: line 82: PenaltyBreakTemplateDeclaration:: command not found /eap/.clang-format: line 83: PenaltyExcessCharacter:: command not found /eap/.clang-format: line 84: PenaltyReturnTypeOnItsOwnLine:: command not found /eap/.clang-format: line 85: PointerAlignment:: command not found /eap/.clang-format: line 86: ReflowComments:: command not found /eap/.clang-format: line 87: SortIncludes:: command not found /eap/.clang-format: line 88: SortUsingDeclarations:: command not found /eap/.clang-format: line 89: SpaceAfterCStyleCast:: command not found /eap/.clang-format: line 90: SpaceAfterLogicalNot:: command not found /eap/.clang-format: line 91: SpaceAfterTemplateKeyword:: command not found /eap/.clang-format: line 92: SpaceBeforeAssignmentOperators:: command not found /eap/.clang-format: line 93: SpaceBeforeCaseColon:: command not found /eap/.clang-format: line 94: SpaceBeforeCpp11BracedList:: command not found /eap/.clang-format: line 95: SpaceBeforeCtorColon:: command not found /eap/.clang-format: line 96: SpaceBeforeInheritanceColon:: command not found /eap/.clang-format: line 97: SpaceBeforeKeywords:: command not found /eap/.clang-format: line 98: SpaceBeforeParens:: command not found /eap/.clang-format: line 99: SpaceBeforeRangeBasedForLoopColon:: command not found /eap/.clang-format: line 100: SpaceInEmptyParentheses:: command not found /eap/.clang-format: line 101: SpacesBeforeTrailingComments:: command not found /eap/.clang-format: line 102: SpacesInAngles:: command not found /eap/.clang-format: line 103: SpacesInContainerLiterals:: command not found /eap/.clang-format: line 104: SpacesInCStyleCastParentheses:: command not found /eap/.clang-format: line 105: SpacesInParentheses:: command not found /eap/.clang-format: line 106: SpacesInSquareBrackets:: command not found /eap/.clang-format: line 107: Standard:: command not found /eap/.clang-format: line 108: $'StatementMacros:\r': command not found /eap/.clang-format: line 109: -: command not found /eap/.clang-format: line 110: -: command not found /eap/.clang-format: line 111: -: command not found /eap/.clang-format: line 112: -: command not found /eap/.clang-format: line 113: -: command not found /eap/.clang-format: line 114: -: command not found /eap/.clang-format: line 115: -: command not found /eap/.clang-format: line 116: -: command not found /eap/.clang-format: line 117: -: command not found /eap/.clang-format: line 118: -: command not found /eap/.clang-format: line 119: -: command not found /eap/.clang-format: line 120: -: command not found /eap/.clang-format: line 121: -: command not found /eap/.clang-format: line 122: -: command not found /eap/.clang-format: line 123: -: command not found /eap/.clang-format: line 124: -: command not found /eap/.clang-format: line 125: -: command not found /eap/.clang-format: line 126: -: command not found /eap/.clang-format: line 127: -: command not found /eap/.clang-format: line 128: -: command not found /eap/.clang-format: line 129: -: command not found /eap/.clang-format: line 130: -: command not found /eap/.clang-format: line 131: -: command not found /eap/.clang-format: line 132: -: command not found /eap/.clang-format: line 133: -: command not found /eap/.clang-format: line 134: -: command not found /eap/.clang-format: line 135: -: command not found /eap/.clang-format: line 136: -: command not found /eap/.clang-format: line 137: -: command not found /eap/.clang-format: line 138: -: command not found /eap/.clang-format: line 139: -: command not found /eap/.clang-format: line 140: -: command not found /eap/.clang-format: line 141: -: command not found /eap/.clang-format: line 142: -: command not found /eap/.clang-format: line 143: -: command not found /eap/.clang-format: line 144: -: command not found /eap/.clang-format: line 145: -: command not found /eap/.clang-format: line 146: -: command not found /eap/.clang-format: line 147: -: command not found /eap/.clang-format: line 148: -: command not found /eap/.clang-format: line 149: -: command not found /eap/.clang-format: line 150: -: command not found /eap/.clang-format: line 151: -: command not found /eap/.clang-format: line 152: -: command not found /eap/.clang-format: line 153: -: command not found /eap/.clang-format: line 154: -: command not found /eap/.clang-format: line 155: -: command not found /eap/.clang-format: line 156: TabWidth:: command not found /eap/.clang-format: line 157: UseCRLF:: command not found /eap/.clang-format: line 158: UseTab:: command not found /eap/.clang-format: line 159: $'WhitespaceSensitiveMacros:\r': command not found /eap/.clang-format: line 160: -: command not found /eap/.clang-format: line 161: -: command not found
08-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值