Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Created September 20, 2010 16:57
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 dustingetz/588215 to your computer and use it in GitHub Desktop.
Save dustingetz/588215 to your computer and use it in GitHub Desktop.
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