anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

win32_memory.cpp (1811B)

      1 /*       +------------------------------------+
      2  *       | Inspire Internet Relay Chat Daemon |
      3  *       +------------------------------------+
      4  *
      5  *  InspIRCd: (C) 2002-2011 InspIRCd Development Team
      6  * See: https://wiki.inspircd.org/Credits
      7  *
      8  * This program is free but copyrighted software; see
      9  *            the file COPYING for details.
     10  *
     11  * ---------------------------------------------------
     12  */
     13 
     14 #ifdef _WIN32
     15 
     16 #include <windows.h>
     17 #include <exception>
     18 #include <new>
     19 #include <new.h>
     20 
     21 /** On windows, all dll files and executables have their own private heap,
     22  * whereas on POSIX systems, shared objects loaded into an executable share
     23  * the executable's heap. This means that if we pass an arbitrary pointer to
     24  * a windows DLL which is not allocated in that dll, without some form of
     25  * marshalling, we get a page fault. To fix this, these overrided operators
     26  * new and delete use the windows HeapAlloc and HeapFree functions to claim
     27  * memory from the windows global heap. This makes windows 'act like' POSIX
     28  * when it comes to memory usage between dlls and exes.
     29  */
     30 
     31 void *::operator new(size_t iSize)
     32 {
     33 	void *ptr = HeapAlloc(GetProcessHeap(), 0, iSize); /* zero memory for unix compatibility */
     34 	/* This is the correct behaviour according to C++ standards for out of memory,
     35 	 * not returning null -- Brain
     36 	 */
     37 	if (!ptr)
     38 		throw std::bad_alloc();
     39 	else
     40 		return ptr;
     41 }
     42 
     43 void ::operator delete(void *ptr)
     44 {
     45 	if (ptr)
     46 		HeapFree(GetProcessHeap(), 0, ptr);
     47 }
     48 
     49 void *operator new[](size_t iSize)
     50 {
     51 	void *ptr = HeapAlloc(GetProcessHeap(), 0, iSize); /* Why were we initializing the memory to zeros here? This is just a waste of cpu! */
     52 	if (!ptr)
     53 		throw std::bad_alloc();
     54 	else
     55 		return ptr;
     56 }
     57 
     58 void operator delete[](void *ptr)
     59 {
     60 	if (ptr)
     61 		HeapFree(GetProcessHeap(), 0, ptr);
     62 }
     63 
     64 #endif