FLEXObjcInternal.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // FLEXObjcInternal.h
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 11/1/18.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. // The macros below are copied straight from
  12. // objc-internal.h, objc-private.h, objc-object.h, and objc-config.h with
  13. // as few modifications as possible. Changes are noted in boxed comments.
  14. // https://opensource.apple.com/source/objc4/objc4-723/
  15. // https://opensource.apple.com/source/objc4/objc4-723/runtime/objc-internal.h.auto.html
  16. // https://opensource.apple.com/source/objc4/objc4-723/runtime/objc-object.h.auto.html
  17. /////////////////////
  18. // objc-internal.h //
  19. /////////////////////
  20. #if __LP64__
  21. #define OBJC_HAVE_TAGGED_POINTERS 1
  22. #endif
  23. #if OBJC_HAVE_TAGGED_POINTERS
  24. #if TARGET_OS_OSX && __x86_64__
  25. // 64-bit Mac - tag bit is LSB
  26. # define OBJC_MSB_TAGGED_POINTERS 0
  27. #else
  28. // Everything else - tag bit is MSB
  29. # define OBJC_MSB_TAGGED_POINTERS 1
  30. #endif
  31. #if OBJC_MSB_TAGGED_POINTERS
  32. # define _OBJC_TAG_MASK (1UL<<63)
  33. # define _OBJC_TAG_EXT_MASK (0xfUL<<60)
  34. #else
  35. # define _OBJC_TAG_MASK 1UL
  36. # define _OBJC_TAG_EXT_MASK 0xfUL
  37. #endif
  38. #endif // OBJC_HAVE_TAGGED_POINTERS
  39. //////////////////////////////////////
  40. // originally _objc_isTaggedPointer //
  41. //////////////////////////////////////
  42. NS_INLINE BOOL flex_isTaggedPointer(const void *ptr) {
  43. #if OBJC_HAVE_TAGGED_POINTERS
  44. return ((uintptr_t)ptr & _OBJC_TAG_MASK) == _OBJC_TAG_MASK;
  45. #else
  46. return NO;
  47. #endif
  48. }
  49. #define FLEXPointerIsTaggedPointer(obj) flex_isTaggedPointer((__bridge void *)obj)
  50. BOOL FLEXPointerIsReadable(const void * ptr);
  51. /// @brief Assumes memory is valid and readable.
  52. /// @discussion objc-internal.h, objc-private.h, and objc-config.h
  53. /// https://blog.timac.org/2016/1124-testing-if-an-arbitrary-pointer-is-a-valid-objective-c-object/
  54. /// https://llvm.org/svn/llvm-project/lldb/trunk/examples/summaries/cocoa/objc_runtime.py
  55. /// https://blog.timac.org/2016/1124-testing-if-an-arbitrary-pointer-is-a-valid-objective-c-object/
  56. BOOL FLEXPointerIsValidObjcObject(const void * ptr);
  57. #ifdef __cplusplus
  58. }
  59. #endif