在内存中创建并使用新堆栈
由于一些特殊原因(请不要问我为什么),对于某些函数,我想使用单独的堆栈。例如,假设我希望函数 malloc
使用不同的堆栈进行处理,我需要在调用它之前切换到新创建的堆栈,并返回到程序使用的原始堆栈完成后。所以算法会是这样的。
switch_to_new_stack
call malloc
swith back to the original stack
做到这一点最简单、最有效的方法是什么?有什么想法吗?
For some special reasons (please don't ask me why), for some functions, I want to use a separate stack. So for example, say I want the function malloc
to use a different stack for its processing, I need to switch to my newly created stack before it is called and get back to the original stack used by the program after it finishes. So the algorithm would be something like this.
switch_to_new_stack
call malloc
swith back to the original stack
What is the easiest and most efficient way of doing this? Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可能不符合您对简单或高效的定义,但以下可能是实现此目的的一种方法:
这应该适用于支持
makecontext(3)
的所有平台。引用手册页(我也从中获得了示例代码的灵感):PPC64 的输出示例:
It probably doesn't fit your definition of easy or efficient, but the following could be one way to do it:
This should work on all platforms where
makecontext(3)
is supported. Quoting from the manpage (where I also got the inspiration for the example code):Sample output from PPC64:
GCC 支持拆分堆栈,其工作方式有点像您所描述的。
http://gcc.gnu.org/wiki/SplitStacks
项目的目标不同,但实施会按照你的要求去做。
GCC has support of splitted stacks, which works a bit like you described.
http://gcc.gnu.org/wiki/SplitStacks
The goal of the project is different, but implementation will do what you ask.