2 * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _OBJC_RUNTIME_OLD_H
25 #define _OBJC_RUNTIME_OLD_H
27 #include "objc-private.h"
31 #define CLS_INITIALIZED 0x4
32 #define CLS_POSING 0x8
33 #define CLS_MAPPED 0x10
34 #define CLS_FLUSH_CACHE 0x20
35 #define CLS_GROW_CACHE 0x40
36 #define CLS_NEED_BIND 0x80
37 #define CLS_METHOD_ARRAY 0x100
38 // the JavaBridge constructs classes with these markers
39 #define CLS_JAVA_HYBRID 0x200
40 #define CLS_JAVA_CLASS 0x400
41 // thread-safe +initialize
42 #define CLS_INITIALIZING 0x800
44 #define CLS_FROM_BUNDLE 0x1000
46 #define CLS_HAS_CXX_STRUCTORS 0x2000
47 // Lazy method list arrays
48 #define CLS_NO_METHOD_ARRAY 0x4000
49 // +load implementation
50 #define CLS_HAS_LOAD_METHOD 0x8000
51 // objc_allocateClassPair API
52 #define CLS_CONSTRUCTING 0x10000
54 #define CLS_HIDDEN 0x20000
55 // GC: class has unsafe finalize method
56 #define CLS_FINALIZE_ON_MAIN_THREAD 0x40000
57 // Lazy property list arrays
58 #define CLS_NO_PROPERTY_ARRAY 0x80000
59 // +load implementation
60 #define CLS_CONNECTED 0x100000
61 #define CLS_LOADED 0x200000
62 // objc_allocateClassPair API
63 #define CLS_CONSTRUCTED 0x400000
64 // class is leaf for cache flushing
65 #define CLS_LEAF 0x800000
66 // class instances may have associative references
67 #define CLS_INSTANCES_HAVE_ASSOCIATED_OBJECTS 0x1000000
68 // class has instance-specific GC layout
69 #define CLS_HAS_INSTANCE_SPECIFIC_LAYOUT 0x2000000
72 // Terminator for array of method lists
73 #define END_OF_METHODS_LIST ((struct old_method_list*)-1)
75 #define ISCLASS(cls) (((cls)->info & CLS_CLASS) != 0)
76 #define ISMETA(cls) (((cls)->info & CLS_META) != 0)
77 #define GETMETA(cls) (ISMETA(cls) ? (cls) : (cls)->ISA())
80 struct objc_class
: objc_object
{
85 uint32_t instance_size
;
86 struct old_ivar_list
*ivars
;
87 struct old_method_list
**methodLists
;
89 struct old_protocol_list
*protocols
;
91 const uint8_t *ivar_layout
;
92 struct old_class_ext
*ext
;
94 void setInfo(uint32_t set
) {
95 OSAtomicOr32Barrier(set
, (volatile uint32_t *)&info
);
98 void clearInfo(uint32_t clear
) {
99 OSAtomicXor32Barrier(clear
, (volatile uint32_t *)&info
);
103 // set and clear must not overlap
104 void changeInfo(uint32_t set
, uint32_t clear
) {
105 assert((set
& clear
) == 0);
110 newf
= (oldf
| set
) & ~clear
;
111 } while (!OSAtomicCompareAndSwap32Barrier(oldf
, newf
, (volatile int32_t *)&info
));
115 // set_superclass propagates the flag from the superclass.
116 return info
& CLS_HAS_CXX_STRUCTORS
;
120 return hasCxxCtor(); // one bit for both ctor and dtor
126 void setHasCustomRR(bool = false) { }
127 void setHasDefaultRR() { }
128 void printCustomRR(bool) { }
130 bool hasCustomAWZ() {
133 void setHasCustomAWZ(bool = false) { }
134 void setHasDefaultAWZ() { }
135 void printCustomAWZ(bool) { }
137 bool instancesHaveAssociatedObjects() {
138 return info
& CLS_INSTANCES_HAVE_ASSOCIATED_OBJECTS
;
141 void setInstancesHaveAssociatedObjects() {
142 setInfo(CLS_INSTANCES_HAVE_ASSOCIATED_OBJECTS
);
145 bool shouldGrowCache() {
146 return info
& CLS_GROW_CACHE
;
149 void setShouldGrowCache(bool grow
) {
150 if (grow
) setInfo(CLS_GROW_CACHE
);
151 else clearInfo(CLS_GROW_CACHE
);
154 bool shouldFinalizeOnMainThread() {
155 return info
& CLS_FINALIZE_ON_MAIN_THREAD
;
158 void setShouldFinalizeOnMainThread() {
159 setInfo(CLS_FINALIZE_ON_MAIN_THREAD
);
162 // +initialize bits are stored on the metaclass only
163 bool isInitializing() {
164 return getMeta()->info
& CLS_INITIALIZING
;
167 // +initialize bits are stored on the metaclass only
168 void setInitializing() {
169 getMeta()->setInfo(CLS_INITIALIZING
);
172 // +initialize bits are stored on the metaclass only
173 bool isInitialized() {
174 return getMeta()->info
& CLS_INITIALIZED
;
177 // +initialize bits are stored on the metaclass only
178 void setInitialized() {
179 getMeta()->changeInfo(CLS_INITIALIZED
, CLS_INITIALIZING
);
183 // A class registered for +load is ready for +load to be called
184 // if it is connected.
185 return isConnected();
192 const char *mangledName() { return name
; }
193 const char *demangledName() { return name
; }
194 const char *nameForLogging() { return name
; }
197 return info
& CLS_META
;
200 // NOT identical to this->ISA() when this is a metaclass
202 if (isMetaClass()) return (Class
)this;
203 else return this->ISA();
206 // May be unaligned depending on class's ivars.
207 uint32_t unalignedInstanceSize() {
208 return instance_size
;
211 // Class's ivar size rounded up to a pointer-size boundary.
212 uint32_t alignedInstanceSize() {
213 return (unalignedInstanceSize() + WORD_MASK
) & ~WORD_MASK
;
216 size_t instanceSize(size_t extraBytes
) {
217 size_t size
= alignedInstanceSize() + extraBytes
;
218 // CF requires all objects be at least 16 bytes.
219 if (size
< 16) size
= 16;
225 struct old_class_ext
{
227 const uint8_t *weak_ivar_layout
;
228 struct old_property_list
**propertyLists
;
231 struct old_category
{
234 struct old_method_list
*instance_methods
;
235 struct old_method_list
*class_methods
;
236 struct old_protocol_list
*protocols
;
238 struct old_property_list
*instance_properties
;
250 struct old_ivar_list
{
255 /* variable length structure */
256 struct old_ivar ivar_list
[1];
266 struct old_method_list
{
273 /* variable length structure */
274 struct old_method method_list
[1];
277 struct old_protocol
{
279 const char *protocol_name
;
280 struct old_protocol_list
*protocol_list
;
281 struct objc_method_description_list
*instance_methods
;
282 struct objc_method_description_list
*class_methods
;
285 struct old_protocol_list
{
286 struct old_protocol_list
*next
;
288 struct old_protocol
*list
[1];
291 struct old_protocol_ext
{
293 struct objc_method_description_list
*optional_instance_methods
;
294 struct objc_method_description_list
*optional_class_methods
;
295 struct old_property_list
*instance_properties
;
296 const char **extendedMethodTypes
;
300 struct old_property
{
302 const char *attributes
;
305 struct old_property_list
{
308 struct old_property first
;
312 #include "hashtable2.h"
316 #define oldprotocol(proto) ((struct old_protocol *)proto)
317 #define oldmethod(meth) ((struct old_method *)meth)
318 #define oldcategory(cat) ((struct old_category *)cat)
319 #define oldivar(ivar) ((struct old_ivar *)ivar)
320 #define oldproperty(prop) ((struct old_property *)prop)
322 extern NXHashTable
*class_hash
;
324 extern void unload_class(Class cls
);
326 extern IMP
lookupNamedMethodInMethodList(struct old_method_list
*mlist
, const char *meth_name
);
327 extern void _objc_insertMethods(Class cls
, struct old_method_list
*mlist
, struct old_category
*cat
);
328 extern void _objc_removeMethods(Class cls
, struct old_method_list
*mlist
);
329 extern void _objc_flush_caches (Class cls
);
330 extern BOOL
_class_addProperties(Class cls
, struct old_property_list
*additions
);
331 extern BOOL
_class_hasLoadMethod(Class cls
);
332 extern void change_class_references(Class imposter
, Class original
, Class copy
, BOOL changeSuperRefs
);
333 extern void flush_marked_caches(void);
334 extern void set_superclass(Class cls
, Class supercls
, BOOL cls_is_new
);
335 extern void try_free(const void *p
);
337 extern struct old_property
*property_list_nth(const struct old_property_list
*plist
, uint32_t i
);
338 extern struct old_property
**copyPropertyList(struct old_property_list
*plist
, unsigned int *outCount
);
340 extern struct objc_method_description
* lookup_protocol_method(struct old_protocol
*proto
, SEL aSel
, BOOL isRequiredMethod
, BOOL isInstanceMethod
, BOOL recursive
);
342 // used by flush_caches outside objc-cache.m
343 extern void _cache_flush(Class cls
);
344 #ifdef OBJC_INSTRUMENTED
345 extern unsigned int LinearFlushCachesCount
;
346 extern unsigned int LinearFlushCachesVisitedCount
;
347 extern unsigned int MaxLinearFlushCachesVisitedCount
;
348 extern unsigned int NonlinearFlushCachesCount
;
349 extern unsigned int NonlinearFlushCachesClassCount
;
350 extern unsigned int NonlinearFlushCachesVisitedCount
;
351 extern unsigned int MaxNonlinearFlushCachesVisitedCount
;
352 extern unsigned int IdealFlushCachesCount
;
353 extern unsigned int MaxIdealFlushCachesCount
;