]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_memory_functions.cpp
Fix using std::cout instead of errstr when a port fails to bind.
[user/henk/code/inspircd.git] / win / inspircd_memory_functions.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
6  *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
7  *   Copyright (C) 2011, 2013 Adam <Adam@anope.org>
8  *   Copyright (C) 2009 John Brooks <special@inspircd.org>
9  *   Copyright (C) 2007-2008 Craig Edwards <brain@inspircd.org>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include <windows.h>
27 #include <exception>
28 #include <new>
29 #include <new.h>
30
31 /** On windows, all dll files and executables have their own private heap,
32  * whereas on POSIX systems, shared objects loaded into an executable share
33  * the executable's heap. This means that if we pass an arbitrary pointer to
34  * a windows DLL which is not allocated in that dll, without some form of
35  * marshalling, we get a page fault. To fix this, these overridden operators
36  * new and delete use the windows HeapAlloc and HeapFree functions to claim
37  * memory from the windows global heap. This makes windows 'act like' POSIX
38  * when it comes to memory usage between dlls and exes.
39  */
40
41 void * ::operator new(size_t iSize)
42 {
43         void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize);
44         /* This is the correct behaviour according to C++ standards for out of memory,
45          * not returning null -- Brain
46          */
47         if (!ptr)
48                 throw std::bad_alloc();
49         else
50                 return ptr;
51 }
52
53 void ::operator delete(void * ptr)
54 {
55         if (ptr)
56                 HeapFree(GetProcessHeap(), 0, ptr);
57 }
58
59 void * operator new[] (size_t iSize)
60 {
61         void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize);
62         if (!ptr)
63                 throw std::bad_alloc();
64         else
65                 return ptr;
66 }
67
68 void operator delete[] (void* ptr)
69 {
70         if (ptr)
71                 HeapFree(GetProcessHeap(), 0, ptr);
72 }