X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/4be4e30906bcb8ee30b4d189205cb70bad6707ce..81345200c95645a1b0d2635520f96ad55dfde63f:/assembler/AssemblerBuffer.h diff --git a/assembler/AssemblerBuffer.h b/assembler/AssemblerBuffer.h index d82c0b9..1c33fe1 100644 --- a/assembler/AssemblerBuffer.h +++ b/assembler/AssemblerBuffer.h @@ -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 #include @@ -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 = fastMallocGoodSize(initialCapacity); + m_buffer = static_cast(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 - 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 = fastMallocGoodSize(m_capacity + m_capacity / 2 + extraCapacity); + m_buffer = static_cast(fastRealloc(m_buffer, m_capacity)); } - template - 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(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 executableCopy(VM& vm, void* ownerUID, JITCompilationEffort effort) - { - if (!m_index) - return 0; - - RefPtr 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 + void putIntegral(IntegralType value) + { + unsigned nextIndex = m_index + sizeof(IntegralType); + if (UNLIKELY(nextIndex > m_storage.capacity())) + grow(); + ASSERT(isAvailable(sizeof(IntegralType))); + *reinterpret_cast_ptr(m_storage.buffer() + m_index) = value; + m_index = nextIndex; } - unsigned debugOffset() { return m_index; } + template + void putIntegralUnchecked(IntegralType value) + { + ASSERT(isAvailable(sizeof(IntegralType))); + *reinterpret_cast_ptr(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 m_storage; - char* m_buffer; - int m_capacity; - int m_index; + AssemblerData m_storage; + unsigned m_index; }; } // namespace JSC