gpt4 book ai didi

objective-c - objc_msgSend 和联合

转载 作者:搜寻专家 更新时间:2023-10-30 20:02:53 26 4
gpt4 key购买 nike

当我有返回一些联合的方法时,我有一个与 objc_msgSend 运行时调用相关的问题。

我正在通过 libffi 调用 objc_msgSend 函数,如果我有小于 16B 的联合,一切都很好,但如果联合的大小大于 16B,我就会遇到段错误。我尝试使用 objc_msgSend_stret 函数,它通过了,但是我得到了返回联合的错误值,而且我不确定调用了哪个函数。

有谁知道 objective c 中的 union,它们在 objc runtime 中是如何处理的?

最佳答案

当您使用 objc_msgSend 时,这是一个 HUUUUGE 问题;基本上,大多数 ABI 喜欢将它们的返回值放在寄存器中(这里有勇敢和好奇的所有血腥细节:http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/LowLevelABI/000-Introduction/introduction.html)但有些对象不能放入寄存器(这就是联合大小 <= 16 的原因为你工作正常)。您需要调用 objc_msgSend_stret,而不是使用您希望将联合填充到的地址。

另一个很好的引用:http://www.sealiesoftware.com/blog/archive/2008/10/30/objc_explain_objc_msgSend_stret.html

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/c/func/objc_msgSend_stret

解决方案:不要强制转换和调用objc_msgSend,而是强制转换和调用objc_msgSend_stret

void objc_msgSend_stret(void * stretAddr, id theReceiver, SEL theSelector,  ...)

所以你的类型转换将是(如 using objc_msgSend to call a Objective C function with named arguments ):

union myUnion {
int a, b;
char c;
double d;
};

// For objc_msgSend_stret
void (*objc_msgSend_stretTyped)(union myUnion* stretAddr, id self, SEL _cmd, float bar) = (void*)objc_msgSend_stret;

union myUnion u;
float pi = 4;

objc_msgSend_stretTyped(&u, obj, sel_getUID(sel), pi);

编辑——跳过以上所有代码

下面是我如何让 objc_msgSend_stret 工作:

#import "ViewController.h"
#import <objc/runtime.h>
#import <objc/objc.h>

@interface ViewController ()

@end

union myUnion {
int myArray[32];
int a,b,c,d;
};

void doStuff(id obj, SEL sel);

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
doStuff(self, @selector(doStuff:));
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (union myUnion) doStuff: (int)myInt {
NSLog(@"Pi equals %d", myInt);
union myUnion u;
for(int i=0; i<32; i++) {
u.myArray[i] = i*i*i*i-1;
}
return u;
}

@end

void doStuff(id obj, SEL sel) {
int pi = 4;
NSLog(@"myUnion is: %luB", sizeof(union myUnion));
NSLog(@"Sizeof(int) = %luB ... Sizeof(char) = %lub ... sizeof(double) = %luB", sizeof(int), sizeof(char), sizeof(double));

union myUnion u = ((union myUnion(*)(id, SEL, int))objc_msgSend_stret)(obj, sel, pi);
NSLog(@"Union u = {%d, %d, %d, %d}", u.myArray[30], u.myArray[29], u.myArray[28], u.myArray[27]);
}

关于objective-c - objc_msgSend 和联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17662011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com