继续学习。pointer swizzling有个很坏的翻译叫“指针混写”,翻译得不知所云。
以下来自Jargon File和Wikipedia。
To convert external names, array indices, or references within a data structure
into address pointers when the data structure is brought into main memory from
external storage; this may be done for speed
in chasing references or to simplify code (e.g., by turning lots of name lookups
into pointer dereferences). The converse operation is sometimes termed
‘unswizzling'.
Jargon File的这一段解释得还是不怎么清楚,看Wikipedia上的这个例子就很清楚了。
In computer science, pointer swizzling is the conversion of references based on name or position to direct pointer references. It is typically performed during the deserialization (loading) of a relocatable object from disk, such as an executable file or pointer-based data structure. The reverse operation, replacing pointers with position-independent symbols or positions, is sometimes referred to as unswizzling, and is performed during serialization(saving).
For example, suppose we have the following linked list data structure:
struct node {
int data;
struct node *next;
};
We can easily create a linked list data structure in memory using such an object, but when we attempt to save it to disk we run into trouble. Directly saving the pointer values won't work on most architectures, because the next time we load it the memory positions the nodes now use may be in use by other data. One way of dealing with this is to assign a unique id number to each node and then unswizzle the pointers by turning them into a field indicating the id number of the next node:
struct node_saved {
int data;
int id_number;
int id_number_of_next_node;
};
We can save these records to disk in any order, and no information will be lost. Other options include saving the file offset of the next node or a number indicating its position in the sequence of saved records.
When we go to load these nodes, however, we quickly discover that attempting to find a node based on its number is cumbersome and inefficient. We'd like our original data structure back so we can simply follow next pointers to traverse the list. To do this, we perform pointer swizzling, finding the address of each node and turning the id_number_of_next_node fields back into direct pointers to the right node.
简单地说来,就是内存中的节点间通过“逻辑”指针(实质是内存地址)连接,而将这些节点保存到磁盘时,“逻辑”指针就没有任何意义了,需要变换一种方式来表示这些节点间的连接关系(这里也不好叫做“物理”指针……),这个变换的过程称为unswizzling。反过来,将这些节点从磁盘load到内存中时的变换就是swizzling。
分享到:
相关推荐
Mehod Swizzling 实现页面统计
Method Swizzling 示例
AopTestDemo: iOS埋点统计方案: 1.Method Swizzling 2.AOP编程
runtime黑魔法 Mehod Swizzling 实现页面统计
AOP 编程必看代码,iOS 逆向开发,iOS SDK开发,iOS深入开发的源点
OLAP, and data-cube systems *Explains error-correction in RAID disks and covers bitmap indexes, data mining, data statistics, and pointer swizzling *Supports additional teaching materials found on ...
介绍个技巧,最好的式就是提出具体的需求,然后它跟其他的解决法做较。所以,先来看看我们的需求:对 App 的户为进追踪和分析。简单说,就是当户看到某个 View
在简书的文章中详细的讲解了Method Swizzling,为了方便大家学习和理解,所以在Github上写了这个小Demo,可以帮助大家更好的理解Method Swizzling。 在Demo中简单实现了一个崩溃拦截的代码,等以后有时间的话,我...
在阅读团队一项目源码时,发现Method Swizzling的写法有些瑕疵。这篇文章主要就介绍iOS Method Swizzling的正确写法应该是什么样的。 下面是iOS Method Swizzling的一种实现: + (void)load { Class class = [self...
要选择退出此行为, WEBP_NO_UIIMAGE_INITIALIZER_SWIZZLING在构建环境中设置WEBP_NO_UIIMAGE_INITIALIZER_SWIZZLING 。 如果您使用的是CocoaPods,则可以将此构建设置添加到Podfile : post_install do | r | r . ...
Method_Swizzling参考大神的方法交叉和AOP编程的小例子
Android方法混乱 Android Studio从位于/build/outputs/aar/.aar下的模块生成aar文件 从aar提取jar文件:使用存档管理器时打开aar,而aar文件包含classes.jar文件。 一旦classes.jar文件包含所有需要的* .class文件,...
主要介绍了iOS 11 使用方法替换(Method Swizzling)去掉导航栏返回按钮的文字,需要的朋友可以参考下
PLPatchMaster PLPatchMaster 提供了一个易于使用的基于块的 API,使用了提供的块蹦床库,以及一组用于 ARMv7、ARMv7s、ARM64 和 x86-64 的自定义汇编蹦床。 PLPatchMaster 可以通过注册 dyld 图像事件的侦听器,...
KVC编程思想:KVC运用了一个isa-swizzling技术。isa-swizzling就是类型混合指针机制。
有一个自动 Swizzling 配置,您可以在每个类的基础上进行切换。 ##安装 pod 'SHKeyValueObserverBlocks' ##设置 将其放在特定文件或您的项目前缀文件中 # import " NSObject+SHKeyValueObserverBlocks.h " ...
这是iOS运行时在几个场景中的应用代码,主要就是在分类中添加属性,对定时器的内存泄漏问题的处理,还有就是Method Swizzling的使用。
作者CoderJackyHuang,源码RuntimeDemo,在我学习runtime的method swizzling特性之前,有很多同事或者朋友经常在我耳边说起swizzling特性,一个个在我面前说这个东西千万不能用,会引起很多问题的。但是,在我学习完...
原理是method swizzling替换了系统方法,处理参数边界,现多个项目使用,一切正常 原名DurexKit,名字太无节操,改为SafeKit SafeKit中针对每个可能crash的方法处理,目前处理的范围有限,如有兴趣,欢迎贡献代码...