Skip to content

Instantly share code, notes, and snippets.

@maxmcguire
Created September 11, 2014 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxmcguire/0ebb3eb19b59b2f50f80 to your computer and use it in GitHub Desktop.
Save maxmcguire/0ebb3eb19b59b2f50f80 to your computer and use it in GitHub Desktop.
Safer alloca?
void* GetStackBase()
{
static __declspec(thread) void* stackBase = nullptr;
if (stackBase == nullptr)
{
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery(&mbi, &mbi, sizeof(mbi));
stackBase = mbi.AllocationBase;
}
return stackBase;
}
bool GetIsRoomOnStack(size_t size)
{
char dummy;
return &dummy - size > GetStackBase();
}
#define Alloca(size) \
(GetIsRoomOnStack((size)) ? alloca((size)) : malloc((size)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment