Changeset 35691 in webkit


Ignore:
Timestamp:
Aug 12, 2008, 1:54:12 PM (16 years ago)
Author:
mitz@apple.com
Message:

JavaScriptCore:

  • JavaScriptCore part of <rdar://problem/6121636> Make fast*alloc() abort() on failure and add "try" variants that return NULL on failure.

Reviewed by Darin Adler.

  • JavaScriptCore.exp: Exported tryFastCalloc().
  • VM/RegisterFile.h: (KJS::RegisterFile::RegisterFile): Removed an ASSERT().
  • kjs/JSArray.cpp: (KJS::JSArray::putSlowCase): Changed to use tryFastRealloc(). (KJS::JSArray::increaseVectorLength): Ditto.
  • kjs/ustring.cpp: (KJS::allocChars): Changed to use tryFastMalloc(). (KJS::reallocChars): Changed to use tryFastRealloc().
  • wtf/FastMalloc.cpp: (WTF::fastZeroedMalloc): Removed null checking of fastMalloc()'s result and removed extra call to InvokeNewHook(). (WTF::tryFastZeroedMalloc): Added. Uses tryFastMalloc(). (WTF::tryFastMalloc): Renamed fastMalloc() to this. (WTF::fastMalloc): Added. This version abort()s if allocation fails. (WTF::tryFastCalloc): Renamed fastCalloc() to this. (WTF::fastCalloc): Added. This version abort()s if allocation fails. (WTF::tryFastRealloc): Renamed fastRealloc() to this. (WTF::fastRealloc): Added. This version abort()s if allocation fails. (WTF::do_malloc): Made this a function template. When the abortOnFailure template parameter is set, the function abort()s on failure to allocate. Otherwise, it sets errno to ENOMEM and returns zero. (WTF::TCMallocStats::fastMalloc): Defined to abort() on failure. (WTF::TCMallocStats::tryFastMalloc): Added. Does not abort() on failure. (WTF::TCMallocStats::fastCalloc): Defined to abort() on failure. (WTF::TCMallocStats::tryFastCalloc): Added. Does not abort() on failure. (WTF::TCMallocStats::fastRealloc): Defined to abort() on failure. (WTF::TCMallocStats::tryFastRealloc): Added. Does not abort() on failure.
  • wtf/FastMalloc.h: Declared the "try" variants.

WebCore:

  • WebCore part of <rdar://problem/6121636> Make fast*alloc() abort() on failure and add "try" variants that return NULL on failure.

