BOOL WINAPI CUSTOM_DLLNAME (HANDLE hinstDLL, DWORD fdwReason, LPVOID lpreserved) { int i; int tid; static int np = HL::CPUInfo::computeNumProcessors(); switch (fdwReason) { case DLL_PROCESS_ATTACH: { LocalTLABIndex = TlsAlloc(); if (LocalTLABIndex == TLS_OUT_OF_INDEXES) { // Not sure what to do here! } HOARD_PRE_ACTION; getCustomHeap(); } break; case DLL_THREAD_ATTACH: if (np == 1) { // We have exactly one processor - just assign the thread to // heap 0. getMainHoardHeap()->chooseZero(); } else { getMainHoardHeap()->findUnusedHeap(); } getCustomHeap(); break; case DLL_THREAD_DETACH: { // Dump the memory from the TLAB. getCustomHeap()->clear(); TheCustomHeapType *heap = (TheCustomHeapType *) TlsGetValue(LocalTLABIndex); if (np != 1) { // If we're on a multiprocessor box, relinquish the heap // assigned to this thread. getMainHoardHeap()->releaseHeap(); } if (heap != 0) { TlsSetValue (LocalTLABIndex, 0); } } break; case DLL_PROCESS_DETACH: HOARD_POST_ACTION; break; default: return TRUE; } return TRUE; }
extern "C" int thr_create (void * stack_base, size_t stack_size, void * (*start_routine)(void *), void * arg, long flags, thread_t * new_tid) { // Force initialization of the TLAB before our first thread is created. static volatile TheCustomHeapType * t = getCustomHeap(); t = t; char fname[] = "_thr_create"; // Instantiate the pointer to thr_create, if it hasn't been // instantiated yet. // A pointer to the library version of thr_create. static thr_create_function real_thr_create = (thr_create_function) dlsym (RTLD_NEXT, fname); anyThreadCreated = true; typedef pair<threadFunctionType, void *> argsType; argsType * args = new (getCustomHeap()->malloc(sizeof(argsType))) argsType (start_routine, arg); int result = (*real_thr_create)(stack_base, stack_size, startMeUp, args, flags, new_tid); return result; }
extern "C" size_t MYCDECL CUSTOM_GETSIZE (void * ptr) { if (ptr == NULL) { return 0; } return getCustomHeap()->getSize(ptr); }
// A special routine we call on thread exits to free up some resources. static void exitRoutine (void) { // Clear the TLAB's buffer. getCustomHeap()->clear(); // Relinquish the assigned heap. getMainHoardHeap()->releaseHeap(); }
extern "C" int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine)(void *), void * arg) #if !defined(__SUNPRO_CC) && !defined(__APPLE__) && !defined(__FreeBSD__) throw () #endif { // Force initialization of the TLAB before our first thread is created. static volatile TheCustomHeapType * t = getCustomHeap(); t = t; #if defined(__linux__) || defined(__APPLE__) char fname[] = "pthread_create"; #else char fname[] = "_pthread_create"; #endif // A pointer to the library version of pthread_create. static pthread_create_function real_pthread_create = reinterpret_cast<pthread_create_function> (reinterpret_cast<intptr_t>(dlsym(RTLD_NEXT, fname))); anyThreadCreated = true; pair<threadFunctionType, void *> * args = // new (_heap.malloc(sizeof(pair<threadFunctionType, void*>))) new pair<threadFunctionType, void *> (start_routine, arg); int result = (*real_pthread_create)(thread, attr, startMeUp, args); return result; }
extern "C" size_t MYCDECL CUSTOM_GETSIZE (void * ptr) { TheCustomHeapType * theCustomHeap = getCustomHeap(); if (ptr == NULL) { return 0; } return theCustomHeap->getSize(ptr); }
void hoard_dllProcessAttach() { LocalTLABIndex = TlsAlloc(); if (LocalTLABIndex == TLS_OUT_OF_INDEXES) { // Not sure what to do here! } getCustomHeap(); }
// A special routine we call on thread exit to free up some resources. static void exitRoutine() { TheCustomHeapType * heap = getCustomHeap(); // Clear the TLAB's buffer. heap->clear(); // Relinquish the assigned heap. getMainHoardHeap()->releaseHeap(); }
extern "C" void * MYCDECL CUSTOM_MALLOC (size_t sz) { void * ptr = getCustomHeap()->malloc (sz); #if !DIEHARD_DIEFAST if (ptr) { memset (ptr, '\0', sz); } #endif return ptr; }
void hoard_dllThreadAttach() { if (s_numberOfProcessors == 1) { // We have exactly one processor - just assign the thread to // heap 0. getMainHoardHeap()->chooseZero(); } else { getMainHoardHeap()->findUnusedHeap(); } getCustomHeap(); }
extern "C" void * MYCDECL CUSTOM_CALLOC (size_t nelem, size_t elsize) { size_t n = nelem * elsize; void * ptr = getCustomHeap()->malloc (n); // In non-replicated mode, it's already initialized to zero. if (ptr != NULL) { memset (ptr, 0, n); } return ptr; }
extern "C" void * MYCDECL CUSTOM_CALLOC (size_t nelem, size_t elsize) { size_t n = nelem * elsize; if (n == 0) { n = 1; } void * ptr = getCustomHeap()->malloc (n); if (ptr) { memset (ptr, '\0', n); } return ptr; }
// A special routine we call on thread exit to free up some resources. static void exitRoutine() { TheCustomHeapType * heap = getCustomHeap(); // Relinquish the assigned heap. getMainHoardHeap()->releaseHeap(); // Clear the heap (via its destructor). heap->~TheCustomHeapType(); #if !defined(USE_THREAD_KEYWORD) // Reclaim the memory associated with the heap (thread-specific data). pthread_key_delete (theHeapKey); #endif }
static inline void * startMeUp(void * a) { getCustomHeap(); getMainHoardHeap()->findUnusedHeap(); pair<threadFunctionType, void *> * z = (pair<threadFunctionType, void *> *) a; threadFunctionType f = z->first; void * arg = z->second; void * result = NULL; result = (*f)(arg); exitRoutine(); return result; }
void hoard_dllThreadDetach() { // Dump the memory from the TLAB. getCustomHeap()->clear(); TheCustomHeapType *heap = (TheCustomHeapType *) TlsGetValue(LocalTLABIndex); if (s_numberOfProcessors != 1) { // If we're on a multiprocessor box, relinquish the heap // assigned to this thread. getMainHoardHeap()->releaseHeap(); } if (heap != 0) { TlsSetValue (LocalTLABIndex, 0); } }
void dumpHeapHandler(int signum, siginfo_t * act, void * foo) { fprintf(stderr,"libdiehard caught signal (%d) handler, pid = %d\n",signum,getpid()); getCustomHeap()->dump_heap(signum != SIGUSR1 && signum != SIGUSR2); if(signum != SIGUSR1) { fprintf(stderr,"libdiehard raising %d on pid %d\n",signum,getpid()); struct sigaction sigfoo; sigfoo.sa_handler = SIG_DFL; sigaction(signum, &sigfoo, NULL); raise(signum); exit(4); } else { fprintf(stderr,"finished dumping core\n"); kill(act->si_pid,SIGUSR1); } }
extern "C" int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine) (void *), void * arg) #if !defined(__SUNPRO_CC) throw () #endif { // A pointer to the library version of pthread_create. static pthread_create_function real_pthread_create = NULL; // Force initialization of the TLAB before our first thread is created. volatile static TheCustomHeapType * t = getCustomHeap(); #if defined(linux) char fname[] = "pthread_create"; #else char fname[] = "_pthread_create"; #endif // Instantiate the pointer to pthread_create, if it hasn't been // instantiated yet. if (real_pthread_create == NULL) { real_pthread_create = (pthread_create_function) dlsym (RTLD_NEXT, fname); if (real_pthread_create == NULL) { fprintf (stderr, "Could not find the pthread_create function!\n"); fprintf (stderr, "Please report this problem to [email protected].\n"); abort(); } } anyThreadCreated = 1; pair<threadFunctionType, void *> * args = new pair<threadFunctionType, void *> (start_routine, arg); int result = (*real_pthread_create)(thread, attr, startMeUp, args); return result; }
extern "C" void MYCDECL CUSTOM_FREE (void * ptr) { TheCustomHeapType * theCustomHeap = getCustomHeap(); theCustomHeap->free (ptr); }
static inline void * internalMalloc (size_t sz) { TheCustomHeapType * theCustomHeap = getCustomHeap(); void * ptr = theCustomHeap->malloc (sz); return ptr; }
extern "C" void * MYCDECL CUSTOM_MALLOC (size_t sz) { TheCustomHeapType * theCustomHeap = getCustomHeap(); void * ptr = theCustomHeap->malloc (sz); return ptr; }
extern "C" void MYCDECL CUSTOM_FREE (void * ptr) { getCustomHeap()->free (ptr); }
inline void __free_impl(void * ptr) { getCustomHeap()->free(ptr); }
inline void * __malloc_impl(size_t sz) { void * ptr = getCustomHeap()->malloc (sz); return ptr; }
extern "C" void MYCDECL CUSTOM_PREFIX(cfree) (void * ptr) { getCustomHeap()->free (ptr); }