Created
May 15, 2023 12:03
-
-
Save Youlean/5ea7fce073b4c342117ef4334547b017 to your computer and use it in GitHub Desktop.
Check allocations with new override
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <thread> | |
#ifdef _DEBUG | |
static std::thread::id YOULEAN_ALLOCATOR_MAIN_THREAD_ID = std::thread::id(); | |
static std::thread::id YOULEAN_ALLOCATOR_AUDIO_THREAD_ID = std::thread::id(); | |
static void YouleanAllocatorCheckUpdateMainThreadID() | |
{ | |
YOULEAN_ALLOCATOR_MAIN_THREAD_ID = std::this_thread::get_id(); | |
} | |
static void YouleanAllocatorCheckUpdateAudioThreadID() | |
{ | |
YOULEAN_ALLOCATOR_AUDIO_THREAD_ID = std::this_thread::get_id(); | |
} | |
void* operator new(std::size_t s) | |
{ | |
if (YOULEAN_ALLOCATOR_MAIN_THREAD_ID == std::this_thread::get_id()) | |
{ | |
// Allocated from main thread | |
std::cout << "Allocated from main thread" << std::endl; | |
} | |
else if (YOULEAN_ALLOCATOR_AUDIO_THREAD_ID == std::this_thread::get_id()) | |
{ | |
// Allocated from audio thread | |
std::cout << "Allocated from audio thread" << std::endl; | |
} | |
else | |
{ | |
// Allocated from unknown thread | |
std::cout << "Allocated from unknown thread" << std::endl; | |
} | |
const void* memory = std::malloc(s); | |
if (!memory) | |
{ | |
throw std::bad_alloc(); | |
} | |
} | |
void operator delete(void* p) | |
{ | |
free(p); | |
} | |
#else | |
static void YouleanAllocatorCheckUpdateMainThreadID() {} | |
static void YouleanAllocatorCheckUpdateAudioThreadID() {} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment