// Copyright 2020 TooYoungTooSimp <lyc@xuming.studio>, All rights reserved.
#include "Utilities.h"
spin_lock MemoryMonitor::lock;
int64_t MemoryMonitor::global_counter = 0;
std::map<MemoryMonitor *, int64_t *> MemoryMonitor::M;
int MemoryMonitor::__crt_alloc_hook(int allocType, void *ptr, size_t size,
                                    int blockType, long requestNumber,
                                    const unsigned char *filename, int lineNumber)
{
    if (blockType == _CRT_BLOCK) return TRUE;
    if (allocType == _HOOK_ALLOC)
        global_counter += size;
    if (allocType == _HOOK_REALLOC)
        global_counter += size - __get_block_size(ptr);
    if (allocType == _HOOK_FREE)
        global_counter -= __get_block_size(ptr);
    for (auto p : M) *p.second = std::max<int64_t>(*p.second, global_counter);
    return TRUE;
}