Skip to content

Commit

Permalink
Fixed Windows issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
emeryberger committed Jan 24, 2019
1 parent de06b8b commit cc8bf6c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/include/hoard/hoardheap.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ using namespace HL;

// The minimum allocation grain for a given object -
// that is, we carve objects out of chunks of this size.
//#define SUPERBLOCK_SIZE 65536


#if defined(_WIN32)
// Larger superblock sizes are not yet working for Windows for some reason to be determined.
#define SUPERBLOCK_SIZE 65536
#else
#define SUPERBLOCK_SIZE (1UL << 21)
#endif

//#define SUPERBLOCK_SIZE (256*1048576)
//#define SUPERBLOCK_SIZE (512*1048576)
Expand Down
58 changes: 42 additions & 16 deletions src/source/wintls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ extern HoardHeapType * getMainHoardHeap();
static TheCustomHeapType * initializeCustomHeap()
{
// Allocate a per-thread heap.
TheCustomHeapType * heap;
void * mh = getMainHoardHeap()->malloc(sizeof(TheCustomHeapType));
heap = new (mh) TheCustomHeapType (getMainHoardHeap());
auto * mainHeap = getMainHoardHeap();
auto * customHeapBuf = mainHeap->malloc(sizeof(TheCustomHeapType));
auto * perThreadHeap = new (customHeapBuf) TheCustomHeapType (mainHeap);

// Store it in the appropriate thread-local area.
#if USE_DECLSPEC_THREADLOCAL
threadLocalHeap = heap;
threadLocalHeap = perThreadHeap;
#else
TlsSetValue (LocalTLABIndex, heap);
TlsSetValue (LocalTLABIndex, perThreadHeap);
#endif

return heap;
return perThreadHeap;
}

bool isCustomHeapInitialized() {
Expand Down Expand Up @@ -108,32 +108,58 @@ extern "C" void FinalizeWinWrapper();
// Intercept thread creation and destruction to flush the TLABs.
//

#include <iostream>
using namespace std;

// example usage: debugMessage(L"Hello, world.\n");
void debugMessage(const wchar_t * lpBuff) {
static bool initialized = false;
if (!initialized) {
AllocConsole();
}
DWORD dwSize = 0;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), lpBuff, lstrlen(lpBuff), &dwSize, NULL);
}

extern "C" void _CRT_INIT();

extern "C" {

BOOL APIENTRY DllMain (HANDLE hinstDLL,
DWORD fdwReason,
LPVOID lpreserved)
{
static int np = HL::CPUInfo::computeNumProcessors();
static auto np = HL::CPUInfo::computeNumProcessors();
switch (fdwReason) {

case DLL_PROCESS_ATTACH:
{
/* fprintf (stderr, versionMessage); */
// Before we do anything, force initialization of the C++
// library. Without this pre-initialization, the Windows heap
// and the Hoard heaps get mixed up, and then nothing
// works. This is quite the hack but seems to do the trick.
// -- Emery Berger, 24/1/2019
cout << "";

// Now we are good to go.
InitializeWinWrapper();
getCustomHeap();
// Force creation of the heap.
volatile auto * ch = 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();
{
if (np == 1) {
// We have exactly one processor - just assign the thread to
// heap 0.
getMainHoardHeap()->chooseZero();
} else {
getMainHoardHeap()->findUnusedHeap();
}
// Force creation of the heap.
volatile auto * ch = getCustomHeap();
}
getCustomHeap();
break;

case DLL_THREAD_DETACH:
Expand Down

0 comments on commit cc8bf6c

Please sign in to comment.