Skip to content

Commit

Permalink
Fix ref counting (hopefully)
Browse files Browse the repository at this point in the history
  • Loading branch information
mszoek committed Mar 4, 2021
1 parent 035dafb commit e8490a8
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
1 change: 0 additions & 1 deletion Foundation/NSMapTable.m
Expand Up @@ -324,7 +324,6 @@ void NSMapInsert(NSMapTable *table,const void *key,const void *value){

return;
}

zone=NSZoneFromPointer(table);

if(table->count>=table->nBuckets){
Expand Down
5 changes: 3 additions & 2 deletions Foundation/NSObject/NSObject.m
Expand Up @@ -61,6 +61,9 @@ +(void)setVersion:(NSInteger)version {
}

+(void)load {
#if defined(__HELIUM__)
objc_create_block_classes_as_subclasses_of(self);
#endif
}


Expand All @@ -71,13 +74,11 @@ static IMP objc_msg_forward(id rcv, SEL message) {
#endif

+(void)initialize {
#if 1
#ifdef GCC_RUNTIME_3
__objc_msg_forward2 = objc_msg_forward;
#else
objc_setForwardHandler(objc_msgForward,objc_msgForward_stret);
#endif
#endif
}

+(Class)superclass {
Expand Down
25 changes: 20 additions & 5 deletions Foundation/NSZone/NSZone.m
Expand Up @@ -23,17 +23,14 @@
// NSZone functions implemented in platform subproject

void NSIncrementExtraRefCount(id object) {
// objc_retain_fast_np(object);
object_incrementExternalRefCount(object);
}

BOOL NSDecrementExtraRefCountWasZero(id object) {
// return objc_release_fast_no_destroy_np(object);
return object_decrementExternalRefCount(object);
return objc_release_fast_no_destroy_np(object);
}

NSUInteger NSExtraRefCount(id object) {
// return object_getRetainCount(object);
return object_externalRefCount(object);
}

Expand All @@ -57,6 +54,21 @@ id NSAllocateObject(Class class, NSUInteger extraBytes, NSZone *zone)
zone = NSDefaultMallocZone();
}

#if defined(__HELIUM__)
int size = class_getInstanceSize(class) + extraBytes + sizeof(uintptr_t);
result = NSZoneMalloc(zone, size);
if(result != nil) {
memset(result, 0, size);
*(uintptr_t*)result = -1;
result = (id)((uintptr_t)result+sizeof(uintptr_t));
object_setClass(result, class);

if (__NSAllocateObjectHook) {
__NSAllocateObjectHook(result);
}
}

#else
result = NSZoneCalloc(zone, 1, class_getInstanceSize(class) + extraBytes);

if (result) {
Expand All @@ -67,11 +79,11 @@ id NSAllocateObject(Class class, NSUInteger extraBytes, NSZone *zone)
// NSZoneFree(zone, result);
// result = nil;
// }

if (__NSAllocateObjectHook) {
__NSAllocateObjectHook(result);
}
}
#endif

return result;
}
Expand All @@ -97,6 +109,9 @@ void NSDeallocateObject(id object)
object->isa = 0;
#endif

#if defined(__HELIUM__)
object = (id)((uintptr_t)object-sizeof(uintptr_t));
#endif
NSZoneFree(zone, object);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/platform_posix/NSPlatform_posix.m
Expand Up @@ -158,7 +158,7 @@ -(NSDictionary *)environment {

objects[count]=[NSString stringWithCString:keyValue+i+1];
keys[count]=[NSString stringWithCString:keyValue];

[self checkEnvironmentKey:keys[count] value:objects[count]];
}

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -61,7 +61,7 @@ mkfiles:
libobjc2: .PHONY
mkdir -p ${MAKEOBJDIRPREFIX}/libobjc2
cd ${MAKEOBJDIRPREFIX}/libobjc2; cmake \
-DCMAKE_C_FLAGS="-DBSD -DFREEBSD" \
-DCMAKE_C_FLAGS="-DBSD -DFREEBSD -D__HELIUM__" \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_INSTALL_PREFIX=/usr \
-DOLDABI_COMPAT=false -DLEGACY_COMPAT=false \
Expand Down
8 changes: 5 additions & 3 deletions libobjc2/arc.mm
Expand Up @@ -322,13 +322,15 @@ static inline id retain(id obj)

extern "C" OBJC_PUBLIC void object_incrementExternalRefCount(id value)
{
//fprintf(stderr,"object_incrementExternalRefCount %p\n",value);
objc_retain_fast_np(value);
// fprintf(stderr,"object_incrementExternalRefCount %p = %d\n",value,object_getRetainCount_np(value));
}


extern "C" OBJC_PUBLIC BOOL object_decrementExternalRefCount(id value)
extern "C" OBJC_PUBLIC void object_decrementExternalRefCount(id value)
{
return objc_release_fast_no_destroy_np(value);
objc_release_fast_np(value);
// fprintf(stderr,"object_decrementExternalRefCount %p = %d\n",value,object_getRetainCount_np(value));
}

#define objc_DecrementExtraRefCountWasZero(object) objc_release_fast_no_destroy_np(object)
Expand Down
2 changes: 1 addition & 1 deletion mk/internal/framework.common.mk
@@ -1,5 +1,5 @@
# Common settings for building frameworks
FMWK_CFLAGS := -O0 -g -DBSD -DFREEBSD -DPLATFORM_IS_POSIX -DGCC_RUNTIME_3 \
FMWK_CFLAGS := -O0 -g -D__HELIUM__ -DBSD -DFREEBSD -DPLATFORM_IS_POSIX -DGCC_RUNTIME_3 \
-DPLATFORM_USES_BSD_SOCKETS -fobjc-runtime=gnustep-2.0 \
-fobjc-nonfragile-abi -fPIC
FMWK_LDFLAGS+= -L${BUILDROOT}/usr/lib -L/usr/lib -L/lib -lobjc -lpthread -lm -lc -lcompiler_rt
Expand Down

0 comments on commit e8490a8

Please sign in to comment.