123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include <string.h>
- #include <stddef.h>
- #include <libkern/OSAtomic.h>
- #include "objc-private.h"
- #include "runtime.h"
- @interface __NSCopyable
- - (id)copyWithZone:(void *)zone;
- @end
- @interface __NSMutableCopyable
- - (id)mutableCopyWithZone:(void *)zone;
- @end
- StripedMap<spinlock_t> PropertyLocks;
- StripedMap<spinlock_t> StructLocks;
- StripedMap<spinlock_t> CppObjectLocks;
- #define MUTABLE_COPY 2
- id objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) {
- if (offset == 0) {
- return object_getClass(self);
- }
-
- id *slot = (id*) ((char*)self + offset);
- if (!atomic) return *slot;
-
-
- spinlock_t& slotlock = PropertyLocks[slot];
- slotlock.lock();
- id value = objc_retain(*slot);
- slotlock.unlock();
-
-
- return objc_autoreleaseReturnValue(value);
- }
- static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy) __attribute__((always_inline));
- static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
- {
- if (offset == 0) {
- object_setClass(self, newValue);
- return;
- }
- id oldValue;
- id *slot = (id*) ((char*)self + offset);
- if (copy) {
- newValue = [newValue copyWithZone:nil];
- } else if (mutableCopy) {
- newValue = [newValue mutableCopyWithZone:nil];
- } else {
- if (*slot == newValue) return;
- newValue = objc_retain(newValue);
- }
- if (!atomic) {
- oldValue = *slot;
- *slot = newValue;
- } else {
- spinlock_t& slotlock = PropertyLocks[slot];
- slotlock.lock();
- oldValue = *slot;
- *slot = newValue;
- slotlock.unlock();
- }
- objc_release(oldValue);
- }
- void objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy)
- {
- bool copy = (shouldCopy && shouldCopy != MUTABLE_COPY);
- bool mutableCopy = (shouldCopy == MUTABLE_COPY);
- reallySetProperty(self, _cmd, newValue, offset, atomic, copy, mutableCopy);
- }
- void objc_setProperty_atomic(id self, SEL _cmd, id newValue, ptrdiff_t offset)
- {
- reallySetProperty(self, _cmd, newValue, offset, true, false, false);
- }
- void objc_setProperty_nonatomic(id self, SEL _cmd, id newValue, ptrdiff_t offset)
- {
- reallySetProperty(self, _cmd, newValue, offset, false, false, false);
- }
- void objc_setProperty_atomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset)
- {
- reallySetProperty(self, _cmd, newValue, offset, true, true, false);
- }
- void objc_setProperty_nonatomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset)
- {
- reallySetProperty(self, _cmd, newValue, offset, false, true, false);
- }
- void objc_copyStruct(void *dest, const void *src, ptrdiff_t size, BOOL atomic, BOOL hasStrong __unused) {
- spinlock_t *srcLock = nil;
- spinlock_t *dstLock = nil;
- if (atomic) {
- srcLock = &StructLocks[src];
- dstLock = &StructLocks[dest];
- spinlock_t::lockTwo(srcLock, dstLock);
- }
- memmove(dest, src, size);
- if (atomic) {
- spinlock_t::unlockTwo(srcLock, dstLock);
- }
- }
- void objc_copyCppObjectAtomic(void *dest, const void *src, void (*copyHelper) (void *dest, const void *source)) {
- spinlock_t *srcLock = &CppObjectLocks[src];
- spinlock_t *dstLock = &CppObjectLocks[dest];
- spinlock_t::lockTwo(srcLock, dstLock);
-
- copyHelper(dest, src);
-
- spinlock_t::unlockTwo(srcLock, dstLock);
- }
|