第27月第25天 clang -rewrite-objc main.m
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:@ forKey:@"age"]; p.cjmName = @"CCCC"; [p showMyself];
}
return ;
}
接着使用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:"), ), (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 ;
}
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
第27月第25天 clang -rewrite-objc main.m的更多相关文章
- 第27月第10天 cmake
1.error: tool 'xcodebuild' requires Xcode的解决办法 sudo xcode-select --switch /Applications/Xcode.app/Co ...
- 第2月第25天 BlocksKit
1.blockskit https://github.com/zwaldowski/BlocksKit bk_showAlertViewWithTitle 2.toast +(void)showToa ...
- 第31月第25天 xcode debug 限制uitextfiled输入
1.xcode debug 了解了每个设置的意思,个人觉得对于一个普通的app来说可以这样配置这些设置: Generate Debug Symbols:DEBUG和RELEASE下均设为YES(和Xc ...
- 第27月第28天 iOS bundle
1. 7.如果将自己打包的bundle给别人使用,别人在打包上传过程中可能会遇到错误提示如: ERROR ITMS-90171: "Invalid Bundle Structure - Th ...
- 第27月第27天 https
1.验证签名 { [self generateRSAKeyPair:kRSA_KEY_SIZE]; NSData *ttDt = [" dataUsingEncoding:NSASCIISt ...
- 第27月第24天 git pull fetch
1. 在进行 pull 操作的同时,其实就是 fetch+merge 的一个过程.我们从 remote 分支中拉取新的更新,然后再合并到本地分支中去. 如果 remote 分支超前于本地分支,并且本地 ...
- 第27月第18天 epoll lt et
1. While the usage of epoll when employed as a level-triggered interface does have the same semantic ...
- 第27月第17天 objc_msgSendSuper
1.objc_msgSendSuper super 的含义,消息转发会调用 objc_msgSendSuper, 就是 去父类的方法列表中找到 initWithFrame:这个方法,然后调用,调用的主 ...
- 第27月第12天 webrtc ios openssl boost
1. source 'https://github.com/CocoaPods/Specs.git' target 'YOUR_APPLICATION_TARGET_NAME_HERE' do pla ...
随机推荐
- 激活函数(ReLU, Swish, Maxout)
神经网络中使用激活函数来加入非线性因素,提高模型的表达能力. ReLU(Rectified Linear Unit,修正线性单元) 形式如下: \[ \begin{equation} f(x)= \b ...
- goto语句
让程序直接跳到自定义标签位置 public static void Main(string[] args) { ; goto myLabel;AppDomainInitializer//直接跳到标签m ...
- Luogu P2852 [USACO06DEC]牛奶模式Milk Patterns
题目链接 \(Click\) \(Here\) 水题.利用\(Height\)的性质维护一个单调栈即可. #include <bits/stdc++.h> using namespace ...
- 构造方法中关键字-- super
package lijun.cn.demo4; public class Person { int num =777; public Person(){ System.out.println(&quo ...
- 跨域、curl、snoopy、file_get_contents()
定义:可以称为”信息采集/模拟登录”技术,可以实现对某个地址做请求,同时按照要求传递get或post参数. curl本身是php的一个扩展,同时也是一个利用URL语法规定来传输文件和数据的工具,支持很 ...
- Microsoft Windows CVE-2017-8464 LNK 远程代码执行漏洞(复现)
2017年6月13日,微软官方发布编号为CVE-2017-8464的漏洞公告,官方介绍Windows系统在解析快捷方式时存在远程执行任意代码的高危漏洞,黑客可以通过U盘.网络共享等途径触发漏洞,完全控 ...
- C# 正则表达式中的顺序环视和逆序环视
环视结构不匹配任何字符,只匹配文本中的特定位置. 顺序环视:从左向右查看文本,尝试匹配子表达式,如果能够匹配则返回匹配成功信息.顺序环视使用「 (?=...) 来标识」,例如「 (?=\d) 」,它表 ...
- M1-day08-数据库加锁
一.关于数据库加锁主要分为几类 1.原生sql 1.开启事物 begin; select * from host for update; commit; 注意:INNODB支持表锁和行锁,MyISAM ...
- django中文学习资料
Django 2.0 中文官方文档地址: https://docs.djangoproject.com/zh-hans/2.0/ <Django Girls>中文版地址: https:// ...
- go 学习资源和GitHub库
go httprouter 源码包 https://github.com/julienschmidt/httprouter 用例 https://github.com/gsingharoy/httpr ...