usermode/library/malloc-original/src/mallocwrap.cpp

00001 
00002 //
00003 // The Hoard Multiprocessor Memory Allocator
00004 // www.hoard.org
00005 //
00006 // Author: Emery Berger, http://www.cs.utexas.edu/users/emery
00007 //
00008 // Copyright (c) 1998-2001, The University of Texas at Austin.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Library General Public License as
00012 // published by the Free Software Foundation, http://www.fsf.org.
00013 //
00014 // This library is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Library General Public License for more details.
00018 //
00020 
00021 /*
00022 
00023   Wrappers that re-route everything, including the Win32 API,
00024   to ordinary ANSI C calls (except for size).
00025 
00026   by Emery Berger
00027 
00028 */
00029 
00030 #include <stdlib.h>
00031 #if !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x420
00032 #include <new>
00033 #endif
00034 
00035 #if 1
00036 extern "C" void * malloc (size_t);
00037 extern "C" void free (void *);
00038 extern "C" void * calloc (size_t, size_t);
00039 extern "C" void * realloc (void *, size_t);
00040 extern "C" size_t malloc_usable_size (void *);
00041 #endif
00042 
00043 
00044 void * operator new (size_t size)
00045 {
00046   return malloc (size);
00047 }
00048 
00049 void operator delete (void * ptr)
00050 {
00051   free (ptr);
00052 }
00053 
00054 #if !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x420
00055 
00056 void * operator new (size_t size, const std::nothrow_t&) throw() {
00057   return malloc (size);
00058 } 
00059 
00060 void * operator new[] (size_t size) throw(std::bad_alloc)
00061 {
00062   return malloc (size);
00063 }
00064 
00065 void * operator new[] (size_t size, const std::nothrow_t&) throw() {
00066   return malloc (size);
00067 } 
00068 
00069 void operator delete[] (void * ptr) throw()
00070 {
00071   free (ptr);
00072 }
00073 #endif
00074 
00075 #if defined(WIN32)
00076 
00077 #include <windows.h>
00078 #include <stdlib.h>
00079 
00080 // Replace the CRT debugging allocation routines, just to be on the safe side.
00081 // This is not a complete solution, but should avoid inadvertent mixing of allocations.
00082 
00083 #include <stdio.h>
00084 
00085 
00086 extern "C" void * __cdecl _heap_alloc_base (size_t size)
00087 {
00088         return malloc (size);
00089 }
00090 
00091 void * operator new (unsigned int cb, int, const char *, int)
00092 {
00093         return ::operator new (cb);
00094 }
00095 
00096 void operator delete(void * p, int, const char *, int)
00097 {
00098   ::operator delete(p);
00099 }
00100 
00101 extern "C" void * __cdecl _malloc_dbg (size_t sz, int, const char *, int) {
00102         return malloc (sz);
00103 }
00104 
00105 extern "C" void * __cdecl _malloc_base (size_t sz) {
00106         return malloc (sz);
00107 }
00108 
00109 extern "C" void * __cdecl _calloc_dbg (size_t num, size_t size, int, const char *, int) {
00110         return calloc (num, size);
00111 }
00112 
00113 extern "C" void * __cdecl _calloc_base (size_t num, size_t size) {
00114         return calloc (num, size);
00115 }
00116 
00117 extern "C" void * __cdecl _realloc_dbg (void * ptr, size_t newSize, int, const char *, int) {
00118         return realloc (ptr, newSize);
00119 }
00120 
00121 extern "C" void * __cdecl _realloc_base (void * ptr, size_t newSize) {
00122         return realloc (ptr, newSize);
00123 }
00124 
00125 extern "C" void __cdecl _free_dbg (void * ptr, int) {
00126         free (ptr);
00127 }
00128 
00129 extern "C" void __cdecl _free_base (void * ptr) {
00130         free (ptr);
00131 }
00132 
00133 
00134 /* Don't allow expand to work ever. */
00135 
00136 extern "C" void * __cdecl _expand (void * ptr, size_t sz) {
00137   return NULL;
00138 }
00139 
00140 extern "C" void * __cdecl _expand_base (void * ptr, size_t sz) {
00141   return NULL;
00142 }
00143 
00144 extern "C" void * __cdecl _expand_dbg (void * ptr, size_t sz, int, const char *, int) {
00145   return NULL;
00146 }
00147 
00148 extern "C" void * __cdecl _nh_malloc (size_t sz, int) {
00149         return malloc (sz);
00150 }
00151 
00152 extern "C" void * __cdecl _nh_malloc_base (size_t sz, int) {
00153         return malloc (sz);
00154 }
00155 
00156 extern "C" void * __cdecl _nh_malloc_dbg (size_t sz, size_t, int, int, const char *, int) {
00157         return malloc (sz);
00158 }
00159 
00160 // We have to replace atexit & _onexit with a version that doesn't use the heap!
00161 // So we make a really large static buffer and hope for the best.
00162 
00163 #if 0
00164 typedef void (__cdecl * exitFunction)(void);
00165 
00166 enum { MAX_EXIT_FUNCTIONS = 16384 };
00167 
00168 static exitFunction exitFunctionBuffer[MAX_EXIT_FUNCTIONS];
00169 static int exitFunctionCount = 0;
00170 
00171 
00172 extern "C" void __cdecl _exit(int);
00173 extern "C" void __cdecl exit(int);
00174 
00175 extern "C" void __cdecl doexit(int code,int quick,int retcaller)
00176 {
00177         if (quick) {
00178                 _exit(code);
00179         } else {
00180                 exit(code);
00181         }
00182 }
00183 
00184 
00185 extern "C" int __cdecl atexit (void (__cdecl * fn)(void)) {
00186         if (exitFunctionCount == MAX_EXIT_FUNCTIONS) {
00187                 return 1;
00188         } else {
00189                 exitFunctionBuffer[exitFunctionCount] = fn;
00190                 exitFunctionCount++;
00191                 return 0;
00192         }
00193 }
00194 
00195 
00196 extern "C" _onexit_t __cdecl _onexit(_onexit_t fn)
00197 {
00198         if (atexit((exitFunction) fn) == 0) {
00199                 return fn;
00200         } else {
00201                 return NULL;
00202         }
00203 }
00204 
00205 extern "C" void __cdecl _exit(int);
00206 extern "C" int __cdecl _fcloseall (void);
00207 
00208 extern "C" void __cdecl exit (int r) {
00209 
00210         // As prescribed, we call all exit functions in LIFO order,
00211         // flush all open buffers to disk, and then really exit.
00212 
00213         for (int i = exitFunctionCount - 1; i >= 0; i--) {
00214                 (*exitFunctionBuffer[i])();
00215         }
00216         _fcloseall();
00217 
00218         _exit(r);
00219 }
00220 #endif
00221 
00222 #endif // WIN32

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7