使用内存区域作为堆栈空间?

时间:2012-05-18 10:49:30

标签: c++ c linux memory stack

在Linux中是否可以启动一个进程(例如使用execve)并使其使用特定的内存区域作为堆栈空间?

背景:

我有一个C ++程序和一个快速分配器,它给了我“快速记忆”。我可以将它用于使用堆的对象并在快速内存中创建它们。精细。但我也有很多变量生活在堆栈上。如何让他们也使用快速记忆?

想法:实现一个“程序包装器”,它分配快速内存,然后启动实际的主程序,将指针传递给快速内存,程序将其用作堆栈。这可能吗?

[更新]

pthread设置似乎有效。

1 个答案:

答案 0 :(得分:9)

使用pthread,您可以使用辅助线程作为程序逻辑,并使用pthread_attr_setstack()设置堆栈地址:

NAME
       pthread_attr_setstack,  pthread_attr_getstack  -  set/get stack
       attributes in thread attributes object

SYNOPSIS
       #include <pthread.h>

       int pthread_attr_setstack(pthread_attr_t *attr,
                                 void *stackaddr, size_t stacksize);

DESCRIPTION
       The pthread_attr_setstack() function sets the stack address and
       stack  size attributes of the thread attributes object referred
       to by attr to the values specified in stackaddr and  stacksize,
       respectively.   These  attributes specify the location and size
       of the stack that should be used by a thread  that  is  created
       using the thread attributes object attr.

       stackaddr should point to the lowest addressable byte of a buf‐
       fer of stacksize bytes that was allocated by the  caller.   The
       pages  of  the  allocated  buffer  should  be both readable and
       writable.

我不遵循的是你期望通过做这样的事情来获得任何性能改进(我假设你的“快速”记忆的目的是更好的表现)。

相关问题