]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_memory_functions.cpp
And a minor cleanup of the /stats z code. Nothing spectacular..
[user/henk/code/inspircd.git] / win / inspircd_memory_functions.cpp
1 #include "inspircd_win32wrapper.h"\r
2 #include <exception>\r
3 #include <new>\r
4 #include <new.h>\r
5 \r
6 /** On windows, all dll files and executables have their own private heap,\r
7  * whereas on POSIX systems, shared objects loaded into an executable share\r
8  * the executable's heap. This means that if we pass an arbitrary pointer to\r
9  * a windows DLL which is not allocated in that dll, without some form of\r
10  * marshalling, we get a page fault. To fix this, these overrided operators\r
11  * new and delete use the windows HeapAlloc and HeapFree functions to claim\r
12  * memory from the windows global heap. This makes windows 'act like' POSIX\r
13  * when it comes to memory usage between dlls and exes.\r
14  */\r
15 \r
16 void * ::operator new(size_t iSize)\r
17 {\r
18         void* ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, iSize);               /* zero memory for unix compatibility */\r
19         /* This is the correct behaviour according to C++ standards for out of memory,\r
20          * not returning null -- Brain\r
21          */\r
22         if (!ptr)\r
23                 throw std::bad_alloc();\r
24         else\r
25                 return ptr;\r
26 }\r
27 \r
28 void ::operator delete(void * ptr)\r
29 {\r
30         HeapFree(GetProcessHeap(), 0, ptr);\r
31 }\r