]> git.saurik.com Git - cycript.git/commitdiff
Both tighten and correct alignment of pool malloc.
authorJay Freeman (saurik) <saurik@saurik.com>
Wed, 23 Dec 2015 02:01:08 +0000 (02:01 +0000)
committerJay Freeman (saurik) <saurik@saurik.com>
Wed, 23 Dec 2015 02:01:08 +0000 (02:01 +0000)
Execute.cpp
Pooling.hpp

index 6581ee1138f0910a02e833ff66cafe17c2f3787a..af61f1a226d3e41db969ba3f73384c216fd0f3cc 100644 (file)
@@ -890,11 +890,6 @@ static bool CString_setProperty(JSContextRef context, JSObjectRef object, JSStri
     return true;
 } CYCatch(false) }
 
-template <typename Type_>
-static void Align(Type_ &data, size_t size) {
-    data = reinterpret_cast<Type_>((reinterpret_cast<uintptr_t>(data) + (size - 1)) & ~(size - 1));
-}
-
 static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
     Type_privateData *typical(internal->type_);
     sig::Type *type(typical->type_);
@@ -931,13 +926,13 @@ static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *inter
   base:
     ffi_type **elements(typical->GetFFI()->elements);
 
-    base = reinterpret_cast<uint8_t *>(internal->value_);
+    size_t offset(0);
     for (ssize_t local(0); local != index; ++local) {
-        Align(base, elements[local]->alignment);
-        base += elements[local]->size;
+        offset += elements[local]->size;
+        CYAlign(offset, elements[local + 1]->alignment);
     }
 
-    Align(base, elements[index]->alignment);
+    base = reinterpret_cast<uint8_t *>(internal->value_) + offset;
     return true;
 }
 
index 78aa1b2ee91dde079bf77e0705baa437618d5d2c..3af05acf2ae74d11ef28bdf41c1bfe4849ac69c0 100644 (file)
 #include "Local.hpp"
 #include "Standard.hpp"
 
+// XXX: std::aligned_storage and alignof
+static const size_t CYAlignment(sizeof(void *));
+
+template <typename Type_>
+static void CYAlign(Type_ &data, size_t size) {
+    data = reinterpret_cast<Type_>((reinterpret_cast<uintptr_t>(data) + (size - 1)) & ~static_cast<uintptr_t>(size - 1));
+}
+
 class CYPool;
 _finline void *operator new(size_t size, CYPool &pool);
 _finline void *operator new [](size_t size, CYPool &pool);
@@ -58,11 +66,6 @@ class CYPool {
         }
     } *cleaner_;
 
-    static _finline size_t align(size_t size) {
-        // XXX: alignment is more complex than this
-        return (size + 7) & ~0x3;
-    }
-
     template <typename Type_>
     static void delete_(void *data) {
         reinterpret_cast<Type_ *>(data)->~Type_();
@@ -88,20 +91,27 @@ class CYPool {
     }
 
     template <typename Type_>
-    Type_ *malloc(size_t size) {
-        size = align(size);
-
-        if (size > size_) {
-            size_ = std::max<size_t>(next_, size + align(sizeof(Cleaner)));
+    Type_ *malloc(size_t size, size_t alignment = CYAlignment) {
+        uint8_t *end(data_);
+        CYAlign(end, alignment);
+        end += size;
+
+        if (size_t(end - data_) > size_) {
+            size_t need(sizeof(Cleaner));
+            CYAlign(need, alignment);
+            need += size;
+            size_ = std::max<size_t>(next_, need);
             next_ *= 2;
             data_ = reinterpret_cast<uint8_t *>(::malloc(size_));
             atexit(free, data_);
             _assert(size <= size_);
         }
 
-        void *data(data_);
-        data_ += size;
-        size_ -= size;
+        uint8_t *data(data_);
+        CYAlign(data, alignment);
+        end = data + size;
+        size_ -= end - data_;
+        data_ = end;
         return reinterpret_cast<Type_ *>(data);
     }
 
@@ -112,8 +122,8 @@ class CYPool {
     }
 
     template <typename Type_>
-    Type_ *memdup(const Type_ *data, size_t size) {
-        Type_ *copy(malloc<Type_>(size));
+    Type_ *memdup(const Type_ *data, size_t size, size_t alignment = CYAlignment) {
+        Type_ *copy(malloc<Type_>(size, alignment));
         memcpy(copy, data, size);
         return copy;
     }
@@ -123,7 +133,7 @@ class CYPool {
     }
 
     char *strmemdup(const char *data, size_t size) {
-        char *copy(malloc<char>(size + 1));
+        char *copy(malloc<char>(size + 1, 1));
         memcpy(copy, data, size);
         copy[size] = '\0';
         return copy;
@@ -142,7 +152,7 @@ class CYPool {
             va_end(args);
         }
 
-        char *copy(malloc<char>(size + 1)); {
+        char *copy(malloc<char>(size + 1, 1)); {
             va_list args;
             va_start(args, data);