Changeset 64782 in webkit


Ignore:
Timestamp:
Aug 5, 2010 1:57:33 PM (14 years ago)
Author:
barraclough@apple.com
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
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r64773 r64782  
     12010-08-05  Gavin Barraclough  <barraclough@apple.com>
     2
     3        Reviewed by Sam Weinig.
     4
     5        Bug 43185 - Switch RegisterFile over to use PageAllocation
     6
     7        Remove platform-specific memory allocation code.
     8
     9        * interpreter/RegisterFile.cpp:
     10        (JSC::RegisterFile::~RegisterFile):
     11        (JSC::RegisterFile::releaseExcessCapacity):
     12        * interpreter/RegisterFile.h:
     13        (JSC::RegisterFile::RegisterFile):
     14        (JSC::RegisterFile::grow):
     15        (JSC::RegisterFile::checkAllocatedOkay):
     16        * wtf/PageAllocation.cpp:
     17        (WTF::PageAllocation::lastError):
     18        * wtf/PageAllocation.h:
     19        (WTF::PageAllocation::allocate):
     20        (WTF::PageAllocation::allocateAt):
     21        (WTF::PageAllocation::allocateAligned):
     22        (WTF::PageAllocation::pageSize):
     23        (WTF::PageAllocation::isPageAligned):
     24        (WTF::PageAllocation::isPowerOfTwo):
     25        * wtf/PageReservation.h:
     26        (WTF::PageReservation::commit):
     27        (WTF::PageReservation::decommit):
     28        (WTF::PageReservation::reserve):
     29        (WTF::PageReservation::reserveAt):
     30
    1312010-08-05  Michael Saboff  <msaboff@apple.com>
    232
  • 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
  • trunk/JavaScriptCore/wtf/PageAllocation.cpp

    r64695 r64782  
    3333size_t PageAllocation::s_pageSize = 0;
    3434
     35#ifndef NDEBUG
     36
     37int PageAllocation::lastError()
     38{
     39#if OS(WINCE)
     40    return GetLastError();
     41#else
     42    return errno;
     43#endif
    3544}
     45
     46#endif
     47
     48}
  • trunk/JavaScriptCore/wtf/PageAllocation.h

    r64695 r64782  
    109109    static PageAllocation allocate(size_t size, Usage usage = UnknownUsage, bool writable = true, bool executable = false)
    110110    {
    111         // size must be a multiple of pageSize.
    112         ASSERT(!(size & (pageSize() - 1)));
     111        ASSERT(isPageAligned(size));
    113112        return systemAllocate(size, usage, writable, executable);
    114113    }
     
    117116    static PageAllocation allocateAt(void* address, bool fixed, size_t size, Usage usage = UnknownUsage, bool writable = true, bool executable = false)
    118117    {
    119         // size must be a multiple of pageSize.
    120         ASSERT(!(size & (pageSize() - 1)));
    121         // address must be aligned to pageSize.
    122         ASSERT(!(reinterpret_cast<intptr_t>(address) & (pageSize() - 1)));
     118        ASSERT(isPageAligned(address));
     119        ASSERT(isPageAligned(size));
    123120        return systemAllocateAt(address, fixed, size, usage, writable, executable);
    124121    }
     
    128125    static PageAllocation allocateAligned(size_t size, Usage usage = UnknownUsage)
    129126    {
    130         // size must be a multiple of pageSize.
    131         ASSERT(!(size & (pageSize() - 1)));
    132         // size must be a power of two.
    133         ASSERT(!(size & (size - 1)));
     127        ASSERT(isPageAligned(size));
     128        ASSERT(isPowerOfTwo(size));
    134129        return systemAllocateAligned(size, usage);
    135130    }
     
    144139    static size_t pageSize()
    145140    {
    146         if (!s_pageSize) {
     141        if (!s_pageSize)
    147142            s_pageSize = systemPageSize();
    148             // system page size must be a power of two.
    149             ASSERT(!(s_pageSize & (s_pageSize - 1)));
    150         }
     143        ASSERT(isPowerOfTwo(s_pageSize));
    151144        return s_pageSize;
    152145    }
     146
     147#ifndef NDEBUG
     148    static bool isPageAligned(void* address) { return !(reinterpret_cast<intptr_t>(address) & (pageSize() - 1)); }
     149    static bool isPageAligned(size_t size) { return !(size & (pageSize() - 1)); }
     150    static bool isPowerOfTwo(size_t size) { return !(size & (size - 1)); }
     151    static int lastError();
     152#endif
    153153
    154154protected:
  • trunk/JavaScriptCore/wtf/PageReservation.h

    r64703 r64782  
    7373    {
    7474        ASSERT(m_base);
    75         // size must be a multiple of pageSize.
    76         ASSERT(!(size & (pageSize() - 1)));
    77         // address must be aligned to pageSize.
    78         ASSERT(!(reinterpret_cast<intptr_t>(start) & (pageSize() - 1)));
     75        ASSERT(isPageAligned(start));
     76        ASSERT(isPageAligned(size));
    7977
    8078        bool commited = systemCommit(start, size);
     
    8886    {
    8987        ASSERT(m_base);
    90         // size must be a multiple of pageSize.
    91         ASSERT(!(size & (pageSize() - 1)));
    92         // address must be aligned to pageSize.
    93         ASSERT(!(reinterpret_cast<intptr_t>(start) & (pageSize() - 1)));
     88        ASSERT(isPageAligned(start));
     89        ASSERT(isPageAligned(size));
    9490
    9591#ifndef NDEBUG
     
    10197    static PageReservation reserve(size_t size, Usage usage = UnknownUsage, bool writable = true, bool executable = false)
    10298    {
    103         // size must be a multiple of pageSize.
    104         ASSERT(!(size & (pageSize() - 1)));
     99        ASSERT(isPageAligned(size));
    105100        return systemReserve(size, usage, writable, executable);
    106101    }
     
    109104    static PageReservation reserveAt(void* address, bool fixed, size_t size, Usage usage = UnknownUsage, bool writable = true, bool executable = false)
    110105    {
    111         // size must be a multiple of pageSize.
    112         ASSERT(!(size & (pageSize() - 1)));
    113         // address must be aligned to pageSize.
    114         ASSERT(!(reinterpret_cast<intptr_t>(address) & (pageSize() - 1)));
     106        ASSERT(isPageAligned(address));
     107        ASSERT(isPageAligned(size));
    115108        return systemReserveAt(address, fixed, size, usage, writable, executable);
    116109    }
     
    123116        systemDeallocate(false);
    124117    }
     118
     119#ifndef NDEBUG
     120    using PageAllocation::lastError;
     121#endif
    125122
    126123private:
Note: See TracChangeset for help on using the changeset viewer.