Question

I have found the following function here and would like to get it to compile under MinGW:

size_t stackavail()
{
    static unsigned StackPtr;
    __asm mov [StackPtr],esp
    static MEMORY_BASIC_INFORMATION mbi;
    VirtualQuery((PVOID)StackPtr,&mbi,sizeof(mbi));
    return StackPtr-(unsigned)mbi.AllocationBase;
}

I tried to modify the code above and managed to compile and run it:

size_t stackavail()
{
    static unsigned StackPtr;

    __asm__ ("movl %%esp, %0\n\t" : "=r" (StackPtr) );
    MEMORY_BASIC_INFORMATION mbi;
    VirtualQuery((PVOID)StackPtr, &mbi, sizeof(mbi));
    return StackPtr-(unsigned)mbi.AllocationBase;
}

But I'm in doubt. Is it correct? Is there another way to do it?

Was it helpful?

Solution

You can get rid of the inline ASM, use

void* StackPtr = &StackPtr;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top