[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Custom Allocator and 64-bit pointers, ARC
From: |
Tom Sheffler |
Subject: |
Custom Allocator and 64-bit pointers, ARC |
Date: |
Mon, 15 Oct 2018 09:12:05 -0700 |
I have an application that models events as objects. They are frequently
allocated and deallocated. It occurred to me that this class might be a
canidate for a custom allocator that allocates objects from a simple pool.
I found this article (from 2010)
https://www.mikeash.com/pyblog/friday-qa-2010-12-17-custom-object-allocators-in-objective-c.html
that describes my situation.
- objects allocated and deallocated on same thread
- not subclassed
- direct subclass of NSObject
The gist of it is to override allocWithZone: to retrieve objects from a 'cache'
(it could be pre-allocated, in my application). There is a line that sets the
ISA Pointer of the new object to the class.
I looked through the source code to NSObject.m and libobjc2/runtime.c and it
looks like his idea doesn't violate any assumptions.
But I've been wondering a couple of things about this in the context of ARC and
a 64-bit implementation.
+ (id)allocWithZone: (NSZone *)zone
{
id obj = GetObjectFromCache();
if(obj)
*(Class *)obj = self; // set the ISA pointer to the CLASS
else
obj = [super allocWithZone: zone];
return obj;
}
1. Is the assignment of the ISA pointer (the first slot in the object) to the
CLASS
still valid in 2018? With 64-bit pointers I think some bits are used for
refcounts
and other things. Maybe this assignment isn't safe.
This article
https://sectionfive.net/blog/2015/03/31/arc-in-depth-part-i/
says that with "tagged pointers" you need to use a function
object_setClass().
Is that the right thing for libobjc2 and GNUstep.
2. Will this work with ARC? It seems like keeping reference counts of
references to instances
is independent of alloc/dealloc of objects, so it shouldn't matter.
Thanks for any information or pointers!
-Tom
- Custom Allocator and 64-bit pointers, ARC,
Tom Sheffler <=