]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_memory_functions.cpp
Comments
[user/henk/code/inspircd.git] / win / inspircd_memory_functions.cpp
1 /*       +------------------------------------+\r
2  *       | Inspire Internet Relay Chat Daemon |\r
3  *       +------------------------------------+\r
4  *\r
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team\r
6  * See: http://www.inspircd.org/wiki/index.php/Credits\r
7  *\r
8  * This program is free but copyrighted software; see\r
9  *            the file COPYING for details.\r
10  *\r
11  * ---------------------------------------------------\r
12  */\r
13 \r
14 #include "inspircd_win32wrapper.h"\r
15 #include <exception>\r
16 #include <new>\r
17 #include <new.h>\r
18 \r
19 /** On windows, all dll files and executables have their own private heap,\r
20  * whereas on POSIX systems, shared objects loaded into an executable share\r
21  * the executable's heap. This means that if we pass an arbitrary pointer to\r
22  * a windows DLL which is not allocated in that dll, without some form of\r
23  * marshalling, we get a page fault. To fix this, these overrided operators\r
24  * new and delete use the windows HeapAlloc and HeapFree functions to claim\r
25  * memory from the windows global heap. This makes windows 'act like' POSIX\r
26  * when it comes to memory usage between dlls and exes.\r
27  */\r
28 \r
29 void * ::operator new(size_t iSize)\r
30 {\r
31         void* ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, iSize);               /* zero memory for unix compatibility */\r
32         /* This is the correct behaviour according to C++ standards for out of memory,\r
33          * not returning null -- Brain\r
34          */\r
35         if (!ptr)\r
36                 throw std::bad_alloc();\r
37         else\r
38                 return ptr;\r
39 }\r
40 \r
41 void ::operator delete(void * ptr)\r
42 {\r
43         HeapFree(GetProcessHeap(), 0, ptr);\r
44 }\r