JavaScriptCore/assembler/AssemblerBuffer.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2008 Apple Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     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.
       
    12  *
       
    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. 
       
    24  */
       
    25 
       
    26 #ifndef AssemblerBuffer_h
       
    27 #define AssemblerBuffer_h
       
    28 
       
    29 #if ENABLE(ASSEMBLER)
       
    30 
       
    31 #include "stdint.h"
       
    32 #include <string.h>
       
    33 #include <jit/ExecutableAllocator.h>
       
    34 #include <wtf/Assertions.h>
       
    35 #include <wtf/FastMalloc.h>
       
    36 
       
    37 namespace JSC {
       
    38 
       
    39     class AssemblerBuffer {
       
    40         static const int inlineCapacity = 128 - sizeof(char*) - 2 * sizeof(int);
       
    41     public:
       
    42         AssemblerBuffer()
       
    43             : m_buffer(m_inlineBuffer)
       
    44             , m_capacity(inlineCapacity)
       
    45             , m_size(0)
       
    46         {
       
    47             COMPILE_ASSERT(sizeof(AssemblerBuffer) == 128, AssemblerBuffer_should_be_128_bytes);
       
    48         }
       
    49 
       
    50         ~AssemblerBuffer()
       
    51         {
       
    52             if (m_buffer != m_inlineBuffer)
       
    53                 fastFree(m_buffer);
       
    54         }
       
    55 
       
    56         void ensureSpace(int space)
       
    57         {
       
    58             if (m_size > m_capacity - space)
       
    59                 grow();
       
    60         }
       
    61 
       
    62         bool isAligned(int alignment) const
       
    63         {
       
    64             return !(m_size & (alignment - 1));
       
    65         }
       
    66 
       
    67         void putByteUnchecked(int value)
       
    68         {
       
    69             ASSERT(!(m_size > m_capacity - 4));
       
    70             m_buffer[m_size] = value;
       
    71             m_size++;
       
    72         }
       
    73 
       
    74         void putByte(int value)
       
    75         {
       
    76             if (m_size > m_capacity - 4)
       
    77                 grow();
       
    78             putByteUnchecked(value);
       
    79         }
       
    80 
       
    81         void putShortUnchecked(int value)
       
    82         {
       
    83             ASSERT(!(m_size > m_capacity - 4));
       
    84             *reinterpret_cast<short*>(&m_buffer[m_size]) = value;
       
    85             m_size += 2;
       
    86         }
       
    87 
       
    88         void putShort(int value)
       
    89         {
       
    90             if (m_size > m_capacity - 4)
       
    91                 grow();
       
    92             putShortUnchecked(value);
       
    93         }
       
    94 
       
    95         void putIntUnchecked(int value)
       
    96         {
       
    97             ASSERT(!(m_size > m_capacity - 4));
       
    98             *reinterpret_cast<int*>(&m_buffer[m_size]) = value;
       
    99             m_size += 4;
       
   100         }
       
   101 
       
   102         void putInt64Unchecked(int64_t value)
       
   103         {
       
   104             ASSERT(!(m_size > m_capacity - 8));
       
   105             *reinterpret_cast<int64_t*>(&m_buffer[m_size]) = value;
       
   106             m_size += 8;
       
   107         }
       
   108 
       
   109         void putInt(int value)
       
   110         {
       
   111             if (m_size > m_capacity - 4)
       
   112                 grow();
       
   113             putIntUnchecked(value);
       
   114         }
       
   115 
       
   116         void* data() const
       
   117         {
       
   118             return m_buffer;
       
   119         }
       
   120 
       
   121         int size() const
       
   122         {
       
   123             return m_size;
       
   124         }
       
   125 
       
   126         void* executableCopy(ExecutablePool* allocator)
       
   127         {
       
   128             if (!m_size)
       
   129                 return 0;
       
   130 
       
   131             void* result = allocator->alloc(m_size);
       
   132 
       
   133             if (!result)
       
   134                 return 0;
       
   135 
       
   136             ExecutableAllocator::makeWritable(result, m_size);
       
   137 
       
   138             return memcpy(result, m_buffer, m_size);
       
   139         }
       
   140 
       
   141     protected:
       
   142         void append(const char* data, int size)
       
   143         {
       
   144             if (m_size > m_capacity - size)
       
   145                 grow(size);
       
   146 
       
   147             memcpy(m_buffer + m_size, data, size);
       
   148             m_size += size;
       
   149         }
       
   150 
       
   151         void grow(int extraCapacity = 0)
       
   152         {
       
   153             m_capacity += m_capacity / 2 + extraCapacity;
       
   154 
       
   155             if (m_buffer == m_inlineBuffer) {
       
   156                 char* newBuffer = static_cast<char*>(fastMalloc(m_capacity));
       
   157                 m_buffer = static_cast<char*>(memcpy(newBuffer, m_buffer, m_size));
       
   158             } else
       
   159                 m_buffer = static_cast<char*>(fastRealloc(m_buffer, m_capacity));
       
   160         }
       
   161 
       
   162         char m_inlineBuffer[inlineCapacity];
       
   163         char* m_buffer;
       
   164         int m_capacity;
       
   165         int m_size;
       
   166     };
       
   167 
       
   168 } // namespace JSC
       
   169 
       
   170 #endif // ENABLE(ASSEMBLER)
       
   171 
       
   172 #endif // AssemblerBuffer_h