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

00001 /*****************************************************************************
00002  *      Hoard: A Fast, Scalable, and Memory-Efficient Allocator for 
00003  *      Shared-Memory Multiprocessors.
00004  *
00005  *      Portions: Copyright (c) 1998-2001, The University of Texas at Austin.
00006  *
00007  *      Portions of this are free software; you can redistribute it and/or
00008  *      modify it under the terms of the GNU Library General Public License
00009  *      as published by the Free Software Foundation, http://www.fsf.org.
00010  *
00011  *      These portions are distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *      Library General Public License for more details.
00015  ****************************************************************************/
00016 
00017 //#include <stdlib.h>
00018 
00019 #define HOARD_PREFIX(x) x
00020 // #define HOARD_PREFIX(x) HOARD_PREFIX(x) lh_##x
00021 
00022 #include "config.h"
00023 //#include "dbmem.h"
00024 
00025 #define _CRTIMP __declspec(dllexport)
00026 
00027 #include "threadheap.h"
00028 #define HEAPTYPE threadHeap
00029 
00030 #include "ntwrapper.h"
00031 #include "processheap.h"
00032 #include "arch-specific.h"
00033 
00034 int atexit(void (__cdecl *)(void));
00035 
00036 #pragma warning(disable: 4074)  // initializers put in compiler reserved area
00037 #pragma init_seg(compiler)
00038 
00039 #pragma comment(linker, "/merge:.CRT=.data")
00040 
00041 #pragma comment(linker, "/disallowlib:libc.lib")
00042 #pragma comment(linker, "/disallowlib:libcd.lib")
00043 #pragma comment(linker, "/disallowlib:libcmt.lib")
00044 #pragma comment(linker, "/disallowlib:libcmtd.lib")
00045 #pragma comment(linker, "/disallowlib:msvcrt.lib")
00046 #pragma comment(linker, "/disallowlib:msvcrtd.lib")
00047 
00048 inline static processHeap * getAllocator (void) {
00049 //  static char buf[sizeof(processHeap)];
00050   //  static char * buf = (char *) hoardGetMemory (sizeof(processHeap));
00051   static processHeap theAllocator;
00052 //  static processHeap * theAllocator = new (buf) processHeap;
00053   return &theAllocator;
00054 }
00055 
00056 
00057 /*---------------------------------------------------------------------------
00058 */
00059 
00060 int atexit(void (__cdecl *)(void)) { return 0; }
00061 _onexit_t __cdecl _onexit(_onexit_t fn) { return 0; }
00062 
00063 /*---------------------------------------------------------------------------
00064 **      these operators are here so libhoard.dll will allocate through itself
00065 */
00066 void * operator new(size_t size) { return HOARD_PREFIX(malloc) (size); }
00067 void * operator new[](size_t size) { return HOARD_PREFIX(malloc) (size); }
00068 void operator delete(void * ptr) { HOARD_PREFIX(free) (ptr); }
00069 void operator delete[](void * ptr) { HOARD_PREFIX(free) (ptr); }
00070 
00071 extern "C" {
00072 
00073 __declspec(dllexport) size_t HOARD_PREFIX(_msize)(void *memblock)
00074 {
00075   return threadHeap::objectSize (memblock);
00076   //    processHeap * pHeap = getAllocator();
00077   //    return pHeap->getHeap(pHeap->getHeapIndex()).objectSize(memblock);
00078 }
00079 
00080 // #ifdef _DEBUG
00081 
00082 __declspec(dllexport) void *  HOARD_PREFIX(debug_operator_new) (unsigned int cb, int, const char *, int)
00083 {
00084         return HOARD_PREFIX(malloc)(cb);
00085 }
00086 
00087 __declspec(dllexport) void    HOARD_PREFIX(debug_operator_delete) (void * p, int, const char *, int)
00088 {
00089         HOARD_PREFIX(free)(p);
00090 }
00091 
00092 __declspec(dllexport) void * HOARD_PREFIX(nh_malloc_dbg)(size_t sz, size_t, int, int, const char *, int) {
00093         return HOARD_PREFIX(malloc) (sz);
00094 }
00095 
00096 
00097 /*---------------------------------------------------------------------------
00098 */
00099 __declspec(dllexport) void HOARD_PREFIX(assert_failed)(const char *assertion, const char *fileName, int lineNumber)
00100                 { __asm int 3 }
00101 
00102 /*---------------------------------------------------------------------------
00103 */
00104 __declspec(dllexport) void *HOARD_PREFIX(_calloc_dbg)(size_t num, size_t size, 
00105                 int blockType, const char *filename, int linenumber)
00106                 { return HOARD_PREFIX(calloc)(num, size); }
00107 
00108 /*---------------------------------------------------------------------------
00109 */
00110 __declspec(dllexport) int HOARD_PREFIX(_CrtCheckMemory)(void) { return 1; }
00111 
00112 /*---------------------------------------------------------------------------
00113 */
00114 __declspec(dllexport) void HOARD_PREFIX(_CrtDoForAllClientObjects)(void(*pfn)(void*,void*), void *context) { }
00115 
00116 /*---------------------------------------------------------------------------
00117 */
00118 __declspec(dllexport) int HOARD_PREFIX(_CrtDumpMemoryLeaks)(void) { return 0; }
00119 
00120 /*---------------------------------------------------------------------------
00121 */
00122 __declspec(dllexport) int HOARD_PREFIX(_CrtIsMemoryBlock)(const void *userData, unsigned int size, 
00123                 long *requestNumber, char **filename, int *linenumber) { return 1; }
00124 
00125 /*---------------------------------------------------------------------------
00126 */
00127 __declspec(dllexport) int HOARD_PREFIX(_CrtIsValidHeapPointer)(const void *userData) { return 1; }
00128 
00129 /*---------------------------------------------------------------------------
00130 */
00131 __declspec(dllexport) void HOARD_PREFIX(_CrtMemCheckpoint)(_CrtMemState *state) { }
00132 
00133 /*---------------------------------------------------------------------------
00134 */
00135 __declspec(dllexport) int HOARD_PREFIX(_CrtMemDifference)(_CrtMemState *stateDiff, 
00136                 const _CrtMemState *oldState, const _CrtMemState *newState) 
00137                 { return 0; }
00138 
00139 /*---------------------------------------------------------------------------
00140 */
00141 __declspec(dllexport) void HOARD_PREFIX(_CrtMemDumpAllObjectsSince)(const _CrtMemState *state) { }
00142 
00143 /*---------------------------------------------------------------------------
00144 */
00145 __declspec(dllexport) void HOARD_PREFIX(_CrtMemDumpStatistics)(const _CrtMemState *state) { }
00146 
00147 /*---------------------------------------------------------------------------
00148 */
00149 __declspec(dllexport) _CRT_ALLOC_HOOK HOARD_PREFIX(_CrtSetAllocHook)(_CRT_ALLOC_HOOK allocHook) { return 0; }
00150 
00151 /*---------------------------------------------------------------------------
00152 */
00153 __declspec(dllexport) long HOARD_PREFIX(_CrtSetBreakAlloc)(long lBreakAlloc) { return 0; }
00154 
00155 /*---------------------------------------------------------------------------
00156 */
00157 __declspec(dllexport) int HOARD_PREFIX(_CrtSetDbgFlag)(int newFlag) { return _CRTDBG_ALLOC_MEM_DF; }
00158 
00159 /*---------------------------------------------------------------------------
00160 */
00161 __declspec(dllexport) _CRT_DUMP_CLIENT HOARD_PREFIX(_CrtSetDumpClient)(_CRT_DUMP_CLIENT dumpClient) { return 0; }
00162 
00163 /*---------------------------------------------------------------------------
00164 */
00165 __declspec(dllexport) void *HOARD_PREFIX(_malloc_dbg)(size_t size, int blockType, 
00166                 const char *filename, int linenumber)
00167                 { return HOARD_PREFIX(malloc)(size); }
00168 
00169 /*---------------------------------------------------------------------------
00170 */
00171 __declspec(dllexport) void *HOARD_PREFIX(_expand_dbg)(void *userData, size_t newSize, int blockType, 
00172                 const char *filename, int linenumber) { return NULL; }
00173 
00174 /*---------------------------------------------------------------------------
00175 */
00176 __declspec(dllexport) void HOARD_PREFIX(_free_dbg)(void *userData, int blockType) { HOARD_PREFIX(free)(userData); }
00177 
00178 /*---------------------------------------------------------------------------
00179 */
00180 __declspec(dllexport) size_t HOARD_PREFIX(_msize_dbg)(void *memblock, int blockType) 
00181                 { return HOARD_PREFIX(_msize)(memblock); }
00182 
00183 /*---------------------------------------------------------------------------
00184 */
00185 __declspec(dllexport) void * HOARD_PREFIX(_realloc_dbg)(void *userData, size_t newSize, 
00186                 int blockType, const char *filename, int linenumber)
00187                 { return HOARD_PREFIX(realloc)(userData, newSize); }
00188 
00189 // #endif       // ? _DEBUG
00190 
00191 /*---------------------------------------------------------------------------
00192 */
00193 __declspec(dllexport) void *HOARD_PREFIX(_expand)(void * mem, size_t sz) { return NULL; }
00194 
00195 /*---------------------------------------------------------------------------
00196 */
00197 __declspec(dllexport) int HOARD_PREFIX(_heapchk)(void) { return _HEAPOK; }
00198 
00199 /*---------------------------------------------------------------------------
00200 */
00201 __declspec(dllexport) int HOARD_PREFIX(_heapmin)(void) { return 0; }
00202 
00203 /*---------------------------------------------------------------------------
00204 */
00205 __declspec(dllexport) int HOARD_PREFIX(_heapset)(unsigned int) { return _HEAPOK; }
00206 
00207 /*---------------------------------------------------------------------------
00208 */
00209 __declspec(dllexport) int HOARD_PREFIX(_heapwalk)(_HEAPINFO *) { return _HEAPEND; }
00210 
00211 
00212 /*---------------------------------------------------------------------------
00213 */
00214 void *  HOARD_PREFIX(new_nothrow)(unsigned int sz,struct std::nothrow_t const &) {
00215         return HOARD_PREFIX(malloc)(sz);
00216 }
00217 
00218 /*---------------------------------------------------------------------------
00219 */
00220 
00221 // From Doug Lea's malloc.
00222 
00223 #define MALLOC_ZERO(charp, nbytes)                                            \
00224 do {                                                                          \
00225   size_t* mzp = (size_t*)(charp);                           \
00226   unsigned long mctmp = (nbytes)/sizeof(size_t);                     \
00227   long mcn;                                                                   \
00228   if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; }             \
00229   switch (mctmp) {                                                            \
00230     case 0: for(;;) { *mzp++ = 0;                                             \
00231     case 7:           *mzp++ = 0;                                             \
00232     case 6:           *mzp++ = 0;                                             \
00233     case 5:           *mzp++ = 0;                                             \
00234     case 4:           *mzp++ = 0;                                             \
00235     case 3:           *mzp++ = 0;                                             \
00236     case 2:           *mzp++ = 0;                                             \
00237     case 1:           *mzp++ = 0; if(mcn <= 0) break; mcn--; }                \
00238   }                                                                           \
00239 } while(0)
00240 
00241 #define MALLOC_COPY(dest,src,nbytes)                                           \
00242 do {                                                                          \
00243   size_t* mcsrc = (size_t*) src;                            \
00244   size_t* mcdst = (size_t*) dest;                           \
00245   unsigned long mctmp = (nbytes)/sizeof(size_t);                     \
00246   long mcn;                                                                   \
00247   if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; }             \
00248   switch (mctmp) {                                                            \
00249     case 0: for(;;) { *mcdst++ = *mcsrc++;                                    \
00250     case 7:           *mcdst++ = *mcsrc++;                                    \
00251     case 6:           *mcdst++ = *mcsrc++;                                    \
00252     case 5:           *mcdst++ = *mcsrc++;                                    \
00253     case 4:           *mcdst++ = *mcsrc++;                                    \
00254     case 3:           *mcdst++ = *mcsrc++;                                    \
00255     case 2:           *mcdst++ = *mcsrc++;                                    \
00256     case 1:           *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; }       \
00257   }                                                                           \
00258 } while(0)
00259 
00260 
00261 /*---------------------------------------------------------------------------
00262 */
00263 __declspec(dllexport) void * HOARD_PREFIX(calloc)(size_t nelem, size_t elsize)
00264 {
00265         processHeap * pHeap = getAllocator();
00266         size_t sz = nelem * elsize;
00267         if (sz == 0) {
00268                 sz = 1;
00269         }
00270         void * ptr = pHeap->getHeap(pHeap->getHeapIndex()).malloc(sz);
00271         MALLOC_ZERO(ptr, sz);
00272 //      memset(ptr, 0, nelem * elsize); // zero out the block (as per calloc)
00273         return ptr;
00274 }
00275 
00276 /*---------------------------------------------------------------------------
00277 */
00278 __declspec(dllexport) void HOARD_PREFIX(free)(void * ptr)
00279 {
00280   static processHeap * pHeap = getAllocator();
00281   pHeap->free(ptr);
00282 }
00283 
00284 /*---------------------------------------------------------------------------
00285 */
00286 __declspec(dllexport) void * HOARD_PREFIX(malloc)(size_t sz)
00287 {
00288   static processHeap * pHeap = getAllocator();
00289   void * addr = pHeap->getHeap(pHeap->getHeapIndex()).malloc(sz);
00290   return addr;
00291 }
00292 
00293 /*---------------------------------------------------------------------------
00294 */
00295 __declspec(dllexport) void * HOARD_PREFIX(realloc)(void * ptr, size_t sz)
00296 {
00297         if (!ptr)
00298                 return HOARD_PREFIX(malloc)(sz);
00299 
00300         if (!sz)
00301         {
00302                 HOARD_PREFIX(free)(ptr);
00303                 return 0;
00304         }
00305 
00306         // If the existing object can hold the new size,
00307         // just return it.
00308 
00309         size_t objSize = threadHeap::objectSize (ptr);
00310 
00311         if (objSize >= sz) {
00312                 return ptr;
00313         }
00314 
00315 
00316         // Allocate a new block of size sz.
00317         void * buf = HOARD_PREFIX(malloc)(sz);
00318 
00319         // Copy the contents of the original object
00320         // up to the size of the new block.
00321         size_t minSize = objSize < sz? objSize: sz;
00322         MALLOC_COPY(buf, ptr, minSize);
00323 //      memcpy(buf, ptr, minSize);
00324 
00325         // Free the old block.
00326         HOARD_PREFIX(free)(ptr);
00327 
00328         // Return a pointer to the new one.
00329         return buf;
00330 }
00331 
00332 __declspec(dllexport) void * HOARD_PREFIX(_nh_malloc)(size_t sz, size_t, int, int, const char *, int) {
00333         return HOARD_PREFIX(malloc) (sz);
00334 }
00335 
00336 }

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