Ignore:
Timestamp:
Apr 22, 2007, 9:01:47 PM (18 years ago)
Author:
mjs
Message:

Reviewed by Darin.



  • kjs/collector.cpp: (KJS::allocateBlock): New function to allocate 64k of 64k-aligned memory (KJS::freeBlock): Corresponding free (KJS::Collector::allocate): (KJS::Collector::collect):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/collector.cpp

    r21015 r21017  
    3939
    4040#include <mach/mach_port.h>
     41#include <mach/mach_init.h>
    4142#include <mach/task.h>
    4243#include <mach/thread_act.h>
     44#include <mach/vm_map.h>
    4345
    4446#elif PLATFORM(WIN_OS)
     
    4749
    4850#elif PLATFORM(UNIX)
     51
     52#include <stdlib.h>
     53#include <sys/mman.h>
    4954
    5055#if HAVE(PTHREAD_NP_H)
     
    5964
    6065namespace KJS {
    61 
    6266
    6367
     
    6872template<> struct CellSize<sizeof(uint64_t)> { static const size_t m_value = 80; }; // 64-bit
    6973
    70 const size_t BLOCK_SIZE = (8 * 4096);
     74const size_t BLOCK_SIZE = (16 * 4096); // 64k
    7175const size_t SPARE_EMPTY_BLOCKS = 2;
    7276const size_t MIN_ARRAY_SIZE = 14;
     
    7680
    7781// derived constants
     82const size_t PTR_IN_BLOCK_MASK = (BLOCK_SIZE - 1);
     83const size_t BLOCK_MASK = (~PTR_IN_BLOCK_MASK);
    7884const size_t MINIMUM_CELL_SIZE = CellSize<sizeof(void*)>::m_value;
    7985const size_t CELL_ARRAY_LENGTH = (MINIMUM_CELL_SIZE / sizeof(double)) + (MINIMUM_CELL_SIZE % sizeof(double) != 0 ? 1 : 0);
     
    135141bool GCLock::isLocked = false;
    136142#endif
     143
     144static CollectorBlock* allocateBlock()
     145{
     146#if PLATFORM(DARWIN)   
     147    vm_address_t address = 0;
     148    vm_map(current_task(), &address, BLOCK_SIZE, PTR_IN_BLOCK_MASK, VM_FLAGS_ANYWHERE, MEMORY_OBJECT_NULL, 0, FALSE, VM_PROT_DEFAULT, VM_PROT_DEFAULT, VM_INHERIT_DEFAULT);
     149#elif PLATFORM(WIN)
     150     // windows virtual address granularity is naturally 64k
     151    LPVOID address = VirtualAlloc(NULL, BLOCK_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
     152#elif HAVE(POSIX_MEMALIGN)
     153    void* address;
     154    posix_memalign(address, BLOCK_SIZE, BLOCK_SIZE);
     155    memset(reinterpret_cast<void*>(address), 0, BLOCK_SIZE);
     156#else
     157    static size_t pagesize = getpagesize();
     158   
     159    size_t extra = 0;
     160    if (BLOCK_SIZE > pagesize)
     161        extra = BLOCK_SIZE - pagesize;
     162
     163    void* mmapResult = mmap(NULL, BLOCK_SIZE + extra, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
     164    uintptr_t address = reinterpret_cast<uintptr_t>(mmapResult);
     165
     166    size_t adjust = 0;
     167    if ((address & PTR_IN_BLOCK_MASK) != 0)
     168        adjust = BLOCK_SIZE - (address & PTR_IN_BLOCK_MASK);
     169
     170    if (adjust > 0)
     171        munmap(reinterpret_cast<void*>(address), adjust);
     172
     173    if (adjust < extra)
     174        munmap(reinterpret_cast<void*>(address + adjust + BLOCK_SIZE), extra - adjust);
     175
     176    address += adjust;
     177    memset(reinterpret_cast<void*>(address), 0, BLOCK_SIZE);
     178#endif
     179
     180    return reinterpret_cast<CollectorBlock*>(address);
     181}
     182
     183static void freeBlock(CollectorBlock* block)
     184{
     185#if PLATFORM(DARWIN)   
     186    vm_deallocate(current_task(), reinterpret_cast<vm_address_t>(block), BLOCK_SIZE);
     187#elif PLATFORM(WIN)
     188    VirtualFree(block, BLOCK_SIZE, MEM_RELEASE);
     189#elif HAVE(POSIX_MEMALIGN)
     190    free(block);
     191#else
     192    munmap(block, BLOCK_SIZE);
     193#endif
     194}
    137195
    138196void* Collector::allocate(size_t s)
     
    186244    }
    187245
    188     targetBlock = static_cast<CollectorBlock *>(fastCalloc(1, sizeof(CollectorBlock)));
     246    targetBlock = allocateBlock();
    189247    targetBlock->freeList = targetBlock->cells;
    190248    targetBlockUsedCells = 0;
     
    800858      if (emptyBlocks > SPARE_EMPTY_BLOCKS) {
    801859#if !DEBUG_COLLECTOR
    802         fastFree(curBlock);
     860        freeBlock(curBlock);
    803861#endif
    804862        // swap with the last block so we compact as we go
Note: See TracChangeset for help on using the changeset viewer.