2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef AssemblerBuffer_h
27 #define AssemblerBuffer_h
29 #include <wtf/Platform.h>
35 #include <jit/ExecutableAllocator.h>
36 #include <wtf/Assertions.h>
37 #include <wtf/FastMalloc.h>
41 class AssemblerBuffer
{
42 static const int inlineCapacity
= 256;
45 : m_buffer(m_inlineBuffer
)
46 , m_capacity(inlineCapacity
)
53 if (m_buffer
!= m_inlineBuffer
)
57 void ensureSpace(int space
)
59 if (m_size
> m_capacity
- space
)
63 bool isAligned(int alignment
) const
65 return !(m_size
& (alignment
- 1));
68 void putByteUnchecked(int value
)
70 ASSERT(!(m_size
> m_capacity
- 4));
71 m_buffer
[m_size
] = value
;
75 void putByte(int value
)
77 if (m_size
> m_capacity
- 4)
79 putByteUnchecked(value
);
82 void putShortUnchecked(int value
)
84 ASSERT(!(m_size
> m_capacity
- 4));
85 *reinterpret_cast<short*>(&m_buffer
[m_size
]) = value
;
89 void putShort(int value
)
91 if (m_size
> m_capacity
- 4)
93 putShortUnchecked(value
);
96 void putIntUnchecked(int value
)
98 ASSERT(!(m_size
> m_capacity
- 4));
99 *reinterpret_cast<int*>(&m_buffer
[m_size
]) = value
;
103 void putInt64Unchecked(int64_t value
)
105 ASSERT(!(m_size
> m_capacity
- 8));
106 *reinterpret_cast<int64_t*>(&m_buffer
[m_size
]) = value
;
110 void putInt(int value
)
112 if (m_size
> m_capacity
- 4)
114 putIntUnchecked(value
);
127 void* executableCopy(ExecutablePool
* allocator
)
132 void* result
= allocator
->alloc(m_size
);
137 ExecutableAllocator::makeWritable(result
, m_size
);
139 return memcpy(result
, m_buffer
, m_size
);
143 void append(const char* data
, int size
)
145 if (m_size
> m_capacity
- size
)
148 memcpy(m_buffer
+ m_size
, data
, size
);
152 void grow(int extraCapacity
= 0)
154 m_capacity
+= m_capacity
/ 2 + extraCapacity
;
156 if (m_buffer
== m_inlineBuffer
) {
157 char* newBuffer
= static_cast<char*>(fastMalloc(m_capacity
));
158 m_buffer
= static_cast<char*>(memcpy(newBuffer
, m_buffer
, m_size
));
160 m_buffer
= static_cast<char*>(fastRealloc(m_buffer
, m_capacity
));
163 char m_inlineBuffer
[inlineCapacity
];
171 #endif // ENABLE(ASSEMBLER)
173 #endif // AssemblerBuffer_h