]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_memory_functions.cpp
Merge pull request #1050 from Aviator45003/insp20
[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 #include <windows.h>
20 #include <exception>
21 #include <new>
22 #include <new.h>
23
24 /** On windows, all dll files and executables have their own private heap,
25  * whereas on POSIX systems, shared objects loaded into an executable share
26  * the executable's heap. This means that if we pass an arbitrary pointer to
27  * a windows DLL which is not allocated in that dll, without some form of
28  * marshalling, we get a page fault. To fix this, these overrided operators
29  * new and delete use the windows HeapAlloc and HeapFree functions to claim
30  * memory from the windows global heap. This makes windows 'act like' POSIX
31  * when it comes to memory usage between dlls and exes.
32  */
33
34 void * ::operator new(size_t iSize)
35 {
36         void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize);
37         /* This is the correct behaviour according to C++ standards for out of memory,
38          * not returning null -- Brain
39          */
40         if (!ptr)
41                 throw std::bad_alloc();
42         else
43                 return ptr;
44 }
45
46 void ::operator delete(void * ptr)
47 {
48         if (ptr)
49                 HeapFree(GetProcessHeap(), 0, ptr);
50 }
51
52 void * operator new[] (size_t iSize)
53 {
54         void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize);
55         if (!ptr)
56                 throw std::bad_alloc();
57         else
58                 return ptr;
59 }
60
61 void operator delete[] (void* ptr)
62 {
63         if (ptr)
64                 HeapFree(GetProcessHeap(), 0, ptr);
65 }