2 * Copyright (c) 2004-2008 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@
27 #include "objc-private.h"
29 /**********************************************************************
32 * Layouts are used by the garbage collector to identify references from
33 * the object to other objects.
35 * Layout information is in the form of a '\0' terminated byte string.
36 * Each byte contains a word skip count in the high nibble and a
37 * consecutive references count in the low nibble. Counts that exceed 15 are
38 * continued in the succeeding byte with a zero in the opposite nibble.
39 * Objects that should be scanned conservatively will have a NULL layout.
40 * Objects that have no references have a empty byte string.
44 * For a class with pointers at offsets 4,12, 16, 32-128
45 * the layout is { 0x11, 0x12, 0x3f, 0x0a, 0x00 } or
46 * skip 1 - 1 reference (4)
47 * skip 1 - 2 references (12, 16)
48 * skip 3 - 15 references (32-88)
49 * no skip - 10 references (92-128)
52 **********************************************************************/
55 /**********************************************************************
57 * Allocates and returns a compressed string matching the given layout bitmap.
58 **********************************************************************/
59 static unsigned char *
60 compress_layout(const uint8_t *bits, size_t bitmap_bits, bool weak)
64 unsigned char *result;
66 // overallocate a lot; reallocate at correct size later
67 unsigned char * const layout = (unsigned char *)
68 calloc(bitmap_bits + 1, 1);
69 unsigned char *l = layout;
72 while (i < bitmap_bits) {
76 // Count one range each of skip and scan.
77 while (i < bitmap_bits) {
78 uint8_t bit = (uint8_t)((bits[i/8] >> (i % 8)) & 1);
83 while (i < bitmap_bits) {
84 uint8_t bit = (uint8_t)((bits[i/8] >> (i % 8)) & 1);
91 // Record skip and scan
92 if (skip) all_set = NO;
93 if (scan) none_set = NO;
99 *l = (uint8_t)(skip << 4); // NOT incremented - merges with scan
101 *l++ |= 0x0f; // May merge with short skip; must calloc
104 *l++ |= scan; // NOT checked for zero - always increments
105 // May merge with short skip; must calloc
109 // insert terminating byte
113 if (none_set && weak) {
114 result = NULL; // NULL weak layout means none-weak
115 } else if (all_set && !weak) {
116 result = NULL; // NULL ivar layout means all-scanned
118 result = (unsigned char *)strdup((char *)layout);
125 static void set_bits(layout_bitmap bits, size_t which, size_t count)
127 // fixme optimize for byte/word at a time
129 for (bit = which; bit < which + count && bit < bits.bitCount; bit++) {
130 bits.bits[bit/8] |= 1 << (bit % 8);
132 if (bit == bits.bitCount && bit < which + count) {
133 // couldn't fit full type in bitmap
134 _objc_fatal("layout bitmap too short");
138 static void clear_bits(layout_bitmap bits, size_t which, size_t count)
140 // fixme optimize for byte/word at a time
142 for (bit = which; bit < which + count && bit < bits.bitCount; bit++) {
143 bits.bits[bit/8] &= ~(1 << (bit % 8));
145 if (bit == bits.bitCount && bit < which + count) {
146 // couldn't fit full type in bitmap
147 _objc_fatal("layout bitmap too short");
151 static void move_bits(layout_bitmap bits, size_t src, size_t dst,
154 // fixme optimize for byte/word at a time
159 else if (dst > src) {
160 // Copy backwards in case of overlap
163 size_t srcbit = src + pos;
164 size_t dstbit = dst + pos;
165 if (bits.bits[srcbit/8] & (1 << (srcbit % 8))) {
166 bits.bits[dstbit/8] |= 1 << (dstbit % 8);
168 bits.bits[dstbit/8] &= ~(1 << (dstbit % 8));
173 // Copy forwards in case of overlap
175 for (pos = 0; pos < count; pos++) {
176 size_t srcbit = src + pos;
177 size_t dstbit = dst + pos;
178 if (bits.bits[srcbit/8] & (1 << (srcbit % 8))) {
179 bits.bits[dstbit/8] |= 1 << (dstbit % 8);
181 bits.bits[dstbit/8] &= ~(1 << (dstbit % 8));
187 // emacs autoindent hack - it doesn't like the loop in set_bits/clear_bits
193 static void decompress_layout(const unsigned char *layout_string, layout_bitmap bits)
197 while ((c = *layout_string++)) {
198 unsigned char skip = (c & 0xf0) >> 4;
199 unsigned char scan = (c & 0x0f);
201 set_bits(bits, bit, scan);
207 /***********************************************************************
208 * layout_bitmap_create
209 * Allocate a layout bitmap.
210 * The new bitmap spans the given instance size bytes.
211 * The start of the bitmap is filled from the given layout string (which
212 * spans an instance size of layoutStringSize); the rest is zero-filled.
213 * The returned bitmap must be freed with layout_bitmap_free().
214 **********************************************************************/
216 layout_bitmap_create(const unsigned char *layout_string,
217 size_t layoutStringInstanceSize,
218 size_t instanceSize, bool weak)
220 layout_bitmap result;
221 size_t words = instanceSize / sizeof(id);
224 result.bitCount = words;
225 result.bitsAllocated = words;
226 result.bits = (uint8_t *)calloc((words+7)/8, 1);
228 if (!layout_string) {
230 // NULL ivar layout means all-scanned
231 // (but only up to layoutStringSize instance size)
232 set_bits(result, 0, layoutStringInstanceSize/sizeof(id));
234 // NULL weak layout means none-weak.
237 decompress_layout(layout_string, result);
244 /***********************************************************************
245 * layout_bitmap_create_empty
246 * Allocate a layout bitmap.
247 * The new bitmap spans the given instance size bytes.
248 * The bitmap is empty, to represent an object whose ivars are completely unscanned.
249 * The returned bitmap must be freed with layout_bitmap_free().
250 **********************************************************************/
252 layout_bitmap_create_empty(size_t instanceSize, bool weak)
254 layout_bitmap result;
255 size_t words = instanceSize / sizeof(id);
258 result.bitCount = words;
259 result.bitsAllocated = words;
260 result.bits = (uint8_t *)calloc((words+7)/8, 1);
266 layout_bitmap_free(layout_bitmap bits)
268 if (bits.bits) free(bits.bits);
271 const unsigned char *
272 layout_string_create(layout_bitmap bits)
274 const unsigned char *result =
275 compress_layout(bits.bits, bits.bitCount, bits.weak);
278 // paranoia: cycle to bitmap and back to string again, and compare
279 layout_bitmap check = layout_bitmap_create(result, bits.bitCount*sizeof(id),
280 bits.bitCount*sizeof(id), bits.weak);
281 unsigned char *result2 =
282 compress_layout(check.bits, check.bitCount, check.weak);
283 if (result != result2 && 0 != strcmp((char*)result, (char *)result2)) {
284 layout_bitmap_print(bits);
285 layout_bitmap_print(check);
286 _objc_fatal("libobjc bug: mishandled layout bitmap");
289 layout_bitmap_free(check);
297 layout_bitmap_set_ivar(layout_bitmap bits, const char *type, size_t offset)
299 // fixme only handles some types
300 size_t bit = offset / sizeof(id);
303 if (type[0] == '@' || 0 == strcmp(type, "^@")) {
307 set_bits(bits, bit, 1);
309 else if (type[0] == '[') {
312 unsigned long count = strtoul(type+1, &t, 10);
313 if (t && t[0] == '@') {
314 set_bits(bits, bit, count);
317 else if (strchr(type, '@')) {
318 _objc_inform("warning: failing to set GC layout for '%s'\n", type);
324 /***********************************************************************
326 * Expand a layout bitmap to span newCount bits.
327 * The new bits are undefined.
328 **********************************************************************/
330 layout_bitmap_grow(layout_bitmap *bits, size_t newCount)
332 if (bits->bitCount >= newCount) return;
333 bits->bitCount = newCount;
334 if (bits->bitsAllocated < newCount) {
335 size_t newAllocated = bits->bitsAllocated * 2;
336 if (newAllocated < newCount) newAllocated = newCount;
337 bits->bits = (uint8_t *)
338 realloc(bits->bits, (newAllocated+7) / 8);
339 bits->bitsAllocated = newAllocated;
341 ASSERT(bits->bitsAllocated >= bits->bitCount);
342 ASSERT(bits->bitsAllocated >= newCount);
346 /***********************************************************************
347 * layout_bitmap_slide
348 * Slide the end of a layout bitmap farther from the start.
349 * Slides bits [oldPos, bits.bitCount) to [newPos, bits.bitCount+newPos-oldPos)
350 * Bits [oldPos, newPos) are zero-filled.
351 * The bitmap is expanded and bitCount updated if necessary.
353 **********************************************************************/
355 layout_bitmap_slide(layout_bitmap *bits, size_t oldPos, size_t newPos)
360 if (oldPos == newPos) return;
361 if (oldPos > newPos) _objc_fatal("layout bitmap sliding backwards");
363 shift = newPos - oldPos;
364 count = bits->bitCount - oldPos;
365 layout_bitmap_grow(bits, bits->bitCount + shift);
366 move_bits(*bits, oldPos, newPos, count); // slide
367 clear_bits(*bits, oldPos, shift); // zero-fill
371 /***********************************************************************
372 * layout_bitmap_slide_anywhere
373 * Slide the end of a layout bitmap relative to the start.
374 * Like layout_bitmap_slide, but can slide backwards too.
375 * The end of the bitmap is truncated.
376 **********************************************************************/
378 layout_bitmap_slide_anywhere(layout_bitmap *bits, size_t oldPos, size_t newPos)
383 if (oldPos == newPos) return;
385 if (oldPos < newPos) {
386 layout_bitmap_slide(bits, oldPos, newPos);
390 shift = oldPos - newPos;
391 count = bits->bitCount - oldPos;
392 move_bits(*bits, oldPos, newPos, count); // slide
393 bits->bitCount -= shift;
397 /***********************************************************************
398 * layout_bitmap_splat
399 * Pastes the contents of bitmap src to the start of bitmap dst.
400 * dst bits between the end of src and oldSrcInstanceSize are zeroed.
401 * dst must be at least as long as src.
402 * Returns YES if any of dst's bits were changed.
403 **********************************************************************/
405 layout_bitmap_splat(layout_bitmap dst, layout_bitmap src,
406 size_t oldSrcInstanceSize)
409 size_t oldSrcBitCount;
412 if (dst.bitCount < src.bitCount) _objc_fatal("layout bitmap too short");
415 oldSrcBitCount = oldSrcInstanceSize / sizeof(id);
417 // fixme optimize for byte/word at a time
418 for (bit = 0; bit < oldSrcBitCount; bit++) {
419 int dstset = dst.bits[bit/8] & (1 << (bit % 8));
420 int srcset = (bit < src.bitCount)
421 ? src.bits[bit/8] & (1 << (bit % 8))
423 if (dstset != srcset) {
426 dst.bits[bit/8] |= 1 << (bit % 8);
428 dst.bits[bit/8] &= ~(1 << (bit % 8));
437 /***********************************************************************
440 * dst must be at least as long as src.
441 * Returns YES if any of dst's bits were changed.
442 **********************************************************************/
444 layout_bitmap_or(layout_bitmap dst, layout_bitmap src, const char *msg)
449 if (dst.bitCount < src.bitCount) {
450 _objc_fatal("layout_bitmap_or: layout bitmap too short%s%s",
451 msg ? ": " : "", msg ? msg : "");
454 // fixme optimize for byte/word at a time
455 for (bit = 0; bit < src.bitCount; bit++) {
456 int dstset = dst.bits[bit/8] & (1 << (bit % 8));
457 int srcset = src.bits[bit/8] & (1 << (bit % 8));
458 if (srcset && !dstset) {
460 dst.bits[bit/8] |= 1 << (bit % 8);
468 /***********************************************************************
469 * layout_bitmap_clear
471 * dst must be at least as long as src.
472 * Returns YES if any of dst's bits were changed.
473 **********************************************************************/
475 layout_bitmap_clear(layout_bitmap dst, layout_bitmap src, const char *msg)
480 if (dst.bitCount < src.bitCount) {
481 _objc_fatal("layout_bitmap_clear: layout bitmap too short%s%s",
482 msg ? ": " : "", msg ? msg : "");
485 // fixme optimize for byte/word at a time
486 for (bit = 0; bit < src.bitCount; bit++) {
487 int dstset = dst.bits[bit/8] & (1 << (bit % 8));
488 int srcset = src.bits[bit/8] & (1 << (bit % 8));
489 if (srcset && dstset) {
491 dst.bits[bit/8] &= ~(1 << (bit % 8));
500 layout_bitmap_print(layout_bitmap bits)
503 printf("%zu: ", bits.bitCount);
504 for (i = 0; i < bits.bitCount; i++) {
505 int set = bits.bits[i/8] & (1 << (i % 8));
506 printf("%c", set ? '#' : '.');
512 // The code below may be useful when interpreting ivar types more precisely.
514 /**********************************************************************
515 * mark_offset_for_layout
517 * Marks the appropriate bit in the bits array cooresponding to a the
518 * offset of a reference. If we are scanning a nested pointer structure
519 * then the bits array will be NULL then this function does nothing.
521 **********************************************************************/
522 static void mark_offset_for_layout(long offset, long bits_size, unsigned char *bits) {
523 // references are ignored if bits is NULL
525 long slot = offset / sizeof(long);
527 // determine byte index using (offset / 8 bits per byte)
528 long i_byte = slot >> 3;
530 // if the byte index is valid
531 if (i_byte < bits_size) {
532 // set the (offset / 8 bits per byte)th bit
533 bits[i_byte] |= 1 << (slot & 7);
535 // offset not within instance size
536 _objc_inform ("layout - offset exceeds instance size");
541 /**********************************************************************
542 * skip_ivar_type_name
544 * Skip over the name of a field/class in an ivar type string. Names
545 * are in the form of a double-quoted string. Returns the remaining
548 **********************************************************************/
549 static char *skip_ivar_type_name(char *type) {
553 // if there is an open quote
558 // while no closing quote
559 while ((ch = *type) != '\"') {
560 // if end of string return end of string
561 if (!ch) return type;
567 // skip closing quote
571 // return remaining string
576 /**********************************************************************
577 * skip_ivar_struct_name
579 * Skip over the name of a struct in an ivar type string. Names
580 * may be followed by an equals sign. Returns the remaining string.
582 **********************************************************************/
583 static char *skip_ivar_struct_name(char *type) {
584 // get first character
587 if (ch == _C_UNDEF) {
588 // skip undefined name
590 } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_') {
593 // scan alphanumerics
597 } while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_' || (ch >= '0' && ch <= '9'));
599 // no struct name present
604 if (*type == '=') type++;
610 /**********************************************************************
611 * scan_basic_ivar_type
613 * Determines the size and alignment of a basic ivar type. If the basic
614 * type is a possible reference to another garbage collected type the
615 * is_reference is set to true (false otherwise.) Returns the remaining
618 **********************************************************************/
619 static char *scan_ivar_type_for_layout(char *type, long offset, long bits_size, unsigned char *bits, long *next_offset);
620 static char *scan_basic_ivar_type(char *type, long *size, long *alignment, bool *is_reference) {
621 // assume it is a non-reference type
624 // get the first character (advancing string)
625 const char *full_type = type;
628 // GCC 4 uses for const type*.
629 if (ch == _C_CONST) ch = *type++;
631 // act on first character
636 // skip over optional class name
637 type = skip_ivar_type_name(type);
639 // size and alignment of an id type
641 *alignment = __alignof(id);
643 // is a reference type
650 // skip underlying type
652 type = scan_ivar_type_for_layout(type, 0, 0, NULL, &ignored_offset);
654 // size and alignment of a generic pointer type
655 *size = sizeof(void *);
656 *alignment = __alignof(void *);
658 // is a reference type
665 // size and alignment of a char pointer type
666 *size = sizeof(char *);
667 *alignment = __alignof(char *);
669 // is a reference type
675 // classes and selectors are ignored for now
676 *size = sizeof(void *);
677 *alignment = __alignof(void *);
682 // char and unsigned char
683 *size = sizeof(char);
684 *alignment = __alignof(char);
689 // short and unsigned short
690 *size = sizeof(short);
691 *alignment = __alignof(short);
697 // int and unsigned int
699 *alignment = __alignof(int);
704 // long and unsigned long
705 *size = sizeof(long);
706 *alignment = __alignof(long);
711 // long long and unsigned long long
712 *size = sizeof(long long);
713 *alignment = __alignof(long long);
724 *size = sizeof(float);
725 *alignment = __alignof(float);
730 *size = sizeof(double);
731 *alignment = __alignof(double);
737 // get number of bits in bit field (advance type string)
738 long lng = strtol(type, &type, 10);
740 // while next type is a bit field
741 while (*type == _C_BFLD) {
745 // get next bit field length
746 long next_lng = strtol(type, &type, 10);
748 // if spans next word then align to next word
749 if ((lng & ~31) != ((lng + next_lng) & ~31)) lng = (lng + 31) & ~31;
751 // increment running length
754 // skip over potential field name
755 type = skip_ivar_type_name(type);
758 // determine number of bytes bits represent
759 *size = (lng + 7) / 8;
762 *alignment = __alignof(char);
767 *size = sizeof(BOOL);
768 *alignment = __alignof(BOOL);
774 *alignment = __alignof(char);
779 *alignment = __alignof(char);
784 _objc_fatal("unrecognized character \'%c\' in ivar type: \"%s\"", ch, full_type);
792 /**********************************************************************
793 * scan_ivar_type_for_layout
795 * Scan an ivar type string looking for references. The offset indicates
796 * where the ivar begins. bits is a byte array of size bits_size used to
797 * contain the references bit map. next_offset is the offset beyond the
798 * ivar. Returns the remaining string.
800 **********************************************************************/
801 static char *scan_ivar_type_for_layout(char *type, long offset, long bits_size, unsigned char *bits, long *next_offset) {
802 long size; // size of a basic type
803 long alignment; // alignment of the basic type
804 bool is_reference; // true if the type indicates a reference to a garbage collected object
806 // get the first character
809 // GCC 4 uses for const type*.
810 if (ch == _C_CONST) ch = *++type;
812 // act on first character
817 // get the array length
818 long lng = strtol(type + 1, &type, 10);
820 // next type will be where to advance the type string once the array is processed
821 char *next_type = type;
823 // repeat the next type x lng
825 next_type = scan_ivar_type_for_layout(type, 0, 0, NULL, &offset);
828 // repeatedly scan the same type
829 next_type = scan_ivar_type_for_layout(type, offset, bits_size, bits, &offset);
833 // advance the type now
836 // after the end of the array
837 *next_offset = offset;
839 // advance over closing bracket
840 if (*type == _C_ARY_E) type++;
841 else _objc_inform("missing \'%c\' in ivar type.", _C_ARY_E);
848 // skip over possible union name
849 type = skip_ivar_struct_name(type + 1);
851 // need to accumulate the maximum element offset
854 // while not closing paren
855 while ((ch = *type) && ch != _C_UNION_E) {
856 // skip over potential field name
857 type = skip_ivar_type_name(type);
861 type = scan_ivar_type_for_layout(type, offset, bits_size, bits, &union_offset);
863 // adjust the maximum element offset
864 if (max_offset < union_offset) max_offset = union_offset;
867 // after the largest element
868 *next_offset = max_offset;
870 // advance over closing paren
871 if (ch == _C_UNION_E) {
874 _objc_inform("missing \'%c\' in ivar type", _C_UNION_E);
882 // skip over possible struct name
883 type = skip_ivar_struct_name(type + 1);
885 // while not closing brace
886 while ((ch = *type) && ch != _C_STRUCT_E) {
887 // skip over potential field name
888 type = skip_ivar_type_name(type);
891 type = scan_ivar_type_for_layout(type, offset, bits_size, bits, &offset);
894 // after the end of the struct
895 *next_offset = offset;
897 // advance over closing brace
898 if (ch == _C_STRUCT_E) type++;
899 else _objc_inform("missing \'%c\' in ivar type", _C_STRUCT_E);
907 type = scan_basic_ivar_type(type, &size, &alignment, &is_reference);
909 // create alignment mask
913 offset = (offset + alignment) & ~alignment;
915 // if is a reference then mark in the bit map
916 if (is_reference) mark_offset_for_layout(offset, bits_size, bits);
918 // after the basic type
919 *next_offset = offset + size;
924 // return remainder of type string