Created
September 11, 2014 00:23
-
-
Save maxmcguire/0ebb3eb19b59b2f50f80 to your computer and use it in GitHub Desktop.
Safer alloca?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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