On Windows I can get the stack-boundaries from the Thread Information Block like this:
void *stackBottom,
*stackTop;
#if defined _MSC_VER
void **teb = (void **)NtCurrentTeb();
stackBottom = teb[2];
stackTop = teb[1];
#else
#error "unsupported platform"
#endif
... or with GetCurrentThreadStackLimits(). But GetCurrentThreadStackLimits() doesn't return the boundaries of the currently allocated Stack (Windows does overcommit stacks) but the whole address-range of the stack to where it ultimately might extend.
Is something similar like the above possible with Linux ?
[EDIT] I've got it:
#include <iostream>
#include <pthread.h>
using namespace std;
int main()
{
pthread_attr_t attrs;
if( pthread_getattr_np( pthread_self(), &attrs ) != 0 )
return -1;
void *stackAddr;
size_t stackSize;
if( pthread_attr_getstack( &attrs, &stackAddr, &stackSize ) != 0 )
return -1;
cout << "stack-address: " << stackAddr << endl;
cout << "stack-size: " << stackSize << endl;
cout << "var-addr: " << (void *)&stackAddr << endl;
}
This determines the base-address of the stack and its size. As var_addr shows stackAddr is the lower bound, i.e. the stack begins at (char *)stackAddr + stackSize. The next thing I'm going to do is to determine the performance of that code.