]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - assembler/AssemblerBuffer.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / assembler / AssemblerBuffer.h
index d82c0b946930853131043150c3c5fcbe7fb13fb5..3632a5b6ef3cf8fd100e29593756945394a3227f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008, 2012 Apple Inc. All rights reserved.
+ * Copyright (C) 2008, 2012, 2014 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,7 +30,6 @@
 
 #include "ExecutableAllocator.h"
 #include "JITCompilationEffort.h"
-#include "VM.h"
 #include "stdint.h"
 #include <string.h>
 #include <wtf/Assertions.h>
@@ -60,50 +59,80 @@ namespace JSC {
         uint32_t m_offset;
     };
 
-    class AssemblerBuffer {
-        static const int inlineCapacity = 128;
+    class AssemblerData {
     public:
-        AssemblerBuffer()
-            : m_storage(inlineCapacity)
-            , m_buffer(m_storage.begin())
-            , m_capacity(inlineCapacity)
-            , m_index(0)
+        AssemblerData()
+            : m_buffer(nullptr)
+            , m_capacity(0)
         {
         }
 
-        ~AssemblerBuffer()
+        AssemblerData(unsigned initialCapacity)
         {
+            m_capacity = initialCapacity;
+            m_buffer = static_cast<char*>(fastMalloc(m_capacity));
         }
 
-        bool isAvailable(int space)
+        AssemblerData(AssemblerData&& other)
         {
-            return m_index + space <= m_capacity;
+            m_buffer = other.m_buffer;
+            other.m_buffer = nullptr;
+            m_capacity = other.m_capacity;
+            other.m_capacity = 0;
         }
 
-        void ensureSpace(int space)
+        AssemblerData& operator=(AssemblerData&& other)
         {
-            if (!isAvailable(space))
-                grow();
+            m_buffer = other.m_buffer;
+            other.m_buffer = nullptr;
+            m_capacity = other.m_capacity;
+            other.m_capacity = 0;
+            return *this;
         }
 
-        bool isAligned(int alignment) const
+        ~AssemblerData()
         {
-            return !(m_index & (alignment - 1));
+            fastFree(m_buffer);
         }
 
-        template<typename IntegralType>
-        void putIntegral(IntegralType value)
+        char* buffer() const { return m_buffer; }
+
+        unsigned capacity() const { return m_capacity; }
+
+        void grow(unsigned extraCapacity = 0)
         {
-            ensureSpace(sizeof(IntegralType));
-            putIntegralUnchecked(value);
+            m_capacity = m_capacity + m_capacity / 2 + extraCapacity;
+            m_buffer = static_cast<char*>(fastRealloc(m_buffer, m_capacity));
         }
 
-        template<typename IntegralType>
-        void putIntegralUnchecked(IntegralType value)
+    private:
+        char* m_buffer;
+        unsigned m_capacity;
+    };
+
+    class AssemblerBuffer {
+        static const int initialCapacity = 128;
+    public:
+        AssemblerBuffer()
+            : m_storage(initialCapacity)
+            , m_index(0)
         {
-            ASSERT(isAvailable(sizeof(IntegralType)));
-            *reinterpret_cast_ptr<IntegralType*>(m_buffer + m_index) = value;
-            m_index += sizeof(IntegralType);
+        }
+
+        bool isAvailable(int space)
+        {
+            return m_index + space <= m_storage.capacity();
+        }
+
+        void ensureSpace(int space)
+        {
+            if (!isAvailable(space))
+                grow();
+        }
+
+        bool isAligned(int alignment) const
+        {
+            return !(m_index & (alignment - 1));
         }
 
         void putByteUnchecked(int8_t value) { putIntegralUnchecked(value); }
@@ -117,7 +146,7 @@ namespace JSC {
 
         void* data() const
         {
-            return m_buffer;
+            return m_storage.buffer();
         }
 
         size_t codeSize() const
@@ -130,48 +159,47 @@ namespace JSC {
             return AssemblerLabel(m_index);
         }
 
-        PassRefPtr<ExecutableMemoryHandle> executableCopy(VM& vm, void* ownerUID, JITCompilationEffort effort)
-        {
-            if (!m_index)
-                return 0;
-
-            RefPtr<ExecutableMemoryHandle> result = vm.executableAllocator.allocate(vm, m_index, ownerUID, effort);
-
-            if (!result)
-                return 0;
+        unsigned debugOffset() { return m_index; }
 
-            ExecutableAllocator::makeWritable(result->start(), result->sizeInBytes());
+        AssemblerData releaseAssemblerData() { return WTF::move(m_storage); }
 
-            memcpy(result->start(), m_buffer, m_index);
-            
-            return result.release();
+    protected:
+        template<typename IntegralType>
+        void putIntegral(IntegralType value)
+        {
+            unsigned nextIndex = m_index + sizeof(IntegralType);
+            if (UNLIKELY(nextIndex > m_storage.capacity()))
+                grow();
+            ASSERT(isAvailable(sizeof(IntegralType)));
+            *reinterpret_cast_ptr<IntegralType*>(m_storage.buffer() + m_index) = value;
+            m_index = nextIndex;
         }
 
-        unsigned debugOffset() { return m_index; }
+        template<typename IntegralType>
+        void putIntegralUnchecked(IntegralType value)
+        {
+            ASSERT(isAvailable(sizeof(IntegralType)));
+            *reinterpret_cast_ptr<IntegralType*>(m_storage.buffer() + m_index) = value;
+            m_index += sizeof(IntegralType);
+        }
 
-    protected:
         void append(const char* data, int size)
         {
             if (!isAvailable(size))
                 grow(size);
 
-            memcpy(m_buffer + m_index, data, size);
+            memcpy(m_storage.buffer() + m_index, data, size);
             m_index += size;
         }
 
         void grow(int extraCapacity = 0)
         {
-            m_capacity += m_capacity / 2 + extraCapacity;
-
-            m_storage.grow(m_capacity);
-            m_buffer = m_storage.begin();
+            m_storage.grow(extraCapacity);
         }
 
     private:
-        Vector<char, inlineCapacity, UnsafeVectorOverflow> m_storage;
-        char* m_buffer;
-        int m_capacity;
-        int m_index;
+        AssemblerData m_storage;
+        unsigned m_index;
     };
 
 } // namespace JSC