2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
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.
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
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/>.
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.
34 void * ::operator new(size_t iSize)
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
41 throw std::bad_alloc();
46 void ::operator delete(void * ptr)
49 HeapFree(GetProcessHeap(), 0, ptr);
52 void * operator new[] (size_t iSize)
54 void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize);
56 throw std::bad_alloc();
61 void operator delete[] (void* ptr)
64 HeapFree(GetProcessHeap(), 0, ptr);