Changeset 85497 in webkit for trunk/Source/JavaScriptCore/assembler/AssemblerBuffer.h
- Timestamp:
- May 2, 2011, 11:30:03 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/assembler/AssemblerBuffer.h
r85448 r85497 39 39 40 40 struct AssemblerLabel { 41 AssemblerLabel(uint32_t offset = std::numeric_limits<uint32_t>::max()) 41 AssemblerLabel() 42 : m_offset(std::numeric_limits<uint32_t>::max()) 43 { 44 } 45 46 explicit AssemblerLabel(uint32_t offset) 42 47 : m_offset(offset) 43 48 { … … 46 51 bool isSet() const { return (m_offset != std::numeric_limits<uint32_t>::max()); } 47 52 53 AssemblerLabel labelAtOffset(int offset) const 54 { 55 return AssemblerLabel(m_offset + offset); 56 } 57 48 58 uint32_t m_offset; 49 59 }; 50 60 51 61 class AssemblerBuffer { 52 static const int inlineCapacity = 128 - sizeof(char*) - 2 * sizeof(int);62 static const int inlineCapacity = 128; 53 63 public: 54 64 AssemblerBuffer() 55 : m_buffer(m_inlineBuffer) 65 : m_storage(inlineCapacity) 66 , m_buffer(m_storage.begin()) 56 67 , m_capacity(inlineCapacity) 57 , m_ size(0)68 , m_index(0) 58 69 { 59 COMPILE_ASSERT(sizeof(AssemblerBuffer) == 128, AssemblerBuffer_should_be_128_bytes);60 70 } 61 71 62 72 ~AssemblerBuffer() 63 73 { 64 if (m_buffer != m_inlineBuffer) 65 fastFree(m_buffer); 74 } 75 76 bool isAvailable(int space) 77 { 78 return m_index + space <= m_capacity; 66 79 } 67 80 68 81 void ensureSpace(int space) 69 82 { 70 if ( m_size > m_capacity - space)83 if (!isAvailable(space)) 71 84 grow(); 72 85 } … … 74 87 bool isAligned(int alignment) const 75 88 { 76 return !(m_size & (alignment - 1)); 77 } 78 79 void putByteUnchecked(int value) 80 { 81 ASSERT(!(m_size > m_capacity - 4)); 82 m_buffer[m_size] = value; 83 m_size++; 84 } 85 86 void putByte(int value) 87 { 88 if (m_size > m_capacity - 4) 89 grow(); 90 putByteUnchecked(value); 91 } 92 93 void putShortUnchecked(int value) 94 { 95 ASSERT(!(m_size > m_capacity - 4)); 96 *reinterpret_cast_ptr<short*>(&m_buffer[m_size]) = value; 97 m_size += 2; 98 } 99 100 void putShort(int value) 101 { 102 if (m_size > m_capacity - 4) 103 grow(); 104 putShortUnchecked(value); 105 } 106 107 void putIntUnchecked(int value) 108 { 109 ASSERT(!(m_size > m_capacity - 4)); 110 *reinterpret_cast_ptr<int*>(&m_buffer[m_size]) = value; 111 m_size += 4; 112 } 113 114 void putInt64Unchecked(int64_t value) 115 { 116 ASSERT(!(m_size > m_capacity - 8)); 117 *reinterpret_cast_ptr<int64_t*>(&m_buffer[m_size]) = value; 118 m_size += 8; 119 } 120 121 void putInt(int value) 122 { 123 if (m_size > m_capacity - 4) 124 grow(); 125 putIntUnchecked(value); 89 return !(m_index & (alignment - 1)); 126 90 } 127 91 … … 129 93 void putIntegral(IntegralType value) 130 94 { 131 if (m_size > m_capacity - sizeof(IntegralType)) 132 grow(); 95 ensureSpace(sizeof(IntegralType)); 133 96 putIntegralUnchecked(value); 134 97 } … … 137 100 void putIntegralUnchecked(IntegralType value) 138 101 { 139 *reinterpret_cast_ptr<IntegralType*>(&m_buffer[m_size]) = value; 140 m_size += sizeof(IntegralType); 102 ASSERT(isAvailable(sizeof(IntegralType))); 103 *reinterpret_cast_ptr<IntegralType*>(m_buffer + m_index) = value; 104 m_index += sizeof(IntegralType); 141 105 } 106 107 void putByteUnchecked(int8_t value) { putIntegralUnchecked(value); } 108 void putByte(int8_t value) { putIntegral(value); } 109 void putShortUnchecked(int16_t value) { putIntegralUnchecked(value); } 110 void putShort(int16_t value) { putIntegral(value); } 111 void putIntUnchecked(int32_t value) { putIntegralUnchecked(value); } 112 void putInt(int32_t value) { putIntegral(value); } 113 void putInt64Unchecked(int64_t value) { putIntegralUnchecked(value); } 114 void putInt64(int64_t value) { putIntegral(value); } 142 115 143 116 void* data() const … … 148 121 size_t codeSize() const 149 122 { 150 return m_ size;123 return m_index; 151 124 } 152 125 153 126 AssemblerLabel label() const 154 127 { 155 return AssemblerLabel(m_ size);128 return AssemblerLabel(m_index); 156 129 } 157 130 158 131 void* executableCopy(ExecutablePool* allocator) 159 132 { 160 if (!m_ size)133 if (!m_index) 161 134 return 0; 162 135 163 void* result = allocator->alloc(m_ size);136 void* result = allocator->alloc(m_index); 164 137 165 138 if (!result) 166 139 return 0; 167 140 168 ExecutableAllocator::makeWritable(result, m_ size);141 ExecutableAllocator::makeWritable(result, m_index); 169 142 170 return memcpy(result, m_buffer, m_ size);143 return memcpy(result, m_buffer, m_index); 171 144 } 172 145 173 void rewindTo Offset(int offset)146 void rewindToLabel(AssemblerLabel label) 174 147 { 175 ASSERT(offset >= 0); 176 m_size = offset; 148 m_index = label.m_offset; 177 149 } 178 150 179 151 #ifndef NDEBUG 180 unsigned debugOffset() { return m_ size; }152 unsigned debugOffset() { return m_index; } 181 153 #endif 182 154 … … 184 156 void append(const char* data, int size) 185 157 { 186 if ( m_size > m_capacity - size)158 if (!isAvailable(size)) 187 159 grow(size); 188 160 189 memcpy(m_buffer + m_ size, data, size);190 m_ size+= size;161 memcpy(m_buffer + m_index, data, size); 162 m_index += size; 191 163 } 192 164 … … 195 167 m_capacity += m_capacity / 2 + extraCapacity; 196 168 197 if (m_buffer == m_inlineBuffer) { 198 char* newBuffer = static_cast<char*>(fastMalloc(m_capacity)); 199 m_buffer = static_cast<char*>(memcpy(newBuffer, m_buffer, m_size)); 200 } else 201 m_buffer = static_cast<char*>(fastRealloc(m_buffer, m_capacity)); 169 m_storage.grow(m_capacity); 170 m_buffer = m_storage.begin(); 202 171 } 203 172 204 char m_inlineBuffer[inlineCapacity]; 173 private: 174 Vector<char, inlineCapacity> m_storage; 205 175 char* m_buffer; 206 176 int m_capacity; 207 int m_ size;177 int m_index; 208 178 }; 209 179
Note:
See TracChangeset
for help on using the changeset viewer.