Ignore:
Timestamp:
Mar 20, 2009, 3:56:27 PM (16 years ago)
Author:
[email protected]
Message:

2009-03-20 Geoffrey Garen <[email protected]>

Reviewed by Oliver Hunt.


A little cleanup in the RegisterFile code.


Moved large inline functions out of the class declaration, to make it
more readable.


Switched over to using the roundUpAllocationSize function to avoid
duplicate code and subtle bugs.


Renamed m_maxCommitted to m_commitEnd, to match m_end.


Renamed allocationSize to commitSize because it's the chunk size for
committing memory, not allocating memory.


SunSpider reports no change.

  • interpreter/RegisterFile.h: (JSC::RegisterFile::RegisterFile): (JSC::RegisterFile::shrink): (JSC::RegisterFile::grow):
  • jit/ExecutableAllocator.h: (JSC::roundUpAllocationSize):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/interpreter/RegisterFile.h

    r39673 r41872  
    3030#define RegisterFile_h
    3131
     32#include "ExecutableAllocator.h"
    3233#include "Register.h"
    3334#include "Collector.h"
     
    112113        static const size_t defaultCapacity = 524288;
    113114        static const size_t defaultMaxGlobals = 8192;
    114         static const size_t allocationSize = 1 << 14;
    115         static const size_t allocationSizeMask = allocationSize - 1;
    116 
    117         RegisterFile(size_t capacity = defaultCapacity, size_t maxGlobals = defaultMaxGlobals)
    118             : m_numGlobals(0)
    119             , m_maxGlobals(maxGlobals)
    120             , m_start(0)
    121             , m_end(0)
    122             , m_max(0)
    123             , m_buffer(0)
    124             , m_globalObject(0)
    125         {
    126             size_t bufferLength = (capacity + maxGlobals) * sizeof(Register);
    127 #if HAVE(MMAP)
    128             m_buffer = static_cast<Register*>(mmap(0, bufferLength, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0));
    129             if (m_buffer == MAP_FAILED) {
    130                 fprintf(stderr, "Could not allocate register file: %d\n", errno);
    131                 CRASH();
    132             }
    133 #elif HAVE(VIRTUALALLOC)
    134             // Ensure bufferLength is a multiple of allocation size
    135             bufferLength = (bufferLength + allocationSizeMask) & ~allocationSizeMask;
    136             m_buffer = static_cast<Register*>(VirtualAlloc(0, bufferLength, MEM_RESERVE, PAGE_READWRITE));
    137             if (!m_buffer) {
    138                 fprintf(stderr, "Could not allocate register file: %d\n", errno);
    139                 CRASH();
    140             }
    141             int initialAllocation = (maxGlobals * sizeof(Register) + allocationSizeMask) & ~allocationSizeMask;
    142             void* commitCheck = VirtualAlloc(m_buffer, initialAllocation, MEM_COMMIT, PAGE_READWRITE);
    143             if (commitCheck != m_buffer) {
    144                 fprintf(stderr, "Could not allocate register file: %d\n", errno);
    145                 CRASH();
    146             }
    147             m_maxCommitted = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_buffer) + initialAllocation);
    148 #else
    149             #error "Don't know how to reserve virtual memory on this platform."
    150 #endif
    151             m_start = m_buffer + maxGlobals;
    152             m_end = m_start;
    153             m_max = m_start + capacity;
    154         }
    155 
     115        static const size_t commitSize = 1 << 14;
     116
     117        RegisterFile(size_t capacity = defaultCapacity, size_t maxGlobals = defaultMaxGlobals);
    156118        ~RegisterFile();
    157119
     
    163125        JSGlobalObject* globalObject() { return m_globalObject; }
    164126
    165         void shrink(Register* newEnd)
    166         {
    167             if (newEnd < m_end)
    168                 m_end = newEnd;
    169         }
    170 
    171         bool grow(Register* newEnd)
    172         {
    173             if (newEnd > m_end) {
    174                 if (newEnd > m_max)
    175                     return false;
    176 #if !HAVE(MMAP) && HAVE(VIRTUALALLOC)
    177                 if (newEnd > m_maxCommitted) {
    178                     ptrdiff_t additionalAllocation = ((reinterpret_cast<char*>(newEnd)  - reinterpret_cast<char*>(m_maxCommitted)) + allocationSizeMask) & ~allocationSizeMask;
    179                     if (!VirtualAlloc(m_maxCommitted, additionalAllocation, MEM_COMMIT, PAGE_READWRITE)) {
    180                         fprintf(stderr, "Could not allocate register file: %d\n", errno);
    181                         CRASH();
    182                     }
    183                     m_maxCommitted = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_maxCommitted) + additionalAllocation);
    184                 }
    185 #endif
    186                 m_end = newEnd;
    187             }
    188             return true;
    189         }
     127        bool grow(Register* newEnd);
     128        void shrink(Register* newEnd);
    190129       
    191130        void setNumGlobals(size_t numGlobals) { m_numGlobals = numGlobals; }
     
    206145        Register* m_buffer;
    207146#if HAVE(VIRTUALALLOC)
    208         Register* m_maxCommitted;
     147        Register* m_commitEnd;
    209148#endif
    210149
     
    212151    };
    213152
     153    inline RegisterFile::RegisterFile(size_t capacity, size_t maxGlobals)
     154        : m_numGlobals(0)
     155        , m_maxGlobals(maxGlobals)
     156        , m_start(0)
     157        , m_end(0)
     158        , m_max(0)
     159        , m_buffer(0)
     160        , m_globalObject(0)
     161    {
     162        size_t bufferLength = (capacity + maxGlobals) * sizeof(Register);
     163    #if HAVE(MMAP)
     164        m_buffer = static_cast<Register*>(mmap(0, bufferLength, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0));
     165        if (m_buffer == MAP_FAILED) {
     166            fprintf(stderr, "Could not allocate register file: %d\n", errno);
     167            CRASH();
     168        }
     169    #elif HAVE(VIRTUALALLOC)
     170        m_buffer = static_cast<Register*>(VirtualAlloc(0, roundUpAllocationSize(bufferLength, commitSize), MEM_RESERVE, PAGE_READWRITE));
     171        if (!m_buffer) {
     172            fprintf(stderr, "Could not allocate register file: %d\n", errno);
     173            CRASH();
     174        }
     175        size_t committedSize = roundUpAllocationSize(maxGlobals * sizeof(Register), commitSize);
     176        void* commitCheck = VirtualAlloc(m_buffer, committedSize, MEM_COMMIT, PAGE_READWRITE);
     177        if (commitCheck != m_buffer) {
     178            fprintf(stderr, "Could not allocate register file: %d\n", errno);
     179            CRASH();
     180        }
     181        m_commitEnd = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_buffer) + committedSize);
     182    #else
     183        #error "Don't know how to reserve virtual memory on this platform."
     184    #endif
     185        m_start = m_buffer + maxGlobals;
     186        m_end = m_start;
     187        m_max = m_start + capacity;
     188    }
     189
     190    inline void RegisterFile::shrink(Register* newEnd)
     191    {
     192        if (newEnd < m_end)
     193            m_end = newEnd;
     194    }
     195
     196    inline bool RegisterFile::grow(Register* newEnd)
     197    {
     198        if (newEnd < m_end)
     199            return true;
     200
     201        if (newEnd > m_max)
     202            return false;
     203
     204#if !HAVE(MMAP) && HAVE(VIRTUALALLOC)
     205        if (newEnd > m_commitEnd) {
     206            size_t size = roundUpAllocationSize(reinterpret_cast<char*>(newEnd) - reinterpret_cast<char*>(m_commitEnd), commitSize);
     207            if (!VirtualAlloc(m_commitEnd, size, MEM_COMMIT, PAGE_READWRITE)) {
     208                fprintf(stderr, "Could not allocate register file: %d\n", errno);
     209                CRASH();
     210            }
     211            m_commitEnd = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_commitEnd) + size);
     212        }
     213#endif
     214
     215        m_end = newEnd;
     216        return true;
     217    }
     218
    214219} // namespace JSC
    215220
Note: See TracChangeset for help on using the changeset viewer.