diff --git a/API b/API index a65c0be3..0b0cd551 100644 --- a/API +++ b/API @@ -88,7 +88,7 @@ follows. unsigned total = sel_copyTypes_np("alloc", types, total); if (total > 16) { - types = calloc(sizeof(char*), total); + types = calloc(total, sizeof(char*)); sel_copyTypes_np("alloc", types, total); } // Do stuff with the types. diff --git a/Test/BoxedForeignException.m b/Test/BoxedForeignException.m index 7ef5863b..1505d727 100644 --- a/Test/BoxedForeignException.m +++ b/Test/BoxedForeignException.m @@ -16,7 +16,7 @@ int throw(void) { - struct foreign_exception *foreign_exception = calloc(sizeof(struct foreign_exception), 1); + struct foreign_exception *foreign_exception = calloc(1, sizeof(struct foreign_exception)); foreign_exception->header.exception_class = 42; foreign_exception->x = 12; _Unwind_RaiseException(&foreign_exception->header); diff --git a/arc.mm b/arc.mm index fbba14c0..502ab450 100644 --- a/arc.mm +++ b/arc.mm @@ -134,7 +134,7 @@ - (void)release; template static inline T* new_zeroed() { - return static_cast(calloc(sizeof(T), 1)); + return static_cast(calloc(1, sizeof(T))); } static inline struct arc_tls* getARCThreadData(void) @@ -737,7 +737,7 @@ void deallocate(T* p, std::size_t) * exists. */ __attribute__((always_inline)) -static BOOL loadWeakPointer(id *addr, id *obj, WeakRef **ref) +static inline BOOL loadWeakPointer(id *addr, id *obj, WeakRef **ref) { id oldObj = *addr; if (oldObj == nil) @@ -779,40 +779,37 @@ static BOOL setObjectHasWeakRefs(id obj) if (obj && cls && objc_test_class_flag(cls, objc_class_flag_fast_arc)) { uintptr_t *refCount = ((uintptr_t*)obj) - 1; - if (obj) - { - uintptr_t refCountVal = __sync_fetch_and_add(refCount, 0); - uintptr_t newVal = refCountVal; - do { - refCountVal = newVal; - size_t realCount = refCountVal & refcount_mask; - // If this object has already been deallocated (or is in the - // process of being deallocated) then don't bother storing it. - if (realCount == refcount_mask) - { - obj = nil; - cls = Nil; - break; - } - // The weak ref flag is monotonic (it is set, never cleared) so - // don't bother trying to re-set it. - if ((refCountVal & weak_mask) == weak_mask) - { - break; - } - // Set the flag in the reference count to indicate that a weak - // reference has been taken. - // - // We currently hold the weak ref lock, so another thread - // racing to deallocate this object will have to wait to do so - // if we manage to do the reference count update first. This - // shouldn't be possible, because `obj` should be a strong - // reference and so it shouldn't be possible to deallocate it - // while we're assigning it. - uintptr_t updated = ((uintptr_t)realCount | weak_mask); - newVal = __sync_val_compare_and_swap(refCount, refCountVal, updated); - } while (newVal != refCountVal); - } + uintptr_t refCountVal = __sync_fetch_and_add(refCount, 0); + uintptr_t newVal = refCountVal; + do { + refCountVal = newVal; + size_t realCount = refCountVal & refcount_mask; + // If this object has already been deallocated (or is in the + // process of being deallocated) then don't bother storing it. + if (realCount == refcount_mask) + { + obj = nil; + cls = Nil; + break; + } + // The weak ref flag is monotonic (it is set, never cleared) so + // don't bother trying to re-set it. + if ((refCountVal & weak_mask) == weak_mask) + { + break; + } + // Set the flag in the reference count to indicate that a weak + // reference has been taken. + // + // We currently hold the weak ref lock, so another thread + // racing to deallocate this object will have to wait to do so + // if we manage to do the reference count update first. This + // shouldn't be possible, because `obj` should be a strong + // reference and so it shouldn't be possible to deallocate it + // while we're assigning it. + uintptr_t updated = ((uintptr_t)realCount | weak_mask); + newVal = __sync_val_compare_and_swap(refCount, refCountVal, updated); + } while (newVal != refCountVal); } return isGlobalObject; } diff --git a/class_table.c b/class_table.c index 128af53e..4d5e6c47 100644 --- a/class_table.c +++ b/class_table.c @@ -525,7 +525,7 @@ int objc_getClassList(Class *buffer, int bufferLen) Class *objc_copyClassList(unsigned int *outCount) { int count = class_table->table_used; - Class *buffer = calloc(sizeof(Class), count); + Class *buffer = calloc(count, sizeof(Class)); if (NULL != outCount) { *outCount = count; diff --git a/dtable.c b/dtable.c index a2cdc08c..fb7b1bd7 100644 --- a/dtable.c +++ b/dtable.c @@ -655,11 +655,6 @@ PRIVATE void objc_resize_dtables(uint32_t newSize) } } -PRIVATE dtable_t objc_copy_dtable_for_class(dtable_t old, Class cls) -{ - return SparseArrayCopy(old); -} - PRIVATE void free_dtable(dtable_t dtable) { SparseArrayDestroy(dtable); diff --git a/gc_none.c b/gc_none.c index f263f37b..3f95348f 100644 --- a/gc_none.c +++ b/gc_none.c @@ -16,7 +16,7 @@ static id allocate_class(Class cls, size_t extraBytes) _aligned_malloc(size, 32); memset(addr, 0, size); #else - calloc(size, 1); + calloc(1, size); #endif return (id)(addr + 1); } @@ -32,7 +32,7 @@ static void free_object(id obj) static void *alloc(size_t size) { - return calloc(size, 1); + return calloc(1, size); } void objc_registerThreadWithCollector(void) {} diff --git a/legacy.c b/legacy.c index 1ef1b810..61728e5d 100644 --- a/legacy.c +++ b/legacy.c @@ -146,7 +146,7 @@ static struct objc_method_list *upgradeMethodList(struct objc_method_list_gcc *o { return NULL; } - struct objc_method_list *l = calloc(sizeof(struct objc_method_list) + old->count * sizeof(struct objc_method), 1); + struct objc_method_list *l = calloc(1, sizeof(struct objc_method_list) + old->count * sizeof(struct objc_method)); l->count = old->count; if (old->next) { @@ -343,7 +343,7 @@ PRIVATE struct objc_class_gsv1* objc_legacy_class_for_class(Class cls) PRIVATE Class objc_upgrade_class(struct objc_class_gsv1 *oldClass) { - Class cls = calloc(sizeof(struct objc_class), 1); + Class cls = calloc(1, sizeof(struct objc_class)); cls->isa = oldClass->isa; // super_class is left nil and we upgrade it later. cls->name = oldClass->name; diff --git a/properties.m b/properties.m index 7c3dcc31..46b2e7f8 100644 --- a/properties.m +++ b/properties.m @@ -285,7 +285,7 @@ objc_property_t class_getProperty(Class cls, const char *name) { return NULL; } - objc_property_t *list = calloc(sizeof(objc_property_t), count); + objc_property_t *list = calloc(count, sizeof(objc_property_t)); unsigned int out = 0; for (struct objc_property_list *l=properties ; NULL!=l ; l=l->next) { @@ -423,7 +423,7 @@ objc_property_t class_getProperty(Class cls, const char *name) } count++; } - objc_property_attribute_t *propAttrs = calloc(sizeof(objc_property_attribute_t), count); + objc_property_attribute_t *propAttrs = calloc(count, sizeof(objc_property_attribute_t)); memcpy(propAttrs, attrs, count * sizeof(objc_property_attribute_t)); if (NULL != outCount) { diff --git a/protocol.c b/protocol.c index 2c2a9d5f..3028ea27 100644 --- a/protocol.c +++ b/protocol.c @@ -316,7 +316,7 @@ struct objc_method_description *protocol_copyMethodDescriptionList(Protocol *p, *count = list->count; struct objc_method_description *out = - calloc(sizeof(struct objc_method_description), list->count); + calloc(list->count, sizeof(struct objc_method_description)); for (int i=0 ; i < (list->count) ; i++) { out[i].name = protocol_method_at_index(list, i)->selector; @@ -335,7 +335,7 @@ Protocol*__unsafe_unretained* protocol_copyProtocolList(Protocol *p, unsigned in } *count = p->protocol_list->count; - Protocol **out = calloc(sizeof(Protocol*), p->protocol_list->count); + Protocol **out = calloc(p->protocol_list->count, sizeof(Protocol*)); for (int i=0 ; iprotocol_list->count ; i++) { out[i] = (Protocol*)p->protocol_list->list[i]; @@ -370,7 +370,7 @@ objc_property_t *protocol_copyPropertyList2(Protocol *p, unsigned int *outCount, { return NULL; } - objc_property_t *list = calloc(sizeof(objc_property_t), count); + objc_property_t *list = calloc(count, sizeof(objc_property_t)); unsigned int out = 0; for (struct objc_property_list *l=properties ; l!=NULL ; l=l->next) { @@ -506,7 +506,7 @@ Protocol*__unsafe_unretained* objc_copyProtocolList(unsigned int *outCount) { LOCK_FOR_SCOPE(&protocol_table_lock); unsigned int total = known_protocol_table->table_used; - Protocol **p = calloc(sizeof(Protocol*), known_protocol_table->table_used); + Protocol **p = calloc(known_protocol_table->table_used, sizeof(Protocol*)); struct protocol_table_enumerator *e = NULL; Protocol *next; diff --git a/sarray2.c b/sarray2.c index 5f50f6d7..b356edee 100644 --- a/sarray2.c +++ b/sarray2.c @@ -183,7 +183,7 @@ PRIVATE void SparseArrayInsert(SparseArray * sarray, uint32_t index, void *value PRIVATE SparseArray *SparseArrayCopy(SparseArray * sarray) { - SparseArray *copy = calloc(sizeof(SparseArray), 1); + SparseArray *copy = calloc(1, sizeof(SparseArray)); memcpy(copy, sarray, sizeof(SparseArray)); copy->refCount = 1; // If the sarray has children, increase their refcounts and link them