Skip to content

Commit 5e5ee1c

Browse files
committed
Create a DISPATCH_PTR_SIZE macro and use it
This introduces a new macro, `DISPATCH_PTR_SIZE` which is defined much like `LLVM_PTR_SIZE`. We can gracefully fallback to alternate means of checking the width of the pointer and use this rather than `__LP64__` to determine if we are on a 64-bit host.
1 parent 14d3d90 commit 5e5ee1c

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

dispatch/base.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,20 @@
254254

255255
typedef void (*dispatch_function_t)(void *_Nullable);
256256

257+
#ifdef __SIZEOF_POINTER__
258+
#define DISPATCH_SIZEOF_PTR __SIZEOF_POINTER__
259+
#elif defined(_WIN64)
260+
#define DISPATCH_SIZEOF_PTR 8
261+
#elif defined(_WIN32)
262+
#define DISPATCH_SIZEOF_PTR 4
263+
#elif defined(_MSC_VER)
264+
#error "could not determine pointer size as a constant int for MSVC"
265+
#elif defined(__LP64__) || defined(__LLP64__)
266+
#define DISPATCH_SIZEOF_PTR 8
267+
#elif defined(__ILP32__)
268+
#define DISPATCH_SIZEOF_PTR 4
269+
#else
270+
#error "could not determine pointer size as a constant int"
271+
#endif // __SIZEOF_POINTER__
272+
257273
#endif

src/event/event_kevent.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ _dispatch_mach_notify_port_init(void *context DISPATCH_UNUSED)
15571557
kern_return_t kr;
15581558
#if HAVE_MACH_PORT_CONSTRUCT
15591559
mach_port_options_t opts = { .flags = MPO_CONTEXT_AS_GUARD | MPO_STRICT };
1560-
#ifdef __LP64__
1560+
#if DISPATCH_SIZEOF_PTR == 8
15611561
const mach_port_context_t guard = 0xfeed09071f1ca7edull;
15621562
#else
15631563
const mach_port_context_t guard = 0xff1ca7edull;

src/object_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT
226226
#endif
227227

228-
#ifdef __LP64__
228+
#if DISPATCH_SIZEOF_PTR == 8
229229
// the bottom nibble must not be zero, the rest of the bits should be random
230230
// we sign extend the 64-bit version so that a better instruction encoding is
231231
// generated on Intel

src/queue_internal.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,6 @@ dispatch_queue_attr_t _dispatch_get_default_queue_attr(void);
748748
void *dc_ctxt; \
749749
void *dc_data; \
750750
void *dc_other
751-
#define _DISPATCH_SIZEOF_PTR 8
752751
#elif OS_OBJECT_HAVE_OBJC1
753752
#define DISPATCH_CONTINUATION_HEADER(x) \
754753
dispatch_function_t dc_func; \
@@ -766,7 +765,6 @@ dispatch_queue_attr_t _dispatch_get_default_queue_attr(void);
766765
void *dc_ctxt; \
767766
void *dc_data; \
768767
void *dc_other
769-
#define _DISPATCH_SIZEOF_PTR 4
770768
#else
771769
#define DISPATCH_CONTINUATION_HEADER(x) \
772770
union { \
@@ -784,17 +782,16 @@ dispatch_queue_attr_t _dispatch_get_default_queue_attr(void);
784782
void *dc_ctxt; \
785783
void *dc_data; \
786784
void *dc_other
787-
#define _DISPATCH_SIZEOF_PTR 4
788785
#endif
789786
#define _DISPATCH_CONTINUATION_PTRS 8
790787
#if DISPATCH_HW_CONFIG_UP
791788
// UP devices don't contend on continuations so we don't need to force them to
792789
// occupy a whole cacheline (which is intended to avoid contention)
793790
#define DISPATCH_CONTINUATION_SIZE \
794-
(_DISPATCH_CONTINUATION_PTRS * _DISPATCH_SIZEOF_PTR)
791+
(_DISPATCH_CONTINUATION_PTRS * DISPATCH_SIZEOF_PTR)
795792
#else
796793
#define DISPATCH_CONTINUATION_SIZE ROUND_UP_TO_CACHELINE_SIZE( \
797-
(_DISPATCH_CONTINUATION_PTRS * _DISPATCH_SIZEOF_PTR))
794+
(_DISPATCH_CONTINUATION_PTRS * DISPATCH_SIZEOF_PTR))
798795
#endif
799796
#define ROUND_UP_TO_CONTINUATION_SIZE(x) \
800797
(((x) + (DISPATCH_CONTINUATION_SIZE - 1u)) & \

tests/dispatch_queue_finalizer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ main(void)
5050
{
5151
dispatch_test_start("Dispatch Queue Finalizer");
5252

53-
#ifdef __LP64__
53+
#if DISPATCH_SIZEOF_PTR == 8
5454
ctxt_magic = (void*)((uintptr_t)arc4random() << 32 | arc4random());
5555
#else
5656
ctxt_magic = (void*)arc4random();

0 commit comments

Comments
 (0)