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