1 \input texinfo @c -*-texinfo-*-
3 @setfilename objc-features.info
4 @settitle GNU Objective-C runtime features
8 Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
9 1999, 2000, 2001 Free Software Foundation, Inc.
11 @node Top, Executing code before main, (dir), (dir)
12 @comment node-name, next, previous, up
14 @chapter GNU Objective-C runtime features
16 This document is meant to describe some of the GNU Objective-C runtime
17 features. It is not intended to teach you Objective-C, there are several
18 resources on the Internet that present the language. Questions and
19 comments about this document to Ovidiu Predescu
20 @email{ovidiu@@cup.hp.com}.
23 * Executing code before main::
25 * Garbage Collection::
26 * Constant string objects::
27 * GNU Free Documentation License::
30 @node Executing code before main, Type encoding, Top, Top
31 @section @code{+load}: Executing code before main
34 The GNU Objective-C runtime provides a way that allows you to execute
35 code before the execution of the program enters the @code{main}
36 function. The code is executed on a per-class and a per-category basis,
37 through a special class method @code{+load}.
39 This facility is very useful if you want to initialize global variables
40 which can be accessed by the program directly, without sending a message
41 to the class first. The usual way to initialize global variables, in the
42 @code{+initialize} method, might not be useful because
43 @code{+initialize} is only called when the first message is sent to a
44 class object, which in some cases could be too late.
46 Suppose for example you have a @code{FileStream} class that declares
47 @code{Stdin}, @code{Stdout} and @code{Stderr} as global variables, like
52 FileStream *Stdin = nil;
53 FileStream *Stdout = nil;
54 FileStream *Stderr = nil;
56 @@implementation FileStream
60 Stdin = [[FileStream new] initWithFd:0];
61 Stdout = [[FileStream new] initWithFd:1];
62 Stderr = [[FileStream new] initWithFd:2];
65 /* Other methods here */
70 In this example, the initialization of @code{Stdin}, @code{Stdout} and
71 @code{Stderr} in @code{+initialize} occurs too late. The programmer can
72 send a message to one of these objects before the variables are actually
73 initialized, thus sending messages to the @code{nil} object. The
74 @code{+initialize} method which actually initializes the global
75 variables is not invoked until the first message is sent to the class
76 object. The solution would require these variables to be initialized
77 just before entering @code{main}.
79 The correct solution of the above problem is to use the @code{+load}
80 method instead of @code{+initialize}:
84 @@implementation FileStream
88 Stdin = [[FileStream new] initWithFd:0];
89 Stdout = [[FileStream new] initWithFd:1];
90 Stderr = [[FileStream new] initWithFd:2];
93 /* Other methods here */
98 The @code{+load} is a method that is not overridden by categories. If a
99 class and a category of it both implement @code{+load}, both methods are
100 invoked. This allows some additional initializations to be performed in
103 This mechanism is not intended to be a replacement for @code{+initialize}.
104 You should be aware of its limitations when you decide to use it
105 instead of @code{+initialize}.
108 * What you can and what you cannot do in +load::
112 @node What you can and what you cannot do in +load, , Executing code before main, Executing code before main
113 @subsection What you can and what you cannot do in @code{+load}
115 The @code{+load} implementation in the GNU runtime guarantees you the following
121 you can write whatever C code you like;
124 you can send messages to Objective-C constant strings (@@"this is a
128 you can allocate and send messages to objects whose class is implemented
132 the @code{+load} implementation of all super classes of a class are executed before the @code{+load} of that class is executed;
135 the @code{+load} implementation of a class is executed before the
136 @code{+load} implementation of any category.
140 In particular, the following things, even if they can work in a
141 particular case, are not guaranteed:
146 allocation of or sending messages to arbitrary objects;
149 allocation of or sending messages to objects whose classes have a
150 category implemented in the same file;
154 You should make no assumptions about receiving @code{+load} in sibling
155 classes when you write @code{+load} of a class. The order in which
156 sibling classes receive @code{+load} is not guaranteed.
158 The order in which @code{+load} and @code{+initialize} are called could
159 be problematic if this matters. If you don't allocate objects inside
160 @code{+load}, it is guaranteed that @code{+load} is called before
161 @code{+initialize}. If you create an object inside @code{+load} the
162 @code{+initialize} method of object's class is invoked even if
163 @code{+load} was not invoked. Note if you explicitly call @code{+load}
164 on a class, @code{+initialize} will be called first. To avoid possible
165 problems try to implement only one of these methods.
167 The @code{+load} method is also invoked when a bundle is dynamically
168 loaded into your running program. This happens automatically without any
169 intervening operation from you. When you write bundles and you need to
170 write @code{+load} you can safely create and send messages to objects whose
171 classes already exist in the running program. The same restrictions as
172 above apply to classes defined in bundle.
176 @node Type encoding, Garbage Collection, Executing code before main, Top
177 @section Type encoding
179 The Objective-C compiler generates type encodings for all the
180 types. These type encodings are used at runtime to find out information
181 about selectors and methods and about objects and classes.
183 The types are encoded in the following way:
187 @multitable @columnfractions .25 .75
190 @item @code{unsigned char}
194 @item @code{unsigned short}
198 @item @code{unsigned int}
202 @item @code{unsigned long}
204 @item @code{long long}
206 @item @code{unsigned long long}
225 @tab @code{b} followed by the starting position of the bitfield, the type of the bitfield and the size of the bitfield (the bitfields encoding was changed from the NeXT's compiler encoding, see below)
230 The encoding of bitfields has changed to allow bitfields to be properly
231 handled by the runtime functions that compute sizes and alignments of
232 types that contain bitfields. The previous encoding contained only the
233 size of the bitfield. Using only this information it is not possible to
234 reliably compute the size occupied by the bitfield. This is very
235 important in the presence of the Boehm's garbage collector because the
236 objects are allocated using the typed memory facility available in this
237 collector. The typed memory allocation requires information about where
238 the pointers are located inside the object.
240 The position in the bitfield is the position, counting in bits, of the
241 bit closest to the beginning of the structure.
243 The non-atomic types are encoded as follows:
247 @multitable @columnfractions .2 .8
249 @tab @code{'^'} followed by the pointed type.
251 @tab @code{'['} followed by the number of elements in the array followed by the type of the elements followed by @code{']'}
253 @tab @code{'@{'} followed by the name of the structure (or '?' if the structure is unnamed), the '=' sign, the type of the members and by @code{'@}'}
255 @tab @code{'('} followed by the name of the structure (or '?' if the union is unnamed), the '=' sign, the type of the members followed by @code{')'}
258 Here are some types and their encodings, as they are generated by the
259 compiler on a i386 machine:
263 @multitable @columnfractions .25 .75
264 @item Objective-C type
265 @tab Compiler encoding
281 @tab @code{@{?=i[3f]b128i3b131i2c@}}
286 In addition to the types the compiler also encodes the type
287 specifiers. The table below describes the encoding of the current
288 Objective-C type specifiers:
292 @multitable @columnfractions .25 .75
311 The type specifiers are encoded just before the type. Unlike types
312 however, the type specifiers are only encoded when they appear in method
316 @node Garbage Collection, Constant string objects, Type encoding, Top
317 @section Garbage Collection
319 Support for a new memory management policy has been added by using a
320 powerful conservative garbage collector, known as the
321 Boehm-Demers-Weiser conservative garbage collector. It is available from
322 @w{@uref{http://www.hpl.hp.com/personal/Hans_Boehm/gc/}}.
324 To enable the support for it you have to configure the compiler using an
325 additional argument, @w{@kbd{--enable-objc-gc}}. You need to have
326 garbage collector installed before building the compiler. This will
327 build an additional runtime library which has several enhancements to
328 support the garbage collector. The new library has a new name,
329 @kbd{libobjc_gc.a} to not conflict with the non-garbage-collected
332 When the garbage collector is used, the objects are allocated using the
333 so-called typed memory allocation mechanism available in the
334 Boehm-Demers-Weiser collector. This mode requires precise information on
335 where pointers are located inside objects. This information is computed
336 once per class, immediately after the class has been initialized.
338 There is a new runtime function @code{class_ivar_set_gcinvisible()}
339 which can be used to declare a so-called @strong{weak pointer}
340 reference. Such a pointer is basically hidden for the garbage collector;
341 this can be useful in certain situations, especially when you want to
342 keep track of the allocated objects, yet allow them to be
343 collected. This kind of pointers can only be members of objects, you
344 cannot declare a global pointer as a weak reference. Every type which is
345 a pointer type can be declared a weak pointer, including @code{id},
346 @code{Class} and @code{SEL}.
348 Here is an example of how to use this feature. Suppose you want to
349 implement a class whose instances hold a weak pointer reference; the
350 following class does this:
354 @@interface WeakPointer : Object
356 const void* weakPointer;
359 - initWithPointer:(const void*)p;
360 - (const void*)weakPointer;
364 @@implementation WeakPointer
368 class_ivar_set_gcinvisible (self, "weakPointer", YES);
371 - initWithPointer:(const void*)p
377 - (const void*)weakPointer
386 Weak pointers are supported through a new type character specifier
387 represented by the @code{'!'} character. The
388 @code{class_ivar_set_gcinvisible()} function adds or removes this
389 specifier to the string type description of the instance variable named
392 @c =========================================================================
393 @node Constant string objects
394 @section Constant string objects
396 GNU Objective-C provides constant string objects that are generated
397 directly by the compiler. You declare a constant string object by
398 prefixing a C constant string with the character @code{@@}:
401 id myString = @@"this is a constant string object";
404 The constant string objects are usually instances of the
405 @code{NXConstantString} class which is provided by the GNU Objective-C
406 runtime. To get the definition of this class you must include the
407 @file{objc/NXConstStr.h} header file.
409 User defined libraries may want to implement their own constant string
410 class. To be able to support them, the GNU Objective-C compiler provides
411 a new command line options @code{-fconstant-string-class=<class
412 name>}. The provided class should adhere to a strict structure, the same
413 as @code{NXConstantString}'s structure:
417 @@interface NXConstantString : Object
426 User class libraries may choose to inherit the customized constant
427 string class from a different class than @code{Object}. There is no
428 requirement in the methods the constant string class has to implement.
430 When a file is compiled with the @code{-fconstant-string-class} option,
431 all the constant string objects will be instances of the class specified
432 as argument to this option. It is possible to have multiple compilation
433 units referring to different constant string classes, neither the
434 compiler nor the linker impose any restrictions in doing this.