Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.1 KiB

  1. #include "intelglmallocworkaround.h"
  2. #include "mach_override.h"
  3. // memdbgon -must- be the last include file in a .cpp file.
  4. #include "tier0/memdbgon.h"
  5. IntelGLMallocWorkaround* IntelGLMallocWorkaround::s_pWorkaround = NULL;
  6. void *IntelGLMallocWorkaround::ZeroingAlloc(size_t size)
  7. {
  8. // We call into this pointer that resumes the original malloc.
  9. void *memory = s_pWorkaround->m_pfnMallocReentry(size);
  10. if (size < 96)
  11. {
  12. // Since the Intel driver has an issue with a small allocation
  13. // that's left uninitialized, we use memset to ensure it's zero-initialized.
  14. memset(memory, 0, size);
  15. }
  16. return memory;
  17. }
  18. IntelGLMallocWorkaround* IntelGLMallocWorkaround::Get()
  19. {
  20. if (!s_pWorkaround)
  21. {
  22. s_pWorkaround = new IntelGLMallocWorkaround();
  23. }
  24. return s_pWorkaround;
  25. }
  26. bool IntelGLMallocWorkaround::Enable()
  27. {
  28. if ( m_pfnMallocReentry != NULL )
  29. {
  30. return true;
  31. }
  32. mach_error_t error = mach_override_ptr( (void*)&malloc, (const void*)&ZeroingAlloc, (void**)&m_pfnMallocReentry );
  33. if ( error == err_cannot_override )
  34. {
  35. m_pfnMallocReentry = NULL;
  36. return false;
  37. }
  38. return true;
  39. }