Acquiring existing memory does not use a funtion on OSX, copy that behavior.

This commit is contained in:
rfm 2024-07-16 19:43:28 +01:00
parent 8c722deb0c
commit 1a1e7f7fc5
3 changed files with 48 additions and 46 deletions

View file

@ -53,13 +53,6 @@ acquireRetainedObject(const void *item,
return [(NSObject*)item retain];
}
static void*
acquireExistingMemory(const void *item,
NSUInteger (*size)(const void *item), BOOL shouldCopy)
{
return (void*)item;
}
static NSString*
describeString(const void *item)
{
@ -217,14 +210,24 @@ relinquishRetainedMemory(const void *item,
}
else
{
_x.relinquishFunction = relinquishRetainedMemory;
/* NSPointerFunctionsStrongMemory uses -release for objects
*/
if (personalityType(options, NSPointerFunctionsObjectPersonality)
|| personalityType(options, NSPointerFunctionsObjectPointerPersonality))
{
_x.relinquishFunction = relinquishRetainedMemory;
}
else
{
_x.relinquishFunction = 0;
}
}
/* Now we look at the personality options to determine other functions.
*/
if (personalityType(options, NSPointerFunctionsOpaquePersonality))
{
_x.acquireFunction = acquireExistingMemory;
_x.acquireFunction = 0;
_x.descriptionFunction = describePointer;
_x.hashFunction = hashShifted;
_x.isEqualFunction = equalDirect;
@ -233,7 +236,7 @@ relinquishRetainedMemory(const void *item,
{
if (memoryType(options, NSPointerFunctionsZeroingWeakMemory))
{
_x.acquireFunction = acquireExistingMemory;
_x.acquireFunction = 0;
}
else
{
@ -245,7 +248,14 @@ relinquishRetainedMemory(const void *item,
}
else if (personalityType(options, NSPointerFunctionsCStringPersonality))
{
_x.acquireFunction = acquireMallocMemory;
if (memoryType(options, NSPointerFunctionsMallocMemory))
{
_x.acquireFunction = acquireMallocMemory;
}
else
{
_x.acquireFunction = NULL;
}
_x.descriptionFunction = describeString;
_x.hashFunction = hashString;
_x.isEqualFunction = equalString;
@ -261,7 +271,7 @@ relinquishRetainedMemory(const void *item,
{
if (memoryType(options, NSPointerFunctionsOpaqueMemory))
{
_x.acquireFunction = acquireExistingMemory;
_x.acquireFunction = 0;
_x.descriptionFunction = describeInteger;
_x.hashFunction = hashDirect;
_x.isEqualFunction = equalDirect;
@ -276,7 +286,7 @@ relinquishRetainedMemory(const void *item,
{
if (memoryType(options, NSPointerFunctionsZeroingWeakMemory))
{
_x.acquireFunction = acquireExistingMemory;
_x.acquireFunction = 0;
}
else
{

View file

@ -22,15 +22,18 @@ int main()
id testObj1 = [[[NSObject alloc] init] autorelease];
id testObj2 = [[[NSObject alloc] init] autorelease];
[mapTable setObject:testObj1 forKey:@"test"];
PASS([mapTable objectForKey:@"test"] == testObj1, "Table retains first active weak reference");
[mapTable setObject: testObj1 forKey: @"test"];
PASS([mapTable objectForKey: @"test"] == testObj1,
"Table retains first active weak reference");
[mapTable setObject:testObj2 forKey:@"test"];
PASS([mapTable objectForKey:@"test"] == testObj2, "Table retains second active weak reference");
[mapTable setObject: testObj2 forKey: @"test"];
PASS([mapTable objectForKey: @"test"] == testObj2,
"Table retains second active weak reference");
[arp2 release]; arp2 = nil;
PASS([mapTable objectForKey:@"test"] == nil, "Table removes dead weak reference");
PASS([mapTable objectForKey: @"test"] == nil,
"Table removes dead weak reference");
[arp release]; arp = nil;
END_SET("NSMapTable weak objects")

View file

@ -78,10 +78,6 @@ int main()
void *(*acquireFunction)\
(const void *src, NSUInteger (*size)(const void *item), BOOL shouldCopy);
NSUInteger (*hashFunction)\
(const void *item, NSUInteger (*size)(const void *item));
BOOL (*isEqualFunction)\
(const void *item1, const void *item2, NSUInteger (*size)(const void *item));
NSString *(*descriptionFunction)(const void *item);
void (*relinquishFunction)\
(const void *item, NSUInteger (*size)(const void *item));
@ -95,9 +91,10 @@ int main()
START_SET("CStringPersonality")
{
const char *cstr1 = "hello";
const char *cstr2 = "hello";
const char *cstr3 = "goodbye";
const char *cstr1 = "hello";
const char *cstr2 = "hello";
const char *cstr3 = "goodbye";
const char *cstr;
pf = [NSPointerFunctions pointerFunctionsWithOptions:
NSPointerFunctionsCStringPersonality];
@ -108,6 +105,11 @@ int main()
PASS_EQUAL([pf descriptionFunction](cstr1),
[NSString stringWithUTF8String: cstr1],
"CStringPersonality description")
PASS(NULL == [pf acquireFunction],
"CStringPersonality no acquireFunction")
PASS(NULL == [pf relinquishFunction],
"CStringPersonality no relinquishFunction")
}
END_SET("CStringPersonality")
@ -122,6 +124,11 @@ int main()
testIsEqualFunction(pf, "IntegerPersonality", int1, int2, int3);
testHashFunction(pf, "IntegerPersonality", int1, int2, int3);
PASS(NULL == [pf acquireFunction],
"IntegerPersonality no acquireFunction")
PASS(NULL == [pf relinquishFunction],
"IntegerPersonality no relinquishFunction")
}
END_SET("IntegerPersonality")
@ -179,27 +186,9 @@ int main()
START_SET("StructPersonality")
{
aStructType s1;
aStructType s2;
aStructType s3;
/* Struct equality testing should be a binary comparison of the
* memory, but because there's inter-field padding we must make
* sure that padding is cleared to guarantee two structs are
* equal.
*/
memset(&s1, '\0', sizeof(s1));
s1.aBool = YES;
s1.anInt = 24;
s1.aChar = 'n';
memset(&s2, '\0', sizeof(s2));
s2.aBool = YES;
s2.anInt = 24;
s2.aChar = 'n';
memset(&s3, '\0', sizeof(s3));
s3.aBool = NO;
s3.anInt = 42;
s3.aChar = 'y';
aStructType s1 = { NO, 24, 'n' };
aStructType s2 = { NO, 24, 'n' };
aStructType s3 = { YES, 42, 'y' };
pf = [NSPointerFunctions pointerFunctionsWithOptions:
NSPointerFunctionsStructPersonality];