Changeset 64782 in webkit for trunk/JavaScriptCore/interpreter


Ignore:
Timestamp:
Aug 5, 2010, 1:57:33 PM (15 years ago)
Author:
[email protected]
Message:

Bug 43185 - Switch RegisterFile over to use PageAllocation

Reviewed by Sam Weinig.

Remove platform-specific memory allocation code.

  • interpreter/RegisterFile.cpp:

(JSC::RegisterFile::~RegisterFile):
(JSC::RegisterFile::releaseExcessCapacity):

  • interpreter/RegisterFile.h:

(JSC::RegisterFile::RegisterFile):
(JSC::RegisterFile::grow):
(JSC::RegisterFile::checkAllocatedOkay):

  • wtf/PageAllocation.cpp:

(WTF::PageAllocation::lastError):

  • wtf/PageAllocation.h:

(WTF::PageAllocation::allocate):
(WTF::PageAllocation::allocateAt):
(WTF::PageAllocation::allocateAligned):
(WTF::PageAllocation::pageSize):
(WTF::PageAllocation::isPageAligned):
(WTF::PageAllocation::isPowerOfTwo):

  • wtf/PageReservation.h:

(WTF::PageReservation::commit):
(WTF::PageReservation::decommit):
(WTF::PageReservation::reserve):
(WTF::PageReservation::reserveAt):

Location:
trunk/JavaScriptCore/interpreter
Files:
2 edited

