Pinned.h 1.2 KB

12345678910111213141516171819202122232425262728
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. // Only need to be used if the pointer is long live within the function and doesn't escape
  7. // NOTE: Currently, there is no way to tell the compiler a local variable need to be on the stack
  8. // and last for a certain life time. But if we take the address of the local and pass it to a non-LTCG
  9. // function the assignment won't be dead stored and the local will not be stack packed away.
  10. // IMPORTANT!!! LeavePinnedScope MUST be called at the end of the desired lifetime of a local
  11. // that you allocate using LEAVE_PINNED_SCOPE().
  12. void EnterPinnedScope(volatile void** var);
  13. void LeavePinnedScope();
  14. // These MACROs enforce scoping so LEAVE must be called for each instance of ENTER
  15. #define ENTER_PINNED_SCOPE(T, var) \
  16. T * var; \
  17. EnterPinnedScope((volatile void**)& ## var); \
  18. {
  19. #define LEAVE_PINNED_SCOPE() \
  20. LeavePinnedScope(); \
  21. }