Reviewed by Darin Adler.

  • platform/Arena.cpp: (WebCore::ArenaAllocate): Removed null checking of fastMalloc()'s result.
  • platform/graphics/cg/ImageBufferCG.cpp: (WebCore::ImageBuffer::create): Changed to use tryFastCalloc().
Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r35665 r35691  
     12008-08-12  Dan Bernstein  <mitz@apple.com>
     2
     3        - JavaScriptCore part of <rdar://problem/6121636>
     4          Make fast*alloc() abort() on failure and add "try" variants that
     5          return NULL on failure.
     6
     7        Reviewed by Darin Adler.
     8
     9        * JavaScriptCore.exp: Exported tryFastCalloc().
     10        * VM/RegisterFile.h:
     11        (KJS::RegisterFile::RegisterFile): Removed an ASSERT().
     12        * kjs/JSArray.cpp:
     13        (KJS::JSArray::putSlowCase): Changed to use tryFastRealloc().
     14        (KJS::JSArray::increaseVectorLength): Ditto.
     15        * kjs/ustring.cpp:
     16        (KJS::allocChars): Changed to use tryFastMalloc().
     17        (KJS::reallocChars): Changed to use tryFastRealloc().
     18        * wtf/FastMalloc.cpp:
     19        (WTF::fastZeroedMalloc): Removed null checking of fastMalloc()'s result
     20        and removed extra call to InvokeNewHook().
     21        (WTF::tryFastZeroedMalloc): Added. Uses tryFastMalloc().
     22        (WTF::tryFastMalloc): Renamed fastMalloc() to this.
     23        (WTF::fastMalloc): Added. This version abort()s if allocation fails.
     24        (WTF::tryFastCalloc): Renamed fastCalloc() to this.
     25        (WTF::fastCalloc): Added. This version abort()s if allocation fails.
     26        (WTF::tryFastRealloc): Renamed fastRealloc() to this.
     27        (WTF::fastRealloc): Added. This version abort()s if allocation fails.
     28        (WTF::do_malloc): Made this a function template. When the abortOnFailure
     29        template parameter is set, the function abort()s on failure to allocate.
     30        Otherwise, it sets errno to ENOMEM and returns zero.
     31        (WTF::TCMallocStats::fastMalloc): Defined to abort() on failure.
     32        (WTF::TCMallocStats::tryFastMalloc): Added. Does not abort() on
     33        failure.
     34        (WTF::TCMallocStats::fastCalloc): Defined to abort() on failure.
     35        (WTF::TCMallocStats::tryFastCalloc): Added. Does not abort() on
     36        failure.
     37        (WTF::TCMallocStats::fastRealloc): Defined to abort() on failure.
     38        (WTF::TCMallocStats::tryFastRealloc): Added. Does not abort() on
     39        failure.
     40        * wtf/FastMalloc.h: Declared the "try" variants.
     41
    1422008-08-11  Adam Roben  <aroben@apple.com>
    243
  • trunk/JavaScriptCore/JavaScriptCore.exp

    r35652 r35691  
    216216__ZN3KJS8JSObject3putEPNS_9ExecStateEjPNS_7JSValueE
    217217__ZN3KJS8JSObject4markEv
    218 __ZN3KJS8Profiler21didFinishAllExecutionEPNS_9ExecStateE
    219218__ZN3KJS8Profiler13stopProfilingEPNS_9ExecStateERKNS_7UStringE
    220219__ZN3KJS8Profiler14startProfilingEPNS_9ExecStateERKNS_7UStringEPNS_14ProfilerClientE
     220__ZN3KJS8Profiler21didFinishAllExecutionEPNS_9ExecStateE
    221221__ZN3KJS8Profiler8profilerEv
    222222__ZN3KJS8jsStringEPNS_9ExecStateEPKc
     
    234234__ZN3WTF12isMainThreadEv
    235235__ZN3WTF13currentThreadEv
     236__ZN3WTF13tryFastCallocEmm
    236237__ZN3WTF15ThreadCondition4waitERNS_5MutexE
    237238__ZN3WTF15ThreadCondition6signalEv
  • trunk/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

    r35665 r35691  
    16191619                        projectDirPath = "";
    16201620                        projectRoot = "";
     1621                        projectRoots = (
     1622                                "",
     1623                        );
    16211624                        targets = (
    16221625                                932F5BE30822A1C700736975 /* All */,
  • trunk/JavaScriptCore/VM/RegisterFile.h

    r34974 r35691  
    125125            // FIXME: Use VirtualAlloc, and commit pages as we go.
    126126            m_buffer = static_cast<Register*>(fastMalloc(bufferLength));
    127             ASSERT(m_buffer);
    128127#else
    129128            #error "Don't know how to reserve virtual memory on this platform."
  • trunk/JavaScriptCore/kjs/JSArray.cpp

    r35291 r35691  
    330330    }
    331331
    332     storage = static_cast<ArrayStorage*>(fastRealloc(storage, storageSize(newVectorLength)));
     332    storage = static_cast<ArrayStorage*>(tryFastRealloc(storage, storageSize(newVectorLength)));
    333333    if (!storage) {
    334334        throwOutOfMemoryError(exec);
     
    446446    unsigned newVectorLength = increasedVectorLength(newLength);
    447447
    448     storage = static_cast<ArrayStorage*>(fastRealloc(storage, storageSize(newVectorLength)));
     448    storage = static_cast<ArrayStorage*>(tryFastRealloc(storage, storageSize(newVectorLength)));
    449449    if (!storage)
    450450        return false;
  • trunk/JavaScriptCore/kjs/ustring.cpp

    r35458 r35691  
    6565    if (length > maxUChars())
    6666        return 0;
    67     return static_cast<UChar*>(fastMalloc(sizeof(UChar) * length));
     67    return static_cast<UChar*>(tryFastMalloc(sizeof(UChar) * length));
    6868}
    6969
     
    7373    if (length > maxUChars())
    7474        return 0;
    75     return static_cast<UChar*>(fastRealloc(buffer, sizeof(UChar) * length));
     75    return static_cast<UChar*>(tryFastRealloc(buffer, sizeof(UChar) * length));
    7676}
    7777
  • trunk/JavaScriptCore/wtf/FastMalloc.cpp

    r35484 r35691  
    151151
    152152namespace WTF {
    153 void *fastZeroedMalloc(size_t n)
     153
     154void* fastZeroedMalloc(size_t n)
    154155{
    155     void *result = fastMalloc(n);
     156    void* result = fastMalloc(n);
     157    memset(result, 0, n);
     158    return result;
     159}
     160   
     161void* tryFastZeroedMalloc(size_t n)
     162{
     163    void* result = tryFastMalloc(n);
    156164    if (!result)
    157165        return 0;
    158166    memset(result, 0, n);
    159 #ifndef WTF_CHANGES
    160     MallocHook::InvokeNewHook(result, n);
    161 #endif
    162167    return result;
    163168}
    164    
    165 }
     169
     170} // namespace WTF
    166171
    167172#if FORCE_SYSTEM_MALLOC
     
    174179namespace WTF {
    175180   
    176 void *fastMalloc(size_t n)
     181void* tryFastMalloc(size_t n)
    177182{
    178183    ASSERT(!isForbidden());
     
    180185}
    181186
    182 void *fastCalloc(size_t n_elements, size_t element_size)
     187void* fastMalloc(size_t n)
     188{
     189    ASSERT(!isForbidden());
     190    void* result = malloc(n);
     191    if (!result)
     192        abort();
     193    return result;
     194}
     195
     196void* tryFastCalloc(size_t n_elements, size_t element_size)
    183197{
    184198    ASSERT(!isForbidden());
    185199    return calloc(n_elements, element_size);
     200}
     201
     202void* fastCalloc(size_t n_elements, size_t element_size)
     203{
     204    ASSERT(!isForbidden());
     205    void* result = calloc(n_elements, element_size);
     206    if (!result)
     207        abort();
     208    return result;
    186209}
    187210
     
    192215}
    193216
    194 void *fastRealloc(void* p, size_t n)
     217void* tryFastRealloc(void* p, size_t n)
    195218{
    196219    ASSERT(!isForbidden());
    197220    return realloc(p, n);
     221}
     222
     223void* fastRealloc(void* p, size_t n)
     224{
     225    ASSERT(!isForbidden());
     226    void* result = realloc(p, n);
     227    if (!result)
     228        abort();
     229    return result;
    198230}
    199231
     
    29622994}
    29632995
     2996#ifdef WTF_CHANGES
     2997template <bool abortOnFailure>
     2998#endif
    29642999static ALWAYS_INLINE void* do_malloc(size_t size) {
    29653000  void* ret = NULL;
     
    29913026    ret = CheckedMallocResult(heap->Allocate(size));
    29923027  }
    2993   if (ret == NULL) errno = ENOMEM;
     3028  if (!ret) {
     3029#ifdef WTF_CHANGES
     3030    if (abortOnFailure) // This branch should be optimized out by the compiler.
     3031        abort();
     3032#else
     3033    errno = ENOMEM;
     3034#endif
     3035  }
    29943036  return ret;
    29953037}
     
    31553197#ifndef WTF_CHANGES
    31563198extern "C"
     3199#else
     3200#define do_malloc do_malloc<abortOnFailure>
     3201
     3202template <bool abortOnFailure>
     3203void* malloc(size_t);
     3204
     3205void* fastMalloc(size_t size)
     3206{
     3207    return malloc<true>(size);
     3208}
     3209
     3210void* tryFastMalloc(size_t size)
     3211{
     3212    return malloc<false>(size);
     3213}
     3214
     3215template <bool abortOnFailure>
     3216ALWAYS_INLINE
    31573217#endif
    31583218void* malloc(size_t size) {
     
    31763236#ifndef WTF_CHANGES
    31773237extern "C"
     3238#else
     3239template <bool abortOnFailure>
     3240void* calloc(size_t, size_t);
     3241
     3242void* fastCalloc(size_t n, size_t elem_size)
     3243{
     3244    return calloc<true>(n, elem_size);
     3245}
     3246
     3247void* tryFastCalloc(size_t n, size_t elem_size)
     3248{
     3249    return calloc<false>(n, elem_size);
     3250}
     3251
     3252template <bool abortOnFailure>
     3253ALWAYS_INLINE
    31783254#endif
    31793255void* calloc(size_t n, size_t elem_size) {
     
    32063282#ifndef WTF_CHANGES
    32073283extern "C"
     3284#else
     3285template <bool abortOnFailure>
     3286void* realloc(void*, size_t);
     3287
     3288void* fastRealloc(void* old_ptr, size_t new_size)
     3289{
     3290    return realloc<true>(old_ptr, new_size);
     3291}
     3292
     3293void* tryFastRealloc(void* old_ptr, size_t new_size)
     3294{
     3295    return realloc<false>(old_ptr, new_size);
     3296}
     3297
     3298template <bool abortOnFailure>
     3299ALWAYS_INLINE
    32083300#endif
    32093301void* realloc(void* old_ptr, size_t new_size) {
     
    32653357}
    32663358
    3267 #ifndef WTF_CHANGES
     3359#ifdef WTF_CHANGES
     3360#undef do_malloc
     3361#else
    32683362
    32693363static SpinLock set_new_handler_lock = SPINLOCK_INITIALIZER;
  • trunk/JavaScriptCore/wtf/FastMalloc.h

    r35055 r35691  
    2828namespace WTF {
    2929
    30     void *fastMalloc(size_t n);
    31     void *fastZeroedMalloc(size_t n);
    32     void *fastCalloc(size_t n_elements, size_t element_size);
     30    // These functions call abort() if an allocation fails.
     31    void* fastMalloc(size_t n);
     32    void* fastZeroedMalloc(size_t n);
     33    void* fastCalloc(size_t n_elements, size_t element_size);
     34    void* fastRealloc(void* p, size_t n);
     35
     36    // These functions return NULL if an allocation fails.
     37    void* tryFastMalloc(size_t n);
     38    void* tryFastZeroedMalloc(size_t n);
     39    void* tryFastCalloc(size_t n_elements, size_t element_size);
     40    void* tryFastRealloc(void* p, size_t n);
     41
    3342    void fastFree(void* p);
    34     void *fastRealloc(void* p, size_t n);
    3543
    3644#ifndef NDEBUG   
     
    4755using WTF::fastCalloc;
    4856using WTF::fastRealloc;
     57using WTF::tryFastMalloc;
     58using WTF::tryFastZeroedMalloc;
     59using WTF::tryFastCalloc;
     60using WTF::tryFastRealloc;
    4961using WTF::fastFree;
    5062
  • trunk/WebCore/ChangeLog

    r35690 r35691  
     12008-08-12  Dan Bernstein  <mitz@apple.com>
     2
     3        - WebCore part of <rdar://problem/6121636>
     4          Make fast*alloc() abort() on failure and add "try" variants that
     5          return NULL on failure.
     6
     7        Reviewed by Darin Adler.
     8
     9        * platform/Arena.cpp:
     10        (WebCore::ArenaAllocate): Removed null checking of fastMalloc()'s
     11        result.
     12        * platform/graphics/cg/ImageBufferCG.cpp:
     13        (WebCore::ImageBuffer::create): Changed to use tryFastCalloc().
     14
    1152008-08-12  Dan Bernstein  <mitz@apple.com>
    216
  • trunk/WebCore/platform/Arena.cpp

    r25754 r35691  
    181181#endif
    182182        a = (Arena*)fastMalloc(sz);
    183         if (a)  {
    184             a->limit = (uword)a + sz;
    185             a->base = a->avail = (uword)ARENA_ALIGN(pool, a + 1);
    186             rp = (char *)a->avail;
    187             a->avail += nb;
    188             /* the newly allocated arena is linked after pool->current
    189             *  and becomes pool->current */
    190             a->next = pool->current->next;
    191             pool->current->next = a;
    192             pool->current = a;
    193             if ( !pool->first.next )
    194                 pool->first.next = a;
    195             return(rp);
    196        }
    197     }
    198 
    199     /* we got to here, and there's no memory to allocate */
    200     return(0);
     183        // fastMalloc will abort() if it fails, so we are guaranteed that a is not 0.
     184        a->limit = (uword)a + sz;
     185        a->base = a->avail = (uword)ARENA_ALIGN(pool, a + 1);
     186        rp = (char *)a->avail;
     187        a->avail += nb;
     188        /* the newly allocated arena is linked after pool->current
     189        *  and becomes pool->current */
     190        a->next = pool->current->next;
     191        pool->current->next = a;
     192        pool->current = a;
     193        if ( !pool->first.next )
     194            pool->first.next = a;
     195        return(rp);
     196    }
    201197} /* --- end ArenaAllocate() --- */
    202198
  • trunk/WebCore/platform/graphics/cg/ImageBufferCG.cpp

    r35290 r35691  
    5656    }
    5757
    58     void* imageBuffer = fastCalloc(size.height(), bytesPerRow);
     58    void* imageBuffer = tryFastCalloc(size.height(), bytesPerRow);
    5959    if (!imageBuffer)
    6060        return auto_ptr<ImageBuffer>();
Note: See TracChangeset for help on using the changeset viewer.