]> git.saurik.com Git - apple/security.git/blobdiff - OSX/libsecurity_utilities/lib/seccfobject.h
Security-58286.260.20.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / seccfobject.h
index 2717d755d7b0b9e1e0bff3b6cec05da53a23937c..0cbec6b16b45bf632e2a0196c155a7b074c7100a 100644 (file)
 #include <CoreFoundation/CFRuntime.h>
 #include <new>
 #include "threading.h"
 #include <CoreFoundation/CFRuntime.h>
 #include <new>
 #include "threading.h"
+#include <os/lock.h>
+
+#if( __cplusplus <= 201103L)
+#include <stdatomic.h>
+#endif
 
 namespace Security {
 
 class CFClass;
 
 
 namespace Security {
 
 class CFClass;
 
-#define SECCFFUNCTIONS(OBJTYPE, APIPTR, ERRCODE, CFCLASS) \
-\
-void *operator new(size_t size) throw(std::bad_alloc) \
-{ return SecCFObject::allocate(size, CFCLASS); } \
+#define SECCFFUNCTIONS_BASE(OBJTYPE, APIPTR) \
 \
 operator APIPTR() const \
 { return (APIPTR)(this->operator CFTypeRef()); } \
 \
 operator APIPTR() const \
 { return (APIPTR)(this->operator CFTypeRef()); } \
@@ -45,7 +47,16 @@ operator APIPTR() const \
 OBJTYPE *retain() \
 { SecCFObject::handle(true); return this; } \
 APIPTR handle(bool retain = true) \
 OBJTYPE *retain() \
 { SecCFObject::handle(true); return this; } \
 APIPTR handle(bool retain = true) \
-{ return (APIPTR)SecCFObject::handle(retain); } \
+{ return (APIPTR)SecCFObject::handle(retain); }
+
+#define SECCFFUNCTIONS_CREATABLE(OBJTYPE, APIPTR, CFCLASS) \
+SECCFFUNCTIONS_BASE(OBJTYPE, APIPTR)\
+\
+void *operator new(size_t size) throw(std::bad_alloc) \
+{ return SecCFObject::allocate(size, CFCLASS); }
+
+#define SECCFFUNCTIONS(OBJTYPE, APIPTR, ERRCODE, CFCLASS) \
+SECCFFUNCTIONS_CREATABLE(OBJTYPE, APIPTR, CFCLASS) \
 \
 static OBJTYPE *required(APIPTR ptr) \
 { if (OBJTYPE *p = dynamic_cast<OBJTYPE *>(SecCFObject::required(ptr, ERRCODE))) \
 \
 static OBJTYPE *required(APIPTR ptr) \
 { if (OBJTYPE *p = dynamic_cast<OBJTYPE *>(SecCFObject::required(ptr, ERRCODE))) \
@@ -60,7 +71,7 @@ static OBJTYPE *optional(APIPTR ptr) \
 
 struct SecRuntimeBase: CFRuntimeBase
 {
 
 struct SecRuntimeBase: CFRuntimeBase
 {
-       bool isNew;
+       atomic_flag isOld;
 };
 
 class SecCFObject
 };
 
 class SecCFObject
@@ -72,16 +83,16 @@ private:
        static const size_t kAlignedRuntimeSize = SECALIGNUP(sizeof(SecRuntimeBase), 4);
 
     uint32_t mRetainCount;
        static const size_t kAlignedRuntimeSize = SECALIGNUP(sizeof(SecRuntimeBase), 4);
 
     uint32_t mRetainCount;
-    OSSpinLock mRetainSpinLock;
+    os_unfair_lock mRetainLock;
 
 public:
        // For use by SecPointer only. Returns true once the first time it's called after the object has been created.
        bool isNew()
        {
                SecRuntimeBase *base = reinterpret_cast<SecRuntimeBase *>(reinterpret_cast<uint8_t *>(this) - kAlignedRuntimeSize);
 
 public:
        // For use by SecPointer only. Returns true once the first time it's called after the object has been created.
        bool isNew()
        {
                SecRuntimeBase *base = reinterpret_cast<SecRuntimeBase *>(reinterpret_cast<uint8_t *>(this) - kAlignedRuntimeSize);
-               bool isNew = base->isNew;
-               base->isNew = false;
-               return isNew;
+
+        // atomic flags start clear, and like to go high.
+        return !atomic_flag_test_and_set(&(base->isOld));
        }
 
        static SecCFObject *optional(CFTypeRef) throw();
        }
 
        static SecCFObject *optional(CFTypeRef) throw();
@@ -94,7 +105,7 @@ public:
     uint32_t getRetainCount() {return updateRetainCount(0, NULL);}
 
        static void operator delete(void *object) throw();
     uint32_t getRetainCount() {return updateRetainCount(0, NULL);}
 
        static void operator delete(void *object) throw();
-       operator CFTypeRef() const throw()
+       virtual operator CFTypeRef() const throw()
        {
                return reinterpret_cast<CFTypeRef>(reinterpret_cast<const uint8_t *>(this) - kAlignedRuntimeSize);
        }
        {
                return reinterpret_cast<CFTypeRef>(reinterpret_cast<const uint8_t *>(this) - kAlignedRuntimeSize);
        }
@@ -107,7 +118,7 @@ public:
        virtual CFStringRef copyFormattingDesc(CFDictionaryRef dict);
        virtual CFStringRef copyDebugDesc();
        virtual void aboutToDestruct();
        virtual CFStringRef copyFormattingDesc(CFDictionaryRef dict);
        virtual CFStringRef copyDebugDesc();
        virtual void aboutToDestruct();
-       virtual Mutex* getMutexForObject();
+       virtual Mutex* getMutexForObject() const;
     virtual bool mayDelete();
 };
 
     virtual bool mayDelete();
 };