]> git.saurik.com Git - cycript.git/blobdiff - Pooling.hpp
@encode syntax doesn't require Objective-C support.
[cycript.git] / Pooling.hpp
index bd37c5c2d77f8d8be13ebcaf750bd0f65eaa4ee1..6ff7ebf9c6b4f6389a8f23bf987119adb318a8fb 100644 (file)
@@ -62,6 +62,11 @@ class CYPool {
         return (size + 7) & ~0x3;
     }
 
+    template <typename Type_>
+    static void delete_(void *data) {
+        reinterpret_cast<Type_ *>(data)->~Type_();
+    }
+
     CYPool(const CYPool &);
 
   public:
@@ -99,6 +104,8 @@ class CYPool {
     }
 
     char *strdup(const char *data) {
+        if (data == NULL)
+            return NULL;
         return reinterpret_cast<char *>(memdup(data, strlen(data) + 1));
     }
 
@@ -180,6 +187,13 @@ class CYPool {
     }
 
     void atexit(void (*code)(void *), void *data = NULL);
+
+    template <typename Type_>
+    Type_ &object() {
+        Type_ *value(new(*this) Type_());
+        atexit(&delete_<Type_>, value);
+        return *value;
+    }
 };
 
 _finline void *operator new(size_t size, CYPool &pool) {
@@ -298,4 +312,7 @@ class CYLocalPool :
 #define $pool \
     (*CYLocal<CYPool>::Get())
 
+template <>
+::pthread_key_t CYLocal<CYPool>::key_;
+
 #endif/*CYCRIPT_POOLING_HPP*/