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