[#0x0032] Pointer Swizzling

本文介绍了计算机科学中的指针混写技术,详细解释了在内存和磁盘间转换数据结构时如何使用指针混写与反混写进行操作,以确保高效及正确地处理指针引用。
部署运行你感兴趣的模型镜像

  继续学习。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。

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

用户升级ios26系统, 使用过程中LSSafeProtector这个第三方库的NSNotificationCenter报错, 如何解决, 中文方式说明, 下面是报错的方法以及错误信息 -(void)safe_addObserver:(id)observer selector:(SEL)aSelector name:(NSNotificationName)aName object:(id)anObject { [observer setIsNotification:YES]; [observer safe_changeDidDeallocSignal]; [self safe_addObserver:observer selector:aSelector name:aName object:anObject]; } #0 0x00000002478cc0cc in __pthread_kill () #1 0x00000001fa931810 in pthread_kill () #2 0x00000001ab154f1c in abort () #3 0x000000019cc43f60 in swift::fatalErrorv () #4 0x000000019cc43f80 in swift::fatalError () #5 0x000000019ccb19e4 in -[_TtCs12_SwiftObject doesNotRecognizeSelector:] () #6 0x000000019fb1f4f8 in ___forwarding___ () #7 0x000000019fb273a0 in _CF_forwarding_prep_0 () #8 0x000000010ea4db04 in -[NSNotificationCenter(Safe) safe_addObserver:selector:name:object:] at /Users/clouder/xiaohui-ios/powerone/3rdParty/LSSafeProtector/Foundation/NSNotificationCenter+Safe.m:103 #9 0x00000001a55f5e70 in ___lldb_unnamed_symbol297340 () #10 0x00000001a544dc4c in ___lldb_unnamed_symbol294151 () #11 0x00000001a544db1c in ___lldb_unnamed_symbol294149 () #12 0x00000001a54512b4 in ___lldb_unnamed_symbol294184 () #13 0x00000001a53eb2e4 in ___lldb_unnamed_symbol293679 () #14 0x00000001a544d39c in -[_UIAppCACommitFuture _invoke] () #15 0x00000001a05485bc in CA::Transaction::run_commit_handlers () #16 0x00000001a05090ac in CA::Context::commit_transaction () #17 0x00000001a0533ab0 in CA::Transaction::commit () #18 0x00000001a05413c0 in CA::Transaction::flush_as_runloop_observer () #19 0x00000001a543f0f0 in _UIApplicationFlushCATransaction () #20 0x00000001a543f024 in __setupUpdateSequence_block_invoke_2 () #21 0x00000001a544cee8 in _UIUpdateSequenceRunNext () #22 0x00000001a544c378 in schedulerStepScheduledMainSectionContinue () #23 0x0000000289db35f8 in UC::DriverCore::continueProcessing () #24 0x000000019fb43230 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ () #25 0x000000019fb431a4 in __CFRunLoopDoSource0 () #26 0x000000019fb20c6c in __CFRunLoopDoSources0 () #27 0x000000019faf68b0 in __CFRunLoopRun () #28 0x000000019faf5c44 in _CFRunLoopRunSpecificWithOptions () #29 0x000000023eec6498 in GSEventRunModal () #30 0x00000001a5470ddc in -[UIApplication _run] () #31 0x00000001a5415b0c in UIApplicationMain ()
最新发布
09-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值