Legend:

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

    r58267 r64782  
    3636RegisterFile::~RegisterFile()
    3737{
    38 #if HAVE(MMAP)
    39     munmap(m_buffer, ((m_max - m_start) + m_maxGlobals) * sizeof(Register));
    40 #elif HAVE(VIRTUALALLOC)
    41 #if OS(WINCE)
    42     VirtualFree(m_buffer, DWORD(m_commitEnd) - DWORD(m_buffer), MEM_DECOMMIT);
    43 #endif
    44     VirtualFree(m_buffer, 0, MEM_RELEASE);
    45 #else
    46     fastFree(m_buffer);
    47 #endif
     38    void* base = m_reservation.base();
     39    m_reservation.decommit(base, reinterpret_cast<intptr_t>(m_commitEnd) - reinterpret_cast<intptr_t>(base));
     40    m_reservation.deallocate();
    4841}
    4942
    5043void RegisterFile::releaseExcessCapacity()
    5144{
    52 #if HAVE(MMAP) && HAVE(MADV_FREE) && !HAVE(VIRTUALALLOC)
    53     while (madvise(m_start, (m_max - m_start) * sizeof(Register), MADV_FREE) == -1 && errno == EAGAIN) { }
    54 #elif HAVE(VIRTUALALLOC)
    55     VirtualFree(m_start, (m_max - m_start) * sizeof(Register), MEM_DECOMMIT);
     45    m_reservation.decommit(m_start, reinterpret_cast<intptr_t>(m_commitEnd) - reinterpret_cast<intptr_t>(m_start));
    5646    m_commitEnd = m_start;
    57 #endif
    5847    m_maxUsed = m_start;
    5948}
  • trunk/JavaScriptCore/interpreter/RegisterFile.h

    r59939 r64782  
    3636#include <stdio.h>
    3737#include <wtf/Noncopyable.h>
     38#include <wtf/PageReservation.h>
    3839#include <wtf/VMTags.h>
    39 
    40 #if HAVE(MMAP)
    41 #include <errno.h>
    42 #include <sys/mman.h>
    43 #endif
    4440
    4541namespace JSC {
     
    110106        enum { ProgramCodeThisRegister = -CallFrameHeaderSize - 1 };
    111107
    112         static const size_t defaultCapacity = 524288;
    113         static const size_t defaultMaxGlobals = 8192;
    114         static const size_t commitSize = 1 << 14;
     108        static const size_t defaultCapacity = 512 * 1024;
     109        static const size_t defaultMaxGlobals = 8 * 1024;
     110        static const size_t commitSize = 16 * 1024;
    115111        // Allow 8k of excess registers before we start trying to reap the registerfile
    116112        static const ptrdiff_t maxExcessCapacity = 8 * 1024;
     
    140136
    141137    private:
     138        void checkAllocatedOkay(bool okay);
     139
    142140        void releaseExcessCapacity();
    143141        size_t m_numGlobals;
     
    146144        Register* m_end;
    147145        Register* m_max;
    148         Register* m_buffer;
    149146        Register* m_maxUsed;
    150 
    151 #if HAVE(VIRTUALALLOC)
    152147        Register* m_commitEnd;
    153 #endif
     148        PageReservation m_reservation;
    154149
    155150        WeakGCPtr<JSGlobalObject> m_globalObject; // The global object whose vars are currently stored in the register file.
    156151    };
    157 
    158     // FIXME: Add a generic getpagesize() to WTF, then move this function to WTF as well.
    159     inline bool isPageAligned(size_t size) { return size != 0 && size % (8 * 1024) == 0; }
    160152
    161153    inline RegisterFile::RegisterFile(size_t capacity, size_t maxGlobals)
     
    165157        , m_end(0)
    166158        , m_max(0)
    167         , m_buffer(0)
    168     {
    169         // Verify that our values will play nice with mmap and VirtualAlloc.
    170         ASSERT(isPageAligned(maxGlobals));
    171         ASSERT(isPageAligned(capacity));
     159    {
     160        ASSERT(maxGlobals && PageAllocation::isPageAligned(maxGlobals));
     161        ASSERT(capacity && PageAllocation::isPageAligned(capacity));
    172162
    173163        size_t bufferLength = (capacity + maxGlobals) * sizeof(Register);
    174     #if HAVE(MMAP)
    175         m_buffer = static_cast<Register*>(mmap(0, bufferLength, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, VM_TAG_FOR_REGISTERFILE_MEMORY, 0));
    176         if (m_buffer == MAP_FAILED) {
    177 #if OS(WINCE)
    178             fprintf(stderr, "Could not allocate register file: %d\n", GetLastError());
    179 #else
    180             fprintf(stderr, "Could not allocate register file: %d\n", errno);
    181 #endif
    182             CRASH();
    183         }
    184     #elif HAVE(VIRTUALALLOC)
    185         m_buffer = static_cast<Register*>(VirtualAlloc(0, roundUpAllocationSize(bufferLength, commitSize), MEM_RESERVE, PAGE_READWRITE));
    186         if (!m_buffer) {
    187 #if OS(WINCE)
    188             fprintf(stderr, "Could not allocate register file: %d\n", GetLastError());
    189 #else
    190             fprintf(stderr, "Could not allocate register file: %d\n", errno);
    191 #endif
    192             CRASH();
    193         }
     164        m_reservation = PageReservation::reserve(roundUpAllocationSize(bufferLength, commitSize), PageAllocation::JSVMStackPages);
     165        void* base = m_reservation.base();
     166        checkAllocatedOkay(base);
    194167        size_t committedSize = roundUpAllocationSize(maxGlobals * sizeof(Register), commitSize);
    195         void* commitCheck = VirtualAlloc(m_buffer, committedSize, MEM_COMMIT, PAGE_READWRITE);
    196         if (commitCheck != m_buffer) {
    197 #if OS(WINCE)
    198             fprintf(stderr, "Could not allocate register file: %d\n", GetLastError());
    199 #else
    200             fprintf(stderr, "Could not allocate register file: %d\n", errno);
    201 #endif
    202             CRASH();
    203         }
    204         m_commitEnd = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_buffer) + committedSize);
    205     #else
    206         /*
    207          * If neither MMAP nor VIRTUALALLOC are available - use fastMalloc instead.
    208          *
    209          * Please note that this is the fallback case, which is non-optimal.
    210          * If any possible, the platform should provide for a better memory
    211          * allocation mechanism that allows for "lazy commit" or dynamic
    212          * pre-allocation, similar to mmap or VirtualAlloc, to avoid waste of memory.
    213          */
    214         m_buffer = static_cast<Register*>(fastMalloc(bufferLength));
    215     #endif
    216         m_start = m_buffer + maxGlobals;
     168        checkAllocatedOkay(m_reservation.commit(base, committedSize));
     169        m_commitEnd = reinterpret_cast<Register*>(reinterpret_cast<char*>(base) + committedSize);
     170        m_start = static_cast<Register*>(base) + maxGlobals;
    217171        m_end = m_start;
    218172        m_maxUsed = m_end;
     
    237191            return false;
    238192
    239 #if !HAVE(MMAP) && HAVE(VIRTUALALLOC)
    240193        if (newEnd > m_commitEnd) {
    241194            size_t size = roundUpAllocationSize(reinterpret_cast<char*>(newEnd) - reinterpret_cast<char*>(m_commitEnd), commitSize);
    242             if (!VirtualAlloc(m_commitEnd, size, MEM_COMMIT, PAGE_READWRITE)) {
    243 #if OS(WINCE)
    244                 fprintf(stderr, "Could not allocate register file: %d\n", GetLastError());
    245 #else
    246                 fprintf(stderr, "Could not allocate register file: %d\n", errno);
    247 #endif
    248                 CRASH();
    249             }
     195            checkAllocatedOkay(m_reservation.commit(m_commitEnd, size));
    250196            m_commitEnd = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_commitEnd) + size);
    251197        }
    252 #endif
    253198
    254199        if (newEnd > m_maxUsed)
     
    259204    }
    260205
     206    inline void RegisterFile::checkAllocatedOkay(bool okay)
     207    {
     208        if (!okay) {
     209#ifndef NDEBUG
     210            fprintf(stderr, "Could not allocate register file: %d\n", PageReservation::lastError());
     211#endif
     212            CRASH();
     213        }
     214    }
     215
    261216} // namespace JSC
    262217
Note: See TracChangeset for help on using the changeset viewer.