]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_memory_functions.cpp
Properly give the service specific exit code on failure to start. Now we just need...
[user/henk/code/inspircd.git] / win / inspircd_memory_functions.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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         HeapFree(GetProcessHeap(), 0, ptr);
44 }
45
46 void * operator new[] (size_t iSize) {
47         void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize); /* Why were we initializing the memory to zeros here? This is just a waste of cpu! */
48         if (!ptr)
49                 throw std::bad_alloc();
50         else
51                 return ptr;
52 }
53
54 void operator delete[] (void* ptr)
55 {
56         HeapFree(GetProcessHeap(), 0, ptr);
57 }