00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #define HOARD_PREFIX(x) x
00020
00021
00022 #include "config.h"
00023
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
00050
00051 static processHeap theAllocator;
00052
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
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
00077
00078 }
00079
00080
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
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
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
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
00307
00308
00309 size_t objSize = threadHeap::objectSize (ptr);
00310
00311 if (objSize >= sz) {
00312 return ptr;
00313 }
00314
00315
00316
00317 void * buf = HOARD_PREFIX(malloc)(sz);
00318
00319
00320
00321 size_t minSize = objSize < sz? objSize: sz;
00322 MALLOC_COPY(buf, ptr, minSize);
00323
00324
00325
00326 HOARD_PREFIX(free)(ptr);
00327
00328
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 }