Team Fortress 2 Source Code as on 22/4/2020
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.

70 lines
2.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. // TOGL CODE LICENSE
  3. //
  4. // Copyright 2011-2014 Valve Corporation
  5. // All Rights Reserved.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #include "intelglmallocworkaround.h"
  25. #include "mach_override.h"
  26. // memdbgon -must- be the last include file in a .cpp file.
  27. #include "tier0/memdbgon.h"
  28. IntelGLMallocWorkaround* IntelGLMallocWorkaround::s_pWorkaround = NULL;
  29. void *IntelGLMallocWorkaround::ZeroingAlloc(size_t size)
  30. {
  31. // We call into this pointer that resumes the original malloc.
  32. void *memory = s_pWorkaround->m_pfnMallocReentry(size);
  33. if (size < 96)
  34. {
  35. // Since the Intel driver has an issue with a small allocation
  36. // that's left uninitialized, we use memset to ensure it's zero-initialized.
  37. memset(memory, 0, size);
  38. }
  39. return memory;
  40. }
  41. IntelGLMallocWorkaround* IntelGLMallocWorkaround::Get()
  42. {
  43. if (!s_pWorkaround)
  44. {
  45. s_pWorkaround = new IntelGLMallocWorkaround();
  46. }
  47. return s_pWorkaround;
  48. }
  49. bool IntelGLMallocWorkaround::Enable()
  50. {
  51. if ( m_pfnMallocReentry != NULL )
  52. {
  53. return true;
  54. }
  55. mach_error_t error = mach_override_ptr( (void*)&malloc, (const void*)&ZeroingAlloc, (void**)&m_pfnMallocReentry );
  56. if ( error == err_cannot_override )
  57. {
  58. m_pfnMallocReentry = NULL;
  59. return false;
  60. }
  61. return true;
  62. }