DictionaryStats* DictionaryStats::Create(const char* name, uint bucketCount)
{
    if (!Js::Configuration::Global.flags.IsEnabled(Js::ProfileDictionaryFlag) ||
        Js::Configuration::Global.flags.ProfileDictionary < 0)
        return NULL;

    return NoCheckHeapNew(DictionaryStats, name, bucketCount);
}
DictionaryStats::DictionaryStats(const char* name, uint bucketCount)
    :
    initialSize(bucketCount),
    finalSize(bucketCount),
    countOfEmptyBuckets(bucketCount),
    countOfResize(0),
    itemCount(0),
    maxDepth(0),
    lookupCount(0),
    collisionCount(0),
    lookupDepthTotal(0),
    maxLookupDepth(0),
    pNext(NULL),
    pName(NULL)
{
    if(dictionaryTypes == NULL)
    {
        InitializeCriticalSection(&DictionaryStats::dictionaryTypesCriticalSection);
    }
    EnterCriticalSection(&DictionaryStats::dictionaryTypesCriticalSection);
    DictionaryType* type = NULL;
    // See if we already created instance(s) of this type
    DictionaryType* current = dictionaryTypes;
    while(current)
    {
        if (strncmp(name, current->name, _countof(current->name)-1) == 0)
        {
            type = current;
            break;
        }
        current = current->pNext;
    }

    if (!type)
    {
        // We haven't seen this type before so add a new entry for it
        type = NoCheckHeapNew(DictionaryType);
        type->pNext = dictionaryTypes;
        dictionaryTypes = type;
        type->instancesCount = 0;
        strncpy_s(type->name, name, _countof(type->name)-1);
        type->name[sizeof(type->name)-1]='\0';
    }
    LeaveCriticalSection(&dictionaryTypesCriticalSection);
    // keep a pointer to the name in case we are asked to clone ourselves
    pName = type->name;

    // Add ourself in the list
    pNext = type->instances;
    type->instances = this;
    ++(type->instancesCount);
}
DictionaryStats* DictionaryStats::Clone()
{
    DictionaryStats* cloned = NoCheckHeapNew(DictionaryStats, pName, initialSize);
    cloned->finalSize = finalSize;
    cloned->countOfEmptyBuckets = countOfEmptyBuckets;
    cloned->countOfResize = countOfResize;
    cloned->itemCount = itemCount;
    cloned->maxDepth = maxDepth;
    cloned->lookupCount = lookupCount;
    cloned->collisionCount = collisionCount;
    cloned->lookupDepthTotal = lookupDepthTotal;
    cloned->maxLookupDepth = maxLookupDepth;
    cloned->pName = pName;
    return cloned;
}
MemoryProfiler *
MemoryProfiler::EnsureMemoryProfiler()
{
    MemoryProfiler * memoryProfiler = MemoryProfiler::Instance;

    if (memoryProfiler == nullptr)
    {
        memoryProfiler = NoCheckHeapNew(MemoryProfiler);

        {
            AutoCriticalSection autocs(&s_cs);
            memoryProfiler->next = MemoryProfiler::profilers.Detach();
            MemoryProfiler::profilers = memoryProfiler;
        }

        MemoryProfiler::Instance = memoryProfiler;
    }
    return memoryProfiler;
}