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