From b0263ae4a8c17c91d1788ebad15115bc5180b29e Mon Sep 17 00:00:00 2001 From: Fred Kiefer Date: Fri, 18 Aug 2023 17:29:43 +0200 Subject: [PATCH 1/2] Try to address issue of treating pointer function values as bitmap. --- Source/NSConcreteHashTable.m | 2 +- Source/NSConcretePointerFunctions.h | 5 ++++ Source/NSConcretePointerFunctions.m | 42 +++++++++++++---------------- Source/NSPointerArray.m | 4 +-- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Source/NSConcreteHashTable.m b/Source/NSConcreteHashTable.m index 0d3a108728..08b735be58 100644 --- a/Source/NSConcreteHashTable.m +++ b/Source/NSConcreteHashTable.m @@ -77,7 +77,7 @@ @interface NSConcreteHashTable : NSHashTable #define GSI_MAP_TABLE_S instanceSize #define IS_WEAK(M) \ - M->cb.pf.options & (NSPointerFunctionsZeroingWeakMemory | NSPointerFunctionsWeakMemory) + memoryType(M->cb.pf.options, NSPointerFunctionsZeroingWeakMemory) | memoryType(M->cb.pf.options, NSPointerFunctionsWeakMemory) #define GSI_MAP_HASH(M, X)\ (M->legacy ? M->cb.old.hash(M, X.ptr) \ : pointerFunctionsHash(&M->cb.pf, X.ptr)) diff --git a/Source/NSConcretePointerFunctions.h b/Source/NSConcretePointerFunctions.h index c3a5f0d99a..96b0a70012 100644 --- a/Source/NSConcretePointerFunctions.h +++ b/Source/NSConcretePointerFunctions.h @@ -81,6 +81,11 @@ inline static BOOL memoryType(int options, int flag) return (options & 0xff) == flag; } +inline static BOOL personalityType(int options, int flag) +{ + return (options & 0xff00) == flag; +} + /* Declare the concrete pointer functions class as a wrapper around * an instance of the PFInfo structure. */ diff --git a/Source/NSConcretePointerFunctions.m b/Source/NSConcretePointerFunctions.m index b2c28d9303..6304f9e550 100644 --- a/Source/NSConcretePointerFunctions.m +++ b/Source/NSConcretePointerFunctions.m @@ -191,19 +191,19 @@ - (id) initWithOptions: (NSPointerFunctionsOptions)options * should be used to relinquish contents of a container with these * options. */ - if (options & NSPointerFunctionsZeroingWeakMemory) + if (memoryType(options, NSPointerFunctionsZeroingWeakMemory)) { _x.relinquishFunction = 0; } - else if (options & NSPointerFunctionsOpaqueMemory) + else if (memoryType(options, NSPointerFunctionsOpaqueMemory)) { _x.relinquishFunction = 0; } - else if (options & NSPointerFunctionsMallocMemory) + else if (memoryType(options, NSPointerFunctionsMallocMemory)) { _x.relinquishFunction = relinquishMallocMemory; } - else if (options & NSPointerFunctionsMachVirtualMemory) + else if (memoryType(options, NSPointerFunctionsMachVirtualMemory)) { _x.relinquishFunction = relinquishMallocMemory; } @@ -214,16 +214,16 @@ - (id) initWithOptions: (NSPointerFunctionsOptions)options /* Now we look at the personality options to determine other functions. */ - if (options & NSPointerFunctionsOpaquePersonality) + if (personalityType(options, NSPointerFunctionsOpaquePersonality)) { _x.acquireFunction = acquireExistingMemory; _x.descriptionFunction = describePointer; _x.hashFunction = hashShifted; _x.isEqualFunction = equalDirect; } - else if (options & NSPointerFunctionsObjectPointerPersonality) + else if (personalityType(options, NSPointerFunctionsObjectPointerPersonality)) { - if (options & NSPointerFunctionsZeroingWeakMemory) + if (memoryType(options, NSPointerFunctionsZeroingWeakMemory)) { _x.acquireFunction = acquireExistingMemory; } @@ -235,21 +235,21 @@ - (id) initWithOptions: (NSPointerFunctionsOptions)options _x.hashFunction = hashShifted; _x.isEqualFunction = equalDirect; } - else if (options & NSPointerFunctionsCStringPersonality) + else if (personalityType(options, NSPointerFunctionsCStringPersonality)) { _x.acquireFunction = acquireMallocMemory; _x.descriptionFunction = describeString; _x.hashFunction = hashString; _x.isEqualFunction = equalString; } - else if (options & NSPointerFunctionsStructPersonality) + else if (personalityType(options, NSPointerFunctionsStructPersonality)) { _x.acquireFunction = acquireMallocMemory; _x.descriptionFunction = describePointer; _x.hashFunction = hashMemory; _x.isEqualFunction = equalMemory; } - else if (options & NSPointerFunctionsIntegerPersonality) + else if (personalityType(options, NSPointerFunctionsIntegerPersonality)) { _x.acquireFunction = acquireExistingMemory; _x.descriptionFunction = describeInteger; @@ -258,7 +258,7 @@ - (id) initWithOptions: (NSPointerFunctionsOptions)options } else /* objects */ { - if (options & NSPointerFunctionsZeroingWeakMemory) + if (memoryType(options, NSPointerFunctionsZeroingWeakMemory)) { _x.acquireFunction = acquireExistingMemory; } @@ -344,16 +344,18 @@ - (void) setUsesStrongWriteBarrier: (BOOL)flag ~(NSPointerFunctionsZeroingWeakMemory |NSPointerFunctionsOpaqueMemory |NSPointerFunctionsMallocMemory - |NSPointerFunctionsMachVirtualMemory); + |NSPointerFunctionsMachVirtualMemory + |NSPointerFunctionsWeakMemory); } - (void) setUsesWeakReadAndWriteBarriers: (BOOL)flag { - _x.options |= NSPointerFunctionsZeroingWeakMemory; _x.options &= ~(NSPointerFunctionsOpaqueMemory |NSPointerFunctionsMallocMemory - |NSPointerFunctionsMachVirtualMemory); + |NSPointerFunctionsMachVirtualMemory + |NSPointerFunctionsWeakMemory); + _x.options |= NSPointerFunctionsZeroingWeakMemory; } - (NSUInteger (*)(const void *item)) sizeFunction @@ -363,20 +365,12 @@ - (void) setUsesWeakReadAndWriteBarriers: (BOOL)flag - (BOOL) usesStrongWriteBarrier { - if ((_x.options & - (NSPointerFunctionsZeroingWeakMemory - |NSPointerFunctionsOpaqueMemory - |NSPointerFunctionsMallocMemory - |NSPointerFunctionsMachVirtualMemory)) == NSPointerFunctionsStrongMemory) - return YES; - return NO; + return memoryType(_x.options, NSPointerFunctionsStrongMemory); } - (BOOL) usesWeakReadAndWriteBarriers { - if (_x.options & NSPointerFunctionsZeroingWeakMemory) - return YES; - return NO; + return memoryType(_x.options, NSPointerFunctionsZeroingWeakMemory); } @end diff --git a/Source/NSPointerArray.m b/Source/NSPointerArray.m index e01da52281..a6ecbc2522 100644 --- a/Source/NSPointerArray.m +++ b/Source/NSPointerArray.m @@ -534,7 +534,7 @@ - (void) setCount: (NSUInteger)count new_gf = new_cap / 2; if (_contents == 0) { - if (_pf.options & NSPointerFunctionsZeroingWeakMemory) + if (memoryType(_pf.options, NSPointerFunctionsZeroingWeakMemory)) { ptr = (void**)NSAllocateCollectable(size, 0); } @@ -545,7 +545,7 @@ - (void) setCount: (NSUInteger)count } else { - if (_pf.options & NSPointerFunctionsZeroingWeakMemory) + if (memoryType(_pf.options, NSPointerFunctionsZeroingWeakMemory)) { ptr = (void**)NSReallocateCollectable( _contents, size, 0); From 243bab97616df39c90e55e6056acca69a3d534a9 Mon Sep 17 00:00:00 2001 From: Fred Kiefer Date: Sun, 20 Aug 2023 00:06:55 +0200 Subject: [PATCH 2/2] Address pull request comments --- Source/NSConcreteHashTable.m | 2 +- Source/NSConcretePointerFunctions.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/NSConcreteHashTable.m b/Source/NSConcreteHashTable.m index 08b735be58..39f5c75b4d 100644 --- a/Source/NSConcreteHashTable.m +++ b/Source/NSConcreteHashTable.m @@ -77,7 +77,7 @@ @interface NSConcreteHashTable : NSHashTable #define GSI_MAP_TABLE_S instanceSize #define IS_WEAK(M) \ - memoryType(M->cb.pf.options, NSPointerFunctionsZeroingWeakMemory) | memoryType(M->cb.pf.options, NSPointerFunctionsWeakMemory) + memoryType(M->cb.pf.options, NSPointerFunctionsZeroingWeakMemory) || memoryType(M->cb.pf.options, NSPointerFunctionsWeakMemory) #define GSI_MAP_HASH(M, X)\ (M->legacy ? M->cb.old.hash(M, X.ptr) \ : pointerFunctionsHash(&M->cb.pf, X.ptr)) diff --git a/Source/NSConcretePointerFunctions.h b/Source/NSConcretePointerFunctions.h index 96b0a70012..94cd365b1f 100644 --- a/Source/NSConcretePointerFunctions.h +++ b/Source/NSConcretePointerFunctions.h @@ -78,12 +78,12 @@ typedef struct inline static BOOL memoryType(int options, int flag) { - return (options & 0xff) == flag; + return ((options & 0xff) == flag) ? YES : NO; } inline static BOOL personalityType(int options, int flag) { - return (options & 0xff00) == flag; + return ((options & 0xff00) == flag) ? YES : NO; } /* Declare the concrete pointer functions class as a wrapper around