Created
September 20, 2010 16:57
-
-
Save dustingetz/588215 to your computer and use it in GitHub Desktop.
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* malloc(unsigned int numBytes) | |
{ | |
void* pointer; //will point to the allocated buffer after its allocated | |
//request virtual memory from OS -- this is a real win32 function | |
pointer = VirtualAlloc( | |
NULL, | |
numBytes, | |
MEM_COMMIT | MEM_RESERVE, //allocate virtual memory, AND put it in real memory right now (virtual memory can return a virtual pointer to the hard drive, and then swap it into memory on demand) | |
PAGE_READWRITE //can't execute code here. e.g. to prevent buffer overflows from being able to execute malicious code | |
); | |
return pointer; //returns NULL if out of memory | |
} | |
//wrap malloc, add support for C++ features like constructors and exceptions | |
template<typename T> T* operator new() throw std::bad_alloc | |
{ | |
T* that = (T*)malloc(sizeof(T)); | |
if (that==NULL) throw std::bad_alloc; | |
that->constructor(); | |
return that; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment