/* The following three functions can be used to determine how a structure is laid out by the compiler. For example: struct objc_struct_layout layout; int i; objc_layout_structure (type, &layout); while (objc_layout_structure_next_member (&layout)) { int position, align; const char *type; objc_layout_structure_get_info (&layout, &position, &align, &type); printf ("element %d has offset %d, alignment %d\n", i++, position, align); } These functions are used by objc_sizeof_type and objc_alignof_type functions to compute the size and alignment of structures. The previous method of computing the size and alignment of a structure was not working on some architectures, particulary on AIX, and in the presence of bitfields inside the structure. */ void objc_layout_structure (const char *type, struct objc_struct_layout *layout) { const char *ntype; if (*type++ != _C_STRUCT_B) { objc_error(nil, OBJC_ERR_BAD_TYPE, "record type expected in objc_layout_structure, got %s\n", type); } layout->original_type = type; /* Skip "<name>=" if any. Avoid embedded structures and unions. */ ntype = type; while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B && *ntype++ != '=') /* do nothing */; /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */ if (*(ntype - 1) == '=') type = ntype; layout->type = type; layout->prev_type = NULL; layout->record_size = 0; layout->record_align = BITS_PER_UNIT; layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY); }
void * objc_atomic_malloc(size_t size) { void* res = (void*) (*_objc_atomic_malloc)(size); if(!res) objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); return res; }
static void *GC_calloc (size_t nelem, size_t size) { void* p = GC_malloc (nelem * size); if (!p) objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted!\n"); memset (p, 0, nelem * size); return p; }
static volatile void __objc_thread_detach_function(struct __objc_thread_start_state *istate) { /* Valid state? */ if (istate) { id (*imp)(id,SEL,id); SEL selector = istate->selector; id object = istate->object; id argument = istate->argument; /* Don't need anymore so free it */ objc_free(istate); /* Clear out the thread local storage */ objc_thread_set_data(NULL); /* Check to see if we just became multi threaded */ if (!__objc_is_multi_threaded) { __objc_is_multi_threaded = 1; /* Call the hook function */ if (_objc_became_multi_threaded != NULL) (*_objc_became_multi_threaded)(); } /* Call the method */ if ((imp = (id(*)(id, SEL, id))objc_msg_lookup(object, selector))) (*imp)(object, selector, argument); else objc_error(object, OBJC_ERR_UNIMPLEMENTED, "objc_thread_detach called with bad selector.\n"); } else objc_error(nil, OBJC_ERR_BAD_STATE, "objc_thread_detach called with NULL state.\n"); /* Exit the thread */ objc_thread_exit(); }
int objc_sizeof_type (const char* type) { /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } switch(*type) { case _C_ID: return sizeof(id); break; case _C_CLASS: return sizeof(Class); break; case _C_SEL: return sizeof(SEL); break; case _C_CHR: return sizeof(char); break; case _C_UCHR: return sizeof(unsigned char); break; case _C_SHT: return sizeof(short); break; case _C_USHT: return sizeof(unsigned short); break; case _C_INT: return sizeof(int); break; case _C_UINT: return sizeof(unsigned int); break; case _C_LNG: return sizeof(long); break; case _C_ULNG: return sizeof(unsigned long); break; case _C_LNG_LNG: return sizeof(long long); break; case _C_ULNG_LNG: return sizeof(unsigned long long); break; case _C_FLT: return sizeof(float); break; case _C_DBL: return sizeof(double); break; case _C_VOID: return sizeof(void); break; case _C_PTR: case _C_ATOM: case _C_CHARPTR: return sizeof(char*); break; case _C_ARY_B: { int len = atoi(type+1); while (isdigit(*++type)); return len*objc_aligned_size (type); } break; case _C_BFLD: { /* The new encoding of bitfields is: b 'position' 'type' 'size' */ int position, size; int startByte, endByte; position = atoi (type + 1); while (isdigit (*++type)); size = atoi (type + 1); startByte = position / BITS_PER_UNIT; endByte = (position + size) / BITS_PER_UNIT; return endByte - startByte; } case _C_STRUCT_B: { struct objc_struct_layout layout; unsigned int size; objc_layout_structure (type, &layout); while (objc_layout_structure_next_member (&layout)) /* do nothing */ ; objc_layout_finish_structure (&layout, &size, NULL); return size; } case _C_UNION_B: { int max_size = 0; while (*type != _C_UNION_E && *type++ != '=') /* do nothing */; while (*type != _C_UNION_E) { /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } max_size = MAX (max_size, objc_sizeof_type (type)); type = objc_skip_typespec (type); } return max_size; } default: { objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); return 0; } } }
const char* objc_skip_typespec (const char* type) { /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } type = objc_skip_type_qualifiers (type); switch (*type) { case _C_ID: /* An id may be annotated by the actual type if it is known with the @"ClassName" syntax */ if (*++type != '"') return type; else { while (*++type != '"') /* do nothing */; return type + 1; } /* The following are one character type codes */ case _C_CLASS: case _C_SEL: case _C_CHR: case _C_UCHR: case _C_CHARPTR: case _C_ATOM: case _C_SHT: case _C_USHT: case _C_INT: case _C_UINT: case _C_LNG: case _C_ULNG: case _C_LNG_LNG: case _C_ULNG_LNG: case _C_FLT: case _C_DBL: case _C_VOID: case _C_UNDEF: return ++type; break; case _C_ARY_B: /* skip digits, typespec and closing ']' */ while(isdigit(*++type)); type = objc_skip_typespec(type); if (*type == _C_ARY_E) return ++type; else { objc_error(nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type); return 0; } case _C_BFLD: /* The new encoding of bitfields is: b 'position' 'type' 'size' */ while (isdigit (*++type)); /* skip position */ while (isdigit (*++type)); /* skip type and size */ return type; case _C_STRUCT_B: /* skip name, and elements until closing '}' */ while (*type != _C_STRUCT_E && *type++ != '='); while (*type != _C_STRUCT_E) { type = objc_skip_typespec (type); } return ++type; case _C_UNION_B: /* skip name, and elements until closing ')' */ while (*type != _C_UNION_E && *type++ != '='); while (*type != _C_UNION_E) { type = objc_skip_typespec (type); } return ++type; case _C_PTR: /* Just skip the following typespec */ return objc_skip_typespec (++type); default: { objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); return 0; } } }
int objc_alignof_type(const char* type) { /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } switch(*type) { case _C_ID: return __alignof__(id); break; case _C_CLASS: return __alignof__(Class); break; case _C_SEL: return __alignof__(SEL); break; case _C_CHR: return __alignof__(char); break; case _C_UCHR: return __alignof__(unsigned char); break; case _C_SHT: return __alignof__(short); break; case _C_USHT: return __alignof__(unsigned short); break; case _C_INT: return __alignof__(int); break; case _C_UINT: return __alignof__(unsigned int); break; case _C_LNG: return __alignof__(long); break; case _C_ULNG: return __alignof__(unsigned long); break; case _C_LNG_LNG: return __alignof__(long long); break; case _C_ULNG_LNG: return __alignof__(unsigned long long); break; case _C_FLT: return __alignof__(float); break; case _C_DBL: return __alignof__(double); break; case _C_PTR: case _C_ATOM: case _C_CHARPTR: return __alignof__(char*); break; case _C_ARY_B: while (isdigit(*++type)) /* do nothing */; return objc_alignof_type (type); case _C_STRUCT_B: { struct objc_struct_layout layout; unsigned int align; objc_layout_structure (type, &layout); while (objc_layout_structure_next_member (&layout)) /* do nothing */; objc_layout_finish_structure (&layout, NULL, &align); return align; } case _C_UNION_B: { int maxalign = 0; while (*type != _C_UNION_E && *type++ != '=') /* do nothing */; while (*type != _C_UNION_E) { /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } maxalign = MAX (maxalign, objc_alignof_type (type)); type = objc_skip_typespec (type); } return maxalign; } default: { objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); return 0; } } }