英文:
Can I designate a specific register to use instead of Thread Local Storage?
问题
I want to use Windows' Vectored Exception Handlers, but there's no way to pass critical information to the handler.
This can be solved by using Thread Local Storage, but I'm looking for a better solution.
Because Vectored Exception Handlers allow you to read the contents of the registers as they were at the time that the exception occurred, it would seem that storing a pointer to an information structure (or just the information itself) in a register would make sense.
While this is trivial in assembly code, C doesn't have any easy way to designate a specific register for you to use for this purpose.
Is there a way to implement this in C safely?
英文:
I want to use Windows' Vectored Exception Handlers, but there's no way to pass critical information to the handler.
This can be solved by using Thread Local Storage, but I'm looking for a better solution.
Because Vectored Exception Handlers allow you to read the contents of the registers as they were at the time that the exception occurred, it would seem that storing a pointer to an information structure (or just the information itself) in a register would make sense.
While this is trivial in assembly code, C doesn't have any easy way to designate a specific register for you to use for this purpose.
Is there a way to implement this in C safely?
答案1
得分: 1
在汇编代码中存储指向信息结构(或者信息本身)的指针似乎是有道理的。
虽然在汇编代码中这很简单,但C语言没有简单的方法来指定一个特定的寄存器供您用于此目的。
在C中是否有安全地实现这一点的方法?
C语言规范完全不涉及CPU寄存器(而且,register
关键字也不矛盾)。因此,在(严格符合)C语言中,没有办法安全地实现它,无论如何。
但您可以在汇编中编写一个辅助函数,然后可以从C中调用它。或者许多C实现支持某种形式的内联汇编。而且一些体系结构甚至可能支持有用的特性,比如将CPU寄存器映射到地址,这也能满足您的目的。但是所有这些机制都依赖于另一种语言,和/或C扩展,和/或特定于实现的行为。它们在任何情况下都不是可移植的,它们的安全性必须根据具体情况逐案评估。
英文:
> it would seem that storing a pointer to an information structure (or just the information itself) in a register would make sense.
>
> While this is trivial in assembly code, C doesn't have any easy way to designate a specific register for you to use for this purpose.
>
> Is there a way to implement this in C safely?
The C language specification has nothing whatever to say about CPU registers (and no, the register
keyword does not contradict that). Therefore, no, there is no way to implement it in (strictly conforming) C (alone), safely or otherwise.
But you could potentially write a helper function in assembly that you could call from C. Or many C implementations support inline assembly of some variety. And some architectures may even support helpful features such as mapping CPU registers to addresses, which would also serve your purpose. But all such mechanisms rely on another language, and / or C extensions, and / or implementation-specific behavior. They are in no way portable, and how safe any of them are must be evaluated on a case by case basis.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论