Changeset 28135 in webkit for trunk/JavaScriptCore/wtf


Ignore:
Timestamp:
Nov 29, 2007, 12:46:14 AM (17 years ago)
Author:
[email protected]
Message:

Merging updated system alloc and spinlock code from r38 of TCMalloc.

Reviewed by Geoff

This is needed as a precursor to the merge of TCMalloc proper.

Location:
trunk/JavaScriptCore/wtf
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/FastMalloc.cpp

    r27711 r28135  
    498498static uint64_t metadata_system_bytes = 0;
    499499static void* MetaDataAlloc(size_t bytes) {
    500   void* result = TCMalloc_SystemAlloc(bytes);
     500  void* result = TCMalloc_SystemAlloc(bytes, 0);
    501501  if (result != NULL) {
    502502    metadata_system_bytes += bytes;
     
    10401040  ASSERT(kMaxPages >= kMinSystemAlloc);
    10411041  Length ask = (n>kMinSystemAlloc) ? n : static_cast<Length>(kMinSystemAlloc);
    1042   void* ptr = TCMalloc_SystemAlloc(ask << kPageShift, kPageSize);
     1042  void* ptr = TCMalloc_SystemAlloc(ask << kPageShift, 0, kPageSize);
    10431043  if (ptr == NULL) {
    10441044    if (n < ask) {
    10451045      // Try growing just "n" pages
    10461046      ask = n;
    1047       ptr = TCMalloc_SystemAlloc(ask << kPageShift, kPageSize);
     1047      ptr = TCMalloc_SystemAlloc(ask << kPageShift, 0, kPageSize);
    10481048    }
    10491049    if (ptr == NULL) return false;
     
    13621362
    13631363void TCMalloc_Central_FreeList::Init(size_t cl) {
    1364   lock_.Init();
    13651364  size_class_ = cl;
    13661365  DLL_Init(&empty_);
  • trunk/JavaScriptCore/wtf/TCSpinLock.h

    r27709 r28135  
    1 // Copyright (c) 2005, Google Inc.
     1// Copyright (c) 2005, 2006, Google Inc.
    22// All rights reserved.
    33//
     
    5858// The following is a struct so that it can be initialized at compile time
    5959struct TCMalloc_SpinLock {
    60   volatile unsigned int private_lockword_;
    61 
    62   inline void Init() { private_lockword_ = 0; }
    63   inline void Finalize() { }
    64    
     60
    6561  inline void Lock() {
    6662    int r;
     
    6965    __asm__ __volatile__
    7066      ("xchgl %0, %1"
    71        : "=r"(r), "=m"(private_lockword_)
    72        : "0"(1), "m"(private_lockword_)
    73        : "memory");
    74 #else
    75     volatile unsigned int *lockword_ptr = &private_lockword_;
     67       : "=r"(r), "=m"(lockword_)
     68       : "0"(1), "m"(lockword_)
     69       : "memory");
     70#else
     71    volatile unsigned int *lockword_ptr = &lockword_;
    7672    __asm__ __volatile__
    7773        ("1: lwarx %0, 0, %1\n\t"
     
    8581#elif COMPILER(MSVC)
    8682    __asm {
    87         mov eax, this    ; store &private_lockword_ (which is this+0) in eax
     83        mov eax, this    ; store &lockword_ (which is this+0) in eax
    8884        mov ebx, 1       ; store 1 in ebx
    89         xchg [eax], ebx  ; exchange private_lockword_ and 1
    90         mov r, ebx       ; store old value of private_lockword_ in r
    91     }
    92 #endif
    93     if (r) TCMalloc_SlowLock(&private_lockword_);
     85        xchg [eax], ebx  ; exchange lockword_ and 1
     86        mov r, ebx       ; store old value of lockword_ in r
     87    }
     88#endif
     89    if (r) TCMalloc_SlowLock(&lockword_);
    9490  }
    9591
     
    9995    __asm__ __volatile__
    10096      ("movl $0, %0"
    101        : "=m"(private_lockword_)
    102        : "m" (private_lockword_)
     97       : "=m"(lockword_)
     98       : "m" (lockword_)
    10399       : "memory");
    104100#else
     
    107103       "eieio\n\t"
    108104       "stw %1, %0"
    109        : "=o" (private_lockword_)
     105       : "=o" (lockword_)
    110106       : "r" (0)
    111107       : "memory");
     
    113109#elif COMPILER(MSVC)
    114110      __asm {
    115           mov eax, this  ; store &private_lockword_ (which is this+0) in eax
    116           mov [eax], 0   ; set private_lockword_ to 0
     111          mov eax, this  ; store &lockword_ (which is this+0) in eax
     112          mov [eax], 0   ; set lockword_ to 0
    117113      }
    118114#endif
    119115  }
    120 
    121 #ifdef WTF_CHANGES 
    122   inline bool IsLocked() {
    123     return private_lockword_ != 0;
    124   }
    125 #endif
     116    // Report if we think the lock can be held by this thread.
     117    // When the lock is truly held by the invoking thread
     118    // we will always return true.
     119    // Indended to be used as CHECK(lock.IsHeld());
     120    inline bool IsHeld() const {
     121        return lockword_ != 0;
     122    }
     123
     124    volatile unsigned int lockword_;
    126125};
    127126
  • trunk/JavaScriptCore/wtf/TCSystemAlloc.cpp

    r27336 r28135  
    100100#if HAVE(SBRK)
    101101
    102 static void* TrySbrk(size_t size, size_t alignment) {
     102static void* TrySbrk(size_t size, size_t *actual_size, size_t alignment) {
    103103  size = ((size + alignment - 1) / alignment) * alignment;
     104 
     105  // could theoretically return the "extra" bytes here, but this
     106  // is simple and correct.
     107  if (actual_size)
     108    *actual_size = size;
     109   
    104110  void* result = sbrk(size);
    105111  if (result == reinterpret_cast<void*>(-1)) {
     
    138144#if HAVE(MMAP)
    139145
    140 static void* TryMmap(size_t size, size_t alignment) {
     146static void* TryMmap(size_t size, size_t *actual_size, size_t alignment) {
    141147  // Enforce page alignment
    142148  if (pagesize == 0) pagesize = getpagesize();
    143149  if (alignment < pagesize) alignment = pagesize;
    144150  size = ((size + alignment - 1) / alignment) * alignment;
    145 
     151 
     152  // could theoretically return the "extra" bytes here, but this
     153  // is simple and correct.
     154  if (actual_size)
     155    *actual_size = size;
     156   
    146157  // Ask for extra memory if alignment > pagesize
    147158  size_t extra = 0;
     
    181192#if HAVE(VIRTUALALLOC)
    182193
    183 static void* TryVirtualAlloc(size_t size, size_t alignment) {
     194static void* TryVirtualAlloc(size_t size, size_t *actual_size, size_t alignment) {
    184195  // Enforce page alignment
    185196  if (pagesize == 0) {
     
    188199    pagesize = system_info.dwPageSize;
    189200  }
     201
    190202  if (alignment < pagesize) alignment = pagesize;
    191203  size = ((size + alignment - 1) / alignment) * alignment;
    192204
     205  // could theoretically return the "extra" bytes here, but this
     206  // is simple and correct.
     207  if (actual_size)
     208    *actual_size = size;
     209   
    193210  // Ask for extra memory if alignment > pagesize
    194211  size_t extra = 0;
     
    228245
    229246#ifndef WTF_CHANGES
    230 static void* TryDevMem(size_t size, size_t alignment) {
     247static void* TryDevMem(size_t size, size_t *actual_size, size_t alignment) {
    231248  static bool initialized = false;
    232249  static off_t physmem_base;  // next physical memory address to allocate
     
    259276  if (alignment < pagesize) alignment = pagesize;
    260277  size = ((size + alignment - 1) / alignment) * alignment;
    261 
     278   
     279  // could theoretically return the "extra" bytes here, but this
     280  // is simple and correct.
     281  if (actual_size)
     282    *actual_size = size;
     283   
    262284  // Ask for extra memory if alignment > pagesize
    263285  size_t extra = 0;
     
    300322#endif
    301323
    302 void* TCMalloc_SystemAlloc(size_t size, size_t alignment) {
    303 #ifndef WTF_CHANGES
    304   if (TCMallocDebug::level >= TCMallocDebug::kVerbose) {
    305     MESSAGE("TCMalloc_SystemAlloc(%" PRIuS ", %" PRIuS")\n",
    306             size, alignment);
    307   }
    308 #endif
     324void* TCMalloc_SystemAlloc(size_t size, size_t *actual_size, size_t alignment) {
     325  // Discard requests that overflow
     326  if (size + alignment < size) return NULL;
     327   
    309328  SpinLockHolder lock_holder(&spinlock);
    310329
     
    318337#ifndef WTF_CHANGES
    319338    if (use_devmem && !devmem_failure) {
    320       void* result = TryDevMem(size, alignment);
     339      void* result = TryDevMem(size, actual_size, alignment);
    321340      if (result != NULL) return result;
    322341    }
     
    325344#if HAVE(SBRK)
    326345    if (use_sbrk && !sbrk_failure) {
    327       void* result = TrySbrk(size, alignment);
     346      void* result = TrySbrk(size, actual_size, alignment);
    328347      if (result != NULL) return result;
    329348    }
     
    332351#if HAVE(MMAP)   
    333352    if (use_mmap && !mmap_failure) {
    334       void* result = TryMmap(size, alignment);
     353      void* result = TryMmap(size, actual_size, alignment);
    335354      if (result != NULL) return result;
    336355    }
     
    339358#if HAVE(VIRTUALALLOC)
    340359    if (use_VirtualAlloc && !VirtualAlloc_failure) {
    341       void* result = TryVirtualAlloc(size, alignment);
     360      void* result = TryVirtualAlloc(size, actual_size, alignment);
    342361      if (result != NULL) return result;
    343362    }
  • trunk/JavaScriptCore/wtf/TCSystemAlloc.h

    r11962 r28135  
    3939// REQUIRES: "alignment" is a power of two or "0" to indicate default alignment
    4040//
    41 // Allocate and return "N" bytes of zeroed memory.  The returned
    42 // pointer is a multiple of "alignment" if non-zero.  Returns NULL
    43 // when out of memory.
    44 extern void* TCMalloc_SystemAlloc(size_t bytes, size_t alignment = 0);
     41// Allocate and return "N" bytes of zeroed memory.
     42//
     43// If actual_bytes is NULL then the returned memory is exactly the
     44// requested size.  If actual bytes is non-NULL then the allocator
     45// may optionally return more bytes than asked for (i.e. return an
     46// entire "huge" page if a huge page allocator is in use).
     47//
     48// The returned pointer is a multiple of "alignment" if non-zero.
     49//
     50// Returns NULL when out of memory.
     51extern void* TCMalloc_SystemAlloc(size_t bytes, size_t *actual_bytes,
     52                                  size_t alignment = 0);
     53
     54// This call is a hint to the operating system that the pages
     55// contained in the specified range of memory will not be used for a
     56// while, and can be released for use by other processes or the OS.
     57// Pages which are released in this way may be destroyed (zeroed) by
     58// the OS.  The benefit of this function is that it frees memory for
     59// use by the system, the cost is that the pages are faulted back into
     60// the address space next time they are touched, which can impact
     61// performance.  (Only pages fully covered by the memory region will
     62// be released, partial pages will not.)
     63extern void TCMalloc_SystemRelease(void* start, size_t length);
    4564
    4665#endif /* TCMALLOC_SYSTEM_ALLOC_H__ */
Note: See TracChangeset for help on using the changeset viewer.